From 2cf41c9cb9e2a3dcf85b9040dc22849fe9b5e9a6 Mon Sep 17 00:00:00 2001 From: henu Date: Wed, 11 Oct 2017 20:26:38 +0300 Subject: [PATCH] Add custom role creation to kaehmy form --- webapp/forms.py | 6 ++--- webapp/migrations/0020_auto_20171011_2020.py | 25 ++++++++++++++++++++ webapp/models.py | 14 ++++++++--- webapp/views.py | 8 ++++++- 4 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 webapp/migrations/0020_auto_20171011_2020.py diff --git a/webapp/forms.py b/webapp/forms.py index 5c28dca..0015e68 100644 --- a/webapp/forms.py +++ b/webapp/forms.py @@ -6,13 +6,11 @@ from webapp.models import OhlhafvChallenge, KaehmyForm class KaehmyForm_Form(forms.ModelForm): - custom_role_name = forms.TextInput( - attrs={'size': 10, 'title': 'Name of the role', }) - class Meta: model = KaehmyForm fields = ['name', 'email', 'year', - 'preset_roles', 'custom_roles', 'text'] + 'preset_roles', 'custom_roles', 'custom_role_name', + 'custom_role_is_board', 'text'] class OhlhafvForm(forms.ModelForm): diff --git a/webapp/migrations/0020_auto_20171011_2020.py b/webapp/migrations/0020_auto_20171011_2020.py new file mode 100644 index 0000000..2aabb06 --- /dev/null +++ b/webapp/migrations/0020_auto_20171011_2020.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11 on 2017-10-11 17:20 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('webapp', '0019_kaehmymessage_timestamp'), + ] + + operations = [ + migrations.AddField( + model_name='kaehmyform', + name='custom_role_is_board', + field=models.BooleanField(default=False, verbose_name='Board member'), + ), + migrations.AddField( + model_name='kaehmyform', + name='custom_role_name', + field=models.CharField(default='', max_length=255, verbose_name='Custom role name'), + ), + ] diff --git a/webapp/models.py b/webapp/models.py index aaa8034..3143438 100644 --- a/webapp/models.py +++ b/webapp/models.py @@ -119,7 +119,7 @@ class KaehmyFormSelectedRole(models.Model): class MessageParent(models.Model): - + def __str__(self): return 'Message parent #{}'.format(self.id) @@ -156,13 +156,21 @@ class KaehmyForm(MessageParent): email = models.EmailField(_('Email')) year = models.IntegerField(_('Year'), choices=YEAR_CHOICES) text = models.TextField(_('Text'), default="", max_length=300) - custom_roles = models.ManyToManyField('CustomKaehmyRole', related_name='forms', blank=True) - preset_roles = models.ManyToManyField('PresetKaehmyRole', related_name='forms', blank=True) + custom_roles = models.ManyToManyField( + 'CustomKaehmyRole', related_name='forms', blank=True) + preset_roles = models.ManyToManyField( + 'PresetKaehmyRole', related_name='forms', blank=True) + custom_role_name = models.CharField( + _('Custom role name'), max_length=255, default="") + custom_role_is_board = models.BooleanField( + _('Board member'), default=False) def __str__(self): + """Return model info.""" return _('Kaehmy application: {}').format(self.name) def comment_count(self): + """Count comments for kaehmy.""" total = 0 def recurse(message): diff --git a/webapp/views.py b/webapp/views.py index f606021..9401e70 100644 --- a/webapp/views.py +++ b/webapp/views.py @@ -8,6 +8,7 @@ from django.http import HttpResponse, HttpResponseRedirect from django.contrib.auth.decorators import permission_required, login_required from django.conf import settings import logging +from webapp.models import CustomKaehmyRole from webapp.models import OhlhafvChallenge, KaehmyForm from webapp.forms import OhlhafvForm, KaehmyForm_Form from webapp.tables import OhlhafvTable @@ -120,6 +121,11 @@ def kaehmy_submit(request, *args, **kwargs): form = KaehmyForm_Form(request.POST) if form.is_valid(): form.save() + custom_name = form.cleaned_data.get('custom_role_name') + custom_is_board = form.cleaned_data.get('custom_role_is_board') + custom_role = CustomKaehmyRole( + name=custom_name, is_board=custom_is_board) + custom_role.save() else: pass # return render(request, 'error.html', {'error': form.errors}) @@ -174,7 +180,7 @@ def kaehmy_list_view(request, *args, **kwargs): """Kaehmy application list""" applications = KaehmyForm.objects.all() - + context = { 'applications': applications, 'application_count': len(applications)