Some DRYing.

This commit is contained in:
Víðir Valberg Guðmundsson 2018-07-23 23:12:57 +02:00
parent c68015fe26
commit 02977acd5e
6 changed files with 36 additions and 62 deletions

View file

@ -33,8 +33,6 @@ Teams | {{ block.super }}
{% for team in teams %}
<tr>
<td>
{{ camp.slug}}
{{ team.slug}}
<a href="{% url 'teams:general' camp_slug=camp.slug team_slug=team.slug %}">
{{ team.name }} Team
</a>

View file

@ -43,12 +43,12 @@
<div class="btn-group-vertical">
<a class="btn btn-danger"
href="{% url 'teams:teammember_remove' camp_slug=camp.slug team_slug=team.slug pk=teammember.id %}">
<i class="fas fa-trash-o"></i> Remove Member
<i class="fas fa-trash"></i> Remove
</a>
{% if not teammember.approved %}
<a class="btn btn-success"
href="{% url 'teams:teammember_approve' camp_slug=camp.slug team_slug=team.slug pk=teammember.id %}">
<i class="fas fa-check"></i> Approve Member
<i class="fas fa-check"></i> Approve
</a>
{% endif %}
</div>

View file

@ -5,20 +5,15 @@ from reversion.views import RevisionMixin
from camps.mixins import CampViewMixin
from info.models import InfoItem, InfoCategory
from teams.views.mixins import EnsureTeamResponsibleMixin
from .mixins import EnsureTeamResponsibleMixin, TeamViewMixin
class InfoItemCreateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibleMixin, CreateView):
class InfoItemCreateView(LoginRequiredMixin, CampViewMixin, TeamViewMixin, EnsureTeamResponsibleMixin, CreateView):
model = InfoItem
template_name = "info_item_form.html"
fields = ['headline', 'body', 'anchor', 'weight']
slug_field = 'anchor'
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(**kwargs)
context['team'] = self.team
return context
def form_valid(self, form):
info_item = form.save(commit=False)
category = InfoCategory.objects.get(camp=self.camp, anchor=self.kwargs.get('category_anchor'))
@ -30,18 +25,13 @@ class InfoItemCreateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibl
return self.team.get_absolute_url()
class InfoItemUpdateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibleMixin, RevisionMixin, UpdateView):
class InfoItemUpdateView(LoginRequiredMixin, CampViewMixin, TeamViewMixin, EnsureTeamResponsibleMixin, RevisionMixin, UpdateView):
model = InfoItem
template_name = "info_item_form.html"
fields = ['headline', 'body', 'anchor', 'weight']
slug_field = 'anchor'
slug_url_kwarg = 'item_anchor'
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(**kwargs)
context['team'] = self.team
return context
def get_success_url(self):
next = self.request.GET.get('next')
if next:
@ -49,7 +39,7 @@ class InfoItemUpdateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibl
return self.team.get_absolute_url()
class InfoItemDeleteView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibleMixin, RevisionMixin, DeleteView):
class InfoItemDeleteView(LoginRequiredMixin, CampViewMixin, TeamViewMixin, EnsureTeamResponsibleMixin, RevisionMixin, DeleteView):
model = InfoItem
template_name = "info_item_delete_confirm.html"
slug_field = 'anchor'

View file

@ -9,7 +9,7 @@ from ..models import Team, TeamMember
from profiles.models import Profile
from camps.mixins import CampViewMixin
from .mixins import EnsureTeamMemberResponsibleMixin
from .mixins import EnsureTeamMemberResponsibleMixin, TeamViewMixin
from ..email import add_added_membership_email, add_removed_membership_email
logger = logging.getLogger("bornhack.%s" % __name__)
@ -71,8 +71,7 @@ class TeamLeaveView(LoginRequiredMixin, CampViewMixin, UpdateView):
return redirect('teams:list', camp_slug=self.get_object().camp.slug)
class TeamMemberRemoveView(LoginRequiredMixin, CampViewMixin, EnsureTeamMemberResponsibleMixin, UpdateView):
class TeamMemberRemoveView(LoginRequiredMixin, CampViewMixin, TeamViewMixin, EnsureTeamMemberResponsibleMixin, UpdateView):
template_name = "teammember_remove.html"
model = TeamMember
fields = []
@ -88,13 +87,8 @@ class TeamMemberRemoveView(LoginRequiredMixin, CampViewMixin, EnsureTeamMemberRe
)
return redirect('teams:general', camp_slug=self.camp.slug, team_slug=form.instance.team.slug)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['team'] = self.get_object().team
return context
class TeamMemberApproveView(LoginRequiredMixin, CampViewMixin, EnsureTeamMemberResponsibleMixin, UpdateView):
class TeamMemberApproveView(LoginRequiredMixin, CampViewMixin, TeamViewMixin, EnsureTeamMemberResponsibleMixin, UpdateView):
template_name = "teammember_approve.html"
model = TeamMember
fields = []
@ -110,8 +104,3 @@ class TeamMemberApproveView(LoginRequiredMixin, CampViewMixin, EnsureTeamMemberR
'Unable to add approved email to outgoing queue for teammember: {}'.format(form.instance)
)
return redirect('teams:general', camp_slug=self.camp.slug, team_slug=form.instance.team.slug)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['team'] = self.get_object().team
return context

View file

@ -33,4 +33,11 @@ class EnsureTeamMemberResponsibleMixin(SingleObjectMixin):
return super().dispatch(
request, *args, **kwargs
)
)
class TeamViewMixin:
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(**kwargs)
context['team'] = self.get_object().team
return context

View file

@ -4,7 +4,7 @@ from django.views.generic import DetailView, CreateView, UpdateView
from camps.mixins import CampViewMixin
from ..models import Team, TeamTask
from .mixins import EnsureTeamResponsibleMixin
from .mixins import EnsureTeamResponsibleMixin, TeamViewMixin
class TeamTasksView(CampViewMixin, DetailView):
@ -14,40 +14,30 @@ class TeamTasksView(CampViewMixin, DetailView):
slug_url_kwarg = 'team_slug'
class TaskDetailView(CampViewMixin, DetailView):
class TaskDetailView(CampViewMixin, TeamViewMixin, DetailView):
template_name = "task_detail.html"
context_object_name = "task"
model = TeamTask
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['team'] = self.object.team
return context
class TaskCreateView(LoginRequiredMixin, CampViewMixin, TeamViewMixin, EnsureTeamResponsibleMixin, CreateView):
model = TeamTask
template_name = "task_form.html"
fields = ['name', 'description']
def form_valid(self, form):
task = form.save(commit=False)
task.team = self.team
if not task.name:
task.name = "noname"
task.save()
return HttpResponseRedirect(task.get_absolute_url())
def get_success_url(self):
return self.get_object().get_absolute_url()
class TaskCreateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibleMixin, CreateView):
model = TeamTask
template_name = "task_form.html"
fields = ['name', 'description']
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(**kwargs)
context['team'] = self.team
return context
def form_valid(self, form):
task = form.save(commit=False)
task.team = self.team
if not task.name:
task.name = "noname"
task.save()
return HttpResponseRedirect(task.get_absolute_url())
def get_success_url(self):
return self.get_object().get_absolute_url()
class TaskUpdateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibleMixin, UpdateView):
class TaskUpdateView(LoginRequiredMixin, CampViewMixin, TeamViewMixin, EnsureTeamResponsibleMixin, UpdateView):
model = TeamTask
template_name = "task_form.html"
fields = ['name', 'description']