Commit 243a0c02 by Chif Gergő

request: Delete code from Request forms for InitialFormFileMixin initialization,…

request: Delete code from Request forms for InitialFormFileMixin initialization, in views get data from dynamic fields, make change in resource test
parent 22d93734
......@@ -65,9 +65,11 @@ class RequestFieldModelForm(ModelForm):
class EditableForm(Form):
def __init__(self, *args, **kwargs):
type = kwargs.pop('type', None)
kwargs.pop("request", None)
super(EditableForm, self).__init__(*args, **kwargs)
fields = self.get_filtered_fields(type)
fields = RequestField.objects.filter(request_type=type)
n = 0
if fields:
for field in fields:
n = n+1
type = field.type
......@@ -91,9 +93,6 @@ class EditableForm(Form):
required=field.required,
label=field.fieldname)
def get_filtered_fields(self, type):
return RequestField.objects.filter(request_type=type)
def get_dynamic_fields(self):
for field_name in self.fields:
if field_name.startswith("field"):
......@@ -154,29 +153,12 @@ class TemplateRequestForm(EditableForm):
level = ChoiceField(TemplateAccessAction.LEVELS, widget=RadioSelect,
initial=TemplateAccessAction.LEVELS.user)
initial_template = "request/initials/template.html"
def __init__(self, *args, **kwargs):
super(TemplateRequestForm, self).__init__(type='template')
class LeaseRequestForm(EditableForm):
lease = ModelChoiceField(LeaseType.objects.all(), label=_("Lease"))
message = CharField(widget=Textarea, label=_("Message"))
initial_template = "request/initials/lease.html"
def __init__(self, *args, **kwargs):
super(LeaseRequestForm, self).__init__(type='lease')
class ResourceRequestForm(EditableForm, VmResourcesForm):
# message = CharField(widget=Textarea, label=_("Message"))
#
# initial_template = "request/initials/resources.html"
def __init__(self, *args, **kwargs):
super(ResourceRequestForm, self).__init__(type='resource')
def clean(self):
cleaned_data = super(ResourceRequestForm, self).clean()
......
......@@ -67,12 +67,16 @@ class RequestTest(LoginMixin, MockCeleryMixin, TestCase):
inst = Instance.objects.get(pk=1)
inst.set_level(self.u1, 'owner')
field = RequestField(fieldname="Oka", type="Char",
request_type="resource", required=True)
field.save()
req_count = Request.objects.count()
resp = c.post("/request/resource/1/", {
'num_cores': 5,
'ram_size': 512,
'priority': 30,
'message': "szia",
'field1': "szia",
})
self.assertEqual(resp.status_code, 302)
self.assertEqual(req_count + 1, Request.objects.count())
......@@ -104,10 +108,11 @@ class RequestTest(LoginMixin, MockCeleryMixin, TestCase):
template = InstanceTemplate.objects.get(pk=1)
self.assertFalse(template.has_level(self.u1, "user"))
field = RequestField(fieldname="Tanszek", type="Char", required=True)
field = RequestField(fieldname="Tanszek", type="Char",
request_type="template", required=True)
field.save()
field = RequestField(fieldname="Szobaszam", type="Integer",
required=False)
request_type="template", required=False)
field.save()
req_count = Request.objects.count()
......
......@@ -206,7 +206,7 @@ class TemplateRequestView(LoginRequiredMixin, FormView):
def get_form_kwargs(self):
kwargs = super(TemplateRequestView, self).get_form_kwargs()
kwargs['request'] = self.request
kwargs['type'] = 'template'
return kwargs
def form_valid(self, form):
......@@ -273,6 +273,12 @@ class LeaseRequestView(VmRequestMixin, FormView):
user_level = "operator"
success_message = _("Request successfully sent.")
def get_form_kwargs(self):
kwargs = super(LeaseRequestView, self).get_form_kwargs()
kwargs['type'] = 'resource'
return kwargs
def form_valid(self, form):
data = form.cleaned_data
user = self.request.user
......@@ -284,9 +290,14 @@ class LeaseRequestView(VmRequestMixin, FormView):
)
el.save()
message = ''
for field in form.get_dynamic_fields():
message += "%s: %s\n" % (unicode(field.label),
unicode(data[field.name]))
req = Request(
user=user,
message=data['message'],
message=message,
type=Request.TYPES.lease,
action=el
)
......@@ -304,6 +315,7 @@ class ResourceRequestView(VmRequestMixin, FormView):
def get_form_kwargs(self):
kwargs = super(ResourceRequestView, self).get_form_kwargs()
kwargs['type'] = 'resource'
kwargs['can_edit'] = True
kwargs['instance'] = self.get_vm()
return kwargs
......@@ -329,9 +341,14 @@ class ResourceRequestView(VmRequestMixin, FormView):
)
rc.save()
message = ''
for field in form.get_dynamic_fields():
message += "%s: %s\n" % (unicode(field.label),
unicode(data[field.name]))
req = Request(
user=user,
message=data['message'],
message=message,
type=Request.TYPES.resource,
action=rc
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment