From 91d3e9e4dc48e8c66e0fc2c7008ac7d0f58a7760 Mon Sep 17 00:00:00 2001 From: gnikwo Date: Tue, 5 Dec 2017 15:24:46 +0100 Subject: [PATCH] Delete function + club tabs --- club/urls.py | 1 + club/views.py | 22 +++++++-- com/models.py | 2 +- com/templates/com/poster_edit.jinja | 6 ++- com/views.py | 77 +++++++++++++++++------------ core/models.py | 7 ++- core/templates/core/base.jinja | 1 + 7 files changed, 77 insertions(+), 39 deletions(-) diff --git a/club/urls.py b/club/urls.py index 153b17a7..4d41e120 100644 --- a/club/urls.py +++ b/club/urls.py @@ -53,4 +53,5 @@ urlpatterns = [ url(r'^(?P[0-9]+)/poster$', PosterListView.as_view(), name='poster_list'), url(r'^(?P[0-9]+)/poster/create$', PosterCreateView.as_view(), name='poster_create'), url(r'^(?P[0-9]+)/poster/(?P[0-9]+)/edit$', PosterEditView.as_view(), name='poster_edit'), + url(r'^(?P[0-9]+)/poster/(?P[0-9]+)/delete$', PosterDeleteView.as_view(), name='poster_delete'), ] diff --git a/club/views.py b/club/views.py index db4b346d..ced6e169 100644 --- a/club/views.py +++ b/club/views.py @@ -43,7 +43,7 @@ from club.models import Club, Membership, Mailing, MailingSubscription from sith.settings import SITH_MAXIMUM_FREE_ROLE from counter.models import Selling, Counter from core.models import User, PageRev -from com.views import PosterListBaseView, PosterCreateBaseView, PosterEditBaseView +from com.views import PosterListBaseView, PosterCreateBaseView, PosterEditBaseView, PosterDeleteBaseView from com.models import Poster from django.conf import settings @@ -90,6 +90,8 @@ class ClubTabsMixin(TabedViewMixin): def get_tabs_title(self): if isinstance(self.object, PageRev): self.object = self.object.page.club + if isinstance(self.object, Poster): + self.object = self.club return self.object.get_display_name() def get_list_of_tabs(self): @@ -601,28 +603,31 @@ class MailingAutoCleanView(View): return redirect('club:mailing', club_id=self.mailing.club.id) -class PosterListView(CanViewMixin, PosterListBaseView): +class PosterListView(PosterListBaseView, CanViewMixin, ClubTabsMixin): """List communication posters""" + def dispatch(self, request, *args, **kwargs): + return super(PosterListView, self).dispatch(request, *args, **kwargs) + def get_context_data(self, **kwargs): kwargs = super(PosterListView, self).get_context_data(**kwargs) kwargs['app'] = "club" return kwargs -class PosterCreateView(CanCreateMixin, PosterCreateBaseView): +class PosterCreateView(PosterCreateBaseView, CanCreateMixin): """Create communication poster""" def get_success_url(self, **kwargs): return reverse_lazy('club:poster_list', kwargs={'club_id': self.club.id}); - def get_context_data(self, **kwargs): kwargs = super(PosterCreateView, self).get_context_data(**kwargs) kwargs['app'] = "club" return kwargs -class PosterEditView(CanEditMixin, PosterEditBaseView): + +class PosterEditView(ClubTabsMixin, PosterEditBaseView, CanEditMixin): """Edit communication poster""" def get_success_url(self): @@ -633,3 +638,10 @@ class PosterEditView(CanEditMixin, PosterEditBaseView): kwargs['app'] = "club" return kwargs + +class PosterDeleteView(PosterDeleteBaseView, ClubTabsMixin, CanEditMixin): + """Delete communication poster""" + + def get_success_url(self): + return reverse_lazy('club:poster_list', kwargs={'club_id': self.club.id}); + diff --git a/com/models.py b/com/models.py index fba0932e..1488fbfc 100644 --- a/com/models.py +++ b/com/models.py @@ -219,7 +219,7 @@ class Poster(models.Model): return super(Poster, self).save(*args, **kwargs) def is_owned_by(self, user): - return user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID) or Club.objects.filter(id__in=user.get_clubs_with_rights()) + return user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID) or Club.objects.filter(id__in=user.clubs_with_rights) def can_be_moderated_by(self, user): return user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID) diff --git a/com/templates/com/poster_edit.jinja b/com/templates/com/poster_edit.jinja index 418b0752..5e7f15cb 100644 --- a/com/templates/com/poster_edit.jinja +++ b/com/templates/com/poster_edit.jinja @@ -17,7 +17,11 @@

{% trans %}Posters - edit{% endtrans %}

diff --git a/com/views.py b/com/views.py index b66a60b5..e9d4481d 100644 --- a/com/views.py +++ b/com/views.py @@ -62,13 +62,11 @@ class PosterForm(forms.ModelForm): } def __init__(self, *args, **kwargs): - user = kwargs.pop('user', None) + self.user = kwargs.pop('user', None) super(PosterForm, self).__init__(*args, **kwargs) - if user: - if user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID): - self.fields['club'].queryset = Club.objects.all() - else: - self.fields['club'].queryset = Club.objects.filter(id__in=user.get_clubs_with_rights()).all() + if self.user: + if not self.user.is_com_admin: + self.fields['club'].queryset = Club.objects.filter(id__in=self.user.clubs_with_rights) class ComTabsMixin(TabedViewMixin): @@ -511,23 +509,21 @@ class PosterListBaseView(ListView): template_name = 'com/poster_list.jinja' def dispatch(self, request, *args, **kwargs): - self.club = None - if 'club_id' in kwargs and kwargs['club_id']: - try: - self.club = Club.objects.get(pk=kwargs['club_id']) - except Club.DoesNotExist: - pass + club_id = kwargs.pop('club_id', None) + if club_id: + self.club = get_object_or_404(Club, pk=club_id) return super(PosterListBaseView, self).dispatch(request, *args, **kwargs) def get_queryset(self): - if 'club' in self.__dict__ and self.club: - return Poster.objects.filter(club=self.club.id) - else: + if self.request.user.is_com_admin: return Poster.objects.all().order_by('-date_begin') + else: + return Poster.objects.filter(club=self.club.id) def get_context_data(self, **kwargs): kwargs = super(PosterListBaseView, self).get_context_data(**kwargs) - kwargs['club'] = self.club + if not self.request.user.is_com_admin: + kwargs['club'] = self.club return kwargs @@ -541,21 +537,26 @@ class PosterCreateBaseView(CreateView): return Poster.objects.all() def dispatch(self, request, *args, **kwargs): - self.club = None if 'club_id' in kwargs and kwargs['club_id']: try: self.club = Club.objects.get(pk=kwargs['club_id']) except Club.DoesNotExist: - pass + raise PermissionDenied return super(PosterCreateBaseView, self).dispatch(request, *args, **kwargs) + def get_form_kwargs(self): + kwargs = super(PosterCreateBaseView, self).get_form_kwargs() + kwargs.update({'user': self.request.user}) + return kwargs + def get_context_data(self, **kwargs): kwargs = super(PosterCreateBaseView, self).get_context_data(**kwargs) - kwargs['club'] = self.club + if not self.request.user.is_com_admin: + kwargs['club'] = self.club return kwargs def form_valid(self, form): - if not('club' in self.__dict__ and self.club): + if self.request.user.is_com_admin: form.instance.is_moderated = True return super(PosterCreateBaseView, self).form_valid(form) @@ -568,20 +569,25 @@ class PosterEditBaseView(UpdateView): template_name = 'com/poster_edit.jinja' def dispatch(self, request, *args, **kwargs): - self.club = None if 'club_id' in kwargs and kwargs['club_id']: try: self.club = Club.objects.get(pk=kwargs['club_id']) except Club.DoesNotExist: - pass + raise PermissionDenied return super(PosterEditBaseView, self).dispatch(request, *args, **kwargs) def get_queryset(self): return Poster.objects.all() + def get_form_kwargs(self): + kwargs = super(PosterEditBaseView, self).get_form_kwargs() + kwargs.update({'user': self.request.user}) + return kwargs + def get_context_data(self, **kwargs): kwargs = super(PosterEditBaseView, self).get_context_data(**kwargs) - kwargs['club'] = self.club + if not self.request.user.is_com_admin: + kwargs['club'] = self.club return kwargs def form_valid(self, form): @@ -590,11 +596,24 @@ class PosterEditBaseView(UpdateView): return super(PosterEditBaseView, self).form_valid(form) -class PosterListView(IsComAdminMixin, ComTabsMixin, PosterListBaseView): - """List communication posters""" +class PosterDeleteBaseView(DeleteView): + """Edit communication poster""" + pk_url_kwarg = "poster_id" current_tab = "posters" model = Poster - template_name = 'com/poster_list.jinja' + template_name = 'core/delete_confirm.jinja' + + def dispatch(self, request, *args, **kwargs): + if 'club_id' in kwargs and kwargs['club_id']: + try: + self.club = Club.objects.get(pk=kwargs['club_id']) + except Club.DoesNotExist: + raise PermissionDenied + return super(PosterDeleteBaseView, self).dispatch(request, *args, **kwargs) + + +class PosterListView(IsComAdminMixin, ComTabsMixin, PosterListBaseView): + """List communication posters""" def get_context_data(self, **kwargs): kwargs = super(PosterListView, self).get_context_data(**kwargs) @@ -622,12 +641,8 @@ class PosterEditView(IsComAdminMixin, ComTabsMixin, PosterEditBaseView): return kwargs -class PosterDeleteView(IsComAdminMixin, ComTabsMixin, DeleteView): +class PosterDeleteView(IsComAdminMixin, ComTabsMixin, PosterDeleteBaseView): """Delete communication poster""" - pk_url_kwarg = "poster_id" - current_tab = "posters" - model = Poster - template_name = 'core/delete_confirm.jinja' success_url = reverse_lazy('com:poster_list') diff --git a/core/models.py b/core/models.py index 9a4b259b..4ba7016a 100644 --- a/core/models.py +++ b/core/models.py @@ -518,9 +518,14 @@ class User(AbstractBaseUser): infos.save() return infos - def get_clubs_with_rights(self): + @cached_property + def clubs_with_rights(self): return [m.club.id for m in self.memberships.filter(models.Q(end_date__isnull=True) | models.Q(end_date__gte=timezone.now())).all() if m.club.has_rights_in_club(self)] + @cached_property + def is_com_admin(self): + return self.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID) + class AnonymousUser(AuthAnonymousUser): def __init__(self, request): diff --git a/core/templates/core/base.jinja b/core/templates/core/base.jinja index 07dc9a63..eb0792bb 100644 --- a/core/templates/core/base.jinja +++ b/core/templates/core/base.jinja @@ -169,6 +169,7 @@
+ {{ object }} {% if list_of_tabs %}
{{ tabs_title }}