From ed080b76a2f82974ef6f86e58745ce8ccdb68e8d Mon Sep 17 00:00:00 2001 From: Skia Date: Thu, 4 Feb 2016 08:59:03 +0100 Subject: [PATCH] WIP: Club member form --- club/models.py | 3 +++ club/templates/club/club_members.jinja | 9 ++++++-- club/views.py | 30 +++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/club/models.py b/club/models.py index 00b7287f..83db8425 100644 --- a/club/models.py +++ b/club/models.py @@ -81,5 +81,8 @@ class Membership(models.Model): " - "+str(_('past member')) if self.end_date is not None else "" ) + def get_absolute_url(self): + return reverse('club:club_members', kwargs={'club_id': self.club.id}) + diff --git a/club/templates/club/club_members.jinja b/club/templates/club/club_members.jinja index 6d93f1ff..2788d934 100644 --- a/club/templates/club/club_members.jinja +++ b/club/templates/club/club_members.jinja @@ -1,11 +1,16 @@ {% extends "core/base.jinja" %} {% block content %} -

Edit club

+

Club members

+
{% csrf_token %} {{ form.as_p() }} -

+

{% endblock %} diff --git a/club/views.py b/club/views.py index b17da97e..2c08b1cd 100644 --- a/club/views.py +++ b/club/views.py @@ -1,8 +1,11 @@ +from django import forms from django.shortcuts import render from django.views.generic import ListView, DetailView -from django.views.generic.edit import UpdateView -from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin +from django.views.generic.edit import UpdateView, CreateView +from django.forms import CheckboxSelectMultiple + +from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin from club.models import Club, Membership class ClubListView(CanViewMixin, ListView): @@ -20,12 +23,33 @@ class ClubEditView(CanEditMixin, UpdateView): fields = ['address'] template_name = 'club/club_edit.jinja' +class ClubMemberForm(forms.ModelForm): + error_css_class = 'error' + required_css_class = 'required' + class Meta: + model = Membership + fields = ['user', 'role'] + + def clean(self): + print(self.__dict__) +# TODO: see how to get access to request.user! We need some right validation somewhere! + return super(ClubMemberForm, self).clean() + class ClubEditMembersView(CanEditMixin, UpdateView): model = Club pk_url_kwarg = "club_id" - fields = ['user'] + form_class = ClubMemberForm template_name = 'club/club_members.jinja' + def get_form(self): + form = super(ClubEditMembersView, self).get_form() + if 'user' in form.data and form.data.get('user') != '': # Load an existing membership if possible + form.instance = Membership.objects.filter(club=self.object).filter(user=form.data.get('user')).filter(end_date=None).first() + if form.instance is None: # Instanciate a new membership + form.instance = Membership(club=self.object, user=self.request.user) + form.initial = {'user': self.request.user} + return form + class ClubEditPropView(CanEditPropMixin, UpdateView): model = Club pk_url_kwarg = "club_id"