views:

122

answers:

2

Where should utility functions live in Django? Functions like custom encrypting/decrypting a number, sending tweets, sending email, verifying object ownership, custom input validation, etc. Repetitive and custom stuff that I use in a number of places in my app. I'm definitely breaking DRY right now.

I saw some demos where functions were defined in models.py, although that didn't seem conceptually right to me. Should they go in a "utilities" app that gets imported into my project? If so, where do they go in the utilities app? The models.py file there?

Thanks for helping this n00b out.

UPDATE: Let me be even more specific. Say I need a function "light_encrypt(number)" which takes the param "number", multiplies it by 7, adds 10 and returns the result, and another function "light_decrypt(encr_number) which takes the param "encr_number", subtracts 10, divides by 7 and returns the results. Where in my Django tree would I put this? This is not middleware, right? As Felix suggests, do I create a python package and import it into the view where I need these functions?

+2  A: 

Different question but same answer:

My usual layout for a django site is:

projects/
templates/
common/
local/

Where:

  • projects contains your main project and any others
  • common contains things you may share across sites, or are at least not project-specific, like if you need to download django-profile and django-registration rather than having it directly in python/site-packages
  • templates contains just that
  • local contains things that are going to be specific to the current machine, so that you can have properly separated data, like database location and password - I then soft-link the machine-specific versions (say "machine1-localconfig.py") to local/localconfig.py and then can "import localconfig" in settings.py
  • I generally put middleware that's project-specific inside a project, and middleware that's not project-specific in common/middleware/
  • make sure to add the templates directory to the right place in settings (or most probably, localconfig.py and then import it in settings), and makse sure to add the projects, common, and local directories to your PYTHONPATH.
eruciform
Thanks for the response, eruciform. So, with my "light_encrypt(number)" example above, you'd suggest that I put this in common?
mitchf
yeah, if you're going to use it in another project one day, better to have it in `common/util/encrypt.py` (or whatever) and make sure it doesn't have internal links to anything project-specific. it'll be easier to reuse, and also easier to port if you need to move things around later. also just cleaner from a data-hiding point of view. :-)
eruciform
oh, if you put it in `common/util/encrypt.py`, make sure to touch a blank `common/util/__init__.py` so that `from common.util.encrypt import light_encrypt` works properly
eruciform
+1  A: 

OK, after reading the comments and answer here I've decided to create a directory called "common/util/" inside my project directory. Within that I have a file "__ init__.py" where I have my little helper functions.

I guess if the file gets too big, I'll then split out the functions into individual .py files in common. So now, my project structure looks like this. Please correct if I'm making any poor choices, I'm early enough in development that I can fix it now while it is still easy to do so!

myproject/         (Django project) 
  common/  
    util/
      __init__.py  (helper functions)  
  middleware/      (custom middleware)  
  templates/       (project templates)  
  myapp/
    fixtures/      (initial data to load)
    migrations/    (south migrations)
    urls/
    views/
    admin.py
    forms.py
    models.py
    test.py

 public/           (static stuff not served by Django)
   media/
     css/
     img/
     js/
     lib/
mitchf
FWIW, I see in the django-profiles app that ubernostrum created a utils.py file in the same directory as urls.py and views.py to hold his utility functions
mitchf