Allows you to search if a user belongs to a given group.
Along the same lines as snippet [390](http://www.djangosnippets.org/snippets/390/), but uses a regular ``if`` tag so it is more flexible.
(Updated for efficiency. Running a boolean test on a QuerySet avoids a bit of unnecessary overhead.)
(Updated to accept a list of groups.)
This is a _very basic_, _easily foolable_, restriction method implemented in a Django middleware.
However, for low security sites that need a cursory barrier to entry (without the ability to assign/administer user accounts), this does very well.
All of the features are fairly well-documented in the code.
A simple script that I have put in my Django applications directory to fetch the latest application code from git and svn.
For example, your directory structure might look like so:
django-apps/
django-tagging/
django-pagination/
django-registration/
django-threadedcomments/
django-mptt/
update_apps.py
Where update_apps.py is the source of this snippet.
To run, simply execute:
# python update_apps.py
And the script will iterate through all of your apps and update them to the latest version.
This class works in compatibility with a ModelForm, but instead of show a form, it shows the field values.
The Meta class attributes can be used to customize fields to show, to be excluded, to divide by sections, to urlize text fields, etc.
**Example 1:**
class InfoSingleCertificate(ModelInfo):
class Meta:
model = SingleCertificate
sections = (
(None, ('vessel','description','document','comments','status',)),
('Issue', ('issue_date','issue_authority','issue_location',)),
)
**Example 2:**
class InfoSingleCertificate(ModelInfo):
class Meta:
model = SingleCertificate
fields = ('vessel','description','document','comments','status',
'issue_date','issue_authority','issue_location',)
**How to use:**
Just save this snippet as a file in your project, import to your views.py module (or a new module named "info.py") and create classes following seemed sytax you use for ModelForms.
Simple DecimalField class extension that automatically adds formatting and validation for comma-separated "decimals". Works wonderfully for price fields.
Could be extended to strip dollar signs or to be locale-agnostic.
I couldn't find any code for a blog-style "Read more after the jump," so I made a custom filter. It will look for **<!--more-->** for the jump, like in Wordpress.
In **settings.py** set **READ_MORE_TEXT** to what you want the text of the link to be.
`READ_MORE_TEXT = 'Read more after the jump.'`
When you call the filter in your template, pass it the absolute link of that entry. Of course, you have to have your **get_absolute_url** function defined in your model, but you should have that already, right? :P
In this example **entry.body** is the content of the blog entry.
`{% load blog_filters %}`
`{{ entry.body|read_more:entry.get_absolute_url }}`
If anyone has a better way to do this, it is, of course, welcome.
The core templatetags for my project [google-chartwrapper](http://code.google.com/p/google-chartwrapper/). It is an easy method of creating dynamic GoogleCharts from the [GoogleChartAPI](http://code.google.com/apis/chart/). To get the most recent version:
`svn checkout http://google-chartwrapper.googlecode.com/svn/trunk/`
and run `python setup.py` in the downloaded trunk directory. There is an included django project there with the [ChartsExamples](http://code.google.com/p/google-chartwrapper/wiki/ChartExamples) all worked out in django templates
This manager is intended for use with models with publication and/or expiration dates. Objects will be retrieved only if their publication and/or expiration dates are within the current date.
Use is very simple:
class ExampleModel(models.Model):
publish_date = models.DateTimeField()
expire_date = models.DateTimeField(blank=True, null=True)
objects = models.Manager()
actives = ActiveManager(from_date='publish_date', to_date='expire_date')
ExampleModel.actives.all() # retrieve active objects according to the current date
The manager works correctly with nullable date fields. A null publication date means "*always published (until expiration date)*" and a null expiration date means "*never expires*".
Most models should define the `objects` manager as the default manager, because otherwise out of date objects won't appear in the admin app.
How to make this work:
* Put the code into your settings.py (or even better, local_settings.py if you have one)
* Put 3rd party apps you'd like to use into the directories specified in APP_DIRS.
* Place 'appname', into INSTALLED_APPS.
Just a little trick I use to keep from having to install apps via setup.py; I can just stick apps into arbitrary locations and use them. I also do this to keep single copies of 3rd party apps I use in multiple projects (but again without installing to the global python path). No idea if this is bad practice or anything, but I find it useful.
Extension of the idea from [WuzHere example from Google IO](http://code.google.com/p/wuzhere/) about creating one compressed js or css file. Original code used not very elegant if statements. I've changed it to template tags. On production server it will also use current version id.
Insert code in *cssjs.py* file in *templatetags* dir in your application and use as below (more details in docs):
`<script type="text/javascript" src="/media/jsmergefile.js"></script>`
**This code does not compress CSS and JS on the fly, because GAE is read-only and using Datastore is too heavy.**
Set a context variable with the returned value by any templatetag.
Useful for example in order to use url templatetag inside blocktrans:
` {% withtag url my_app.views.my_view as my_view_url %}`
` {% blocktrans %}`
` Click <a href="{{ my_view_url }}">here</a>`
` {% endblocktrans %}`
` {% endwithtab %}`
Or with include templatetag:
` {% withtag include "js_template.js" as js_template %}`
` {{ js_template }}`
` {% endwithtab %}`
It works properly with your own custom templatetags.
Sometimes you need to prevent concurrent access to update/calculate some properties right. Here is (MySQL) specific example to lock one table with new object manager functions.