From 92e394cce9117469755ca79eddcd565b389720f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Sat, 5 May 2018 00:38:32 +0200 Subject: [PATCH] Add creation of info items. --- src/info/models.py | 2 +- src/teams/templates/info_item_form.html | 4 ++-- src/teams/templates/team_detail.html | 5 ++++- src/teams/urls.py | 16 ++++++++-------- src/teams/views/info.py | 18 +++++++++++++----- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/info/models.py b/src/info/models.py index 75bddf20..6e7f5676 100644 --- a/src/info/models.py +++ b/src/info/models.py @@ -92,7 +92,7 @@ class InfoItem(CampRelatedModel): return self.category.camp def clean(self): - if InfoCategory.objects.filter(camp=self.category.camp, anchor=self.anchor).exists(): + if hasattr(self, 'category') and InfoCategory.objects.filter(camp=self.category.camp, anchor=self.anchor).exists(): # this anchor is already in use on a category, so it cannot be used here (they must be unique on the entire page) raise ValidationError({'anchor': 'Anchor is already in use on an info category for this camp'}) diff --git a/src/teams/templates/info_item_form.html b/src/teams/templates/info_item_form.html index 465671db..3a15e8e8 100644 --- a/src/teams/templates/info_item_form.html +++ b/src/teams/templates/info_item_form.html @@ -8,7 +8,7 @@ Edit Info Item: {{ form.instance.headline }} {% else %} Create Info item {% endif %} -for in {{ form.instance.category.headline }} +in {{ form.instance.category.headline }} {% endblock %} {% block content %} @@ -20,7 +20,7 @@ for in {{ form.instance.category.headline }} {% else %} Create Info Item {% endif %} - for in {{ form.instance.category.headline }} + in {{ form.instance.category.headline }}
diff --git a/src/teams/templates/team_detail.html b/src/teams/templates/team_detail.html index 86a96040..86586f3a 100644 --- a/src/teams/templates/team_detail.html +++ b/src/teams/templates/team_detail.html @@ -137,7 +137,7 @@ Team: {{ team.name }} | {{ block.super }} {{ item.headline }} - Edit Task @@ -147,8 +147,11 @@ Team: {{ team.name }} | {{ block.super }} + Create Info Item + {% endfor %} + {% endif %}
diff --git a/src/teams/urls.py b/src/teams/urls.py index 555bc945..039aabb6 100644 --- a/src/teams/urls.py +++ b/src/teams/urls.py @@ -10,7 +10,7 @@ from teams.views.base import ( TeamManageView, FixIrcAclView, ) -from teams.views.info import InfoItemUpdateView +from teams.views.info import InfoItemUpdateView, InfoItemCreateView from teams.views.tasks import ( TaskCreateView, @@ -92,14 +92,14 @@ urlpatterns = [ ]), ), url( - r'^info_items/', include([ + r'^info_items/(?P[-_\w+]+)/', include([ url( - r'^(?P[-_\w+]+)/', include([ - # url( - # r'^$', - # TaskDetailView.as_view(), - # name='task_detail', - # ), + r'^create/$', + InfoItemCreateView.as_view(), + name='info_item_create', + ), + url( + r'^(?P[-_\w+]+)/', include([ url( r'^update/$', InfoItemUpdateView.as_view(), diff --git a/src/teams/views/info.py b/src/teams/views/info.py index 64c77916..b3dc5a8e 100644 --- a/src/teams/views/info.py +++ b/src/teams/views/info.py @@ -1,9 +1,10 @@ from django.contrib.auth.mixins import LoginRequiredMixin +from django.http import HttpResponseRedirect from django.views.generic import CreateView, UpdateView from reversion.views import RevisionMixin from camps.mixins import CampViewMixin -from info.models import InfoItem +from info.models import InfoItem, InfoCategory from teams.views.mixins import EnsureTeamResponsibleMixin @@ -15,11 +16,18 @@ class InfoItemCreateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibl def get_context_data(self, *args, **kwargs): context = super().get_context_data(**kwargs) - context['team'] = self.object.category.team + 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')) + info_item.category = category + info_item.save() + return HttpResponseRedirect(self.get_success_url()) + def get_success_url(self): - return self.object.category.team.get_absolute_url() + return self.team.get_absolute_url() class InfoItemUpdateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibleMixin, RevisionMixin, UpdateView): @@ -31,9 +39,9 @@ class InfoItemUpdateView(LoginRequiredMixin, CampViewMixin, EnsureTeamResponsibl def get_context_data(self, *args, **kwargs): context = super().get_context_data(**kwargs) - context['team'] = self.object.category.team + context['team'] = self.team return context def get_success_url(self): - return self.object.category.team.get_absolute_url() + return self.team.get_absolute_url()