This is an update to [snippet 765](http://www.djangosnippets.org/snippets/765/) as I was having trouble getting it to work on branches/newforms-admin @ r7771.
There are just a few minor changes to the previous snippet, all simple stuff. I went ahead an added can_delete and can_order options that the previous snippet didn't include.
[More details in blog post](http://paltman.com/2008/06/29/edit-inline-support-for-generic-relations/).
when you deploy djangos apps, some servers have problems resolving the absolute path of some files (e.g: sqlite3 + lighttpd + apache), using the snippet above solves this issue :)
As users would login to their accounts to update their CC info, the expiration date always threw them off. The default format for displaying a datetime.date object is
>YYYY-MM-DD
Obviously the expiration date on your credit card uses the MM/YY format. I finally got around to creating a custom field/widget to handle this particular piece of data.
Use like so...
class CustomerForm(forms.ModelForm):
cc_exp = DateFieldCCEXP()
class Meta:
model = Customer
Here is a Django template tag that allows you to create complex variables specified in JSON format within a template.
It enables you to do stuff like:
{% var as person %}
{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
"212 555-1234",
"646 555-4567"
]
}
{% endvar %}
<p>{{person.firstName}}, </br>
{{person.address.postalCode}}, </br>
{{person.phoneNumbers.1}}
</p>
This tag also enables me to do dynamic CSS using as follows:
# urlpatters
urlpatterns = patterns('',
(r'^css/(?P<path>.*\.css)$', 'get_css'),
)
#views
def get_css(request, path):
return render_to_response('css/%s' % path, {},
mimetype="text/css; charset=utf-8")
# dynamic css within in /path/to/app/templates/css'
{% load var %}
{% var as col %}
{
"darkbg": "#999",
"lightbg": "#666"
}
{% endvar %}
{% var as dim %}
{
"thinmargin": "2em",
"thickmargin": "10em"
}
{% endvar %}
body {
background: {{col.darkbg}};
margin: {{dim.thinmargin}};
}
Simple decorator that checks for authentication and activation of account and redirect to login or activation page if needed
Your ulrsconf file must have named urls with parameters that you call that decorator
Dont forget to import reverse function
from django.core.urlresolvers import reverse
Modify a query string on a url. The comments in the code should explain sufficiently. String_to_dict, and string_to_list are also useful for templatetags that require variable arguments.
Warning: This python script is designed for Django 0.96.
It exports data from models quite like the `dumpdata` command, and throws the
data to the standard output.
It fixes glitches with unicode/ascii characters. It looked like the 0.96
handles very badly unicode characters, unless you specify an argument that is
not available via the command line. The simple usage is:
$ python export_models.py -a <application1> [application2, application3...]
As a plus, it allows you to export only one or several models inside your
application, and not all of them:
$ python export_models.py application1.MyModelStuff application1.MyOtherModel
Of course, you can specify the output format (serializer) with the -f
(--format) option.
$ python export_models.py --format=xml application1.MyModel
Here's a simple way to transparently encrypt a field (just repeat the idiom for more than one) so that it is not stored plaintext in the database. This might be useful for social security numbers, etc.
The storage size for the ciphertext depends on which algorithm you use. Blowfish here requires 32 characters to store an encrypted 16 characters. Note also that Blowfish requires a block size of a multiple of 8, so that is what the repeat in the _set_ssn() is all about.
The Crypto module is from http://www.amk.ca/python/code/crypto.html
I make no claims as to how secure this scheme is overall, comments on this are welcome.
`backupdb` command allows to make a database backup automatically. It's supposed to do this just before a `syncdb`, `reset` or `flush` command in a server deployment. A usual upgrade task in production server could be:
./manage.py backupdb
./manage.py reset myapp
./manage.py syncdb
Put this code in your project's `management/commands/backupdb.py` file.
Usage: (if you save it as pigmentation.py as I did)
{% load pigmentation %}
{% autoescape off %}
{{ somevariable|pygmentize }}
{% endautoescape %}
There already a few of this code around, but this one is pretty clean, and includes css. It also works in both the development server and Dreamhost (python2.4 in my django config) without any unicode problems.
Decorator, written for views simplification. Will render dict, returned by view, as context for template, using RequestContext. Additionally you can override template, returning two-tuple (context's dict and template name) instead of just dict.
Usage:
@render_to('my/template.html')
def my_view(request, param):
if param == 'something':
return {'data': 'some_data'}
else:
return {'data': 'some_other_data'}, 'another/template.html'
Just add it in templatetags/delicious.py
In your template:
<h3>Del.icio.us</h3>
<ul class="list">
{% load delicious %}
{% load_delicious_links %}
{% for link in delicious_links %}
<li><a href="{{link.link}}">{{link.title|safe}}</a></li>
{% endfor %}
</ul>
This creates a fixture in the form of a python script.
Handles:
1. `ForeignKey` and `ManyToManyField`s (using python variables, not IDs)
2. Self-referencing `ForeignKey` (and M2M) fields
3. Sub-classed models
4. `ContentType` fields
5. Recursive references
6. `AutoField`s are excluded
7. Parent models are only included when no other child model links to it
There are a few benefits to this:
1. edit script to create 1,000s of generated entries using `for` loops, python modules etc.
2. little drama with model evolution: foreign keys handled naturally without IDs, new and removed columns are ignored
The [runscript command by poelzi](http://code.djangoproject.com/ticket/6243), complements this command very nicely! e.g.
$ ./manage.py dumpscript appname > scripts/testdata.py
$ ./manage.py reset appname
$ ./manage.py runscript testdata
based on Snippet [799](http://www.djangosnippets.org/snippets/799/) but added code inspection capabilities. Credit goes to django-logging for the actual inspection code.
Got the idea from the [This Week in Django](http://blog.michaeltrier.com/2008/6/18/this-week-in-django-26-2008-06-16) Podcast ;)
This adds the filename, lineno, functionname and the actual python-code line which caused a sql statement to be executed.
Note that i am adding keys to 'connection.queries' dict on request, which may be dangerous, so use with care!
The code inspection functionality can be toggled via FRAME_INSPECT.