Unkillable zombie blog

This blog just won't die

Aug 06, 2008

GeoDjango γίνεται merge στο Django trunk?

Πριν λίγο κάνοντας update το Django working copy μου, είδα οτι προστέθηκε ένα νέο contrib app με το όνομα gis.

Απο οτι καταλαβαίνω το GeoDjango θα γίνει merge στο Django trunk σαν contrib εφαρμογή.

 

Super-awesome!

 

ps (φτιάξανε και το ticket 7683 που με βασάνιζε )

posted at 01:01  ·   ·  django  greek
Click to read and post comments

Apr 03, 2008

Detecting new objects on post_save handler

Reading the scarce documentation on Django’s signals there did not seem to be a way to detect on the post_save handler if the object is a new instance or an update of an existing instance.

Reading the source code, however, reveals that on django/db/base.py at the end of save() method of Model class, we see this snippet:

# Run any post-save hooks.
dispatcher.send(signal=signals.post_save, sender=self.__class__,
instance=self, created=(not record_exists), raw=raw)

So on your post_save hook all you have to do is accept an extra parameter called ‘created’ which will be True if the instance was just created or False if it is simply an update.

 

Although the documentation of Django is pretty good, and certainly much better than a lot of other Python web frameworks, its still far from complete.

I used to use Webware once upon a time, and the only documentation for the bloody thing was the source code.

posted at 14:24  ·   ·  django
Click to read and post comments

Mar 26, 2008

Παίζοντας με το GeoDjango

Υπάρχει ένα branch του Django που λέγεται GeoDjango, και ειδικεύεται σε GIS. Εδώ και καιρό, απο τότε που έμαθα την ύπαρξή του,  είχα ενθουσιαστεί με την ιδέα και ήθελά να ‘παίξω’ μαζί του. Επιτρέπει την ανάπτυξη web εφαρμογών με γεωγραφικό περιεχόμενο πολύ εύκολα (τουλάχιστον σε σχέση με άλλους τρόπους).

Ουσιαστικά πρόκειται για ένα νέο contrib που περιέχει νέα model fields, ένα νέο model Manager (GeoManager) και ένα πλήθος βοηθητικών εργαλείων. Τα νέα Fields ξέρουν πώς να χειρίζονται γεωγραφικά δεδομένα (points, polygonts κλπ) και ο GeoManager επεκτείνει το Django ORM (το Database api) με  δυνατότητες σχετικές με τα νέα fields.

Για παράδειγμα μπορείς ζητήσεις τα αντικείμενα που έχουν ένα συγκεκριμένο Point (απο το model A) μέσα στην επιφάνεια που ορίζεται απο ένα σύνολο σημείων (απο το model B).

Μπορείς επίσεις να ζητήσεις τα αντικείμενα που έχουν μια συγκεκριμένη σχέση απόστασης απο ένα σημείο (πχ ποιά Βενζινάδικα είναι κάτω απο 5 χιλιόμετρα απο το σημείο που έχω επιλέξει στον χάρτη.)  Και πολλά άλλα queries γεωγραφικού (και συνεπώς γεωμετρικού) περιεχομένου.

Μέχρι στιγμής δεν έχω προσέξει κάποια σοβαρή ασύμβατη απόκλιση απο το trunk και έχω την απορία αν είναι εφικτό (και θεμιτό) στο μέλλον να γίνει merge αυτό το branch με το trunk και απλά να υπάρχει σαν contrib. Θα ήταν καλή ιδέα γιατι χρειάζεται hackery για να τρέχεις και τα 2 ταυτόχρονα (μιας και παίρνουν το ίδιο όνομα στο site-packages).

Ακόμα δεν είναι πλήρες το branch, για παράδειγμα δεν έχουν newforms support κλπ. Επίσης σχεδιάζουν integration με google maps (και άλλα mapping services) ώστε το framework να κάνει generate το σωστό XHTML/javscript για το google maps. Πολύ ενδιαφέρον. (αν και σε non-trivial site η ποσότητα javascript είναι τέτοια που δεν βλέπω πόσο χρήσιμο μπορεί να είναι στην πράξη, αλλα αφού δεν έχω δεί τι πραγματικά σχεδιάζουν περιμένω να δώ για να έχω γνώμη.

Στα αρνητικά είναι οτι απαιτεί κάποια πακέτα (όπως το postgis αν χρησιμοποιείς postgresql) που δεν μπορείς να τα βάλεις έυκολα σε shared hosting  (στο webfaction όμως , απο το οποίο μέχρι στιγμής είμαι αρκετά ευχαριστημένος, στο βάζουν αν το ζητήσεις!)

Έχω φτιάξει μια  δοκιμαστική εφαρμογή και έχω διάφορες καλές ιδέες για νέα site. Ξεκινώ full ολοταχώς :p

posted at 23:07  ·   ·  django  greek
Click to read and post comments

Feb 21, 2008

Resolver is a great idea.

I was reading about Resolver quite often, mostly on planet python. I had made a mental memo to try it out for my self sometime but always seemed to be delaying it. I tried it out yesterday i was really impressed by the concept. 

With Resolver you have a spreadsheet with tightly integrated python programmability. The window has 3 panes. The ‘spreadsheet’ pane, the Python editor, and the ‘output’ pane. You can define any python function you like and make it available for use in your spreadsheet. This means you can do database connectivity using python, or advanced data manipulation that is either no possible with Excel, or very difficult.  I can’t tell you how many times i wished i could do that easily with Excel. In fact, i very often needed to pull data from the database of a Django project and do some manipulation using Excel , and present it to some users. But i wanted to use the Django ORM instead of raw SQL. With Resolver it should be very easy.

At least for me, it was often the case that i needed to solve a problem that was too complex for Excel, and too simple for writing a full-blown application. I needed something that would combine the best of both worlds.

Resolver is written in IronPython and uses Python for programming.  It might be possible to run it using mono but i haven’t tried anything of the sort. Ironpython supposedly runs on linux , but mono has an incomplete Forms implmementation.

Best of all, it free for non-commercial use, so you need to try it out.

Click to read and post comments

Oct 07, 2007

Displaying a message after a POST/Redirect/GET

I use the POST/Redirect/GET (RPG) pattern to avoid multiple submission problems. Sometimes however i want the GET part to display a message related to the POST part. Eg, “Your change has been done. Thank you” without creating special view just for this.
I found the solution to this problem in the www.djangosnippets.org site. It’s snippet no 319 titled “Flash Message Template Tag”.
What it does is use sessions and a special template tag to display a message in the template and immediately delete it from the session. After you set it up, all you have to do is something like this:

request.session['flash_msg'] = 'Your FOO has been BARed'
request.session['flash_params'] = {'type': 'success'}

Thanks Robert Conner!

posted at 23:37  ·   ·  django
Click to read and post comments

Aug 17, 2007

Captcha widgets in Django

This blog was getting stormed by comment spam so i decided to use some captcha protection. I chose pycaptcha for this and had to integrate it nicely with Django. I wanted to make it an easy widget that does it’s own validation and does not require view code for validating the captcha. So here is what i came up with:

EDIT: Updated for SVN trunk version of Django ( value_from_datadict has an extra parameter)

First, the custom form field:


class CaptchaField(CharField):
widget = CaptchaWidget
def __init__(self, *args, **kwargs):
super(CaptchaField, self).__init__(*args, **kwargs)


def clean(self,values):
value,captchaid = values
#Check if the value matches that in the session
factory = PersistentFactory('/tmp/captcha_persistence')
test = factory.get(str(captchaid))
factory.storedInstances.close()
if (not test) or (not test.valid):
raise ValidationError(u"""Captcha does not exist or is
invalid. Please refresh to get new captcha""")
if not test.testSolutions([value.strip()]):
raise ValidationError(u"""You did not type the correct
captcha text""")
return ""

The Widget used by this form field:


class CaptchaWidget(Input):
input_type = "text"
def render(self,name,value,attrs=None):
#Always generate a new captcha on rendering
factory = PersistentFactory('/tmp/captcha_persistence')
test = factory.new(Tests.PseudoGimpy)
factory.storedInstances.close()
captchaid = test.id
superclass_output = super(CaptchaWidget,self).render(name,"",attrs)
output = """<img src="/captcha/%s" border="0"/><br/>
<input type="hidden" name="captchaid" value="%s" />
%s
""" %(captchaid,captchaid,superclass_output)
return output

def value_from_datadict(self,data,files,name):
return [ data[name],data['captchaid'] ]


We also need a view to deliver the image for the captcha so here it is:

def captcha_image(request,id):

factory = PersistentFactory('/tmp/captcha_persistence')
test = factory.get(str(id))
factory.storedInstances.close()
if not test:
return HttpResponseNotFound()

response = HttpResponse(mimetype='image/jpeg')
test.render().save(response,'JPEG')
return response


Dont forget to add a urls.py entry for our view. something like:

(r'^captcha/(?P<id>\w+)','blogon.blog.views.captcha_image'),

should be enough (change the view to your view. If you change the url then be sure to change it in the html generated by the widget.

. Notice that in our code we use a file in /tmp for writing the persistence of pycaptcha. This may be a security problem depending on our environment. So be careful, or us a trusted location.
I should use a template for the HTML generated by the widget so as to make it more easily customizable but i just got bored after i got it working.

Click to read and post comments

Jul 11, 2007

doesn’t pownce.com support unicode properly?

I was invited to pownce.com today. It’s a hot new Web 2.0 messaging applications that is also developed by a really hot developer in a hot language using a hot framework.
It’s still in BETA and the new accounts are invite only. I suspect that most people have accounts and the whole ‘invite’ thing is a way to make it sound cool. After all, upon registration you get 6 invites automatically. It’s not like they are controlling how fast they grow like google did by giving invites using their own criteria to control growth.
Being in BETA it means there are bugs, and features missing. It seems that one particular bug is rather significant. My Greek friends tried to send messages in Greek (in unicode encoding) and all they got was question marks. Not being able to communicate in my native language is a major bug. I am also a little curious as to why this is happening. Django and Python have excellent unicode support, Mysql (horrible as it may be) has unicode support, so what gives?
Click to read and post comments
← Previous Page 2 of 2