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
Pipeline #685 passed with stage
in 0 seconds
...@@ -65,9 +65,11 @@ class RequestFieldModelForm(ModelForm): ...@@ -65,9 +65,11 @@ class RequestFieldModelForm(ModelForm):
class EditableForm(Form): class EditableForm(Form):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
type = kwargs.pop('type', None) type = kwargs.pop('type', None)
kwargs.pop("request", None)
super(EditableForm, self).__init__(*args, **kwargs) super(EditableForm, self).__init__(*args, **kwargs)
fields = self.get_filtered_fields(type) fields = RequestField.objects.filter(request_type=type)
n = 0 n = 0
if fields:
for field in fields: for field in fields:
n = n+1 n = n+1
type = field.type type = field.type
...@@ -91,9 +93,6 @@ class EditableForm(Form): ...@@ -91,9 +93,6 @@ class EditableForm(Form):
required=field.required, required=field.required,
label=field.fieldname) label=field.fieldname)
def get_filtered_fields(self, type):
return RequestField.objects.filter(request_type=type)
def get_dynamic_fields(self): def get_dynamic_fields(self):
for field_name in self.fields: for field_name in self.fields:
if field_name.startswith("field"): if field_name.startswith("field"):
...@@ -154,29 +153,12 @@ class TemplateRequestForm(EditableForm): ...@@ -154,29 +153,12 @@ class TemplateRequestForm(EditableForm):
level = ChoiceField(TemplateAccessAction.LEVELS, widget=RadioSelect, level = ChoiceField(TemplateAccessAction.LEVELS, widget=RadioSelect,
initial=TemplateAccessAction.LEVELS.user) initial=TemplateAccessAction.LEVELS.user)
initial_template = "request/initials/template.html"
def __init__(self, *args, **kwargs):
super(TemplateRequestForm, self).__init__(type='template')
class LeaseRequestForm(EditableForm): class LeaseRequestForm(EditableForm):
lease = ModelChoiceField(LeaseType.objects.all(), label=_("Lease")) 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): 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): def clean(self):
cleaned_data = super(ResourceRequestForm, self).clean() cleaned_data = super(ResourceRequestForm, self).clean()
......
...@@ -67,12 +67,16 @@ class RequestTest(LoginMixin, MockCeleryMixin, TestCase): ...@@ -67,12 +67,16 @@ class RequestTest(LoginMixin, MockCeleryMixin, TestCase):
inst = Instance.objects.get(pk=1) inst = Instance.objects.get(pk=1)
inst.set_level(self.u1, 'owner') inst.set_level(self.u1, 'owner')
field = RequestField(fieldname="Oka", type="Char",
request_type="resource", required=True)
field.save()
req_count = Request.objects.count() req_count = Request.objects.count()
resp = c.post("/request/resource/1/", { resp = c.post("/request/resource/1/", {
'num_cores': 5, 'num_cores': 5,
'ram_size': 512, 'ram_size': 512,
'priority': 30, 'priority': 30,
'message': "szia", 'field1': "szia",
}) })
self.assertEqual(resp.status_code, 302) self.assertEqual(resp.status_code, 302)
self.assertEqual(req_count + 1, Request.objects.count()) self.assertEqual(req_count + 1, Request.objects.count())
...@@ -104,10 +108,11 @@ class RequestTest(LoginMixin, MockCeleryMixin, TestCase): ...@@ -104,10 +108,11 @@ class RequestTest(LoginMixin, MockCeleryMixin, TestCase):
template = InstanceTemplate.objects.get(pk=1) template = InstanceTemplate.objects.get(pk=1)
self.assertFalse(template.has_level(self.u1, "user")) 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.save()
field = RequestField(fieldname="Szobaszam", type="Integer", field = RequestField(fieldname="Szobaszam", type="Integer",
required=False) request_type="template", required=False)
field.save() field.save()
req_count = Request.objects.count() req_count = Request.objects.count()
......
...@@ -206,7 +206,7 @@ class TemplateRequestView(LoginRequiredMixin, FormView): ...@@ -206,7 +206,7 @@ class TemplateRequestView(LoginRequiredMixin, FormView):
def get_form_kwargs(self): def get_form_kwargs(self):
kwargs = super(TemplateRequestView, self).get_form_kwargs() kwargs = super(TemplateRequestView, self).get_form_kwargs()
kwargs['request'] = self.request kwargs['type'] = 'template'
return kwargs return kwargs
def form_valid(self, form): def form_valid(self, form):
...@@ -273,6 +273,12 @@ class LeaseRequestView(VmRequestMixin, FormView): ...@@ -273,6 +273,12 @@ class LeaseRequestView(VmRequestMixin, FormView):
user_level = "operator" user_level = "operator"
success_message = _("Request successfully sent.") 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): def form_valid(self, form):
data = form.cleaned_data data = form.cleaned_data
user = self.request.user user = self.request.user
...@@ -284,9 +290,14 @@ class LeaseRequestView(VmRequestMixin, FormView): ...@@ -284,9 +290,14 @@ class LeaseRequestView(VmRequestMixin, FormView):
) )
el.save() el.save()
message = ''
for field in form.get_dynamic_fields():
message += "%s: %s\n" % (unicode(field.label),
unicode(data[field.name]))
req = Request( req = Request(
user=user, user=user,
message=data['message'], message=message,
type=Request.TYPES.lease, type=Request.TYPES.lease,
action=el action=el
) )
...@@ -304,6 +315,7 @@ class ResourceRequestView(VmRequestMixin, FormView): ...@@ -304,6 +315,7 @@ class ResourceRequestView(VmRequestMixin, FormView):
def get_form_kwargs(self): def get_form_kwargs(self):
kwargs = super(ResourceRequestView, self).get_form_kwargs() kwargs = super(ResourceRequestView, self).get_form_kwargs()
kwargs['type'] = 'resource'
kwargs['can_edit'] = True kwargs['can_edit'] = True
kwargs['instance'] = self.get_vm() kwargs['instance'] = self.get_vm()
return kwargs return kwargs
...@@ -329,9 +341,14 @@ class ResourceRequestView(VmRequestMixin, FormView): ...@@ -329,9 +341,14 @@ class ResourceRequestView(VmRequestMixin, FormView):
) )
rc.save() rc.save()
message = ''
for field in form.get_dynamic_fields():
message += "%s: %s\n" % (unicode(field.label),
unicode(data[field.name]))
req = Request( req = Request(
user=user, user=user,
message=data['message'], message=message,
type=Request.TYPES.resource, type=Request.TYPES.resource,
action=rc 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