Login

All snippets written in Python

Snippet List

iPernity thumbnail helper

A simple template filter that detects [iPernity](http://www.ipernity.com) static URLs and creates clickable thumbnail for them. Use it with [Lightbox](http://www.huddletogether.com/projects/lightbox2/) or any other funky image overlay script. Your HTML code may contain this: <p>A short example <img src="http://...ipernity..." title="The Description" />.</p> After applying this filter it will become: <p>A short example <a href="http://...large version..." title="The Title"><img alt="The Title" src="http://...thumb version..."/> </a>.</p> Thats all! You may have a look at this: [iPernity and static URLs](http://www.ipernity.com/group/api-users/discuss/20098)

  • template
  • image
  • thumbnail
  • resize
  • ipernity
Read More

Using another memcached for sessions

This solves the problem of losing sessions data when you restart memcached. So you use a different memcached instance for sessions which you rarely restart. Use the above code and add the following to you settings.py SESSION_ENGINE = "kwippyproject.session_backend" SESSION_CACHE = 'memcached://127.0.0.1:11200/' (Above assumes that your session's memcached is running on port 11200) Feel free to contact me in case you need help. By [Dipankar sarkar](http://dipankar.name) [email protected]

  • django
  • python
  • memcached
  • sessions
Read More

Cheap direct_to_tempalte patterns

Django cheap-pages Methods to use when you just want to use the Django dispatcher and there will be no extra business logic in your pages. In some cases flatpages is too flat, and store templates in DB is too much hassle >>> url(^name/$, ... direct_to_template, ... {'template': 'name.html'}, ... name='name') can be expressed as: >>> page('name') urlpatterns = patterns('', ...) urlpatterns += build('Regexp', ['page1', 'page2', ...])

  • generic
  • urlpatterns
  • direct_to_template
Read More

A form field for valdating PDF and Microsoft Word document

This is form field for PDF or Microsoft Word Document (both .doc and .docx) It will validate the file uploaded as a valid PDF and MS Word Document. It extends a forms.FileField, so you can put all the arguments relevant to FileField. IMPORTANT NOTE: The method of validation is actually run thru *nix OS shell command 'file', therefore, 1. only *nix system can use this class. 2. The file uploaded must be saved on disk, meaning you need to set your upload handler to use TempoaryFileUploadHandler Only. # (i.e. put this in your settings.py) FILE_UPLOAD_HANDLERS = ( "django.core.files.uploadhandler.TemporaryFileUploadHandler", )

  • pdf
  • microsoft-word-document
  • form-field
Read More

Add a print link to the admin to print individual record

This is a view that can be used to add a print button (link) in the admin change form for an individual record. Pretty simple to use. A nice enhancement to it is to be able to pull the model field name tie with the field value, something like making {{ object.as_dl }} available to the template. PS: The code is a modification from the django.views.generic.list_detail.object_detail

  • print
  • admin
  • record
Read More

Child aware model inheritance

Base models aren't aware of its inherited models, here is a quick solution to access child models from the base model.

  • model
  • inheritance
  • child
  • content-type
Read More
Author: rix
  • 2
  • 4

Test Integration for mako templates

This is a small addition to the mako template processing plugin for django that allows you to use the unit test framework with mako templates. To install, put the code into a file on your python path, and add the python path to your settings.py file. For example, if you install the code at /usr/lib/python2.5/site-packages/mako_django/test_integration.py you would add the following line to settings.py: TEST_RUNNER="mako_django.test_integration.run_mako_tests" This code will still call all of the normal test code, it just adds the mako template handler onto the list of things that are monitored.

  • mako
  • testing
  • test
  • mako-django
Read More

ShowOnly widget for froms

This form widget allows you to just display data in a rendered form, not giving the user the opportunity to change it. The initial data will just be carried through the form and showed to the user. In combination with snipped [1184](http://www.djangosnippets.org/snippets/1184/) you can make this even tamper safe. ;-)

  • newforms
  • forms
  • display
  • content
Read More

Tamper safe HiddenFields

This snippet prevents people from tampering with the data in hidden form fields. This is something you usually want unless you have some Javascript Vodoo going on on the browser side. For the people scratching their heads: This form class will dynamically create a clean function for every passed additional hidden field, which just returns the original value of the hidden field. So the data in the hidden field posted gets actually ignored when calling the (overwritten) clean_{field name} function. This class is just an example using the protected hidden field feature for all passed field variables, which is probably not what you want. You have to add the editable fields the end of the __init__ function in the class. Example: `self.fields['bestbeer'] = forms.CharField(max_length=23)`

  • newforms
  • field
  • hidden
  • froms
Read More

Hierarchical Flatpage Tag

Two template tags (I keep them in an app called "utils") handy for building menus out of flatpages. I only have two levels of hierarchy, and the frontpage is dynamic, so that's what it does. There is some flexibility, however, in that you can change the regex that defines a "root" page. You can pass it the flatpage object that is normally given to a flatpage template, or you can pass it a string (for any other page). Deliberatley does not create any markup -- flexibility is key, so it adds the list of root urls or child urls as a name in the current context. You get to pick the name. Please visit the [GitHub archive](http://wiki.github.com/0sn/nameremoved/flatpages) where i keep this up to date, there's a better explanation and you can see it in use.

  • tag
  • flatpage
  • hierarchy
Read More
Author: 0sn
  • 1
  • 6

Django Paypal

This is a snippet to use Paypal with python. This modifies Mike Atlas's geocept Paypal library. (Which was not working, for me at least) and adds recurring payments. I would try to write an article explaining how to use this and update the snippet.

  • paypal
  • payments
  • payment-processors
Read More

SuperChoices

Seeing [snippet 1178](http://www.djangosnippets.org/snippets/1178/) reminded me that I also had a go at writing a Choices class at some point. I'm content with the result, but I doubt xgettext will discover your translation strings, which will no doubt be inconvenient. Here it is anyway, in all its overly-complicated glory :-) The following demo was pulled from the function's docstring tests. >>> simple = Choices("one", "two", "three") >>> simple Choices(one=0, two=1, three=2) >>> tuple(simple) ((0, u'ein'), (1, u'zwei'), (2, u'drei')) >>> (0, _('one')) in simple True >>> simple.ONE 0 >>> hasattr(simple, 'FOUR') False Ordering just follows the order that positional arguments were given. Keyword arguments are ordered by their value at appear after positional arguments. >>> [ key for key, val in simple ] [0, 1, 2] >>> Choices(one=1, two=2, three=3) Choices(one=1, two=2, three=3) A Mix of keyword and non-keyword arguments >>> Choices("one", two=2, three=3) Choices(one=0, two=2, three=3) Automatically generated values (for "one" below) should not clash. >>> Choices("one", none=0, three=1, four=2) Choices(one=3, none=0, three=1, four=2) Here is an example of combined usage, using different object types. >>> combined = Choices(one=1, two="two", three=None, four=False) >>> len(combined) 4 >>> (1, _('one')) in combined True >>> ('two', _('two')) in combined True >>> (None, _('three')) in combined True >>> (False, _('four')) in combined True And here is an empty choices set. Not sure why you would want this.... >>> empty = Choices() >>> empty Choices()

  • models
  • choices
Read More

Login Required Middleware

Sometimes it's a real pain to use the `@login_required` decorator all over the views of a complicated site. This middleware requires login on every page by default and supports a list of regular expression to figure out the exceptions. This way you don't have to worry about forgetting to decorate a view. This snippet requires `LOGIN_URL` to be set in settings.py, and optionally allows you fill out `LOGIN_EXEMPT_URLS`, a tuple of regular expressions (similar to urls.py) that lists your exceptions. Example: `` LOGIN_EXEMPT_URLS = ( r'^about\.html$', r'^legal/', # allow the entire /legal/* subsection ) ``

  • middleware
  • authentication
Read More

Choices class

Yet another class to simplify field choices creation. Keeps order, allows i18n. Before: ONLINE = 0 OFFLINE = 1 STATES = ( (ONLINE, _('online')), (OFFLINE, _('offline')) ) state = models.IntegerField(choices=STATES, default=OFFLINE) After: STATES = Choices( ('ONLINE', _('online')), ('OFFLINE', _('offline')) ) state = models.IntegerField(choices=STATES, default=STATES.OFFLINE)

  • models
  • choices
Read More
Author: dc
  • 7
  • 8

2955 snippets posted so far.