Snippet List
I needed a way to quickly get a direction of html pages templated such that another person could drop new templates in to a subdirectory and without modifying urls.py or views.py get them up and being displayed.
Now, the direct_to_template view provided django.views.generic.simple can sort of do this with a urlpattern like:
`url(r'^(?P<template>.*\.html)$', direct_to_template)`
But that means your templates, no matter what level in the url hierarchy they are reached at, have to be defined at the root of a template directory. I wanted them retrieved from a specific subdirectory instead so I could provide a little wall for them. Hence this snippet.
To use you would have url pattern that looked like:
`url(r'^foo/(?P<template>.*\.html)$', direct_to_template, {'subdir' : 'subdir/'}),`
Which will template any url that matches <parent url>/foo/bar.html for any 'bar'. The problem is if this is a sub-url pattern match this is going to look for the template "bar.html" when we may actually want it to get the template "<parent url>/foo/bar.html"
- templating
- wildcard
- subdir
A simple decorator that requires a user to be logged in. If they are not logged in the request is examined for a 'authorization' header.
If the header is present it is tested for basic authentication and the user is logged in with the provided credentials.
If the header is not present a http 401 is sent back to the requestor to provide credentials.
The purpose of this is that in several django projects I have needed several specific views that need to support basic authentication, yet the web site as a whole used django's provided authentication.
The uses for this are for urls that are access programmatically such as by rss feed readers, yet the view requires a user to be logged in. Many rss readers support supplying the authentication credentials via http basic auth (and they do NOT support a redirect to a form where they post a username/password.)
Use is simple:
`
@logged_in_or_basicauth
def your_view:
...
`
You can provide the name of the realm to ask for authentication within.
- basic
- authentication
- decorator
The django templating language is quite nice, and specifically limited to guide people in to making their business logic in the view, not in the template itself.
Sometimes it can be difficult to do certain things in the template even though it seems like the most appropriate place to do this.
I have an application that is heavily influenced by the user that is logged in in several ways.
For example let us say I have a list of forums. Each forum has several discussions full of posts. This system of forums and discussions has permissions such that some users can not see some entities. Now, I can produce in the view the query set of what forums a user is allowed to see, and I want this list to display the latest post in each of those forums, but I have to restrict that to the posts that they can see.
Easy enough, I have a method on the Forum object that takes a user object and does the appropriate filter to return the latest post that the user can see. The trick is the template is passed the query set of forums, and then iterates through them using the template language. How can it invoke the method on the forum for the latest post that needs the 'user' variable from the template context? The template language lets me say 'forum.latest_post' but I need to pass in 'user'. 'forum.latest_post(user)' does not work because the template language does not allow it.
This tag lets me specify an object, the method on that object to call, and the variable to pass to that method.
It is not the prettiest thing but with this add on you can do:
`{% method_arg forum latest_post user as post %}`
This is a helper field & widget that lets you ask for a 'user or group'. It presents a select box for whether you wish to enter a user or a group, and then a text field for the name of the user or group you wish to have.
It will validate that the username or group name selected exists.
You would use this like any other field:
`
class AddForumCollectionCreatePermForm(forms.Form):
user_or_group = UserOrGroupField(label = "User or Group",
help_text = "The user or group to grant "
"forum collection create priveleges to.")
`
I have users in many timezones and I let them set their preferred display timezone in their user profile as a string (validated aginst the pytz valid timezones).
This is a filter that takes a datetime as input and as an argument the user to figure out the timezone from.
It requires that there is a user profile model with a 'timezone' field. If a specific user does not have a user profile we fall back on the settings.TIME_ZONE. If the datetime is naieve then we again fallback on the settings.TIME_ZONE.
It requires the 'pytz' module: [http://pytz.sourceforge.net/](http://pytz.sourceforge.net/)
Use: `{{ post.created|user_tz:user|date:"D, M j, Y H:i" }}`
The example is assuming a context in which the 'user' variable refers to a User object.
- filter
- timezone
- userprofile
I am working on some apps that use row level permissions. For many permission tests I intend to make custom tags so I can locate the permission test used by the template and by the view in a common module.
However, frequently, before I decide what the actual end-tag is going to be I need a way to expression a more powerful 'if' structure in the template language.
Frequently the existing if and if_has_perm tags are just not powerful enough to express these things.
This may make the template language unacceptably more like a programming language but it has helped me quickly modify complex permission statements without having to next and repeat four or five clauses.
As you can see this is intended to work with django off of the row level permissions branch.
Here is an example of a template that is using the 'fancy_if' statement:
`
{% fancy_if or ( and not discussion.locked not discussion.closed "asforums.post_discussion" discussion ) "asforums.moderate_forum" discussion.forum %}
<li><a href="./create_post/?in_reply_to={{ post.id }}">{% icon "comment_add" "Reply to post" %}</a></li>
{% end_fancy_if %}
{% fancy_if or "asforums.moderate_forum" discussion.forum ( and not discussion.closed ( or eq post.author user eq discussion.author user ) ) %}
{% fancy_if or "asforums.moderate_forum" discussion.forum eq post.author user %}
<li><a href="{{ post.get_absolute_url }}update/">{% icon "comment_edit" "Edit Post" %}</a></li>
{% end_fancy_if %}
<li><a href="{{ post.get_absolute_url }}delete/">{% icon "comment_delete" "Delete Post" %}</a></li>
{% end_fancy_if %}
`
- row-level-permissions
- compound-conditional
Scanner has posted 6 snippets.