Drop all tables in MySQL database
Drops all tables in MySQL table (either managed by Django or not). Useful when resetting the database with no CREATE DATABASE privileges. MySQL-only.
- sql
- mysql
- maintenance
Drops all tables in MySQL table (either managed by Django or not). Useful when resetting the database with no CREATE DATABASE privileges. MySQL-only.
According to the KML Specification, Polygons must be oriented according to the Right-Hand Rule (Counter Clockwise orientation) for them to display correctly in Google Earth. Since not all Polygons are defined according to the Right-Hand Rule, you can use this code to orient them correctly when using GeoDjango. Thanks goes to jbronn of #geodjango on irc.freenode.net for this!
Defining a custom template tag consists of three parts: a compiling function, a rendering `Node` subclass and a tag registration with `register.tag()`. The latter can be used as a (function) decorator on the compiling function, simplifying things into two parts. A neat fact is that `register.tag()` can actually be used as a class decorator in Python 2.6+ to condense all steps into the `Node` subclass. The compiling function simply becomes the `__init__()`. Below are two variants of the 'current_time' tag example from the Django docs: the first passing an explicit tag name and the second using the class name instead.
Django Filebrowser (http://code.google.com/p/django-filebrowser/) provides a signal called filebrowser_post_upload. Adding this method to a model that has a FileBrowse field will cause uploaded .zip files to be detected and unzipped on the server in place, then deleted (leaving their folder behind). It will also delete the garbage __MACOSX folder created by Mac zip files. This is probably not safe to do for publicly uploaded files. The use case here was to allow journalists to upload entire SoundSlides projects into a custom CMS.
This is my attempt at a convenience class for Django model choices which will use a [Small]IntegerField for storage. It's very similar to [jacobian's version](http://www.djangosnippets.org/snippets/1664/), but I wanted to be able to use simple attributes for access to the integer values. It's not technically dependent on Django, but it's probably not a datatype that would be useful for much else. Feel free to do so however if you have a use-case. >>> statuses = Choices( ... ('live', 'Live'), ... ('draft', 'Draft'), ... ('hidden', 'Not Live'), ... ) >>> statuses.live 0 >>> statuses.hidden 2 >>> statuses.get_choices() ((0, 'Live'), (1, 'Draft'), (2, 'Not Live')) This is then useful for use in a model field with the choices attribute. >>> from django.db import models >>> class Entry(models.Model): ... STATUSES = Choices( ... ('live', 'Live'), ... ('draft', 'Draft'), ... ('hidden', 'Not Live'), ... ) ... status = models.SmallIntegerField(choices=STATUSES.get_choices(), ... default=STATUSES.live) It's also useful later when you need to filter by your choices. >>> live_entries = Entry.objects.filter(status=Entries.STATUSES.live)
There's no direct way to save the contents of a URL to a Django File field: you're required to use a File instance but those can only safely wrap normal files, not the file-like object returned by urllib2.urlopen. Several examples online use urllib.urlretrieve() which creates a temporary file but performs no error handling without writing a ton of hackish code. This demonstrates how to create a NamedTemporaryFile, fill it with the URL contents and save it, all using APIs which raise exceptions on errors.
I am not sure what to say about the state of PyAWS, or its future, what with the multiple forks available and lack of recent updates. The best version I've found is [http://github.com/IanLewis/pyaws](this one), a spiffed-up version of 0.2.2 by Ian Lewis. I wrote this class on top of PyAWS so I could have more pythonic/django-y calling conventions, and to isolate the calls in case I have to swap libraries or versions down the road. You may want to familiarize yourself with PyAWS before using this. You'll definately need Amazon web service login credentials and keys -- they're available [here](http://aws.amazon.com/) for free. personally I use it with [these monkeypatching and decorator-decorators](http://www.djangosnippets.org/snippets/1888/) -- at the top of my personal version of the file containing this snippet I use the two (non-silly) examples from that snippet, to make the PyAWS internal Bag collection class work for me. EXAMPLE USE: # search Amazon's product database (returns a list of nested dicts) from amazon import aws books = aws.search(q='raymond carver') lenses = aws.search(q='leica summicron', idx='Photo') # get the data for a specific ASIN/ISBN/EAN/etc ID number what_we_talk_about_when_we_talk_about_love = aws.fetch(qid='0679723056', idtype='ASIN')
as with all things related to monkeypatching, the caveat is to use things like these for good, and not for evil. I wrote these decorators because I did not want to rewrite all of [PyAWS](http://github.com/IanLewis/pyaws) -- instead I use these to add some standard/useful methods to the Bag collection that PyAWS uses internally. AN EXAMPLE: class PatchMyMonkey: pass @monkeypatch(PatchMyMonkey) def throwfecesat(self, who="nobody"): print "Throwing Feces At: %s" % who @monkeypatch(PatchMyMonkey) def nicoderm(self, why="no reason"): print "Trying To Quit Because: %s" % why return {'why':str(why)} trampstamp = PatchMyMonkey() trampstamp.throwfecesat(who="another monkey") print trampstamp.nicoderm(why="cigarettes are pricey") A LESS SILLY EXAMPLE: from pyaws import ecs @monkeypatch(ecs.Bag, fname='get') def get(self, param, *args, **kw): return self.__dict__.get(param, *args, **kw) @monkeypatch(ecs.Bag, fname='clearurls') def clearurls(self, *args, **kw): for k, v in self.__dict__.items(): if isinstance(self.__dict__[k], ecs.Bag): self.__dict__[k].clearurls(*args, **kw) if type(v) == type([]): [ii.clearurls() for ii in self.__dict__[k]] if type(v) == type(u''): if self.__dict__[k].count(u'http://') > 0: self.__dict__[k] = "<URL>" (amazon's URLs are extremely long, and can muddle your test/log output, hence that last function.) based on sample code from [here](http://pypi.python.org/pypi/decorator) and [here](http://mail.python.org/pipermail/python-dev/2008-January/076194.html).
An update to snippet 1718 (http://www.djangosnippets.org/snippets/1718/). This update lets you pass an image URL string as well as a model ImageField instance. This means that you can then create thumbnails on demand for images outside of model
Having things like DATABASE_NAME in the admin interface is handy if you're working on development and deployment systems. Replace the template admin/base_site.html with the template code. The variables to be displayed in the admin need to be exported into the environment before running the server. The python code shown is an example of a wsgi handler, and the same format can be used in manage.py for the development server.
This is yet another partitioning filter, but I wanted to be able to distribute my objects across two columns, and have them read from left to right, top to bottom. So if n = 2 this would return every other object from a queryset. With gratitude to the author of snippet 6.
Put this in your templates/admin/change_form.html template and you will be able to double click a manytomany in a multi select to be able to edit it.
This Snippet allows a view to controle the printed forms on the templates, in a similar way to the fieldsets used by the django admin. How to Use: In the view in question, put: def some_view(request): ... fieldsets = ( (u'Title 1', {'hidden' : ('field_1', 'field_2',), 'fields' : ('field_3',)}), (u'Title 2', {'hidden' : ('field_5', 'field_6',), 'fields' : ('field_4',)}),) ) return render_to_response('some.html', {'fieldsets': fieldsets}) fieldsets = ( (None, {'hidden' : ('evento', 'colaborador',), 'fields' : ('acompanhantes',)}), ) Next, in the html just add: <form enctype="multipart/form-data" id="edit" method="post" ...> ... {% include "inc/form_snippet.html" %} ... <input type="submit" value="Submit"> </form>
This Snippet allows for your project's apps names to be displayed as you want in the Admin, including translations. The lists of apps and languages are created from your settings.py file. **How to use** 1st part: - Create a application called 'apps_verbose' in you project with the models.py and admin.py showed here - Create a folder inside it with named 'templatetags' with the verbose_tags.py file inside it. - Add this app to installed apps in your settings.py - If you change this app name, dont forget to correct the imports. 2nd part: - Create a folder named 'admin' in your templates folder and copy the following files form your /django/contrib/admin/templates/admin/ folder. - /app_index.html - /base.html - /change_form.html - /change_list.html - /delete_confirmation.html - /delete_selected_confirmation.html - /index.html - /object_history.html - Make the necessary changes in each file, like shown here. 3rd part: - Create translations in the Admin and enjoy.
Set session to never expire in settings, and when remember_me is found false in login POST, set it to browser session expiry. Works only in Django 1+.
3109 snippets posted so far.