This middleware will add a log of the SQL queries executed at the top of every page, in the form of an IE-like 'infobar'. It also includes a query count and total and per-query execution times.
This snippet is based on snippet #344 "SQL Log Middleware + duplicates" by guettli.
I did some adjustments to the code, to also add support for the application/xhtml+xml mime type, aswell as add the code for the infobar.
Need DEBUG on, aswell as DEBUG_SQL, should not be used on production sites.
It also expects a 16x16 png image called infobar_icon.png, which it is looking for in the /media/images folder (or /static/images, depending on your MEDIA_URL setting). I used a little light bulb icon from the Tango icon set, but am unable to attach the image. Any 16x16 png will do off course.
Update 0.1.1: In the initial version of this middleware, the path to /media/images was hard-coded, and you had to adjust this middleware accordingly. Now, it correctly uses the MEDIA_URL setting from your settings.py file, so you no longer have to edit this middleware.
0.1.2: Made some adjustments to the CSS to get rid of a padding bug when the bar was displayed on a Django error page. Also if a page contains no queries, it won't bother printing an 'empty' table, with just column headers.
0.1.3: More tweaks to the CSS, odd/even row shading on queries table.
0.1.4: More CSS tweaks again
0.1.5: Minor tweaks
0.1.6: Sorry, I just realised that after some time, this snippet broke in the latest SVN of Django with a Unicode error, as arthur78 mentioned.
I have managed to fix it, by wrapping line 258 and 259 in an str() function.
This is a very simple script to do a **simple** migration from plone content to django.
ATNewsItems and PloneArticles are imported into the django model *Article* (with Foreignkey to the django models *File* and *Image*). ATDocuments are imported into the django model *Page*.
**Usage**
1. Make sure that the Python version running Zope can import django
2. The django database should be writeable from within the Zope environment
3. Set the shell variable *DJANGO_MODULE_SETTING* to the settings file of the django project you want to import your Plone content to
4. Start the Zope server in the debug modus:
$ bin/zopectl debug
Then import the python module above, and pass the site you want to migrate to the start function:
import simple_migration
mysite = app.mysite
simple_migration.start(mysite)
Found 1253 objects
Saved Test Document Type: ATNewsItem
...
Hope it helps someone.
Used for the migration of **Plone-2.5.3** content in a **Python-2.4.4** environment.
Some template tags/filter for working with query strings in templates.
Examples:
{% load qstring %}
{% qstring %} # Prints current request's query string
{% qstring as current_qstring %} # Same but goes to context
{{ current_qstring|qstring_del:"key1" }} # Deletes all key1 values
{{ current_qstring|qstring_del:"key1&key2" }} # Deletes all key1 and key2values
{{ current_qstring|qstring_set:"key1=1&key2=2" }} # Deletes all old key1 and key2 values and adds the new values.
Formats a number in the local currency format. E.g., if `foo` is equal to `49277`, then
> ` {{ foo|currency }}`
would print
> `$49,277`
If your locale is the U.S. You can use this filter in your templates as described in the [Django documentation](http://www.djangoproject.com/documentation/templates_python/)
Template filter to format a start and end time in to a range. Uses Django's ["P" format](http://www.djangoproject.com/documentation/templates/#now) and assumes start and end time are on the same day or night before/morning after.
`{{ start_time|time_range:end_time }}`
Examples:
7-8 p.m.
8 p.m. - midnight
noon - 4 p.m.
9:45 a.m. - 5:15 p.m.
10:30 p.m. - 1:30 a.m.
This is a little improvement to the [idea](http://www.djangosnippets.org/snippets/540/) from sheats a few days ago.
I like it over the previous solutions because it doesn't involve doing anything other than running `./manage.py shell` inside your project directory. You don't have to create any files anywhere or remember to call anything, and `ipython` still works fine outside of a Django project.
Throw this code in `~/.ipython/ipy_user_conf.py` (`ipythonrc` has apparently been deprecated).
Add the attribute "rel='lightbox'" to all Links, if the target is an image.
`<a href="/path/to/image.jpg">Image</a>`
becomes
`<a rel="lightbox" href="/path/to/image.jpg">Image</a>`
Works for JPG, GIF and PNG Files.
This tag is meant to override the current implementation of '{% spaceless %}' tag and remove spaces at the beginning of a line too. I.e. a template like this:
<div>
<div>useless space up front</div>
</div>
will become this
`<div>`
`<div>useless space up front</div>`
`</div>`
All the other behaviour of spaceless stays the same!
Put this in your app/name/templatetags/tags.py
And if you want it to override the default "spaceless"-tag to the following
from django.template import add_to_builtins
add_to_builtins('app.name.templatetags.tags')
This is a customized version of the default `for` template tag which takes an optional `{% else %}` clause that will be displayed if the given array is empty.
from django.template import *
>>> t1 = Template("""
{% load mytags %}
{% for athlete in athlete_list %}
{{ athlete }}
{% else %}
No athlete in list!
{% endfor %}
""")
>>> c1 = Context(
{'athlete_list':
['me', 'myself', 'I']
})
>>> t1.render(c1)
u'me myself I '
>>> c2 = Context({})
>>> t1.render(c2)
u'No athlete in list!'
If you want to automatically override the builtin `for` template-tag add it to the builtins:
from django.template import add_to_builtins
add_to_builtins('python.path.to.mytags')
Tags like `url` and `trans` provide no way to get the result as a context variable. But how would you get a computed URL into a `blocktrans`?
This snippet solves the general problem. Just put the template code whose output you want to capture within `captureas` tags. For example:
{% captureas login_url %}{% url login %}{% endcaptureas %}
{% blocktrans %}
<a href="{{login_url}}">login</a>
{%endblocktrans%}
Disclaimer: I'm not the world's greatest programmer, so there may be better ways to do this, but it works for me (feel free to offer your improvements, though!).
Basically, this will pad an integer with leading zeros and return a string representation. User it like this:
{% forloop.counter|leading_zeros:"5" %}
...where "5" is the number of desired digits. In this case, if it was the 12th time through the forloop, the filter would return "00012".
Why do this? Either for alignment, such as in tables, or for aesthetics -- for an example, see [Shaun Inman's comment section](http://shauninman.com/archive/2007/11/16/mobilesafari_view_source).
This is a very basic -- yet fully functional -- framework for producing a loosely coupled plugin architecture. Full details of its use can be found [on my blog](http://gulopine.gamemusic.org/2008/jan/10/simple-plugin-framework/), but the basics are listed below.
## Defining a mount point for plugins
class ActionProvider:
__metaclass__ = PluginMount
## Implementing plugins
class Insert(ActionProvider):
def perform(self):
# Do stuff here
class Update(ActionProvider):
def perform(self):
# Do stuff here
## Utilizing plugins
for action in ActionProvider.plugins:
action.perform()
Yes, it really is that simple.
In the process of adapting to the new autoescaping thing, I've occasionally run into the sad situation where my development Django instance needs autoescape template tags in order to work, but when that code goes into production, I'm on a pre-autoescape Django revision. Hilarious hijinx ensue.
This snippet will attempt to load the new safe and force_safe filters and the autoescape template tag - failing the imports, it'll safely do nothing. The effect is that if you write templates for post-autoescape, but you still need them to work in pre-autoescape, this'll prevent the new filters and tags from causing errors in your older Django instances.
It's a pain to import all the Django models you want to use in the Python shell every time you start it. Here's how you can get IPython to autoload all your Django models for you every time you start the shell using ./manage.py shell.
Put the code in a .py file in the root of your project. Then tell IPython to load the script in ~/.ipython/ipythonrc in the "Python files to load and execute" section.
Sometimes you want to create a temporal variable to store something or do anything you want with it, problem is that django template system doesn't provide this kind of feature. This template tag is just for that.
Syntax::
{% assign [name] [value] %}
This will create a context variable named [name] with a value of [value]
[name] can be any identifier and [value] can be anything. If [value] is a callable, it will be called first and the returning result will be assigned to [name]. [value] can even be filtered.
Example::
{% assign count 0 %}
{% assign str "an string" %}
{% assign number 10 %}
{% assign list entry.get_related %}
{% assign other_str "another"|capfirst %}
{% ifequal count 0 %}
...
{% endifequal %}
{% ifequal str "an string" %}
...
{% endifequal %}
{% if list %}
{% for item in list %}
...
{% endfor %}
{% endif %}