Friday, January 20, 2012

Basic Django on Apache2

My Django project is called 'automation' and lives in the directory /home/tlichtenberg/workspace/ptest/django/src/automation

It has CSS files in the regression/static/styles directory under the home project. I needed to add some stuff to Apache to allow it to find the files it needs to run the Django project

--------------------------------

My Apache2 httpd.conf:


WSGIScriptAlias / /home/tlichtenberg/workspace/ptest/django/src/automation/django.wsgi

AliasMatch ^/([^/]*\.css) /home/tlichtenberg/workspace/ptest/django/src/automation/regression/static/styles/$1

Alias /static/ /home/tlichtenberg/workspace/ptest/django/src/automation/regression/static/

<Directory /home/tlichtenberg/workspace/ptest/django/src/automation/regression/static>
Order deny,allow
Allow from all
</Directory>

<Directory /home/tlichtenberg/workspace/ptest/django/src/automation>
Order deny,allow
Allow from all
</Directory>

------------------------------

The django,wsgi file is critical for setting the path and connecting Django to Apache. It looks like this:


import os
import sys

path = '/home/tlichtenberg/workspace/ptest/django/src/automation/'
if path not in sys.path:
   sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

------------------------------------------

The 'settings' defined by django,wsgi points to the Django project's 'settings.py' file, which defines a lot of the paths that Django needs. Also important is the 'urls.py' file, like settings.py, it is a standard for all Django projects and defines all the URLs associated with the web applications being served by Django. For example:

settings.py:

  STATIC_ROOT = '/home/tlichtenberg/workspace/ptest/django/src/automation/regression/static/'
  STATIC_URL = '/static/'
  ROOT_URLCONF = 'urls'
  INSTALLED_APPS = (
    'my_project',
    'my_other_project',
 )

urls.py
   (r'^my_project/$', 'my_project.views.my_project_main'),
defines that any url beginning with 'my_project' (e.g. http://localhost/my_project) will direct the code to the my_project_main method in the project's views.py file.

---------------------------------------------------


 


Pyjamas on Django for Web Automation


I've been working on developing a web application for use in automating some product testing. The commands being implemented are communicated via HTTP, so there isn't strictly a need for a browser interface just to test the api's, but certain functionality requires doing stuff on the device, then going to a different website and verifying some side effects (e.g. purchases recorded, emails sent, etc ...) Using a web application for these cases lets me do some automation using Selenium.

When I first thought about the design, I assumed I would do a Java Servlet or Java Server Pages, but since I've been mostly working in Python recently, I thought I'd see if there was anything I could do that way. Sure enough, I came across a project called Pyjamas, a Python port of Google Web Toolkit. I was interested right away, because not only was it Python, but it could run in Django, and I was already running a Django server on Apache2 on Ubuntu. This web app would theoretically plug right in. And it did, after some minor bumps in the road.

Pyjamas is a sort of miracle wonder drug, where you create your UI in Python, and then "build" it into JavaScript/HTML/Ajax using its 'pyjsbuild' tool. So you can make real-world web apps without having to look under the hood, so to speak. It provides a simple GUI kit, similar to Java AWT 1.0 - primitive but useful. If I was a better UI designer, the above would look a lot nicer, but since I'm primarily a backend coder, this will just have to do :}

Codewise, you import a lot of Pyjamas modules:
   from pyjamas.ui.TextBox import TextBox

and then go about building your UI, creating panels and sticking widgets into them. I won't go into great detail here. There are a lot of excellent examples in the Pyjamas download and elsewhere, but basically, something like this to get going ...

  class RemoteController:
     
    def onModuleLoad(self):
        self.remote = DataService()    
        dockPanel = DockPanel()
        ...
       # create all your widgets in here, add them to you main panel,
       # and then add the main panel to the RootPanel object
       self.myTextBox = HTML()
       self. myTextBox.setID('my_text_box')
       dockPanel.add( myTextBox )
       RootPanel().add(dockPanel)

The DataService class is interesting - this is what lets you connect into functions running on Django, and mine looks like this:
 from pyjamas.JSONService import JSONProxy
 class DataService(JSONProxy):
    def __init__(self):
        JSONProxy.__init__(self, "/services/", ["method_one", "method_two"])


In my Django views.py, I just have to define those methods:
  from django.pimentech.network import * # use the pimentech module described here to define the JSON service and decorator

  service = JSONRPCService()

 @jsonremote(service)
 def method_one(request):
    print >> sys.stderr,  'method_one called"
    request.session['ip'] = text # set the ip as specific to this session
    ecp.set_url(text)
    return "put this into my text box, please"

now in your Django urls.py you define the /services/ path used by the DataService class above:
  (r'^services/$', 'YOUR_PROJECT.views.service'),

when your method returns from Django, you handle it in your Pyjamas class like this:

 def onRemoteResponse(self, response, request_info):
        try:    
            if request_info.method == 'method_one':
                self.myMyTextBox.setText(response)

Other GUI stuff should be familiar if you've done any similar programming - with clickListeners, mouseListeners, keyListeners and so on.

finally, have your main and have it call onModuleLoad:

if __name__ == "__main__":
    app = RemoteController()
    app.onModuleLoad()

I have myTextBox an id just like Ajax would. I did this for Selenium. My Selenium test could now find the widget By.Id or by xpath using the id:
    text = selenium.get_text("//*[@id='my_text_box']")

  For Selenium you usually want to be able to get text out of text fields but the PyjamasTextBox and TextArea widgets don't actually put the text into the HTML they render. Instead, I used an HTML() widget. It's like a Label and DOES embed the text in the web page so you can get to it from Selenium.

Some other stuff:

CSS - It's nice to set the background colors of your various widgets, and you can do that using inline CSS  in the Python code like this:
   from pyjamas import DOM
   DOM.setStyleAttribute(myWidget.getElement(), "background-color", "#8fbc8f")

 If you have multiple concurrent users, you want your web app to be session based. This is done, in Django, by adding a few things to your settings.py. I also had some issues with CSRF so I needed to disable that (you may not want to, depending on your security concerns. I had none in my test environment)

   MIDDLEWARE_CLASSES = (
    'regression.middleware.DisableCSRF',            
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.middleware.csrf.CsrfResponseMiddleware',
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
)

 CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': 'c:/django_cache',
    }
}

CACHE_MIDDLEWARE_ALIAS = "default"
CACHE_MIDDLEWARE_SECONDS = 600
CACHE_MIDDLEWARE_KEY_PREFIX = ""