WIP: Club member form

This commit is contained in:
Skia 2016-02-04 08:59:03 +01:00
parent d51ab088d8
commit ed080b76a2
3 changed files with 37 additions and 5 deletions

View File

@ -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})

View File

@ -1,11 +1,16 @@
{% extends "core/base.jinja" %}
{% block content %}
<h2>Edit club</h2>
<h2>Club members</h2>
<ul>
{% for m in club.members.all() %}
<li>{{ m }}</li>
{% endfor %}
</ul>
<form action="{{ url('club:club_members', club_id=club.id) }}" method="post">
{% csrf_token %}
{{ form.as_p() }}
<p><input type="submit" value="Save!" /></p>
<p><input type="submit" value="Add" /></p>
</form>
{% endblock %}

View File

@ -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"