Login

Snippets by santuri

Snippet List

State Machine inspired by acts_as_state_machine

A quick and dirty state machine inspired by the rails plugin, acts_as_state_machine. When implementing this on a model, the model is expected to have a character field in the database called "state." Next, come up with a few states that a model can be in and initialize a machine object with those states. Now you need to define the way a model moves from one state to the next. You do this by calling the 'event' method on the machine object. The first event argument defines the name of the method which will transition you to the next (to) state. The next argument is a dictionary defining what the current state must be in order to transition to the ending state. So, in the example, an Entry can transition from any state (the '*') to the draft state by calling entry.draft(). An entry can also transition from the 'draft' or 'hidden' state to the 'published' state which is enforced when calling entry.publish(). You can see what state a model object is in by calling <model_object>.state but sometimes it is more desirable to explicitly ask if a model object is in a particular state; this is done by the calling <model_object>.is_published() or <model_object>.is_draft(). All the code does is prefix "is_" to the "to" state which returns a True or False if the model is in that particular state.

  • state-machine
Read More

encode_mailto

This is a django filter which will create an html mailto link when you use the syntax: {{ "[email protected]"|encode_mailto:"Name" }} Results in: <a href="mailto:[email protected]">Name</a> Except everything is encoded as html digits. The encoding is a string of html hex and decimal entities generated randomly. If you simply want a string encoding use: {{ "name"|encode_string }} This was inspired by John Gruber's [Markdown](http://daringfireball.net/projects/markdown/syntax#autolink) project

  • filter
  • email
  • encode
Read More

slugify js -> python

This code is derived from the slugify JS function used in django's admin interface. It will create django-compatible slugs for you. Sometimes I do batch imports and need my items to have slugs, I give this script the item's title and get a slug back. Simple

  • slugs
Read More

santuri has posted 3 snippets.