NinjaCipher:-*-:ro60

Archive for the ‘python’ Category

django template wrapper helper

Sunday, March 9th, 2008

edit:
# ericflo pointed out something that I totally forgot about… render_to_string. And he’s completely correct… It does exactly the same thing and most likely does it better… DOH! Well whatever… here is my version anyway ;)

—————————
Here is a handy little helper I cooked up for dynamically loading dynamic content into a reusable template wrapper. It takes the path of a template and a dict of vars to populate, compiles the template and returns the resulting value as a string. You can then pass in the resulting variable into your main context. Pretty simple but very useful.

def wrap_content(template, var_dict):
    from django.template import Context, loader
    c = Context(var_dict)
    t = loader.get_template(template)
    return t.render(c)

called like so

mod = wrap_content('shared/mod_wrapper.html',{'title':'test title','content':'content test',})

django alternate development settings file

Sunday, March 2nd, 2008

So lately I’ve been working out all the kinks of running Django via pydev/eclipse so I can do more dev on my local machine. Since localy I run windows but I deploy onto Ubuntu Linux I’ve come up with a little hack that lets me conditionally load my project settings based on which environment it’s being run in. It’s pretty basic but I find it very helpful. If anyone else has any other approaches pls shout em out.

So all you have to do is create a file with all your development settings in your project folder (same folder the normal settings.py is in) and name it development_settings.py. Besides that you just have to replace the original manage.py in your project with the following manage.py. This one looks to see if your settings.py has DEBUG = True and if so loads the development.py instead.


#!/usr/bin/env python
from django.core.management import execute_manager
try:
    import settings # Assumed to be in the same directory.

    if settings.DEBUG:
        import development_settings
        startup_settings = development_settings
    else:
        startup_settings = settings

except ImportError:
    import sys
    sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.nYou'll have to run django-admin.py, passing it your settings module.n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)n" % __file__)
    sys.exit(1)

if __name__ == "__main__":
    execute_manager(startup_settings)

Python vs Ruby on Google Blogs

Thursday, February 28th, 2008

Here is a graph I generated from trendrr.com to show how many search results google blogs returns for ruby vs python. It makes me happy on the inside to see them so neck and neck.

Django find_or_create helpers

Wednesday, February 27th, 2008

OK I’m going to be completely frank. You VERY likely could do this FAR better. But! I looked and have yet to see anything that does this for Django. So, here is my half ass’d attempt at two very helpful methods I picked up from my shady rails past. So by all means call doodoo on my methods and show me your skills because I would love to see someone else’s take on this. Again shouts out to perva.de for being the inspiration for these snippets and thank ifni for Django giving me reason to be excited about a web framework again.

find_or_create

def find_or_create(class_type, params, by='id'):
   if params == None:return None

   try:
      return class_type.objects.get(by+" = "+params[by])
   except class_type.DoesNotExist:
      obj = class_type()
      for column, value in params:
         if hasattr(obj, column):
         _member = getattr(obj,column)
         _member(value)

      obj.save()

      return obj

update_or_create

def update_or_create(class_type, params, by='id'):
   if params == None:return None

   try:
      obj = class_type.objects.get(by+" = "+params[by])
   except class_type.DoesNotExist:
      obj = class_type()

   for column, value in params:
      if hasattr(obj, column):
         _member = getattr(obj,column)
         _member(value)

         obj.save()

         return obj

called like so:

update_or_create(MyModel, request.POST, 'id')
find_or_create(MyModel, request.POST, 'id')