diff --git a/members/forms/__init__.py b/members/forms/__init__.py index be92b7cb..30017b7f 100644 --- a/members/forms/__init__.py +++ b/members/forms/__init__.py @@ -1,7 +1,7 @@ from .person_form import PersonForm from .signup_form import signupForm from .volunteer_signup_form import vol_signupForm -from .volunteer_request_new_form import volunteerRequestNewForm +from .volunteer_request_form import VolunteerRequestForm from .admin_signup_form import adminSignupForm from .activity_signup_form import ActivitySignupForm from .activity_invite_decline_form import ActivivtyInviteDeclineForm @@ -13,7 +13,5 @@ signupForm, vol_signupForm, adminSignupForm, - volunteerRequestNewForm, - volunteer_request_new_form, - + VolunteerRequestForm, ] diff --git a/members/forms/volunteer_request_form.py b/members/forms/volunteer_request_form.py new file mode 100644 index 00000000..ee103ab2 --- /dev/null +++ b/members/forms/volunteer_request_form.py @@ -0,0 +1,103 @@ +from django import forms +from crispy_forms.helper import FormHelper +from crispy_forms.layout import Layout, Fieldset, Hidden, Div, Field, Submit + +from members.models.volunteerrequest import VolunteerRequest + +from members.models.department import Department + +from django.forms.widgets import CheckboxSelectMultiple +from django.utils.html import format_html + + +class CustomCheckboxSelectMultiple(CheckboxSelectMultiple): + def render(self, name, value, attrs=None, choices=()): + output = [] + for option in self.choices: + obj = option[1] + address = f"{obj.name} (" + if obj.address.descriptiontext: + address += f"{obj.address.descriptiontext} " + if obj.address.streetname: + address += f"{obj.address.streetname} " + if obj.address.housenumber: + address += f"{obj.address.housenumber}, " + if obj.address.zipcode: + address += f"{obj.address.zipcode} " + if obj.address.city: + address += f"{obj.address.city}" + address += ")" + output.append( + format_html( + '', + name, + option[0], + address, + ) + ) + return format_html("
{}
", format_html("".join(output))) + + +class VolunteerRequestForm(forms.ModelForm): + department_list = forms.ModelMultipleChoiceField( + queryset=Department.objects.filter(closed_dtm__isnull=True) + .order_by("id") + .distinct(), + widget=CustomCheckboxSelectMultiple(), + required=True, + label="Vælg Afdeling(er)x", + ) + + class Meta: + model = VolunteerRequest + fields = [ + "name", + "email", + "phone", + "age", + "zip", + "info_reference", + "info_whishes", + "department_list", + ] + + def __init__(self, *args, **kwargs): + print("INIT") + super(VolunteerRequestForm, self).__init__(*args, **kwargs) + self.fields["department_list"].label_from_instance = self.label_from_instance + self.helper = FormHelper() + self.helper.form_method = "post" + self.helper.layout = Layout( + Hidden("form_id", "volunteer_requestForm", id="id_form_id"), + Fieldset( + "Frivilliges oplysninger", + Div( + Div(Field("name"), css_class="col-md-12"), + Div(Field("email"), css_class="col-md-12"), + Div(Field("phone"), css_class="col-md-4"), + Div(Field("age"), css_class="col-md-4"), + Div(Field("zip"), css_class="col-md-4"), + Div(Field("info_reference"), css_class="col-md-12"), + Div(Field("info_whishes"), css_class="col-md-12"), + Div(Field("department_list"), css_class="col-md-12"), + css_class="row", + ), + ), + Submit("submit", "Opret", css_class="btn-success"), + ) + + def label_from_instance(self, obj): + address = f"{obj.name} [{obj.id}](" + if obj.address.descriptiontext is not None: + address += f"{obj.address.descriptiontext} " + if obj.address.streetname is not None: + address += f"{obj.address.streetname} " + if obj.address.housenumber is not None: + address += f"{obj.address.housenumber}, " + if obj.address.zipcode is not None: + address += f"{obj.address.zipcode} " + if obj.address.city is not None: + address += f"{obj.address.city}" + if address is not None: + address = f"({address})" + return f"{obj.name} {address}" diff --git a/members/forms/volunteer_request_new_form.py b/members/forms/volunteer_request_new_form.py deleted file mode 100644 index 038c84f3..00000000 --- a/members/forms/volunteer_request_new_form.py +++ /dev/null @@ -1,156 +0,0 @@ -from django import forms -from django.conf import settings -from crispy_forms.helper import FormHelper -from crispy_forms.layout import Layout, Fieldset, Submit, Field, Hidden, Div - -from members.models.department import Department -from members.models.person import Person - -from django.contrib.auth.password_validation import validate_password - - -class volunteerRequestNewForm(forms.Form): - def __init__(self, *args, **kwargs): - super(volunteerRequestNewForm, self).__init__(*args, **kwargs) - self.helper = FormHelper() - self.helper.form_method = "post" - self.helper.form_action = "volunteerRequestNewForm" - self.helper.html5_required = True - self.fields["volunteer_birthday"].widget.format = "%d-%m-%Y" - self.helper.layout = Layout( - Hidden("form_id", "vol_requestNewForm", id="id_form_id"), - Fieldset( - "Frivilliges oplysninger", - Div( - Div(Field("volunteer_gender"), css_class="col-md-2"), - Div(Field("volunteer_name"), css_class="col-md-10"), - Div( - Field( - "volunteer_birthday", - ), - css_class="col-md-3", - ), - Div(Field("volunteer_email"), css_class="col-md-3"), - Div(Field("volunteer_phone"), css_class="col-md-3"), - Div(Field("volunteer_department"), css_class="col-md-3"), - css_class="row", - ), - ), - Fieldset( - "Adgangskode", - Div( - Div(Field("password1"), css_class="col"), - Div(Field("password2"), css_class="col"), - css_class="row", - ), - ), - Fieldset( - "Adresse oplysninger", - Div( - # - Div( - Field("search_address", id="search-address"), - css_class="col-md-10", - ), - Div(Field("manual_entry", id="manual-entry"), css_class="col-md-2"), - Div( - Field( - "streetname", readonly=True, css_class="autofilled-address" - ), - css_class="col-md-9", - ), - Div( - Field( - "housenumber", readonly=True, css_class="autofilled-address" - ), - css_class="col-md-1", - ), - Div( - Field("floor", readonly=True, css_class="autofilled-address"), - css_class="col-md-1", - ), - Div( - Field("door", readonly=True, css_class="autofilled-address"), - css_class="col-md-1", - ), - # - Div( - Field("zipcode", readonly=True, css_class="autofilled-address"), - css_class="col-md-1", - ), - Div( - Field("city", readonly=True, css_class="autofilled-address"), - css_class="col-md-5", - ), - Div( - Field( - "placename", readonly=True, css_class="autofilled-address" - ), - css_class="col-md-5", - ), - Hidden("dawa_id", "", id="id_dawa_id"), - css_class="row", - ), - ), - Submit("submit", "Opret", css_class="btn-success"), - ) - - volunteer_gender = forms.ChoiceField( - label="Køn", required=True, choices=Person.MEMBER_ADULT_GENDER_CHOICES - ) - volunteer_name = forms.CharField(label="Fulde navn", required=True, max_length=200) - volunteer_email = forms.EmailField(label="Email", required=True) - volunteer_phone = forms.CharField(label="Telefon", required=True, max_length=50) - volunteer_birthday = forms.DateField( - label="Fødselsdato", - required=True, - input_formats=(settings.DATE_INPUT_FORMATS), - error_messages={"invalid": "Indtast en gyldig dato."}, - widget=forms.DateInput(attrs={"type": "date"}), - ) - volunteer_department = forms.ModelChoiceField( - queryset=Department.objects.filter(closed_dtm__isnull=True).order_by("name"), - required=True, - label="Afdeling", - empty_label="-", - ) - - - password1 = forms.CharField( - widget=forms.PasswordInput(), - label="Adgangskode", - required=True, - max_length=20, - validators=[validate_password], - ) - password2 = forms.CharField( - widget=forms.PasswordInput(), - label="Gentag adgangskode", - required=True, - max_length=20, - ) - - search_address = forms.CharField( - label="Indtast adresse", required=False, max_length=200 - ) - streetname = forms.CharField(label="Vejnavn", required=True, max_length=200) - housenumber = forms.CharField(label="Nummer", required=True, max_length=5) - floor = forms.CharField(label="Etage", required=False, max_length=3) - door = forms.CharField(label="Dør", required=False, max_length=5) - placename = forms.CharField(label="Stednavn", required=False, max_length=200) - zipcode = forms.CharField(label="Postnummer", max_length=4.0, required=True) - city = forms.CharField(label="By", max_length=200, required=True) - dawa_id = forms.CharField( - label="Dawa ID", max_length=200, widget=forms.HiddenInput(), required=False - ) - form_id = forms.CharField( - label="Form ID", max_length=10, widget=forms.HiddenInput(), initial="signup" - ) - - manual_entry = forms.ChoiceField( - label="Indtast felter manuelt", - widget=forms.CheckboxInput, - required=False, - choices=((True, "True"), (False, "False")), - ) - diff --git a/members/migrations/0061_merge_20241125_1626.py b/members/migrations/0061_merge_20241125_1626.py new file mode 100644 index 00000000..501f78cd --- /dev/null +++ b/members/migrations/0061_merge_20241125_1626.py @@ -0,0 +1,13 @@ +# Generated by Django 4.2.11 on 2024-11-25 15:26 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("members", "0047_volunteerrequest_volunteerrequestdepartment_and_more"), + ("members", "0060_alter_union_options"), + ] + + operations = [] diff --git a/members/templates/members/header.html b/members/templates/members/header.html index 7177079a..04cb7a8e 100644 --- a/members/templates/members/header.html +++ b/members/templates/members/header.html @@ -17,7 +17,7 @@ Log ud {% else %} Tilmeld barn - Bliv frivillig + Bliv frivillig Log ind {% endif %} diff --git a/members/templates/members/volunteer_request.html b/members/templates/members/volunteer_request.html index d30e00fc..e5013654 100644 --- a/members/templates/members/volunteer_request.html +++ b/members/templates/members/volunteer_request.html @@ -3,14 +3,28 @@ {% block content %}

Ønsker du at være frivillig - og ikke allerede oprettet i medlemssystemet ?

-

Hvis du har børn der er tilmeldt i systemet og allerede er oprettet som familie, så log blot på jeres familie side med den email du opskrev dig med, da du tilmeldte dig. Her kan du også tilføje flere personer, hvis det f.eks. er din partner der har tilmeldt børnene men du nu selv vil være med som frivillig.

+

Hvis du har børn der er tilmeldt i systemet og allerede er oprettet som familie, + så log blot på jeres familie side med den email du opskrev dig med, da du tilmeldte dig
+ Her kan du også tilføje flere personer, hvis det f.eks. er din partner der har tilmeldt børnene men du nu selv vil være med som frivillig.

Denne formular er tænkt til frivillige, der ikke har børn der er medlemmer i Coding Pirates.

-
-
- {% crispy vol_signupform %} -
+
+
+ {% if messages %} + {% for message in messages %} +
+ {{ message }} +
+ {% endfor %} + {% endif %} + +
+ {% csrf_token %} + {{ volunteer_request_form|crispy }} + +
+
{% endblock %} \ No newline at end of file diff --git a/members/templates/members/volunteer_request_created.html b/members/templates/members/volunteer_request_created.html new file mode 100644 index 00000000..04d04180 --- /dev/null +++ b/members/templates/members/volunteer_request_created.html @@ -0,0 +1,7 @@ +{% extends 'members/base.html' %} +{% block content %} +
+

Volunteer Request Submitted

+

Your volunteer request has been submitted successfully!

+
+{% endblock %} \ No newline at end of file diff --git a/members/urls.py b/members/urls.py index 8468c2f4..2241e848 100644 --- a/members/urls.py +++ b/members/urls.py @@ -1,4 +1,6 @@ from django.urls import re_path +from django.views.generic import TemplateView + from members.views import ( AccountCreate, Activities, @@ -14,13 +16,12 @@ PersonUpdate, QuickpayCallback, SupportMembership, - VolunteerRequestNew, + volunteer_request_view, WaitingListSetSubscription, departmentView, paymentGatewayErrorView, userCreated, volunteerSignup, - VolunteerRequestNew, ) from django.contrib.auth import views as auth_views from graphene_django.views import GraphQLView @@ -76,10 +77,12 @@ re_path(r"^membership/$", Membership, name="membership"), re_path(r"^support_membership/$", SupportMembership, name="support_membership"), re_path(r"^volunteer$", volunteerSignup, name="volunteer_signup"), - # re_path(r"^volunteer_request_new/$", VolunteerRequestNew, name="volunteer_request_new"), + re_path(r"^volunteer_request/$", volunteer_request_view, name="volunteer_request"), re_path( - r"^new_vr/$", VolunteerRequestNew, name="volunteer_request_new_form" - ), # name="volunteer_request_new"), + r"^volunteer_request_created/$", + TemplateView.as_view(template_name="members/volunteer_request_created.html"), + name="volunteer_request_created", + ), re_path(r"^user_created/$", userCreated, name="user_created"), re_path(r"^admin_signup/$", AdminSignup, name="admin_signup"), re_path(r"^family/$", FamilyDetails, name="family_detail"), diff --git a/members/views/VolunteerRequest.py b/members/views/VolunteerRequest.py new file mode 100644 index 00000000..40836a63 --- /dev/null +++ b/members/views/VolunteerRequest.py @@ -0,0 +1,42 @@ +from django.shortcuts import render +from django.http import HttpResponseRedirect +from django.urls import reverse +from django.contrib import messages +from members.forms import VolunteerRequestForm +from members.models.volunteerrequestdepartment import VolunteerRequestDepartment + + +def volunteer_request_view(request): + if request.method == "POST": + volunteer_request_form = VolunteerRequestForm(request.POST) + + if volunteer_request_form.is_valid(): + vol_req_obj = volunteer_request_form.save() + departments = volunteer_request_form.cleaned_data["department_list"] + for department in departments: + VolunteerRequestDepartment.objects.create( + volunteer_request=vol_req_obj, department=department + ) + + messages.success( + request, "Your volunteer request has been submitted successfully!" + ) + return HttpResponseRedirect(reverse("volunteer_request_created")) + else: + return render( + request, + "members/volunteer_request.html", + { + "volunteer_request_form": volunteer_request_form, + }, + ) + else: + volunteer_request_form = VolunteerRequestForm() + + return render( + request, + "members/volunteer_request.html", + { + "volunteer_request_form": volunteer_request_form, + }, + ) diff --git a/members/views/VolunteerRequestNew.py b/members/views/VolunteerRequestNew.py deleted file mode 100644 index eae12d33..00000000 --- a/members/views/VolunteerRequestNew.py +++ /dev/null @@ -1,79 +0,0 @@ -from django.urls import reverse -from django.http import HttpResponseRedirect -from django.shortcuts import render -from django.utils import timezone -from django.views.decorators.clickjacking import xframe_options_exempt - -from members.forms import volunteer_request_new_form -from members.models.department import Department -from members.models.family import Family -from members.models.person import Person -from members.models.volunteer import Volunteer -from members.models.volunteerrequest import VolunteerRequest -from members.models.volunteerrequestdepartment import VolunteerRequestDepartment -from django.contrib.auth.models import User - - -@xframe_options_exempt -def VolunteerRequestNew(request): - if request.method == "POST": - # which form was filled out ? - if request.POST["form_id"] == "volunteer_request_new_form": - vol_request_form = volunteer_request_new_form(request.POST) - if vol_request_form.is_valid(): - ######################################### - # Check for minimum 1 department selected - ######################################### - - # Check if email already exist for a user - try: - family = Family.objects.get( - email__iexact=request.POST["volunteer_email"] - ) - vol_request_form.add_error( - "volunteer_email", - f"Denne email addresse ({family.email}) er allerede oprettet. Log ind ovenfor, for at få adgang.", - ) - # using family.email to ensure family is used, to avoid warning from flake8 - return render( - request, - "members/volunteer_request.html", - {"volunteer_request_new_form": vol_request_form}, - ) - except: # noqa: E722 - # all is fine - we did not expect any - pass - - # Create record for the request - vol_req_obj = VolunteerRequest.objects.create( - person=None, - name=vol_request_form.cleaned_data["volunteer_name"], - email=None, - phone=None, - age=None, - zip=None, - info_reference=None, - info_whishes=None, - token=None, - ) - vol_req_obj.save() - - # Loop through all the selected departments and make records for them - - # Send email(s) to department(s) and requestor ? - - return HttpResponseRedirect(reverse("user_created")) # THIS IS WRONG! - else: - return render( - request, - "members/volunteer_request.html", - {"volunteer_request_new_form": vol_request_form}, - ) - - # initial load (if we did not return above) - vol_request_form = volunteer_request_new_form() - return render( - request, - "members/volunteer_request.html", - {"volunteer_request_new_form": vol_request_form}, - ) diff --git a/members/views/__init__.py b/members/views/__init__.py index daa19ac0..c64a6180 100644 --- a/members/views/__init__.py +++ b/members/views/__init__.py @@ -19,4 +19,4 @@ from members.views.paymentGatewayErrorView import paymentGatewayErrorView from members.views.userCreated import userCreated from members.views.volunteerSignup import volunteerSignup -from members.views.VolunteerRequestNew import VolunteerRequestNew +from members.views.VolunteerRequest import volunteer_request_view