Commit 22d93734 by Chif Gergő

request: add RequestType to RequestFields

Now administrators can choose the RequestField in which request should appear. The create form display select field for types. Resource request shows the resource dynamic fields.
parent 906fbf37
Pipeline #684 failed with stage
in 0 seconds
......@@ -64,8 +64,9 @@ class RequestFieldModelForm(ModelForm):
class EditableForm(Form):
def __init__(self, *args, **kwargs):
type = kwargs.pop('type', None)
super(EditableForm, self).__init__(*args, **kwargs)
fields = RequestField.objects.all()
fields = self.get_filtered_fields(type)
n = 0
for field in fields:
n = n+1
......@@ -90,6 +91,9 @@ 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"):
......@@ -144,7 +148,7 @@ class InitialFromFileMixin(object):
return message.strip()
class TemplateRequestForm(InitialFromFileMixin, EditableForm):
class TemplateRequestForm(EditableForm):
template = ModelChoiceField(TemplateAccessType.objects.all(),
label=_("Template share"))
level = ChoiceField(TemplateAccessAction.LEVELS, widget=RadioSelect,
......@@ -152,18 +156,27 @@ class TemplateRequestForm(InitialFromFileMixin, EditableForm):
initial_template = "request/initials/template.html"
def __init__(self, *args, **kwargs):
super(TemplateRequestForm, self).__init__(type='template')
class LeaseRequestForm(InitialFromFileMixin, Form):
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(InitialFromFileMixin, VmResourcesForm):
message = CharField(widget=Textarea, label=_("Message"))
initial_template = "request/initials/resources.html"
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()
......
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-12-12 14:12
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('request', '0006_auto_20181115_1548'),
]
operations = [
migrations.AddField(
model_name='requestfield',
name='request_type',
field=models.CharField(choices=[(b'resource', 'resource request'), (b'lease', 'lease request'), (b'template', 'template access request'), (b'resize', 'disk resize request')], default=b'template', max_length=20),
),
]
......@@ -45,26 +45,6 @@ from storage.models import Disk
logger = logging.getLogger(__name__)
TYPES = (
('Char', 'CharField'),
('Integer', 'IntegerField'),
('Email', 'EmailField')
)
class RequestField(Model):
fieldname = CharField(max_length=50, blank=False, unique=True)
type = CharField(choices=TYPES, default='Char', max_length=20)
choices = CharField(max_length=300, null=True)
required = BooleanField(default=True)
def __unicode__(self):
return self.fieldname
def get_absolute_url(self):
return reverse('fields_detail', kwargs={'pk': self.pk})
class RequestAction(Model):
def accept(self):
......@@ -178,6 +158,27 @@ class Request(TimeStampedModel):
return self.action.is_acceptable()
class RequestField(Model):
TYPES = (
('Char', 'CharField'),
('Integer', 'IntegerField'),
('Email', 'EmailField')
)
fieldname = CharField(max_length=50, blank=False, unique=True)
type = CharField(choices=TYPES, default='Char', max_length=20)
request_type = CharField(choices=Request.TYPES, default='template',
max_length=20)
choices = CharField(max_length=300, null=True)
required = BooleanField(default=True)
def __unicode__(self):
return self.fieldname
def get_absolute_url(self):
return reverse('fields_detail', kwargs={'pk': self.pk})
class LeaseType(RequestType):
lease = ForeignKey(Lease, verbose_name=_("Lease"))
......
......@@ -6,5 +6,8 @@
{% csrf_token %}
{{ form.lease|as_crispy_field }}
{{ form.message|as_crispy_field }}
{% for fields in form.get_dynamic_fields %}
{{ field|as_crispy_field }}
{% endfor %}
<input type="submit" class="btn btn-primary"/>
</form>
......@@ -17,6 +17,7 @@
<tr>
<th class="text-center">{% trans "Fieldname" %}</th>
<th class="text-center">{% trans "Type" %}</th>
<th class="text-center">{% trans "RequestType" %}</th>
<th class="text-center">{% trans "Choices" %}</th>
<th class="text-center">{% trans "Required" %}</th>
<th class="text-center">{% trans "Delete" %}</th>
......@@ -27,6 +28,7 @@
<tr>
<td>{{field.fieldname}}</td>
<td>{{field.type}}</td>
<td>{{field.request_type}}</td>
<td>{{field.choices}}</td>
<td>{{field.required}}</td>
<td><a href={% url "request.views.field-delete" pk=field.pk %}>
......
......@@ -24,7 +24,9 @@
</div>
{% include "display-form-errors.html" %}
{% include "dashboard/_resources-sliders.html" with field_priority=form.priority field_num_cores=form.num_cores field_ram_size=form.ram_size %}
{{ form.message|as_crispy_field }}
{% for field in form.get_dynamic_fields %}
{{ field|as_crispy_field }}
{% endfor %}
<button type="submit" class="btn btn-success">
{% trans "Request new resources" %}
</button>
......
......@@ -64,7 +64,6 @@ class RequestFieldListView(LoginRequiredMixin, ListView):
**kwargs)
ctx['add_form'] = RequestFieldModelForm()
ctx['add_form'].helper.form_show_labels = False
ctx['requests'] = Request.objects.all()
return ctx
......
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