From d089984cda3e98355b6646c0e1782145f8662a1d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 20 May 2022 09:59:06 +0200
Subject: [PATCH 3/3] pricing: simplify mixins and permission checks (#65442)

---
 lingo/agendas/models.py                       |  6 ------
 lingo/agendas/views.py                        | 20 +------------------
 .../manager_agenda_pricing_detail.html        |  4 ----
 lingo/pricing/views.py                        | 16 ++++++---------
 4 files changed, 7 insertions(+), 39 deletions(-)

diff --git a/lingo/agendas/models.py b/lingo/agendas/models.py
index 166f2b2..3732f10 100644
--- a/lingo/agendas/models.py
+++ b/lingo/agendas/models.py
@@ -67,12 +67,6 @@ class Agenda(models.Model):
 
         return agenda, False
 
-    def can_be_managed(self, user):
-        return True
-
-    def can_be_viewed(self, user):
-        return True
-
 
 class CheckTypeGroup(models.Model):
     slug = models.SlugField(_('Identifier'), max_length=160, unique=True)
diff --git a/lingo/agendas/views.py b/lingo/agendas/views.py
index a74fd16..9fad020 100644
--- a/lingo/agendas/views.py
+++ b/lingo/agendas/views.py
@@ -14,13 +14,12 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from django.core.exceptions import PermissionDenied
 from django.shortcuts import get_object_or_404
 
 from .models import Agenda
 
 
-class ViewableAgendaMixin:
+class AgendaMixin:
     agenda = None
 
     def set_agenda(self, **kwargs):
@@ -28,30 +27,13 @@ class ViewableAgendaMixin:
 
     def dispatch(self, request, *args, **kwargs):
         self.set_agenda(**kwargs)
-        if not self.check_permissions(request.user):
-            raise PermissionDenied()
         return super().dispatch(request, *args, **kwargs)
 
-    def check_permissions(self, user):
-        return True  # return self.agenda.can_be_viewed(user)
-
     def get_context_data(self, **kwargs):
         context = super().get_context_data(**kwargs)
         context['agenda'] = self.agenda
         return context
 
-
-class ManagedAgendaMixin(ViewableAgendaMixin):
-    def check_permissions(self, user):
-        return True  # XXX: return self.agenda.can_be_managed(user)
-
-    def get_form_kwargs(self):
-        kwargs = super().get_form_kwargs()
-        if not kwargs.get('instance'):
-            kwargs['instance'] = self.model()
-        kwargs['instance'].agenda = self.agenda
-        return kwargs
-
     def get_success_url(self):
         # XXX:  return reverse('lingo-manager-agenda-settings', kwargs={'pk': self.agenda.id})
         return '/'
diff --git a/lingo/pricing/templates/lingo/pricing/manager_agenda_pricing_detail.html b/lingo/pricing/templates/lingo/pricing/manager_agenda_pricing_detail.html
index 6443f6a..6fab390 100644
--- a/lingo/pricing/templates/lingo/pricing/manager_agenda_pricing_detail.html
+++ b/lingo/pricing/templates/lingo/pricing/manager_agenda_pricing_detail.html
@@ -10,7 +10,6 @@
 <h2>
     {{ object.pricing }} ({{ object.date_start|date:'d/m/Y' }} - {{ object.date_end|date:'d/m/Y' }})
 </h2>
-{% if user_can_manage %}
 <span class="actions">
   <a class="extra-actions-menu-opener"></a>
   <ul class="extra-actions-menu">
@@ -18,7 +17,6 @@
     <li><a rel="popup" href="{% url 'lingo-manager-agenda-pricing-delete' agenda.pk object.pk %}">{% trans 'Delete' %}</a></li>
   </ul>
 </span>
-{% endif %}
 {% endblock %}
 
 {% block content %}
@@ -44,11 +42,9 @@
       {% endfor %}
     </tbody>
   </table>
-  {% if user_can_manage %}
   <p>
   <a class="pk-button" href="{% if matrix.criteria %}{% url 'lingo-manager-agenda-pricing-matrix-slug-edit' agenda.pk object.pk matrix.criteria.slug %}{% else %}{% url 'lingo-manager-agenda-pricing-matrix-edit' agenda.pk object.pk %}{% endif %}">{% trans "Edit pricing" %}</a>
   </p>
-  {% endif %}
 </div>
 </div>
 {% empty %}
diff --git a/lingo/pricing/views.py b/lingo/pricing/views.py
index 40c39d6..5f62ebe 100644
--- a/lingo/pricing/views.py
+++ b/lingo/pricing/views.py
@@ -33,7 +33,7 @@ from django.views.generic import CreateView, DeleteView, DetailView, FormView, L
 from django.views.generic.detail import SingleObjectMixin
 
 from lingo.agendas.models import Agenda
-from lingo.agendas.views import ManagedAgendaMixin, ViewableAgendaMixin
+from lingo.agendas.views import AgendaMixin
 from lingo.pricing.forms import (
     AgendaPricingForm,
     CriteriaForm,
@@ -661,7 +661,7 @@ class CriteriaDeleteView(DeleteView):
 criteria_delete = CriteriaDeleteView.as_view()
 
 
-class AgendaPricingAddView(ManagedAgendaMixin, CreateView):
+class AgendaPricingAddView(AgendaMixin, CreateView):
     template_name = 'lingo/pricing/manager_agenda_pricing_form.html'
     model = AgendaPricing
     form_class = AgendaPricingForm
@@ -673,7 +673,7 @@ class AgendaPricingAddView(ManagedAgendaMixin, CreateView):
 agenda_pricing_add = AgendaPricingAddView.as_view()
 
 
-class AgendaPricingDetailView(ViewableAgendaMixin, DetailView):
+class AgendaPricingDetailView(AgendaMixin, DetailView):
     model = AgendaPricing
     pk_url_kwarg = 'pricing_pk'
     template_name = 'lingo/pricing/manager_agenda_pricing_detail.html'
@@ -686,15 +686,11 @@ class AgendaPricingDetailView(ViewableAgendaMixin, DetailView):
             'pricing__criterias__category'
         )
 
-    def get_context_data(self, **kwargs):
-        kwargs['user_can_manage'] = self.agenda.can_be_managed(self.request.user)
-        return super().get_context_data(**kwargs)
-
 
 agenda_pricing_detail = AgendaPricingDetailView.as_view()
 
 
-class AgendaPricingEditView(ManagedAgendaMixin, UpdateView):
+class AgendaPricingEditView(AgendaMixin, UpdateView):
     template_name = 'lingo/pricing/manager_agenda_pricing_form.html'
     model = AgendaPricing
     pk_url_kwarg = 'pricing_pk'
@@ -713,7 +709,7 @@ class AgendaPricingEditView(ManagedAgendaMixin, UpdateView):
 agenda_pricing_edit = AgendaPricingEditView.as_view()
 
 
-class AgendaPricingDeleteView(ManagedAgendaMixin, DeleteView):
+class AgendaPricingDeleteView(AgendaMixin, DeleteView):
     template_name = 'lingo/manager_confirm_delete.html'
     model = AgendaPricing
     pk_url_kwarg = 'pricing_pk'
@@ -728,7 +724,7 @@ class AgendaPricingDeleteView(ManagedAgendaMixin, DeleteView):
 agenda_pricing_delete = AgendaPricingDeleteView.as_view()
 
 
-class AgendaPricingMatrixEdit(ManagedAgendaMixin, FormView):
+class AgendaPricingMatrixEdit(AgendaMixin, FormView):
     template_name = 'lingo/pricing/manager_agenda_pricing_matrix_form.html'
 
     def set_agenda(self, **kwargs):
-- 
2.35.1

