Created
May 5, 2011 14:58
-
-
Save liamcurry/957195 to your computer and use it in GitHub Desktop.
Django Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class TimeAwareModel(models.Model): | |
created_on = models.DateTimeField(editable=False, auto_now_add=True) | |
updated_at = models.DateTimeField(editable=False, auto_now=True) | |
class Meta: | |
abstract = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use the absolute path of the current file to declare template directories | |
import os.path | |
DIRNAME = os.path.dirname(os.path.realpath(__file__)) | |
# Always load future template tags | |
from django.template.loader import add_to_builtins | |
add_to_builtins('django.templatetags.future') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Lazy reverse for using in settings.py and urls.py | |
from django.core import urlresolvers | |
from django.utils.functional import lazy | |
reverse = lazy(urlresolvers.reverse, unicode) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Extending this mixin requires the user to be logged in to see the view | |
from django.contrib.auth.decorators import login_required | |
from django.utils.decorators import method_decorator | |
class LoginRequiredMixin(object): | |
@method_decorator(login_required) | |
def dispatch(self, *args, **kwargs): | |
return super(LoginRequiredMixin, self).dispatch(*args, **kwargs) | |
# These two child mixins are for nested models | |
class ChildMixin(object): | |
parent_model = None | |
parent_slug_field = 'slug' | |
parent_context_object_name = None | |
def get_parent_context_object_name(self, obj): | |
""" | |
Get the name to use for the object. | |
""" | |
if self.parent_context_object_name: | |
return self.parent_context_object_name | |
elif hasattr(obj, '_meta'): | |
return smart_str(obj._meta.object_name.lower()) | |
else: | |
return None | |
def get_parent(self): | |
parent_slug = self.kwargs.get('parent_slug', None) | |
parent_pk = self.kwargs.get('parent_pk', None) | |
parent_id = self.kwargs.get('parent_id', None) | |
kwargs = {} | |
if parent_slug: | |
kwargs[parent_slug_field] = parent_slug | |
elif parent_pk: | |
kwargs['pk'] = parent_pk | |
else: | |
kwargs['id'] = parent_id | |
self.parent = get_object_or_404(parent_model, **kwargs) | |
return self.parent | |
def get_context_data(self, *args, **kwargs): | |
context = super(ChildMixin, self).get_context_data(*args, **kwargs) | |
# Had to do this for the form views, which don't call get_queryset() | |
if not self.parent: | |
self.get_parent() | |
self.parent_context_object_name = self.get_parent_context_object_name(self.parent) | |
context[self.parent_context_object_name] = self.parent | |
return context | |
class ChildFormMixin(ChildMixin): | |
def form_valid(self, form): | |
self.object = form.save(commit=False) | |
self.object.parent = self.get_parent() | |
self.object.save() | |
return HttpResponseRedirect(self.get_success_url()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment