A mildly crufty script to slurp mail from an mbox into a Django model. I use a
variant of this script to pull the contents of my scammy-spam mbox into the database displayed at <http://purportal.com/spam/>
This is a copy paste job of mediawiki's syntax parser built in Python. You'll probably have to edit it to fit your needs
MediaWiki-style markup
parse(text) -- returns safe-html from wiki markup
code based off of mediawiki
I use these helper methods in my unit tests. They turn many simple getting-and-posting tests into one-liners. Definitely a work in progress, and I can't be the only person who has done this sort of thing -- comments are more than welcome.
Enables cookie based authentication with apache.
I needed user authentication for some static files, but couldn't use the method described [here](http://www.djangoproject.com/documentation/apache_auth/) as this prompts the user for his credentials, making him log in twice. There is some overhead in the code, because it runs all request middleware components (only session and auth would be needed). All arguments described in the link above are supported.
I use it like this in the apache config:
<Location "/protected/location">
PythonPath "['/path/to/proj/'] + sys.path"
PythonOption DJANGO_SETTINGS_MODULE myproj.settings
PythonOption DjangoPermissionName '<permission.codename>'
PythonAccessHandler my_proj.modpython #this should point to accesshandler
SetHandler None
</Location>
UPDATED:
This now supports an argument for the initial header level.
This is a modified version of `django.contrib.markup` that allows you to highlight code via [pygments](http://pygments.pocoo.org/). The code block can be used as:
`Here's a paragraph, and a code example:
.. code:: language
*insert code here*
continue with your normal document.`
Setup:
Insert the snippet into `mysite/templatetags/rest.py`, then add `mysite` to your installed apps in `settings.py`.
In your template, `{% load rest %}` and `{{ mycontent|rest }}`.
A simple way to get started using newforms. Implement a contact form. Mine saves the data in a table and sends a dedicated mailbox the feedback as well. I added support for TinyMCE as described in the django wiki.
http://code.djangoproject.com/wiki/CustomWidgetsTinyMCE?format=txt
Anyway use this as a starting point for writing your own form handling.
I knew that template in myght template system can receive some parameters just like a function. And I also want to implement this function in django template. So I finish a rough one, the code is pasted here. It just like include, but in order to distinguish with "include" tag, I call it "call". So you can use it:
{% call "some.html" %}
This way just like include tag, and the advanced way:
{% call "some.html" with "a" "b"|capfirst title="title1" %}
{% call "some.html" with "c" "d" title="title2" %}
So you can see, "call" tag can do like a python function, it can receive tuple parameters and key word parameters, just like the function:
def func(*args, **kwargs):pass
How to use it
===============
test_call.html
{% expr "limodou" as name %}
{% call "test/test_sub.html" with "a"|capfirst "b" title="title1" %}<br/>
{% call "test/test_sub.html" with "c" "d" title="title2" %}
expr is also a custom tag written by me. It'll calculate a python expression and save to result to a variable. In this case, the variable it "name".
test_sub.html
{% for i in args %}{{ i }}{% endfor %}
<h2>{{ title }}</h2>
<p>{{ name }}</p>
<h3>args</h3>
{{ args }}
<h3>kwargs</h3>
{{ kwargs }}
And you also can see, call tag will auto create args and kwargs context variables.
I hope this will be some useful.
This will take your Django template and will search for images in <table> and <img> tags, and will replace it with attached files in the email to send. You can use it without a template param.
This template can be included by other template passing the vars page_url and title:
{% with content.get_url as page_url %}
{% with content.title as title %}
{% include 'share.html' %}
{% endwith %}
{% endwith %}
This snippet adds support for OSM maps for GeometryField in Admin TabularInlines.
The one possible issue with this snippet is that the OSMGeoInlineForm has to know about the parent ModelAdmin which it does through the code
`model_admin_instance = admin.sites.site._registry[self.parent_model]`
which won't work if you don't use the default model admin to register the model admin. I'll try and come up with a work around and edit the snippet.
Due to the need to mess around with inline style sheets and IE not playing ball with just copying the innerHTML I've settled on using the jQuery.Rule plugin which I've included here as the last version published on the site was incompatible with jQuery 1.4.2 and I found a pathced version online, I also had to modify it due to the Django admin using the compatibility mode of jQuery so there is no global jQuery variable it's django.jQuery instead.
1. Create an osmgeo_inline.py file in your app and copy the top code into it.
2. Create the template file in a directory called admin within a template directory for your app, the template file must be called osmgeo_tabular_inline.html, and copy the middle code into it.
3. Create the jquery rule plugin file in your media or admin-media js directory and copy the bottom code into it. Don't forget to change the OSMGeoInlineForm's class Media's js = ('.....',) to the correct path to the file if need be.
4. In your admin.py you can create inline models using OSMGeoTabularInline just as you would TabularInline.
Examples all based on the following in models.py
from django.contrib.gis.db import models
class MyModel(models.Model):
name = models.CharField(max_length=64)
route = models.LineStringField(srid=settings.WGS_SRID)
class MySubModel(models.Model):
mymodel = models.ForeignKey(MyModel)
name = models.CharField(max_length=64)
location = models.PointField(srid=settings.WGS_SRID)
Example 1 - basic usage (admin.py):
from django.contrib.gis.admin import OSMGeoAdmin
from myapp.osmgeo_inline import OSMGeoTabularInline
from myapp.models import MyModel, MySubModel
class MySubModelInline(OSMGeoTabularInline):
model = MySubModel
class MyModelAdmin(OSMGeoAdmin):
inlines = [MySubModelInline]
admin.site.register(MyModel, MyModelAdmin)
Example 2 - overriding the default map widget params and setting the inline map's centre point to match the main models map centre (admin.py):
from django.contrib.gis.admin import OSMGeoAdmin
from myapp.osmgeo_inline import OSMGeoTabularInline
from myapp.models import MyModel, MySubModel
class MySubModelInline(OSMGeoTabularInline):
model = MySubModel
class MyModelAdmin(OSMGeoAdmin):
inlines = [MySubModelInline]
params = {'map_width: 200, 'map_height': 200]
def get_formset(self, request, obj=None, **kwargs):
centre = None
if obj is not None:
centre = obj.route.centroid.transform(900913, clone=True)
self.params['default_lon'] = centre.coords[0]
self.params['default_lat'] = centre.coords[1]
self.params['default_zoom'] = 12
return super(TrailSubmissionInlineBase, self).get_formset(request, obj, **kwargs)
admin.site.register(MyModel, MyModelAdmin)
I've not looked at StackedInlines because I don't use them but all that would be needed is to add a class OSMGeoStackedInline(StackedInline) that was a copy of OSMGeoTabularInline but with a template based on the StackedInline template - the javascript code in var initMap = function(row) would probably have to be adapted though.
This will allow you to attach HTML multipart emails (HTML/text) and use inline images.
In my example, I'm attaching an image that's stored as an 'attachment' to an 'event.' The file name of the attachment is called "inline.jpg" and I'm referencing it in my HTML message. I'm also attaching a VCAL file (.ics) file that has some information about an associated event.
Based on [Extended Profiling Middleware](http://djangosnippets.org/snippets/605/), this version allows interactive sorting of functions and inspection of SQL queries.
The new changelist supports clicking on a column header to order by that column, like iTunes. Unlike iTunes, which sorts by track number if you click the Artist or Album column header, it can only order by the column clicked on. By adding a property to my ModelAdmin, and subclassing ChangeList, I was able to make it also sort by track number when sorting by Artist or Album.
[Blog post](http://python-web.blogspot.com/2010/07/sorting-by-more-than-one-field-in-admin.html)
[Repository with full example](http://github.com/benatkin/tuneage)
Using this method you can combine form for standart django.contrib.auth.models.User model and for your project profile model. As now, ProfileForm can be used as usual, and it will also contain UserForm fields.
This is what I use to send simple status emails from my sites. Instead of a django.core.mail.send_mail call, which can take an irrritatingly, nondeterministically long time to return (regardless of error state), you can stow the emails in the database and rely on a separate interpreter process send them off (using a per-minute cron job or what have you). You then also have a record of everything you've sent via email from your code without having to configure your outbound SMTP server to store them or anything like that.
Usage notes are in the docstring; share and enjoy.
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.