diff options
Diffstat (limited to 'objectapp/views/dynamicAT.py')
-rw-r--r-- | objectapp/views/dynamicAT.py | 166 |
1 files changed, 32 insertions, 134 deletions
diff --git a/objectapp/views/dynamicAT.py b/objectapp/views/dynamicAT.py index 52a6256..aec02fc 100644 --- a/objectapp/views/dynamicAT.py +++ b/objectapp/views/dynamicAT.py @@ -1,155 +1,53 @@ from django.http import * -from django import forms +from django.forms import ModelForm from django.shortcuts import render_to_response from django.template import RequestContext +from django.forms.models import modelform_factory from objectapp.models import * from objectapp.forms import * from gstudio.models import * +from gstudio.admin.forms import * + +def MakeForm(model_cls, *args, **kwargs): + class ContextForm(ModelForm): + class Meta: + model = model_cls.values()[0] + fields = ('value',) + def __init__(self, *args, **kwargs): + super(ContextForm,self).__init__(*args, **kwargs) + + + return ContextForm(*args, **kwargs) -def dynamic_view(request): - rdict = {} - for j in Gbobject.objects.all(): - rdict.update({j.id:j.title}) - - template="objectapp/selectAT.html" - context= RequestContext(request,{ 'rdict':rdict }) - return render_to_response(template,context) - -def dynamic_save(request,tit): #view for gb and ot too - testlst = [] - parenttype = [] - attributetype = [] - rdict = {} - nodelist = [] - syst = []#parent for system - try: - offset = str(tit) - except ValueError: - raise Http404() - - #flag = 0 #means its object - - if Objecttype.objects.filter(title = offset): - flag = 1 - elif Gbobject.objects.filter(title = offset): - flag = 0 - elif Systemtype.objects.filter(title = offset): - flag = 2 - - if flag == 1: - parenttype.append(Objecttype.objects.get(title = offset)) - pt_id = NID.objects.get(title = offset) - name = pt_id - - elif flag == 0: - #utit = Gbobject.objects.get(title = offset) - gb=Gbobject.objects.get(title=offset) - name = gb - # checking whether object is gb or system coz created objects are treated as Gbobject n then System objects - if System.objects.filter(title = (gb.ref).title): - gb = System.objects.get(title = offset) - syst = gb.systemtypes.all() - for i in syst: - nodelist = i.nodetype_set.all() - parenttype.append(i) - - for i in nodelist: - parenttype.append(i.ref) - - elif Gbobject.objects.filter(title = (gb.ref).title): - parenttype = gb.objecttypes.all() - - pt_id = NID.objects.get(id = gb.id) - - elif flag == 2: - systype = Systemtype.objects.get(title = offset) - nodelist = systype.nodetype_set.all() - parenttype.append(Systemtype.objects.get(title = offset)) - for i in nodelist: - parenttype.append(i.ref) - pt_id = NID.objects.get(title = offset) - name = pt_id - - +def dynamic_save(request, attit, memtit): + rdict ={} + savedict = {} + memtit = NID.objects.get(title = str(memtit)) + name = memtit.ref absolute_url_node = name.get_absolute_url() - for each in parenttype: - attributetype.append(each.subjecttype_of.all()) - - attributetype = [num for elem in attributetype for num in elem] - - for each in range(len(attributetype)): - rdict.update({attributetype[each]:str(Attributetype.get_dataType_display(attributetype[each]))}) - - if request.method=='POST': - form = ContextForm(rdict,request.POST) - bound= form.is_bound - + at = Attributetype.objects.get(title = str(attit)) + dt = str(at.get_dataType_display()) + MyModel = eval('Attribute'+dt) + rdict.update({str(at.title):MyModel}) + if request.method == 'POST': + form = MakeForm(rdict,request.POST) if form.is_valid(): - for key,val in rdict.items(): - testlst.append(str(request.POST[str(key)+"_"+str(val)])) - - for val in range(len(testlst)): - if testlst[val] != '' : - savedict = {'title':testlst[val],'slug':testlst[val], 'svalue':testlst[val], 'subject':pt_id, 'attributetype':rdict.keys()[val]} - att = Attribute.objects.create(**savedict) - att.save() + value = form.cleaned_data['value'] + savedict = {'title':value,'slug':value,'svalue':value,'subject':memtit, 'attributetype':at,'value':value} + att = MyModel.objects.create(**savedict) + att.save() + return HttpResponseRedirect(absolute_url_node) - return HttpResponseRedirect(absolute_url_node) else: - form = ContextForm(rdict) - + form = MakeForm(rdict) template = "objectapp/fillAT.html" - context = RequestContext(request,{'form' : form, 'name':name,'absolute_url_node':absolute_url_node}) + context = RequestContext(request,{'form' : form,'title':str(attit), 'absolute_url_node':absolute_url_node}) return render_to_response(template,context) -# def dynamic_objecttype(request,iden): #gb view for dynamic at -# testlst = [] -# attributetype = [] -# rdict = {} -# try: -# offset = str(iden) -# except ValueError: -# raise Http404() - -# #uid = Gbobject.objects.get(title = offset) -# gb=Gbobject.objects.get(title=offset) -# parenttype = gb.objecttypes.all() -# pt_id = NID.objects.get(id = gb.id) - - -# for each in parenttype: -# attributetype.append(each.subjecttype_of.all()) - -# attributetype = [num for elem in attributetype for num in elem] - -# for each in range(len(attributetype)): -# rdict.update({attributetype[each]:str(Attributetype.get_dataType_display(attributetype[each]))}) - -# if request.method=='POST': -# form = ContextForm(rdict,request.POST) -# bound= form.is_bound - -# if form.is_valid(): -# for key,val in rdict.items(): -# testlst.append(str(request.POST[str(key)+"_"+str(val)])) - -# for val in range(len(testlst)): -# if testlst[val] != '' : -# savedict = {'title':testlst[val],'slug':testlst[val], 'svalue':testlst[val], 'subject':pt_id, 'attributetype':rdict.keys()[val]} -# att = Attribute.objects.create(**savedict) -# att.save() - -# return HttpResponseRedirect("/objects/") -# else: -# form = ContextForm(rdict) - -# template = "objectapp/fillAT.html" -# context = RequestContext(request,{'form' : form}) -# return render_to_response(template,context) |