diff --git a/RIGS/models/events.py b/RIGS/models/events.py index d75f1e7c..151dd606 100644 --- a/RIGS/models/events.py +++ b/RIGS/models/events.py @@ -17,7 +17,30 @@ from .utils import filter_by_pk from .finance import VatRate -class EventManager(models.Manager): +class BaseEventManager(models.Manager): + def event_search(self, q, start, end, status): + filt = Q() + if end: + filt &= Q(start_date__lte=end) + if start: + filt &= Q(start_date__gte=start) + + objects = self.all() + + if q: + objects = self.search(q) + + if len(status) > 0: + filt &= Q(status__in=status) + + qs = objects.filter(filt).order_by('-start_date') + + # Preselect related for efficiency + qs.select_related('person', 'organisation', 'venue', 'mic') + + return qs + +class EventManager(BaseEventManager): def current_events(self): events = self.filter( (models.Q(start_date__gte=timezone.now(), end_date__isnull=True, dry_hire=False) & ~models.Q( @@ -390,7 +413,7 @@ class EventAuthorisation(models.Model, RevisionMixin): return f"{self.event.display_id} (requested by {self.sent_by.initials})" -class SubhireManager(models.Manager): +class SubhireManager(BaseEventManager): def current_events(self): events = self.exclude(status=BaseEvent.CANCELLED).filter( (models.Q(start_date__gte=timezone.now(), end_date__isnull=True)) | # Starts after with no end @@ -406,7 +429,6 @@ class SubhireManager(models.Manager): ).count() return event_count - @reversion.register class Subhire(BaseEvent): insurance_value = models.DecimalField(max_digits=10, decimal_places=2) # TODO Validate if this is over notifiable threshold diff --git a/RIGS/templates/event_archive.html b/RIGS/templates/event_archive.html index ae3c0a1c..899748d3 100644 --- a/RIGS/templates/event_archive.html +++ b/RIGS/templates/event_archive.html @@ -15,36 +15,7 @@ {% endblock %} {% block content %} -
-
-
-
-
- Start -
- -
-
-
- End -
- -
-
-
- Keyword -
- -
- - {% button 'search' %} -
-
-
+{% include 'partials/archive_form.html' %}
{% with object_list as events %} diff --git a/RIGS/templates/partials/archive_form.html b/RIGS/templates/partials/archive_form.html new file mode 100644 index 00000000..8e6db8ea --- /dev/null +++ b/RIGS/templates/partials/archive_form.html @@ -0,0 +1,32 @@ +{% load get_list from filters %} +{% load button from filters %} +
+
+
+
+
+ Start +
+ +
+
+
+ End +
+ +
+
+
+ Keyword +
+ +
+ + {% button 'search' %} +
+
+
\ No newline at end of file diff --git a/RIGS/templates/partials/subhire_table.html b/RIGS/templates/partials/subhire_table.html new file mode 100644 index 00000000..d9ef926c --- /dev/null +++ b/RIGS/templates/partials/subhire_table.html @@ -0,0 +1,84 @@ +{% load linked_name from filters %} +{% load markdown_tags %} +
+ + + + + + + + {% if perms.RIGS.subhire_finance %} + + {% endif %} + + + + {% for event in events %} + + + + + + + + + {% if perms.RIGS.subhire_finance %} + + {% endif %} + + {% empty %} + + + + {% endfor %} + + + + + + + + + + +
#Dates & TimesHire DetailsAssociated Event(s)Insurance Value
{{ event.display_id }} + Start: {{ event.start_date|date:"D d/m/Y" }} + {% if event.has_start_time %} + {{ event.start_time|date:"H:i" }} + {% endif %} + + {% if event.end_date %} +
+ End: {% if event.end_date != event.start_date %}{{ event.end_date|date:"D d/m/Y" }}{% endif %} + {% if event.has_end_time %} + {{ event.end_time|date:"H:i" }} + {% endif %} + + {% endif %} +
+

+ + {{ event.name }} + +

+
+ Primary Contact: {{ event.person|linked_name }} + {% if event.organisation %} + ({{ event.organisation|linked_name }}) + {% endif %} +
+ {% if not event.cancelled and event.description %} +

{{ event.description|markdown }}

+ {% endif %} + {% include 'partials/event_status.html' %} +
+
    + {% for event in event.events.all %} +
  • {{ event }}
  • + {% endfor %} +
+
+ £{{ event.insurance_value }} +
No events found
Total Value:£{{ total_value }}
+
diff --git a/RIGS/templates/subhire_list.html b/RIGS/templates/subhire_list.html new file mode 100644 index 00000000..b34cf2af --- /dev/null +++ b/RIGS/templates/subhire_list.html @@ -0,0 +1,27 @@ +{% extends 'base_rigs.html' %} +{% load paginator from filters %} +{% load static %} + +{% block css %} + {{ block.super }} + +{% endblock %} + +{% block preload_js %} + {{ block.super }} + +{% endblock %} + +{% block content %} +{% include 'partials/archive_form.html' %} +
+
+ {% with object_list as events %} + {% include 'partials/subhire_table.html' %} + {% endwith %} +
+
+ +{% paginator %} + +{% endblock %} diff --git a/RIGS/templates/subhire_table.html b/RIGS/templates/subhire_table.html deleted file mode 100644 index e69de29b..00000000 diff --git a/RIGS/templatetags/filters.py b/RIGS/templatetags/filters.py index 21d4a1af..f242362e 100644 --- a/RIGS/templatetags/filters.py +++ b/RIGS/templatetags/filters.py @@ -175,6 +175,9 @@ def namewithnotes(obj, url, autoescape=True): else: return obj.name +@register.filter(needs_autoescape=True) +def linked_name(object, autoescape=True): + return mark_safe(f"{object.name}") @register.filter(needs_autoescape=True) def linkornone(target, namespace=None, autoescape=True): diff --git a/RIGS/urls.py b/RIGS/urls.py index 13c9c907..5c966ba9 100644 --- a/RIGS/urls.py +++ b/RIGS/urls.py @@ -74,7 +74,7 @@ urlpatterns = [ name='subhire_create'), path('subhire//edit', permission_required_with_403('RIGS.change_event')(views.SubhireEdit.as_view()), name='subhire_update'), - path('subhire/upcoming', login_required(views.SubhireList.as_view()), + path('subhire/list/', login_required(views.SubhireList.as_view()), name='subhire_list'), # Dashboards diff --git a/RIGS/views/rigboard.py b/RIGS/views/rigboard.py index e845ed65..051174db 100644 --- a/RIGS/views/rigboard.py +++ b/RIGS/views/rigboard.py @@ -204,27 +204,7 @@ class EventArchive(generic.ListView): "Muppet! Check the dates, it has been fixed for you.") start, end = end, start # Stop the impending fail - filter = Q() - if end != "": - filter &= Q(start_date__lte=end) - if start: - filter &= Q(start_date__gte=start) - - q = self.request.GET.get('q', "") - objects = self.model.objects.all() - - if q: - objects = self.model.objects.search(q) - - status = self.request.GET.getlist('status', "") - - if len(status) > 0: - filter &= Q(status__in=status) - - qs = objects.filter(filter).order_by('-start_date') - - # Preselect related for efficiency - qs.select_related('person', 'organisation', 'venue', 'mic') + qs = self.model.objects.event_search(self.request.GET.get('q', None), start, end, self.request.GET.get('status', "")) if not qs.exists(): messages.add_message(self.request, messages.WARNING, "No events have been found matching those criteria.") diff --git a/RIGS/views/subhire.py b/RIGS/views/subhire.py index 1c49ff90..0f1d4ba0 100644 --- a/RIGS/views/subhire.py +++ b/RIGS/views/subhire.py @@ -1,7 +1,9 @@ from django.urls import reverse_lazy from django.views import generic +from django.db.models import Sum from PyRIGS.views import ModalURLMixin, get_related from RIGS import models, forms +from RIGS.views import EventArchive class SubhireDetail(generic.DetailView, ModalURLMixin): @@ -48,11 +50,13 @@ class SubhireEdit(generic.UpdateView): return reverse_lazy('subhire_detail', kwargs={'pk': self.object.pk}) -class SubhireList(generic.TemplateView): - template_name = 'rigboard.html' +class SubhireList(EventArchive): + template_name = 'subhire_list.html' + model = models.Subhire + paginate_by = 25 def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - context['events'] = models.Subhire.objects.current_events() - context['page_title'] = "Upcoming Subhire" + context['total_value'] = self.get_queryset().aggregate(sum=Sum('insurance_value'))['sum'] + context['page_title'] = "Subhire List" return context