From 6b19fc43b78ca3411353b3959965ad4abec4aee6 Mon Sep 17 00:00:00 2001 From: Taib Hayat Date: Wed, 4 Aug 2021 15:05:58 +0200 Subject: [PATCH 1/2] basic views and models --- self_registration/main/models.py | 37 +++++++++++++++++++++++++++ self_registration/main/views.py | 37 +++++++++++++++++++++++++++ self_registration/settings/default.py | 1 + self_registration/urls.py | 3 +++ 4 files changed, 78 insertions(+) create mode 100644 self_registration/main/models.py create mode 100644 self_registration/main/views.py diff --git a/self_registration/main/models.py b/self_registration/main/models.py new file mode 100644 index 0000000..cd50681 --- /dev/null +++ b/self_registration/main/models.py @@ -0,0 +1,37 @@ +# (c) 2020 MPIB , +# +# This file is part of castellum-self-registration. +# +# castellum-self-registration is free software; you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation; either version 3 of the +# License, or (at your option) any later version. +# +# Castellum is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with Castellum. If not, see +# . + + +from django.db import models +from django.utils.translation import gettext_lazy as _ + +from castellum.utils.fields import DateField +from castellum.utils.fields import PhoneNumberField + + +class SelfRegisteredSubject(models.Model): + GENDER = [ + ("f", _("female")), + ("m", _("male")), + ("*", _("diverse")), + ] + + first_name = models.CharField(_('First name'), max_length=64) + last_name = models.CharField(_('Last name'), max_length=64) + gender = models.CharField(_('Gender'), max_length=1, choices=GENDER, blank=True) + email = models.EmailField(_('Email'), max_length=128, blank=True) diff --git a/self_registration/main/views.py b/self_registration/main/views.py new file mode 100644 index 0000000..cef22ce --- /dev/null +++ b/self_registration/main/views.py @@ -0,0 +1,37 @@ +# (c) 2020 MPIB , +# +# This file is part of castellum-self-registration. +# +# castellum-self-registration is free software; you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation; either version 3 of the +# License, or (at your option) any later version. +# +# Castellum is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with Castellum. If not, see +# . + + +from django.contrib import messages +from django.urls import reverse +from django.views.generic import CreateView + +from self_registration.models import SelfRegisteredSubject + + +class SelfRegisteredSubjectCreateView(CreateView): + model = SelfRegisteredSubject + template_name = 'subject_form.html' + fields = ['first_name', 'last_name', 'gender', 'date_of_birth', 'email', 'phone_number'] + + def get_success_url(self): + return reverse('subject-create') + + def form_valid(self, form, *args): + messages.success(self.request, _('Data has been saved.')) + return super().form_valid(form, *args) diff --git a/self_registration/settings/default.py b/self_registration/settings/default.py index 9c26bd0..d971746 100644 --- a/self_registration/settings/default.py +++ b/self_registration/settings/default.py @@ -13,6 +13,7 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'bootstrap4', + 'self_registration.main', ] MIDDLEWARE = [ diff --git a/self_registration/urls.py b/self_registration/urls.py index 1a14f30..d64f656 100644 --- a/self_registration/urls.py +++ b/self_registration/urls.py @@ -16,6 +16,9 @@ Including another URLconf from django.contrib import admin from django.urls import path +from self_registration.views import SelfRegisteredSubjectCreateView + urlpatterns = [ path('admin/', admin.site.urls), + path('create/', SelfRegisteredSubjectCreateView.as_view(), name='subject-create'), ] -- GitLab From 8bfa9d5d7a77bd30ef9c95f9eed6f01a71ffe925 Mon Sep 17 00:00:00 2001 From: Taib Hayat Date: Wed, 4 Aug 2021 16:00:17 +0200 Subject: [PATCH 2/2] add PhoneNumberField and DateField --- self_registration/main/models.py | 4 +-- self_registration/utils/fields.py | 36 ++++++++++++++++++++ self_registration/utils/forms.py | 56 +++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 self_registration/utils/fields.py create mode 100644 self_registration/utils/forms.py diff --git a/self_registration/main/models.py b/self_registration/main/models.py index cd50681..810ea2d 100644 --- a/self_registration/main/models.py +++ b/self_registration/main/models.py @@ -20,8 +20,8 @@ from django.db import models from django.utils.translation import gettext_lazy as _ -from castellum.utils.fields import DateField -from castellum.utils.fields import PhoneNumberField +from self_registration.utils.fields import DateField +from self_registration.utils.fields import PhoneNumberField class SelfRegisteredSubject(models.Model): diff --git a/self_registration/utils/fields.py b/self_registration/utils/fields.py new file mode 100644 index 0000000..b32f469 --- /dev/null +++ b/self_registration/utils/fields.py @@ -0,0 +1,36 @@ +# (c) 2020 MPIB , +# +# This file is part of castellum-self-registration. +# +# castellum-self-registration is free software; you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation; either version 3 of the +# License, or (at your option) any later version. +# +# Castellum is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with Castellum. If not, see +# . + +from django.db import models + +from phonenumber_field.modelfields import PhoneNumberField as _PhoneNumberField + +from .forms import DateField as DateFormField +from .forms import PhoneNumberField as PhoneNumberFormField + + +class DateField(models.DateField): + def formfield(self, **kwargs): + kwargs.setdefault('form_class', DateFormField) + return super().formfield(**kwargs) + + +class PhoneNumberField(_PhoneNumberField): + def formfield(self, **kwargs): + kwargs.setdefault('form_class', PhoneNumberFormField) + return super().formfield(**kwargs) diff --git a/self_registration/utils/forms.py b/self_registration/utils/forms.py new file mode 100644 index 0000000..d1458b0 --- /dev/null +++ b/self_registration/utils/forms.py @@ -0,0 +1,56 @@ +# (c) 2018-2021 +# MPIB , +# MPI-CBS , +# MPIP +# +# This file is part of Castellum. +# +# Castellum is free software; you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Castellum is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with Castellum. If not, see +# . + +from django import forms +from django.utils.translation import gettext_lazy as _ + +from phonenumber_field.formfields import PhoneNumberField as _PhoneNumberFormField +from phonenumber_field.widgets import PhoneNumberInternationalFallbackWidget + + +class DateInput(forms.DateInput): + def __init__(self, attrs=None): + defaults = { + 'placeholder': _('yyyy-mm-dd, e.g. 2018-07-21'), + 'autocomplete': 'off', + 'type': 'date', + } + if attrs: + defaults.update(attrs) + super().__init__(format='%Y-%m-%d', attrs=defaults) + + +class DateField(forms.DateField): + widget = DateInput + + +class PhoneNumberWidget(PhoneNumberInternationalFallbackWidget): + def __init__(self, attrs=None): + defaults = { + 'placeholder': _('e.g. 030 123456'), + } + if attrs: + defaults.update(defaults) + super().__init__(attrs=defaults) + + +class PhoneNumberField(_PhoneNumberFormField): + widget = PhoneNumberWidget -- GitLab