oweb / utility.py

from django.http                    import HttpResponseRedirect, HttpResponse
from django.shortcuts               import render_to_response, get_object_or_404
from django.template                import RequestContext
from django.contrib.auth.models     import User
from django.core.urlresolvers       import reverse


def update(obj_model=None, obj_form=None, request=None, id=None, pageOk=None, templateForm=None, title=None):
    obj_instance = get_object_or_404(obj_model, id=id, user=request.user.id)
    form = obj_form(request.POST or None ,instance=obj_instance)
    if form.is_valid():
       form.save()
       return HttpResponseRedirect(reverse (pageOk))

    return render_to_response(templateForm , 
                              {'form': form,
                              'title' : title,} ,
                               context_instance=RequestContext(request))

def new(obj_form=None, request=None, pageOk=None, templateForm=None, title=None):
    form = obj_form(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            obj_model_form = form.save(commit=False)
            obj_model_form.user = User.objects.get(id=request.user.id)
            obj_model_form.save()
            return HttpResponseRedirect(reverse (pageOk))

    return render_to_response( templateForm , 
                              {'form' : form,
                               'title' : title, } ,
                               context_instance=RequestContext(request))

def delete(obj_model=None, request=None, id=None, pageOk=None):
    if request.method == 'GET':
        obj_to_delete = get_object_or_404(obj_model, id=id, user=request.user.id)
        obj_to_delete.delete()
    
    return HttpResponseRedirect(reverse (pageOk))

def date_ita(dt_to_translate = None):
    return "%02d/%02d/%04d" %(dt_to_translate.day, dt_to_translate.month, dt_to_translate.year)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.