Login

All snippets written in Python

Snippet List

limit view request rate decorator

Limit rate request decorator for view. Authenificated user can't request decorated view often then timeout. Usage: @limit_request_rate(time_beetween_request_sec) def my_view(request): ... get_cell_value from [here](http://code.activestate.com/recipes/439096/)

  • django
  • cache
  • view
  • decorator
  • limit-request-rate
  • closure
  • cell
Read More

StaticField for non-changing text data in forms

This Field, Widget and the three lines of code in the form's clean method allow an easy (and hopefully general) way to add simple, unchangable text to a form. Example use: class OnlyTextForm(forms.ModelForm): points = StaticField(label=_('Points')) reviewer = StaticField(label=_('Reviewer')) def clean(self): for name, field in self.fields.items(): if isinstance(field, StaticField): self.cleaned_data.update({name: self.initial[name]}) return self.cleaned_data

  • forms
  • field
  • widget
Read More
Author: V
  • 1
  • 2

pavement file for deploying django projects

This is the pavement file I use to deploy a django site. It's in early stages. Right now it copies everything up to the desired server over scp. But I think I'll change it to rsync in the future. It requires pavement, and pexpect. The pavement file takes slight instruction from your settings.py file. For server information, and "lib_apps" to copy into the lib directory. An example of a settings file that I use with this pavement file: http://gitweb.codeendeavor.com/?p=django_empty.git;a=blob_plain;f=settings.py;h=23bda7d2a1eb2a52ca0859004ecccd206dade4ec;hb=5d672178dab282caeed5ff0de7ed807c72e44f74 Specifically, check out the bottom for two vars: "LIB_APPS" and "DEPLOYMENTS" A good example of my empty django project is here: http://gitweb.codeendeavor.com/?p=django_empty.git;a=tree;h=5d672178dab282caeed5ff0de7ed807c72e44f74;hb=5d672178dab282caeed5ff0de7ed807c72e44f74 I think the one thing that's missing is a way to re-spawn fcgi processes. Which I'll hopefully get around to adding sometime soon. Also, I need to do a little work at making sure source control files don't get pushed through scp.

  • django
  • python
  • deploy
  • paver
  • pavement
Read More

range tag

Create a list containing an arithmetic progression that can be iterated through in templates. Emulate the [range](http://docs.python.org/library/functions.html#range) syntax. You can use either numbers or variables. Syntax: {% num_range [start] stop [step] as some_range %} {% for i in some_range %} ... do something {% endfor %} **About the author**: Take a look at [my website](http://www.marcofucci.com)

  • template
  • tag
  • django
Read More

"an" filter

A template filter which returns "a" or "an" depending on the phonetic value of given text.

  • template-filter
  • grammar
Read More

Absolute URL Templatetag

The {% url %} templatetag is awesome sometimes it is useful to get the full blown URL with the domain name - for instance for links in emails. The **{% absurl %}** templatetag mirrors the behaviour of {% url %} but inserts absolute URLs with the domain of the current Site object. Usage: {% absurl viewname %} >>> http://www.example.org/my/view/

  • url
  • templatetags
  • absolute
  • uri
Read More

Link TemplateTag that checks for permissions and url address

This template tag was built to be used in web applications that are permission based. It renders the html for an html link tag if the user has permissions to access the view (if not, returns an empty string). It also checks if the current token is the active url address and, if so, adds class="active" to the html link for presentation purposes. Example usage: 1. {% url home as home_url %} {% get_link_if_allowed home_url "Home" %} 2. {% url project_dashboard project.id as project_dashboard_url %} {% get_link_if_allowed project_dashboard_url "Projects" %}

  • template
  • url
  • permissions
  • address
Read More

Word-boundary-aware string truncation template filter

This is a custom template filter that allows you to truncate a string to a maximum of num characters, but respecting word boundaries. So, for example, if `string = "This is a test string."`, then `{{ string|truncatechars:12 }}` would give you "This is a..." instead of "This is a te".

  • template
  • filter
  • truncate
  • words
Read More

Extend simplejson to understand closures, functors, generators and iterators

For most applications, simplejson.dumps() is enough. But I’m especially fond of iterators, generators, functors (objects with a `__call__()` method) and closures, dense components that express one thought well: the structure of a tree, or the rows of a database, to be sent to the browser. The routine dumps() doesn’t understand any of those things, but with a simple addition, you can plug them into your code and be on your way without headache. Dumps() just calls JSONEncoder(), and JSONEncoder has a routine for extending its functionality. The routine is to override a method named default() (why “default?” I have no idea) and add the object types and signatures you want to send to the browser. Normally, this exists for you to provide custom “object to JSON” handlers for your objects, but there’s nothing custom about iterators, generators, functors and closures. They are native Python objects. This snippet provides the functionality needed by JSONEncoder to correctly dereference these useful Python objects and render their contents. (Originally posted [here](http://www.elfsternberg.com/2009/05/20/fixing-an-omission-from-djangos-simplejson-iterators-generators-functors-and-closures/) )

  • ajax
  • python
  • json
Read More

MultiFormWizard

This is an extended version of the FormWizard which allows display of multiple forms per step and allows usage of ModelForms with initial objects

  • multiple
  • forms
  • form
  • wizzard
  • multi-form-per-step
Read More

VAT field

VAT field with a field to select the country and a free field for the code

  • django
  • fields
  • forms
  • form
  • field
  • formfield
  • vat
  • tva
Read More

Copy media files to central location for easier sharing

I was in need to have pluggable components that all have more or less some media files. I didn't want to pollute my config with dozens of media paths so I wrote this custom command that copies contents `<appdir>/app_media` to your `MEDIA_ROOT/<appname>` path. In template you will refer your media files like `{{MEDIA_URL}}/appname/<path to media>`

  • media
  • static
  • custom
  • command
Read More

2956 snippets posted so far.