From 02977acd5e0b6dc510d959690b11ef60bddd9f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Mon, 23 Jul 2018 23:12:57 +0200 Subject: [PATCH] Some DRYing. --- src/teams/templates/team_list.html | 2 -- src/teams/templates/team_members.html | 4 +-- src/teams/views/info.py | 18 +++------- src/teams/views/members.py | 17 ++-------- src/teams/views/mixins.py | 9 ++++- src/teams/views/tasks.py | 48 +++++++++++---------------- 6 files changed, 36 insertions(+), 62 deletions(-) diff --git a/src/teams/templates/team_list.html b/src/teams/templates/team_list.html index c8a16f5e..c0a314da 100644 --- a/src/teams/templates/team_list.html +++ b/src/teams/templates/team_list.html @@ -33,8 +33,6 @@ Teams | {{ block.super }} {% for team in teams %} - {{ camp.slug}} - {{ team.slug}} {{ team.name }} Team diff --git a/src/teams/templates/team_members.html b/src/teams/templates/team_members.html index 75024b49..49a1e25a 100644 --- a/src/teams/templates/team_members.html +++ b/src/teams/templates/team_members.html @@ -43,12 +43,12 @@
- Remove Member + Remove {% if not teammember.approved %} - Approve Member + Approve {% endif %}
diff --git a/src/teams/views/info.py b/src/teams/views/info.py index 5a6efaaa..f2da0bcb 100644 --- a/src/teams/views/info.py +++ b/src/teams/views/info.py @@ -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' diff --git a/src/teams/views/members.py b/src/teams/views/members.py index 35bdc78a..cbd03e00 100644 --- a/src/teams/views/members.py +++ b/src/teams/views/members.py @@ -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 diff --git a/src/teams/views/mixins.py b/src/teams/views/mixins.py index 87d8d5e0..2b661827 100644 --- a/src/teams/views/mixins.py +++ b/src/teams/views/mixins.py @@ -33,4 +33,11 @@ class EnsureTeamMemberResponsibleMixin(SingleObjectMixin): return super().dispatch( request, *args, **kwargs - ) \ No newline at end of file + ) + + +class TeamViewMixin: + def get_context_data(self, *args, **kwargs): + context = super().get_context_data(**kwargs) + context['team'] = self.get_object().team + return context diff --git a/src/teams/views/tasks.py b/src/teams/views/tasks.py index 8d19f265..27f0f574 100644 --- a/src/teams/views/tasks.py +++ b/src/teams/views/tasks.py @@ -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']