Install InSync on Opensuse

Posted on Mon 20 April 2015 in Systems • Tagged with django, python


Continue reading

Ajax view decorator

Posted on Fri 21 November 2014 in Programming • Tagged with python, django

To force a django view to be called only via AJAX calls.

def ajax_required(f):
    """
    AJAX request required decorator
    use it in your views:

    @ajax_required
    def my_view(request):
        ....

    """
    def wrap(request, *args, **kwargs):
        if not request.is_ajax():
            return HttpResponseBadRequest()
        return f(request, *args, **kwargs)
    wrap.__doc__ = f.__doc__
    wrap.__name__ …

Continue reading

Generic function to use as upload_to

Posted on Sat 11 October 2014 in Programming • Tagged with django, python

FileFields in Django need an upload_to function that determines where the file will be uploaded. I usually have a generic function in utils.py that leaves them in a subfolder with the model name.

import os

def generic_upload_to(instance, filename):
    """
    Generic `upload_to` function for models.FileField and models.ImageField
    which …

Continue reading

Django: links in object list

Posted on Wed 21 May 2014 in Programming • Tagged with django, python

In my projects I usually use links to related items in the admin change_list.

utils.py

I have this in utils.py

from django.contrib import admin
from django.contrib.contenttypes.models import ContentType
from django.core import urlresolvers
from django.utils.datastructures import SortedDict …

Continue reading

Settings vars processor

Posted on Wed 08 January 2014 in Programming • Tagged with python, django

I usually use this at the beginning of projects.

from django.conf import settings as django_settings
from django.core.exceptions import ImproperlyConfigured


def settings(request):
    """
    Adds the settings specified in settings.TEMPLATE_VISIBLE_SETTINGS to
    the request context.
    """
    new_settings = {}
    for attr in django_settings.TEMPLATE_VISIBLE_SETTINGS:
        try:
            new_settings[attr] = getattr(django_settings, attr)
        except AttributeError …

Continue reading