Login

All snippets written in Python

2956 snippets

Snippet List

Apache X-sendfile with permissions checking

This allows the mod_xsendfile module for Apache safely serving private files. Django take cake about processing and permissions checking, Apache server requested files. Installation of mod_xsendfile: $ tar -xzvf mod_xsendfile-0.12.tar.gz $ /usr/sbin/apxs -c mod_xsendfile-0.12/mod_xsendfile.c $ ld -Bshareable -o mod_xsendfile-0.12/mod_xsendfile.so mod_xsendfile-0.12/mod_xsendfile.o Copy mod_xsendfile.so to your local Apache modules folder. Modify httpd.conf to load an enable the module: LoadModule xsendfile_module modules/mod_xsendfile.so Add to virtual host container: <Virtual ...:80> XSendFile On XSendFilePath /home/django_projects/mysite/media/ </Virtual>

  • http
  • media
  • static
  • xsendfile
Read More

Custom requests auth class for Tastypie API key authentication

In case you ever use [requests](http://python-requests.org/) (or [slumber](http://slumber.in/)) to do requests against a Tastypie API that requires API key authentication, this small custom auth class will help you. Use it like that (with requests): auth = TastypieApiKeyAuth('jezdez', '25fdd0d9d210acb78b5b845fe8284a3c93630252') response = requests.get('http://api.foo.bar/v1/spam/', auth=auth) or with slumber: auth = TastypieApiKeyAuth('jezdez', '25fdd0d9d210acb78b5b845fe8284a3c93630252') api = slumber.API("http://api.foo.bar/v1/", auth=auth) spam = api.spam().get()

  • api
  • auth
  • tastypie
Read More

Database template loader

This snippet provides getting templates from the model in database. We work with templates as usual (using as a template name value of the field **"slug"**). You can do your own application without "TemplateTypes" model - it's added for ability to filter templates. You can use choices or remove "template_type" field and "TemplateTypes" model at all. For ease of editing, you can connect all this to the admin interface, adding to the field "template_body" widget with syntax highlighting (I used [CodeMirror](http://codemirror.net/)).

  • templates
  • template loader
Read More

Overcome the bulk_create() size limitation using SQLite

As of django 1.4, the newly introduced `bulk_create()` function has a strong limitation when using SQLite. Namely it causes an error if you want to create more than `999/F` objects, where `F` is the number of fields in the object type. The above `safe_bulk_create(objs)` function solves this issue by splitting the list of objects to appropriately sized bulks. This solution also works fine with other db backends, and according to my experiments, it causes no significant overhead comparing to using `bulk_create()` directly. For more details on the issue, see https://code.djangoproject.com/ticket/17788 Thanks to charettes for pointing out how to calculate the number of fields in an object.

  • bulk_create
Read More

Delete template fragment cache

Template: {% load cache %} {% cache 1800 posts blog.pk %} {# Show posts #} {% endcache %} Code: def view(request, pk): # Code blog = get_object_or_404(Blog, pk=pk) delete_template_fragment_cache('posts', blog.pk) # Code

  • template
  • cache
  • fragment
Read More

DRY menu Custom Template Tag

This is the description of a custom template tag to create DRY menu. It solves the problem of markup duplication in templates of your site. The menu always has one active option and one or several inactive options. HOW TO USE Define a structure of your menu in a parent template: {% defmenu "menu1" %} {% active %}<span class='active'>__text__</span>{% endactive %} {% inactive %}<a href='__url__'>__text__</a>{% endinactive %} {% opt "opt1" "/opt1/" %}Go to opt1{% endopt %} {% opt "opt2" "/opt2/" %}Go to opt2{% endopt %} {% opt "opt3" "/opt3/" %}Go to opt3{% endopt %} {% enddefmenu %} The menu has it's name (first parameter of the tag 'defmenu'. First parameter of a tag 'opt' is menu option's name. '__text__' inside of 'active'/'inactive' will be substituted by inner text of a tag 'opt' (Go to opt...), '__url__' indide of 'active'/'inactive' will be substituted by second parameter of a tag 'opt' To generate menu with one selected option in child template do: {% menu "menu1" "opt1" %} Here: "menu1" is a name of menu that was defined by 'defmenu' tag, "opt1" is selected option. Result of the applying 'menu' is the next: <span class='active'> Go to opt1</span> <a href='"/opt2/"'>Go to opt2</a> <a href='"/opt3/"'>Go to opt3</a>

  • menu
  • dry
  • menubar
Read More

Add

Based on an answer here: <http://stackoverflow.com/a/6288863>

  • groups
Read More

dropbox integration

This is a simple module for use with django to make a per user dropbox access simple Requirements: * standard django authentication * django sessions enabeled * dropbox python api >> easy_install dropbox To use this dropbox module you have to add the following configuration to your settings.py file `DROPBOX_SETTINGS = { 'app_key' : "insert key", 'app_secret' : "insert secret", 'type' : "app_folder", }` and of course to include it in INSTALLED_APPS `INSTALLED_APPS = ( ..., 'django_dropbox', )` to make a table to store personal access tokens for your users run >> python manage.py syncdb In your views you can import the dropbox_user_required decorator to mark views that should recive the named parameter dropbox_client ` from django_dropbox.decorator import dropbox_user_required @dropbox_user_required def myViewFunk(request, ..., dropbox_client): file = ... dropbox_client.put_file(file) `

  • dropbox
  • cloud-storage
Read More

Decode HTML Template Tag

This is useful if you have a string that is html encoded (i.e. "&lt;p&gt;Hello world!&lt;/p&gt;") and you want to do something more complex than just display it as html, such as using the striptags filter.

  • template
  • tag
  • django
  • templatetag
  • html
  • decode
Read More

Simple random file CAPTCHA

This is a snippet for a simple CAPTCHA. A random image from a list of images is shown, and the form checks if the correct solution was given. Normally I would use django-simple-captcha or maybe reCAPTCHA, but in this case I wanted to have a number of fixed images, nothing dynamically generated. I wanted to include the contact form in multiple pages, most of which are `direct_to_template` generic views. However, passing the random image to the `extra_context` of `direct_to_template` didn't work, because the value was only initialized once on startup. Therefore I pass the list of possible choices to `extra_context`, and use the template filter `|random` to select one image. The form's clean method will check if the correct solution was given when `form.is_valid()` is called in the view. If not, the view will display a new captcha. Of course there are other, more elegant solutions like a custom template tag or overriding the generic view, but this works fine for me. Using a fixed number of images will probably not provide good protection for sites that are of much interest to spammers, but for smaller sites it should be sufficient. You can see the CAPTCHA in action at [http://www.lackieren-in-polen.de/](http://www.lackieren-in-polen.de/)

  • captcha
  • random
Read More