Commit 6d41f4b8 by Chif Gergő

request: Adjust request fields urls add EditableForm

URL names now follow the convention. Added EditableForm in request/forms.py. Its __init__ method adds the fields from RequestField model. The TemplateRequest inherit the EditableForm class.
parent efab5d92
......@@ -16,7 +16,7 @@
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
from django.forms import (
ModelForm, ModelChoiceField, ChoiceField, Form, CharField, RadioSelect,
Textarea, ValidationError, TextInput,
Textarea, ValidationError, TextInput, IntegerField, EmailField
)
from django.utils.translation import ugettext_lazy as _
from django.template.loader import render_to_string
......@@ -50,6 +50,40 @@ class RequestFieldModelForm(ModelForm):
raise ValidationError(_("Email field can't have choices!"))
class EditableForm(Form):
def __init__(self, *args, **kwargs):
super(EditableForm, self).__init__(*args, **kwargs)
fields = RequestField.objects.all()
n = 0
for field in fields:
n = n+1
type = field.type
if(type == 'Char'):
self.fields['field'+str(n)] = CharField(
max_length=30,
required=field.required,
label=field.fieldname)
elif (type == 'Integer'):
self.fields['field'+str(n)] = IntegerField(
required=field.required,
label=field.fieldname)
elif (type == 'Email'):
self.fields['field'+str(n)] = EmailField(
required=field.required,
label=field.fieldname)
if(field.choices):
choices = [(ch, ch)for ch in field.choices.split(',')]
self.fields['field'+str(n)] = ChoiceField(
choices=choices,
required=field.required,
label=field.fieldname)
def get_dynamic_fields(self):
for field_name in self.fields:
if field_name.startswith("field"):
yield self[field_name]
class LeaseTypeForm(ModelForm):
@property
def helper(self):
......@@ -98,8 +132,7 @@ class InitialFromFileMixin(object):
return message.strip()
class TemplateRequestForm(InitialFromFileMixin, Form):
message = CharField(widget=Textarea, label=_("Message"))
class TemplateRequestForm(InitialFromFileMixin, EditableForm):
template = ModelChoiceField(TemplateAccessType.objects.all(),
label=_("Template share"))
level = ChoiceField(TemplateAccessAction.LEVELS, widget=RadioSelect,
......
......@@ -37,12 +37,14 @@ urlpatterns = [
url(r'^type/list/$', RequestTypeList.as_view(),
name="request.views.type-list"),
url(r'fields/add/$', RequestFieldFormView.as_view(), name='field_add'),
url(r'fields/$', RequestFieldListView.as_view(), name='fields_list'),
url(r'fields/add/$', RequestFieldFormView.as_view(),
name='request.views.request-field-add'),
url(r'fields/$', RequestFieldListView.as_view(),
name='request.views.field-list'),
url(r'fields/field/(?P<pk>[0-9]+)/$', RequestFieldDetailView.as_view(),
name='fields_detail'),
url(r'^delete/(?P<pk>\d+)/$', RequestFieldDeleteView.as_view(),
name='field_delete'),
name='request.views.fields-detail'),
url(r'^field/delete/(?P<pk>\d+)/$', RequestFieldDeleteView.as_view(),
name='request.views.field-delete'),
# request types
url(r'^type/lease/create/$', LeaseTypeCreate.as_view(),
......
......@@ -49,14 +49,14 @@ from django.urls import reverse_lazy
class RequestFieldFormView(LoginRequiredMixin, CreateView):
template_name = 'request/field_form.html'
template_name = 'request/_request-field-form.html'
model = RequestField
form_class = RequestFieldModelForm
success_url = reverse_lazy('fields_list')
success_url = reverse_lazy('request.views.field-list')
class RequestFieldListView(LoginRequiredMixin, ListView):
template_name = 'request/field_list.html'
template_name = 'request/field-list.html'
model = RequestField
def get_context_data(self, *args, **kwargs):
......@@ -74,7 +74,8 @@ class RequestFieldDetailView(LoginRequiredMixin, DetailView):
class RequestFieldDeleteView(LoginRequiredMixin, DeleteView):
model = RequestField
success_url = reverse_lazy('fields_list')
template_name = "dashboard/confirm/base-delete.html"
success_url = reverse_lazy('request.views.field-list')
class RequestList(LoginRequiredMixin, SuperuserRequiredMixin, SingleTableView):
......@@ -219,9 +220,13 @@ class TemplateRequestView(LoginRequiredMixin, FormView):
)
ta.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.template,
action=ta
)
......
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