コード例 #1
0
ファイル: admin.py プロジェクト: suinonatante/pymobile
def mod_attribute(request, attribute, object_id):
    template = "tariffa/attribute_modelform.html"
    action = "mod"
    model = get_model("administration", attribute + "tariffa")
    
    if request.method == "POST":
        post_query = request.POST.copy()
        obj = get_object_or_404(model, pk=object_id)
        form = u.get_form(attribute + "tariffaform")(post_query, instance=obj)
    
        if form.is_valid():
            new_obj = form.save()

            if attribute == "servizio":
                logger.debug("{}: modificato il servizio {} [id={}]"
                             .format(request.user, obj, obj.id))
                messages.add_message(request, messages.SUCCESS, '{} modificato'.format(attribute.title()))
            else:
                logger.debug("{}: modificata la {} {} [id={}]"
                             .format(request.user, attribute, new_obj, new_obj.id))
                messages.add_message(request, messages.SUCCESS, '{} modificata'.format(attribute.title()))            
            if request.POST.has_key("add_another"):              
                return HttpResponseRedirect(reverse("add_attribute", 
                                                    args=[attribute])) 
            else:
                return HttpResponseRedirect(reverse("init_attribute", 
                                                    args=[attribute]))
    else:
        obj = get_object_or_404(model, pk=object_id)         
        form = u.get_form(attribute + "tariffaform")(instance=obj)
    
    data = {"modelform": form, "action": action, "attributo": attribute,}
    return render_to_response(template,
                              data,
                              context_instance=RequestContext(request))
コード例 #2
0
ファイル: admin.py プロジェクト: suinonatante/pymobile
def init_attribute(request, attribute):
    template = "tariffa/attribute_admin.html"
    model = get_model("administration", attribute + "tariffa")
    objs = model.objects.all()
    initial = {}
    pag = 1
    ordering = None
    
    if request.method == "GET" and request.GET:
        query_get = request.GET.copy()
        initial = {}
        
        if query_get.has_key("pag"):
            pag = query_get["pag"]
            del query_get["pag"]
        if query_get.has_key("sort"):
            ordering = query_get["sort"]
            del query_get["sort"]
        if query_get:
            objs, initial = u.filter_objs(objs, query_get)
        
    table = u.get_table(attribute + "tariffatable")(objs, order_by=(ordering,))
    table.paginate(page=pag)
    
    if request.is_ajax():
        template = table.as_html()
        return HttpResponse(template)
        
    filterform = u.get_form(attribute + "tariffafilterform")(initial=initial)
    
    data = {"modeltable": table, "filterform": filterform, "attributo": attribute,}
    return render_to_response(template, data,
                              context_instance=RequestContext(request))