Code Monkey home page Code Monkey logo

django-annoying's Introduction

Description

This django application eliminates certain annoyances in the Django framework.

Features

  • render_to decorator - Reduce typing in django views.
  • signals decorator - Allow using signals as decorators.
  • ajax_request decorator - Returns JsonResponse with dict as content.
  • autostrip decorator - Strip form text fields before validation
  • get_object_or_None function - Similar to get_object_or_404, but returns None if the object is not found.
  • get_config function - Get settings from django.conf if exists, return a default value otherwise.
  • AutoOneToOne field - Creates a related object on first call if it doesn't exist yet.
  • JSONField - A field that stores a Python object as JSON and retrieves it as a Python object.
  • HttpResponseReload - Reload and stay on same page from where the request was made.
  • StaticServer middleware - Instead of configuring urls.py, just add this middleware and it will serve your static files when you are in debug mode.

Installation instructions

  • Copy the annoying directory to your django project or put in on your PYTHONPATH.
  • You can also run sudo python setup.py install, sudo easy\_install django-annoying, or sudo pip install django-annoying.
  • Add "annoying" under INSTALLED_APPS in your settings.py file.

Examples

render_to decorator

from annoying.decorators import render_to

# 1. Template name in decorator parameters

@render_to('template.html')
def foo(request):
    bar = Bar.object.all()
    return {'bar': bar}

# equals to
def foo(request):
    bar = Bar.object.all()
    return render_to_response('template.html',
                              {'bar': bar},
                               context_instance=RequestContext(request))


# 2. Template name as TEMPLATE item value in return dictionary

@render_to()
def foo(request, category):
    template_name = '%s.html' % category
    return {'bar': bar, 'TEMPLATE': template_name}

#equals to
def foo(request, category):
    template_name = '%s.html' % category
    return render_to_response(template_name,
                              {'bar': bar},
                              context_instance=RequestContext(request))

signals decorator

from annoying.decorators import signals

# connect to registered signal
@signals.post_save(sender=YourModel)
def sighandler(instance, **kwargs):
    pass

# connect to any signal
signals.register_signal(siginstance, signame) # and then as in example above

#or

@signals(siginstance, sender=YourModel)
def sighandler(instance, **kwargs):
    pass

#In any case defined function will remain as is, without any changes.

ajax_request decorator

from annoying.decorators import ajax_request

@ajax_request
def my_view(request):
    news = News.objects.all()
    news_titles = [entry.title for entry in news]
    return {'news_titles': news_titles}

autostrip decorator

from annoying.decorators import autostrip

class PersonForm(forms.Form):
    name = forms.CharField(min_length=2, max_length=10)
    email = forms.EmailField()

PersonForm = autostrip(PersonForm)

#or in python >= 2.6

@autostrip
class PersonForm(forms.Form):
    name = forms.CharField(min_length=2, max_length=10)
    email = forms.EmailField()

get_object_or_None function

from annoying.functions import get_object_or_None

def get_user(request, user_id):
    user = get_object_or_None(User, id=user_id)
    if not user:
        ...

AutoOneToOneField

from annoying.fields import AutoOneToOneField


class MyProfile(models.Model):
    user = AutoOneToOneField(User, primary_key=True)
    home_page = models.URLField(max_length=255, blank=True)
    icq = models.IntegerField(blank=True, null=True)

JSONField

from annoying.fields import JSONField


#model
class Page(models.Model):
    data = JSONField(blank=True, null=True)



# view or another place..
page = Page.objects.get(pk=5)
page.data = {'title': 'test', 'type': 3}
page.save()

get_config function

from annoying.functions import get_config

ADMIN_EMAIL = get_config('ADMIN_EMAIL', '[email protected]')

StaticServer middleware

Add this middleware as first item in MIDDLEWARE_CLASSES

example:

MIDDLEWARE_CLASSES = (
    'annoying.middlewares.StaticServe',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.doc.XViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)

It will serve static files in debug mode. Also it helps when you debug one of your middleware by responding to static requests before they get to debugged middleware and will save you from constantly typing "continue" in debugger.

Used on python community portal.

django-annoying's People

Contributors

skorokithakis avatar jshwright avatar wrar avatar beniwohli avatar mstevens avatar kmike avatar

Watchers

Vincent C Fulco avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.