summaryrefslogtreecommitdiff
path: root/objectapp/views/dynamicAT.py
blob: aec02fc255e034d0a021fb4b3c29f08a026ccc94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

from django.http import *
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_save(request, attit, memtit):
	rdict ={}
	savedict = {}
	memtit = NID.objects.get(title = str(memtit))
	name = memtit.ref
	absolute_url_node = name.get_absolute_url()
	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():
			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)	

	else:
		form = MakeForm(rdict)

	template = "objectapp/fillAT.html"
	context = RequestContext(request,{'form' : form,'title':str(attit), 'absolute_url_node':absolute_url_node}) 
	return render_to_response(template,context)