From ea8e25a7b3f61c223f902655033083085e0ecc19 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sun, 12 Apr 2020 02:43:22 +0200 Subject: [PATCH] Create WEI --- apps/note/signals.py | 7 +++---- apps/wei/urls.py | 3 ++- apps/wei/views.py | 26 +++++++++++++++++++++++--- templates/wei/weiclub_list.html | 2 +- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/apps/note/signals.py b/apps/note/signals.py index 37737a45..115ddab9 100644 --- a/apps/note/signals.py +++ b/apps/note/signals.py @@ -17,7 +17,7 @@ def save_user_note(instance, raw, **_kwargs): instance.note.save() -def save_club_note(instance, created, raw, **_kwargs): +def save_club_note(instance, raw, **_kwargs): """ Hook to create and save a note when a club is updated """ @@ -25,7 +25,6 @@ def save_club_note(instance, created, raw, **_kwargs): # When provisionning data, do not try to autocreate return - if created: - from .models import NoteClub - NoteClub.objects.create(club=instance) + from .models import NoteClub + NoteClub.objects.get_or_create(club=instance) instance.note.save() diff --git a/apps/wei/urls.py b/apps/wei/urls.py index f8981ed8..bce87e1f 100644 --- a/apps/wei/urls.py +++ b/apps/wei/urls.py @@ -3,12 +3,13 @@ from django.urls import path -from .views import WEIListView, WEIDetailView, WEIUpdateView +from .views import WEIListView, WEICreateView, WEIDetailView, WEIUpdateView app_name = 'wei' urlpatterns = [ path('list/', WEIListView.as_view(), name="wei_list"), + path('create/', WEICreateView.as_view(), name="wei_create"), path('detail//', WEIDetailView.as_view(), name="wei_detail"), path('update//', WEIUpdateView.as_view(), name="wei_update"), ] diff --git a/apps/wei/views.py b/apps/wei/views.py index 0ead8b94..59059d4c 100644 --- a/apps/wei/views.py +++ b/apps/wei/views.py @@ -7,11 +7,11 @@ from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.models import User from django.db.models import Q from django.urls import reverse_lazy -from django.views.generic import DetailView, UpdateView +from django.views.generic import DetailView, UpdateView, CreateView from django_tables2 import SingleTableView -from member.models import Membership +from member.models import Membership, Club from member.tables import MembershipTable -from note.models import Transaction +from note.models import Transaction, NoteClub from note.tables import HistoryTable from permission.backends import PermissionBackend from permission.views import ProtectQuerysetMixin @@ -29,6 +29,26 @@ class WEIListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): table_class = WEITable +class WEICreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): + """ + Create WEI + """ + model = WEIClub + form_class = WEIForm + + def form_valid(self, form): + form.instance.requires_membership = True + form.instance.parent_club = Club.objects.get(name="Kfet") + ret = super().form_valid(form) + NoteClub.objects.create(club=form.instance) + return ret + + def get_success_url(self): + self.object.refresh_from_db() + return reverse_lazy("wei:wei_detail", kwargs={"pk": self.object.pk}) + + + class WEIDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): """ View WEI information diff --git a/templates/wei/weiclub_list.html b/templates/wei/weiclub_list.html index 89591beb..fd8e0dd9 100644 --- a/templates/wei/weiclub_list.html +++ b/templates/wei/weiclub_list.html @@ -9,7 +9,7 @@
- {% trans "Create WEI" %} + {% trans "Create WEI" %}