Back to Play framework after a while

Play framework is amazing. Yesterday, I just finished a RESTful web application for my Android Client in just 30 minutes. The web system uses JSoup for crawling, FlexJson for publishing Json data and Heroku for hosting. I had a small trouble during the deployment (connecting the system with the Postgres database is a little bit tough), but it is ok finally. I will make a tutorial about this after the holiday.

Happy new year, everyone

Boring lunch

I am quite bored today since there are not many things to do (iteration is done, only reading for the new iteration mostly).  Like normal people, it is the Facebook time (yay !!).

I read a status of a friend which is about a puzzle (I think it is from children ‘s garden :) ) : 4 dogs at the corners of the square chasing each other clockwise. Where will they meet?

The answer is easy I guess. But I want to see it more clearly, so this is it

It is 3 PM now. 2 hours left. Thinking about what left to play with :D

How is my Monday ?

I have a requriement from the client today. They sent me a list of images and asked to show the images which match with the data in the database. The data are the floating numbers which are inserted by the user through a TEXTBOX and there are 7 images which¬† contains these texts: “1”, “1 1/2″, “2 1/2″, “3”, “3 1/2″, “4”, “4 1/2″. There is no special requirement for the inserted data. If there is a match, then show the image, otherwise, don’t show

And that is how a Monday started happily

Bug about getOffsetParent (Prototype.js) on Internet Explorer

Due to bug of prototype framework: https://prototype.lighthouseapp.com/projects/8886/tickets/618-getoffsetparent-returns-body-for-new-hidden-elements-in-ie8-final, the auto complete menu will appear in the wrong position in IE. The real reason is because the child element is hidden; therefore, when prototype running into the function getOffsetParent (on IE) will return the body of the page, which makes the calculation of the position of the autocomplete menu wrong

Solution of problem: Do not try to override the getOffsetParent in order to patch it. Instead, using prototype to change the state of your element (div, span, … which makes the auto complete menu) into visible

$(yourComp).show();

before getOffsetParent (or Position.clone) is called. The problem will automatically disappear

Preventing to scroll up to top when clicking on a hyperlink

When you have an hyperlink and you want to hook some effects to the link before going further, normally, you will use something like this

<a href="#" id="..." >blah</a>

However, the “#” of the href attribute will make the page automatically scroll to top. I used to use something like this

<a href="javascript:void(0)" id="...">blah</a>

to prevent this issue, but I just find out a better solution for this. Instead, put it like this

<a href="#!" id="..." >blah</a>

And you are good to go. I haven’t found any issue relating to this solution yet

How to become a good software engineer ?

I read a lot of tips about becoming one: Have to be good at algorithm, design before coding, …. So I dedicate one entry based on my personal taste about this. In short,

CODE, CODE MORE, CODE FOREVER

Just work your ass off. Imagine as if some body will hire you to do a ecommerce site if you want to know about Django (or any web framework), a dating app if you want to know about IPhone development and do everything you could (google, IRC, StackOverflow) until you know how to do it. Stop doing the theory, stop reading/doing tutorial, stop finding ebook because you cannot remember anything until you actually do it

(Inspired by this)

Logging with Django (or Python)

If you need to create the log information on the console, as well as on the log file like the Log4j style, then this is the way to do it

1. Config in the settings.py file

import logging.config

MAIN_DIR = os.path.dirname(__file__)
PROJECT_DIR = MAIN_DIR.rsplit(os.sep, 1)[0]

logging.config.fileConfig( os.path.join(PROJECT_DIR, 'configs', 'logging.conf'))

This will tell Django that the log configuration file is in the “configs” folder, named “logging.conf”

2. Create the log config file

Now, create a “configs” folder on your root project and make a “logging.conf” file in it

[loggers]
keys=root

[handlers]
keys=consoleHandler,fileHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler,fileHandler

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=simpleFormatter
args=(sys.stdout,)


[handler_fileHandler]
class=FileHandler
formatter=simpleFormatter
level=INFO
args=('log.log', 'a',)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

Here, I tell Django that I need to print out the log message both on console and on file through handlers: consoleHandler and fileHandler. Everytime I want to write to log, Django will append the message to the log.log file in the root folder of the project

3. Print log

Finally step is triggering the log command

# views.py
import logging
logger = logging.getLogger(__name__)

......
logger.info("-------------- begin to crawl ---------------")

Hope this helps

Django app deployment – Static folder problem

I had to deploy my web application on the client ‘s environment today. The platform is CentOS 6.3 with apache2 and mod_wsgi . As usual, the process ran smoothly, I had my site on the domain, but I got an issue that my application could not locate all the static (css, js) files even I had pointed the alias to the right folder. It turned out that I set the configuration as

    Alias /static/ /opt/project_folder/wwwroot/project/static 

instead of

    Alias /static /opt/project_folder/wwwroot/project/static 

After removing the splash and reloading the apache2, everything went fine