Projet

Général

Profil

0003-pricing-simplify-mixins-and-permission-checks-65442.patch

Lauréline Guérin, 20 mai 2022 10:00

Télécharger (6,5 ko)

Voir les différences:

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(-)
lingo/agendas/models.py
67 67

  
68 68
        return agenda, False
69 69

  
70
    def can_be_managed(self, user):
71
        return True
72

  
73
    def can_be_viewed(self, user):
74
        return True
75

  
76 70

  
77 71
class CheckTypeGroup(models.Model):
78 72
    slug = models.SlugField(_('Identifier'), max_length=160, unique=True)
lingo/agendas/views.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17
from django.core.exceptions import PermissionDenied
18 17
from django.shortcuts import get_object_or_404
19 18

  
20 19
from .models import Agenda
21 20

  
22 21

  
23
class ViewableAgendaMixin:
22
class AgendaMixin:
24 23
    agenda = None
25 24

  
26 25
    def set_agenda(self, **kwargs):
......
28 27

  
29 28
    def dispatch(self, request, *args, **kwargs):
30 29
        self.set_agenda(**kwargs)
31
        if not self.check_permissions(request.user):
32
            raise PermissionDenied()
33 30
        return super().dispatch(request, *args, **kwargs)
34 31

  
35
    def check_permissions(self, user):
36
        return True  # return self.agenda.can_be_viewed(user)
37

  
38 32
    def get_context_data(self, **kwargs):
39 33
        context = super().get_context_data(**kwargs)
40 34
        context['agenda'] = self.agenda
41 35
        return context
42 36

  
43

  
44
class ManagedAgendaMixin(ViewableAgendaMixin):
45
    def check_permissions(self, user):
46
        return True  # XXX: return self.agenda.can_be_managed(user)
47

  
48
    def get_form_kwargs(self):
49
        kwargs = super().get_form_kwargs()
50
        if not kwargs.get('instance'):
51
            kwargs['instance'] = self.model()
52
        kwargs['instance'].agenda = self.agenda
53
        return kwargs
54

  
55 37
    def get_success_url(self):
56 38
        # XXX:  return reverse('lingo-manager-agenda-settings', kwargs={'pk': self.agenda.id})
57 39
        return '/'
lingo/pricing/templates/lingo/pricing/manager_agenda_pricing_detail.html
10 10
<h2>
11 11
    {{ object.pricing }} ({{ object.date_start|date:'d/m/Y' }} - {{ object.date_end|date:'d/m/Y' }})
12 12
</h2>
13
{% if user_can_manage %}
14 13
<span class="actions">
15 14
  <a class="extra-actions-menu-opener"></a>
16 15
  <ul class="extra-actions-menu">
......
18 17
    <li><a rel="popup" href="{% url 'lingo-manager-agenda-pricing-delete' agenda.pk object.pk %}">{% trans 'Delete' %}</a></li>
19 18
  </ul>
20 19
</span>
21
{% endif %}
22 20
{% endblock %}
23 21

  
24 22
{% block content %}
......
44 42
      {% endfor %}
45 43
    </tbody>
46 44
  </table>
47
  {% if user_can_manage %}
48 45
  <p>
49 46
  <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>
50 47
  </p>
51
  {% endif %}
52 48
</div>
53 49
</div>
54 50
{% empty %}
lingo/pricing/views.py
33 33
from django.views.generic.detail import SingleObjectMixin
34 34

  
35 35
from lingo.agendas.models import Agenda
36
from lingo.agendas.views import ManagedAgendaMixin, ViewableAgendaMixin
36
from lingo.agendas.views import AgendaMixin
37 37
from lingo.pricing.forms import (
38 38
    AgendaPricingForm,
39 39
    CriteriaForm,
......
661 661
criteria_delete = CriteriaDeleteView.as_view()
662 662

  
663 663

  
664
class AgendaPricingAddView(ManagedAgendaMixin, CreateView):
664
class AgendaPricingAddView(AgendaMixin, CreateView):
665 665
    template_name = 'lingo/pricing/manager_agenda_pricing_form.html'
666 666
    model = AgendaPricing
667 667
    form_class = AgendaPricingForm
......
673 673
agenda_pricing_add = AgendaPricingAddView.as_view()
674 674

  
675 675

  
676
class AgendaPricingDetailView(ViewableAgendaMixin, DetailView):
676
class AgendaPricingDetailView(AgendaMixin, DetailView):
677 677
    model = AgendaPricing
678 678
    pk_url_kwarg = 'pricing_pk'
679 679
    template_name = 'lingo/pricing/manager_agenda_pricing_detail.html'
......
686 686
            'pricing__criterias__category'
687 687
        )
688 688

  
689
    def get_context_data(self, **kwargs):
690
        kwargs['user_can_manage'] = self.agenda.can_be_managed(self.request.user)
691
        return super().get_context_data(**kwargs)
692

  
693 689

  
694 690
agenda_pricing_detail = AgendaPricingDetailView.as_view()
695 691

  
696 692

  
697
class AgendaPricingEditView(ManagedAgendaMixin, UpdateView):
693
class AgendaPricingEditView(AgendaMixin, UpdateView):
698 694
    template_name = 'lingo/pricing/manager_agenda_pricing_form.html'
699 695
    model = AgendaPricing
700 696
    pk_url_kwarg = 'pricing_pk'
......
713 709
agenda_pricing_edit = AgendaPricingEditView.as_view()
714 710

  
715 711

  
716
class AgendaPricingDeleteView(ManagedAgendaMixin, DeleteView):
712
class AgendaPricingDeleteView(AgendaMixin, DeleteView):
717 713
    template_name = 'lingo/manager_confirm_delete.html'
718 714
    model = AgendaPricing
719 715
    pk_url_kwarg = 'pricing_pk'
......
728 724
agenda_pricing_delete = AgendaPricingDeleteView.as_view()
729 725

  
730 726

  
731
class AgendaPricingMatrixEdit(ManagedAgendaMixin, FormView):
727
class AgendaPricingMatrixEdit(AgendaMixin, FormView):
732 728
    template_name = 'lingo/pricing/manager_agenda_pricing_matrix_form.html'
733 729

  
734 730
    def set_agenda(self, **kwargs):
735
-