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 %} {% for team in teams %}
<tr> <tr>
<td> <td>
{{ camp.slug}}
{{ team.slug}}
<a href="{% url 'teams:general' camp_slug=camp.slug team_slug=team.slug %}"> <a href="{% url 'teams:general' camp_slug=camp.slug team_slug=team.slug %}">
{{ team.name }} Team {{ team.name }} Team
</a> </a>

View file

@ -43,12 +43,12 @@
<div class="btn-group-vertical"> <div class="btn-group-vertical">
<a class="btn btn-danger" <a class="btn btn-danger"
href="{% url 'teams:teammember_remove' camp_slug=camp.slug team_slug=team.slug pk=teammember.id %}"> 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> </a>
{% if not teammember.approved %} {% if not teammember.approved %}
<a class="btn btn-success" <a class="btn btn-success"
href="{% url 'teams:teammember_approve' camp_slug=camp.slug team_slug=team.slug pk=teammember.id %}"> 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> </a>
{% endif %} {% endif %}
</div> </div>

View file

@ -5,20 +5,15 @@ from reversion.views import RevisionMixin
from camps.mixins import CampViewMixin from camps.mixins import CampViewMixin
from info.models import InfoItem, InfoCategory 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 model = InfoItem
template_name = "info_item_form.html" template_name = "info_item_form.html"
fields = ['headline', 'body', 'anchor', 'weight'] fields = ['headline', 'body', 'anchor', 'weight']
slug_field = 'anchor' 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): def form_valid(self, form):
info_item = form.save(commit=False) info_item = form.save(commit=False)
category = InfoCategory.objects.get(camp=self.camp, anchor=self.kwargs.get('category_anchor')) 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() return self.team.get_absolute_url()
class InfoItemUpdateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibleMixin, RevisionMixin, UpdateView): class InfoItemUpdateView(LoginRequiredMixin, CampViewMixin, TeamViewMixin, EnsureTeamResponsibleMixin, RevisionMixin, UpdateView):
model = InfoItem model = InfoItem
template_name = "info_item_form.html" template_name = "info_item_form.html"
fields = ['headline', 'body', 'anchor', 'weight'] fields = ['headline', 'body', 'anchor', 'weight']
slug_field = 'anchor' slug_field = 'anchor'
slug_url_kwarg = 'item_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): def get_success_url(self):
next = self.request.GET.get('next') next = self.request.GET.get('next')
if next: if next:
@ -49,7 +39,7 @@ class InfoItemUpdateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibl
return self.team.get_absolute_url() return self.team.get_absolute_url()
class InfoItemDeleteView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibleMixin, RevisionMixin, DeleteView): class InfoItemDeleteView(LoginRequiredMixin, CampViewMixin, TeamViewMixin, EnsureTeamResponsibleMixin, RevisionMixin, DeleteView):
model = InfoItem model = InfoItem
template_name = "info_item_delete_confirm.html" template_name = "info_item_delete_confirm.html"
slug_field = 'anchor' slug_field = 'anchor'

View file

@ -9,7 +9,7 @@ from ..models import Team, TeamMember
from profiles.models import Profile from profiles.models import Profile
from camps.mixins import CampViewMixin 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 from ..email import add_added_membership_email, add_removed_membership_email
logger = logging.getLogger("bornhack.%s" % __name__) 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) return redirect('teams:list', camp_slug=self.get_object().camp.slug)
class TeamMemberRemoveView(LoginRequiredMixin, CampViewMixin, TeamViewMixin, EnsureTeamMemberResponsibleMixin, UpdateView):
class TeamMemberRemoveView(LoginRequiredMixin, CampViewMixin, EnsureTeamMemberResponsibleMixin, UpdateView):
template_name = "teammember_remove.html" template_name = "teammember_remove.html"
model = TeamMember model = TeamMember
fields = [] 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) 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, TeamViewMixin, EnsureTeamMemberResponsibleMixin, UpdateView):
class TeamMemberApproveView(LoginRequiredMixin, CampViewMixin, EnsureTeamMemberResponsibleMixin, UpdateView):
template_name = "teammember_approve.html" template_name = "teammember_approve.html"
model = TeamMember model = TeamMember
fields = [] fields = []
@ -110,8 +104,3 @@ class TeamMemberApproveView(LoginRequiredMixin, CampViewMixin, EnsureTeamMemberR
'Unable to add approved email to outgoing queue for teammember: {}'.format(form.instance) '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) 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

@ -34,3 +34,10 @@ class EnsureTeamMemberResponsibleMixin(SingleObjectMixin):
return super().dispatch( return super().dispatch(
request, *args, **kwargs 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 camps.mixins import CampViewMixin
from ..models import Team, TeamTask from ..models import Team, TeamTask
from .mixins import EnsureTeamResponsibleMixin from .mixins import EnsureTeamResponsibleMixin, TeamViewMixin
class TeamTasksView(CampViewMixin, DetailView): class TeamTasksView(CampViewMixin, DetailView):
@ -14,40 +14,30 @@ class TeamTasksView(CampViewMixin, DetailView):
slug_url_kwarg = 'team_slug' slug_url_kwarg = 'team_slug'
class TaskDetailView(CampViewMixin, DetailView): class TaskDetailView(CampViewMixin, TeamViewMixin, DetailView):
template_name = "task_detail.html" template_name = "task_detail.html"
context_object_name = "task" context_object_name = "task"
model = TeamTask model = TeamTask
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) class TaskCreateView(LoginRequiredMixin, CampViewMixin, TeamViewMixin, EnsureTeamResponsibleMixin, CreateView):
context['team'] = self.object.team model = TeamTask
return context 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): class TaskUpdateView(LoginRequiredMixin, CampViewMixin, TeamViewMixin, EnsureTeamResponsibleMixin, UpdateView):
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):
model = TeamTask model = TeamTask
template_name = "task_form.html" template_name = "task_form.html"
fields = ['name', 'description'] fields = ['name', 'description']