Login

All snippets written in Python

Snippet List

Logging to rotating files

It took me some time to figure out how to set up logging in Django. What I want to do is to log to a pair of rotating files each 1MB in size. What I come up with is this code segment in the `settings.py` file.

  • settings
  • logging
Read More

Many 2 Many Admin Ordering with Mysql

My Models has a FK to translations and also a many 2 many to categories which also them are translated With this code I concatenate the translation of the categories and allow the changelist to order them. works only on mysql but you can adapt to your DB SET SESSION is required by mysql.

  • admin
  • mysql
  • m2m
  • ordering
Read More

Save Geolocation for an Address based model on save()

This code will work on any model using correct address data in its fields that also require latitude and longitude data to be updated on saving. It uses pythons own default urllib and json, so no need to install 3rd party stuff to make it work. This method is preferred to getting it on the fly, due to the OVER_QUERY_LIMIT that you will get when parsing many address, this way means it stays up to day in the model and will update when any part of the address changes.

  • Geolocation
  • Geolocation on save
  • Google Geolocation
Read More

Basic PDF view mixin and utils using reportlab.

Simplified version of the snippet that renders model to PDF [http://djangosnippets.org/snippets/2540/](http://djangosnippets.org/snippets/2540/) This PDF view mixin for Django Class Based Views. See working project example: https://github.com/elena/django-pdfmixin-example --- This is based on the case scenario where you have a model which has a `DetailView`. You then construct a bespoke PDF for the same model that is unrelated in any way to the `DetailView`. The PDF needs to be returned as a `HTTPResponse` object. The model object is provided.

  • pdf
  • mixin
  • reportlab
  • class-based-views
  • cbv
Read More

A Lazy ModelChoiceField implementation

Sometimes we may need to generate a *ModelChoiceField* in which choices are generated at runtime, depending on the locale language. The snippet generates a *ChoiceField* based on a queryset and a specific attribute of the Model, ordering the choices by the attribute content in the locale language. **Usage example** (inside a form declaration) country = LazyModelChoiceField(sort_by='name', queryset = \ Country.objects.all, empty_label=_('All countries'), label=_('Country')) Based on lsbardel LazyChoiceField implementation (snippet 1767)

  • forms
  • model
  • field
  • ModelChoiceField
Read More

South unran migration check

Middleware that checks if you ran all South migrations. If not, it will throw an exception. Make sure to only use this middleware in development!

  • middleware
  • migration
  • south
Read More

model queries to Google Visualization DataTables

Helper functions to use the Google Visualization API in Django. Module stores queries to use and creates a JSon representation of a Google Visualization DataTable for each by inspecting the query's model. Looks up any choices defined for the columns. Depends on Google Visualization Python API: https://code.google.com/p/google-visualization-python/ To use, define the queries to use with the Visualization API at the head of views.py with: add_query(queryset, 'query name') then in view functions, get the JSon representation of a Google Visualization DataTable for the query with: my_json = get_viz_json('query name') Within the HTML template, pipe the JSon through safe: var dataTable = new google.visualization.DataTable( {{ my_json|safe }} );

  • Google Visualization
  • charts
Read More

Password Obfuscation Log Filter

This is a simple logging [filter](https://docs.djangoproject.com/en/1.5/topics/logging/#topic-logging-parts-filters) to ensure that user-entered passwords aren't recorded in the log or emailed to admins as part of the request data if an error occurs during registration/login.

  • filter
  • log
  • password
  • logging
  • obfuscation
Read More

Multi-DB Reconnecting Persistent Postgres Connection

This is a modification of http://djangosnippets.org/snippets/1707/ that handles the database going down or PG Bouncer killing the connection. This also works in things like Twisted to make sure the connection is alive before doing a real query. Thanks @mike_tk for the original post! EDIT: Updated the wrapper to handle multi-db. Before it was using the first connection it made, now it creates an attribute name for the connection based on the name of the database.

  • database
  • multi-db
  • twisted
  • connection
  • persistent
  • multiple-databases
  • socket
  • web-socket
Read More

More generic CSV export admin action factory

based on #2020 This one is even more generic than the previous generic ones since you can specify in fields any attribute you will give to the admin interface and not just fields from the model. You can for example directly export the list_display as a list of fields, including look-ups for related attributes that you may have defined in a function inside the Admin Class

  • admin
  • csv
  • related
  • admin-actions
Read More

model save memos

A simple example to show how to manage an history of memo-on-save. Each time the user saves the model, he must provide a memo message. The full memos history is shown as a read-only tabular-inline. For each memo, user and date are also registered ([Django-Admin-Collapsed-Inlines](https://github.com/virajkanwade/Django-Admin-Collapsed-Inlines) could be usefull)

  • admin
Read More

2955 snippets posted so far.