mirror of
				https://github.com/ae-utbm/sith.git
				synced 2025-11-04 02:53:06 +00:00 
			
		
		
		
	Merge pull request #530 from ae-utbm/redirection_for_barmen
redirect the user directly on counter when barman
This commit is contained in:
		@@ -65,9 +65,15 @@
 | 
				
			|||||||
            <div id="header_bar">
 | 
					            <div id="header_bar">
 | 
				
			||||||
                <ul id="header_bars_infos">
 | 
					                <ul id="header_bars_infos">
 | 
				
			||||||
                {% cache 100 "counters_activity" %}
 | 
					                {% cache 100 "counters_activity" %}
 | 
				
			||||||
                  {% for bar in Counter.objects.filter(type="BAR").all() %}
 | 
					                    {% for bar in Counter.objects.annotate_has_barman(user).filter(type="BAR") %}
 | 
				
			||||||
                        <li>
 | 
					                        <li>
 | 
				
			||||||
                      <a href="{{ url('counter:activity', counter_id=bar.id) }}" style="padding: 0px">
 | 
					                            {# If the user is a barman, we redirect him directly to the barman page
 | 
				
			||||||
 | 
					                            else we redirect him to the activity page #}
 | 
				
			||||||
 | 
					                            {% if bar.has_annotated_barman %}
 | 
				
			||||||
 | 
					                                <a href="{{ url('counter:details', counter_id=bar.id) }}" style="padding: 0">
 | 
				
			||||||
 | 
					                            {% else %}
 | 
				
			||||||
 | 
					                                <a href="{{ url('counter:activity', counter_id=bar.id) }}" style="padding: 0">
 | 
				
			||||||
 | 
					                            {% endif %}
 | 
				
			||||||
                                {% if bar.is_inactive(): %}
 | 
					                                {% if bar.is_inactive(): %}
 | 
				
			||||||
                                  <i class="fa fa-question" style="color: #f39c12"></i>
 | 
					                                  <i class="fa fa-question" style="color: #f39c12"></i>
 | 
				
			||||||
                                {% elif bar.is_open(): %}
 | 
					                                {% elif bar.is_open(): %}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -25,7 +25,9 @@ from __future__ import annotations
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
from typing import Tuple
 | 
					from typing import Tuple
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from __future__ import annotations
 | 
				
			||||||
from django.db import models
 | 
					from django.db import models
 | 
				
			||||||
 | 
					from django.db.models import OuterRef, Exists, QuerySet
 | 
				
			||||||
from django.db.models.functions import Length
 | 
					from django.db.models.functions import Length
 | 
				
			||||||
from django.utils.translation import gettext_lazy as _
 | 
					from django.utils.translation import gettext_lazy as _
 | 
				
			||||||
from django.utils import timezone
 | 
					from django.utils import timezone
 | 
				
			||||||
@@ -336,6 +338,32 @@ class Product(models.Model):
 | 
				
			|||||||
        return "%s (%s)" % (self.name, self.code)
 | 
					        return "%s (%s)" % (self.name, self.code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class CounterQuerySet(models.QuerySet):
 | 
				
			||||||
 | 
					    def annotate_has_barman(self, user: User) -> CounterQuerySet:
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Annotate the queryset with the `user_is_barman` field.
 | 
				
			||||||
 | 
					        For each counter, this field has value True if the user
 | 
				
			||||||
 | 
					        is a barman of this counter, else False.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        :param user: the user we want to check if he is a barman
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Example::
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            sli = User.objects.get(username="sli")
 | 
				
			||||||
 | 
					            counters = (
 | 
				
			||||||
 | 
					                Counter.objects
 | 
				
			||||||
 | 
					                .annotate_has_barman(sli)  # add the user_has_barman boolean field
 | 
				
			||||||
 | 
					                .filter(has_annotated_barman=True)  # keep only counters where this user is barman
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            print("Sli est barman dans les comptoirs suivants :")
 | 
				
			||||||
 | 
					            for counter in counters:
 | 
				
			||||||
 | 
					                print(f"- {counter.name}")
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        subquery = user.counters.filter(pk=OuterRef("pk"))
 | 
				
			||||||
 | 
					        # noinspection PyTypeChecker
 | 
				
			||||||
 | 
					        return self.annotate(has_annotated_barman=Exists(subquery))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Counter(models.Model):
 | 
					class Counter(models.Model):
 | 
				
			||||||
    name = models.CharField(_("name"), max_length=30)
 | 
					    name = models.CharField(_("name"), max_length=30)
 | 
				
			||||||
    club = models.ForeignKey(
 | 
					    club = models.ForeignKey(
 | 
				
			||||||
@@ -360,6 +388,8 @@ class Counter(models.Model):
 | 
				
			|||||||
    )
 | 
					    )
 | 
				
			||||||
    token = models.CharField(_("token"), max_length=30, null=True, blank=True)
 | 
					    token = models.CharField(_("token"), max_length=30, null=True, blank=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    objects = CounterQuerySet.as_manager()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    class Meta:
 | 
					    class Meta:
 | 
				
			||||||
        verbose_name = _("counter")
 | 
					        verbose_name = _("counter")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -148,6 +148,20 @@ class CounterTest(TestCase):
 | 
				
			|||||||
        )
 | 
					        )
 | 
				
			||||||
        self.assertTrue(response.status_code == 200)
 | 
					        self.assertTrue(response.status_code == 200)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_annotate_has_barman_queryset(self):
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Test if the custom queryset method ``annotate_has_barman``
 | 
				
			||||||
 | 
					        works as intended
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        self.sli.counters.clear()
 | 
				
			||||||
 | 
					        self.sli.counters.add(self.foyer, self.mde)
 | 
				
			||||||
 | 
					        counters = Counter.objects.annotate_has_barman(self.sli)
 | 
				
			||||||
 | 
					        for counter in counters:
 | 
				
			||||||
 | 
					            if counter.name in ("Foyer", "MDE"):
 | 
				
			||||||
 | 
					                self.assertTrue(counter.has_annotated_barman)
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                self.assertFalse(counter.has_annotated_barman)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CounterStatsTest(TestCase):
 | 
					class CounterStatsTest(TestCase):
 | 
				
			||||||
    def setUp(self):
 | 
					    def setUp(self):
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user