add member remove button

This commit is contained in:
Thomas Steen Rasmussen 2017-05-23 21:21:47 +02:00
parent 167ec6cd90
commit 916dba52c1
7 changed files with 119 additions and 10 deletions

View file

@ -285,6 +285,11 @@ urlpatterns = [
TeamListView.as_view(),
name='team_list'
),
url(
r'^members/(?P<pk>[0-9]+)/remove/$',
TeamMemberRemoveView.as_view(),
name='teammember_remove',
),
url(
r'(?P<slug>[-_\w+]+)/join/$',
TeamJoinView.as_view(),

View file

@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-05-23 18:46
from __future__ import unicode_literals
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('teams', '0012_teammember_responsible'),
]
operations = [
migrations.AddField(
model_name='teammember',
name='created',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='teammember',
name='deleted',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='teammember',
name='updated',
field=models.DateTimeField(auto_now=True),
),
]

View file

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-05-23 19:13
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('teams', '0013_auto_20170523_2046'),
]
operations = [
migrations.RemoveField(
model_name='teammember',
name='deleted',
),
]

View file

@ -83,7 +83,7 @@ class Team(CampRelatedModel):
return self.area.responsible.all()
class TeamMember(models.Model):
class TeamMember(CampRelatedModel):
user = models.ForeignKey('auth.User')
team = models.ForeignKey('teams.Team')
approved = models.BooleanField(default=False)
@ -94,6 +94,10 @@ class TeamMember(models.Model):
self.user, '' if self.approved else 'an unapproved', self.team
)
@property
def camp(self):
return self.team.camp
@receiver(post_save, sender=TeamMember)
def send_responsible_email(sender, instance, created, **kwargs):

View file

@ -35,6 +35,8 @@ Team: {{ team.name }} | {{ block.super }}
{% endfor %}
</ul>
<hr>
{% if request.user in team.responsible %}
<h3>Manage {{ team.name }} Team</h3>
@ -60,30 +62,32 @@ Team: {{ team.name }} | {{ block.super }}
Profile
<th >
Name
<th>
Status
<th>
Email
<th>
Description
<th>
Membership Status
<th>
Action
<tbody>
{% for member in team.teammember_set.all %}
{% for membership in team.teammember_set.all %}
<tr>
<td>
{{ member.user }}
{{ membership.user }}
<td>
{{ member.user.profile.name }}
{{ membership.user.profile.name }}
<td>
{{ member.approved }}
{{ membership.user.profile.email }}
<td>
{{ member.user.profile.email }}
{{ membership.user.profile.description }}
<td>
{{ member.user.profile.description }}
{% if membership.approved %}member{% else %}pending{% endif %}
<td>
TBD
{% if membership.approved %}
<a class="btn btn-danger" href="{% url 'teammember_remove' camp_slug=camp.slug pk=membership.id %}"><i class="fa fa-trash-o"></i> Remove</a>
{% endif %}
{% endfor %}
{% endif %}

View file

@ -0,0 +1,18 @@
{% extends 'base.html' %}
{% load commonmark %}
{% block title %}
Remove member {{ teammember.user.profile.name }} from team {{ teammember.team.name }}
{% endblock %}
{% block content %}
<h3>Remove member {{ teammember.user.profile.name }} from the {{ teammember.team.name }} team</h3>
<p class="lead">Really remove the user <b>{{ teammember.user.profile.name }}</b> from the {{ teammember.team.name }} team? The user will receive an email with a message.<p>
<form method="POST">
{% csrf_token %}
{{ form }}
<button class="btn btn-danger" type="submit"><i class="fa fa-trash-o"></i> Remove teammember</button>
<a href="{% url 'team_detail' camp_slug=teammember.team.camp.slug slug=teammember.team.slug %}" class="btn btn-default" type="submit"><i class="fa fa-remove"></i> Cancel</a>
</form>
{% endblock %}

View file

@ -7,6 +7,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import redirect
from django.contrib import messages
from django.http import Http404
from django.views.generic.detail import SingleObjectMixin
class TeamListView(CampViewMixin, ListView):
@ -60,3 +61,29 @@ class TeamLeaveView(LoginRequiredMixin, CampViewMixin, UpdateView):
TeamMember.objects.filter(team=self.get_object(), user=self.request.user).delete()
messages.success(self.request, "You are no longer a member of the team %s" % self.get_object().name)
return redirect('team_list', camp_slug=self.get_object().camp.slug)
class EnsureTeamResponsibleMixin(SingleObjectMixin):
model = TeamMember
def dispatch(self, request, *args, **kwargs):
if not request.user in self.get_object().team.responsible.all():
messages.error(request, 'No thanks')
return HttpResponseRedirect(reverse_lazy('team_detail', slug=self.get_object().team.slug))
return super().dispatch(
request, *args, **kwargs
)
class TeamMemberRemoveView(LoginRequiredMixin, EnsureTeamResponsibleMixin, UpdateView):
template_name = "teammember_remove.html"
model = TeamMember
fields = []
def form_valid(self, form):
form.instance.delete()
messages.success(self.request, "Team member removed")
return redirect('team_detail', camp_slug=form.instance.team.camp.slug, slug=form.instance.team.slug)