Login

Snippets by bradmontgomery

Snippet List

slug_and_slash_to_dash - modified slugify for urls

This filter converts slashes to spaces in a a sting and then slugify's the result. However, it ignores leading and trailing slashes. For example, it can take something like this: /some/url/with-an-existing-slug/ And turn it into this: some-url-with-an-existing-slug The filter was originally written to use the *curent* url as the `disqus_identifier` for Disqus comments. For example: {{ request.META.PATH_INFO|slug_and_slash_to_dash }}

  • template
  • filter
  • slugify
Read More

Flatpage Suggester Template tag for 404 templates

This template tag finds FlatPages with urls 'similar' to the given request_path. It takes the request_path from the page_not_found view (django.views.defaults), picks it apart, and attempts to match existing FlatPages that have a 'similar' URL. For example, if the URL that resulted in a 404 was: /foo/bar/baz/whatever/ This tag would look for FlatPages whose URL starts with the following: /foo/bar/baz/whatever/ /foo/bar/baz/ /foo/bar/ /foo/

  • template-tag
  • flatpage
  • 404
  • suggestion
Read More

Row-Level, URL-based permissions for FlatPages

I'm using Django's FlatPages, but I want to be able to restrict admin access to Users based on a FlatPage url. For example, User John Doe should be able to edit any FlatPage objects whose URL begins with `/johndoe/` (such as `/johndoe/about/` or `/johndoe/projects/whatever/`). For this to work, John Doe would already need the appropriate admin permissions for FlatPage (such as can_add and can_change). I have set this up as a separate *flatpage_addons* app. It consists of the **Permission** model, which maps a starting URL to one or more django Users. It consists of the minimal necessary admin code so Permissions can be created using the admin. The bulk of this code consists of the *ifhasflatpagepermission* template tag as well as the *flatpage_result_list* inclusion tag. The former works much like django's existing *if*, *else*, *endif* tags while the latter is modified from the django admin's *result_list* inclusion tag. This may not be the most elegant solution to my problem, but so far it works for me. Any comments or suggestions are welcome!

  • urls
  • url
  • permission
  • permissions
  • flatpage
  • flatpages
Read More

Simple Flatpage Navigation Items

Flatpages are great for simple html content. However, I wanted some way to associate a navigation menu (just a snippet of HTML) with one or more FlatPage objects. Additionally, I wanted to be able to edit these throught the Admin. This was my solution.

  • html
  • navigation
  • flatpage
  • nav
Read More

SplitSelectDateTimeWidget

This class extends MultiWidget to create a widget that consists of HTML select elements for Django's forms.DateTimeFields. This results in a select elements with options for month, day, year, hour, minute, second, and (if using the twelve_hr option) meridiem. # Default usage of SplitSelectDateTimeWidget class TimeForm(Form): dt = DateTimeField(widget=SplitSelectDateTimeWidget()) Another example hooks into the flexibility of the underlying Select Widgets: class TimeForm(Form) dt = DateTimeField(widget=SplitSelectDateTimeWidget(hour_step=2, \ minute_step=15, second_step=30, twelve_hr=True, years=[2008,2009,2010])) The above example displays hours in increments of 2, minutes in increments of 15, and seconds in increments of 30. Likewise, only the years 2008, 2009,and 2010 are displayed in the years' options.

  • forms
  • widgets
  • widget
Read More

SelectTimeWidget

This snippet defines a Widget that is very similar to the **SelectDateWidget** located in django.forms.extras.widgets. The main difference however is that it works with Times instead of Dates. The SelectTimeWidget supports both 24-hr and 12-hr formats, and flexible time increments for hours, minutes and seconds. Sample usage is illustrated below: # Specify a basic 24-hr time Widget (the default) t = forms.TimeField(widget=SelectTimeWidget()) # Force minutes and seconds to be displayed in increments of 10 t = forms.TimeField(widget=SelectTimeWidget(minute_step=10, second_step=10)) # Use a 12-hr time format, which will display a 4th select # element containing a.m. and p.m. options) t = forms.TimeField(widget=SelectTimeWidget(twelve_hr=True))

  • forms
  • widgets
Read More

bradmontgomery has posted 6 snippets.