Clone method for Django models
Add this method to any model to make it clonable.
- django
- models
- clone
Add this method to any model to make it clonable.
** Help me get better! If you vote (either way) please leave a comment if you have time and say what was good or bad. I appreciate any and all feedback. Thanks! ** I keep finding places in my apps where I need an isolated snippet of text that can periodically be changed from the admin interface. Most often it's html but sometimes it's text, javascript, or css. Use it like so: (Assuming this snippet lives in snippy_snip/models.py and there is a snippet named "Welcome Message" in the database) from snippy_snip.models import snip msg = snip("Welcome Message") Or, you might populate a parameter hash for a template: def showpage(request): params = { 'welcome': snip('Welcome Message'), 'video1': snip('Video 1'), 'NavHeader': snip('Nav.SectionHeader'), } return render_to_response("main.html", params) For clarity, *params* might look something like this: welcome -> "Welcome to our site. Please use the menu on the left..." video1 - > a YouTube snippet NavHeader -> Some HTML which comprises the top of a navigation menu. This is a very simple bit of code but I've found it very useful. It isn't intended for instant changes... Your snippets will cache like anything else, which may cause confusion if you expect immediate changes. And it's probably not great for a high traffic site, but for my moderate traffic sites and workgroup apps I've found it useful. (This code was created for 0.96, but I'm working to bring it into alignment with the latest svn version of Django, see comments.)
Is a very simple atlassian crowd integration, I know the client soap requests are not so elegant but they work perfecly! you need to install amara ..
**Update**: [Django AdMob](http://github.com/johnboxall/django_admob/tree/master) pluggable app on github. Given a Django ``request`` object and dict of admob parameters returns a Admob ad. If no ad can be retrieved displays a one pixel Admob tracker image. For the future: * Make a template tag for this? * Filter out other unneeded META parameters for the admob_post dict?
This snippet provides support for dynamically importing templates. This helps you to avoid naming collisions and other problems. The format is as follows: 1. `module.submodule.app:template.html` 2. `module.submodule.app:subfolder/template.html` 3. `module.submodule.app:/subfolder/template.html` Assuming the module is located in '/var/', these would map (respectively) to: 1. `/var/module/submodule/app/templates/template.html` 2. `/var/module/submodule/app/templates/subfolder/template.html` 3. `/var/module/submodule/app/templates/subfolder/template.html` The colon splits the the python module from the template directory, meaning you can import from anything that has a "templates" directory. This helps me to avoid naming collisions by specifying the application I'm referring to, without having to put long paths in my extends and include tags inside other templates. It's also dynamic in that if I move a library outside the old path, it has no effect on the templates. To get this rolling, in your `settings.py` file, add the following:: >TEMPLATE_LOADERS = ( > 'addons.template_loader.load_template_source', # <--- add this > 'django.template.loaders.app_directories.load_template_source', > 'django.template.loaders.filesystem.load_template_source', ># 'django.template.loaders.eggs.load_template_source', >)
When using mysql the sql that is generated by syncdb doesn't create the foreign key relationship in all cases. This code will run through a file called create_table.sql in which you store all your create sql statements ( use "python manage.py sqlall app1 app2 > create_table.sql" ) and outputs all the neccesary alter table scripts that add the foreign key. Its not 100% proof since the generated names can end up being more than 40 characters. Need to work on that. I have [written](http://vidyanand.wordpress.com/2008/06/16/is-it-a-mysql-or-django-fault/) about it a little more in detail.
Template: `<div id="messageBox_{{ forloop.counter0 }}" style="border:1px solid #ccc;background-color:white" onclick="document.getElementById ('messageBox_{{ forloop.counter0 }}').style.display = 'none';"> {% ifstartswith message "#ok#" %} <font color="green"> {% endifstartswith %} {% ifstartswith message "#error#" %} <font color="red"> {% endifstartswith %} {{ message|cut:"#ok#"|cut:"#error#" }} </font> </div>` In a view you can now do something like that: ` request.user.message_set.create(message="#ok#Hello User, this is a ok message!")`
This translates a given message with ugettext. That's it. `{% load where_you_have_it %}` `{% ugettext "German" %}`
This filter truncates words like the original truncate words Django filter, but instead of being based on the number of words, it's based on the number of characters. I found the need for this when building a website where i'd have to show labels on really small text boxes and truncating by words didn't always gave me the best results (and truncating by character is...well...not that elegant). Usage example: {{var|truncatewords_by_chars:"16 2 4"}} If string lenght is higher than 16, truncates by 2 words, if lesser, truncates by 4 words.
I use Django's built-in development server all the time, but get tired of typing out the command to run it, and if I'm testing some custom admin css/js, I *really* get tired of typing out the command that correctly directs the middleware to use my admin media dir. So I added this alias to my .bashrc and when I'm in a project's root, I just type 'pyserver' and the development server fires up, automatically passing an --adminmedia arg if I have some custom admin media for the project (otherwise it's 'runserver' as always). Note: This relies on the convention that my custom admin media will be in a folder called admin_media that resides at the project's base. Of course, change it to whatever convention you use. I'm not a bash expert or anything, but this works for me ;)
Maybe there is a better solution, feedback welcome!
This allows you to define a 'prefilter' function in your view modules which will be invoked before any view in that same. This provides an easy place to decorate the request or modify arguments. For simplicity it doesn't allow configuration of the name of the prefilter function. I also skipped recursing into parent modules since that's somewhat edgecase.
Automatically sets date to today if only time part was entered. If today is 01/01/2008, then both DateTimeField and TodayDateTimeField clean '2008-01-01 15:05' to datetime(2008,01,01,15,5,0), while '15:05' is cleaned as datetime(1900,01,01,15,5,0) for DateTimeField but datetime(2008,01,01,15,5,0) for TodayDateTimeField.
A middleware that parses the HTTP_ACCEPT header of a request. The request gets a new method called "accepts" that takes a string and returns True if it was in the list of accepted mime-types. It makes it possible to write views like: def exampleview(request): if request.accepts('application/json'): # return a json representation if request.accepts('text/html'): # return html Please note that with this middleware the view defines the priority of the mime-types, not the order in which they where provided in the HTTP-Header.
django.contrib.formtools in preview displaying only field.data by default. Its not convenient to see integer value for fields with radio buttons or select choices. This custom tag trying to show string value from choices if available.
3110 snippets posted so far.