Django does not have a suitable model field can process time type in mysql, the DateTimeField built-in django can not process more than 24 hours. That's why the code born.
**Simply usage**
`from myapp.models import TimeAsTimeDeltaField, SECONDS_PER_MIN, SECONDS_PER_HOUR, SECONDS_PER_DAY
class EstimatedTime:
days = None
hours = None
minutes = None
seconds = None
class Case(models.Model):
estimated_time = TimeAsTimeDeltaField(null=True, blank=True)
def get_estimated_time(self):
estimated_time = EstimatedTime()
if self.estimated_time:
total_seconds = self.estimated_time.seconds + (self.estimated_time.days * SECONDS_PER_DAY)
days = total_seconds / SECONDS_PER_DAY
hours = total_seconds / SECONDS_PER_HOUR - days * 24
minutes = total_seconds / SECONDS_PER_MIN - hours * 60 - days * 24 * 60
seconds = total_seconds % SECONDS_PER_MIN
estimated_time.days = days
estimated_time.hours = hours
estimated_time.minutes = minutes
estimated_time.seconds = seconds
return estimated_time
else:
return estimated_time
Middleware to decorate views with user_passes_test in a centralized, url-matching manner. Makes it easy to apply permissions across large sections or all of a site.
I tried to think of a way to use generic views to show all my blog posts on one page organized chronologically with each post title under one month name and all the month names under one year name.
Like so:
`
2009
October
My last example blog entry
Entry even before the last one
First of October
September
Only one entry in Sept.
2008
December
It is cold in December
First posting of my blog
`
I could not think of a way to do this using generic views, before I wrote a view and some template logic that does.
The template logic is here:
[http://www.djangosnippets.org/snippets/1765/](http://www.djangosnippets.org/snippets/1765/)
Any suggestions for design patterns are greatly appreciated.
I tried to think of a way to use generic views to show all my blog posts on one page organized chronologically with each post title under one month name and all the month names under one year name.
Like so:
`
2009
October
My last example blog entry
Entry even before the last one
First of October
September
Only one entry in Sept.
2008
December
It is cold in December
First posting of my blog
`
I could not think of a way to do this using generic views, before I wrote a view and some template logic that does.
The view code is here:
[http://www.djangosnippets.org/snippets/1766/](http://www.djangosnippets.org/snippets/1766/)
Any suggestions for design patterns are greatly appreciated.
This is a replacement for settings.py, which moves the actual settings files into a directory called "env" and establishes different versions for different settings of the environment variable DJANGO_ENV. At runtime, the specified development environment can be found and loaded into the local context of settings.py, which is then picked up by whatever routine manage.py is kicking off.
This get_sorted_items tag takes app.model, a number n, a a field name to sort ,and a variable-name as arguments and will deliver the n first objects of the model.
it checks if a Manager *publicmgr* exists and calls this if the user isn't authenticated.
Additionally it write the count of the model to the context
Shows field value as plain text which can't be edited by user. Field value (or key value for foreign keys) is stored in hiddden input.
Value of field is stored in hidden input and current value is placed as plain text. User can't change it's value. If field is foreign key then additional attribute 'key' should be set to True (key is stored in hidden field and unicode value is visible):
self.fields['my_field'].widget = HiddenInputWithText(attrs={ 'key' : True })
There is one condition: for foreign key field its name have to be same as lowercased model name.
Default 'key' value - False is correct for non-foreign key fields:
self.fields['my_field'].widget = HiddenInputWithText()
This decorator is to make it's decorated function to commit the transaction on success (rollback otherwise) unless the transactions are being already managed.
This is simple validation weather a string is close enough to what we want. First param is keyword we are comparing to Second is user's input and third is tolerance level. Its very rudimentary. I have my mind fixed upon some imperfections. I am trying to make it good for human like(crazy keyboard) error.
When debugging/developing you want to be able to refresh your views every time you make a little change. But when in production mode you might want to cache these views because they contain long and resource hungry calculations or something.
By putting this above "hack" in after importing `cache_page` you only cache the views in production mode.
Variation on dictsort using attribute access. Nested attributes can be used, like, "obj.attr.attr_attr"
Example usage:
{% for entry in entries|sortby:'category.title' %}
Based on [1609](http://www.djangosnippets.org/snippets/1609/)
Many of my projects heavily depend on other non-django projects to create the databases. To simplify setting up a test environment, I modified the simple test runner so that it will treat all models as managed.
This will also allow for easier test set up against models that point to views. You can directly populate the view with the specific data needed for the tests, instead of populating (potentially) several models.
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.