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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from django.http import HttpResponseNotAllowed | |
from django.template import RequestContext | |
from django.template import loader | |
class HttpResponseNotAllowedMiddleware(object): | |
def process_response(self, request, response): | |
if isinstance(response, HttpResponseNotAllowed): |
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
# pip install requests | |
import requests | |
from django.core.files import File | |
from django.core.files.temp import NamedTemporaryFile | |
def save_image_from_url(model, url): | |
r = requests.get(url) | |
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from django.template.loader import get_template | |
from django.template.loader_tags import BlockNode | |
from django.template import Context | |
from django.core import mail | |
class EmailMessage(mail.EmailMultiAlternatives): |
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
dictionary = {} | |
Model.objects.create(**dictionary) | |
Model.objects.filter(pk=pk).update(**dictionary) |
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
active_required = user_passes_test(lambda u: u.is_active, | |
login_url='login.views.not_active', | |
redirect_field_name='redirect_to' | |
) | |
def active_and_login_required(view_func): | |
decorated_view_func = login_required( |
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
class MyForm(forms.Form): | |
""" | |
Extend from this form so your widgets have an 'error' class if there's | |
an error on the field. | |
""" | |
def __init__(self, *args, **kwargs): | |
super(MyForm, self).__init__(*args, **kwargs) | |
if self.errors: | |
for f_name in self.fields: | |
if f_name in self.errors: |
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from django.conf import settings | |
from django.core.cache import cache | |
from django.core.management.base import BaseCommand, CommandError | |
class Command(BaseCommand): | |
help = 'Fully clear your site-wide cache.' |
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
class BaseObject(models.Model): | |
id = models.AutoField(primary_key=True) | |
creation_datetime = models.DateTimeField(auto_now_add=True) | |
update_datetime = models.DateTimeField(auto_now=True) | |
class News(BaseObject): | |
class Meta: | |
managed = True | |
db_table = 'news' |
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import json | |
from functools import wraps | |
from django.http import HttpResponse | |
def render_to_json(**jsonargs): | |
""" | |
Renders a JSON response with a given returned instance. Assumes json.dumps() can |
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import re | |
from django.utils.safestring import mark_safe | |
from django import template | |
register = template.Library() | |
@register.filter(name='twitter_tags') |