From c6b6443901a086b6ee02b9f7a37ef79b3c2d0381 Mon Sep 17 00:00:00 2001 From: Peter Pfeufer Date: Mon, 6 Jan 2025 18:08:20 +0100 Subject: [PATCH] [CHANGE] Move structure label BG detection to Python instead of Django MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python is faster and needs less memory … It's also much mire readable that way. --- .../templates/timerboard/timertable.html | 64 +-------- allianceauth/timerboard/views.py | 135 +++++++++++++++--- 2 files changed, 114 insertions(+), 85 deletions(-) diff --git a/allianceauth/timerboard/templates/timerboard/timertable.html b/allianceauth/timerboard/templates/timerboard/timertable.html index 455d0d94..014b01e8 100644 --- a/allianceauth/timerboard/templates/timerboard/timertable.html +++ b/allianceauth/timerboard/templates/timerboard/timertable.html @@ -51,69 +51,7 @@ - {% comment %} BG: Info {% endcomment %} - {% if timer.structure == "POCO" %} -
- {% elif timer.structure == "POS[S]" %} -
- {% elif timer.structure == "POS[M]" %} -
- {% elif timer.structure == "POS[L]" %} -
- - {% comment %} BG: Warning {% endcomment %} - {% elif timer.structure == "Station" %} {% comment %} Pending Removal {% endcomment %} -
- {% elif timer.structure == "Orbital Skyhook" %} -
- {% elif timer.structure == "I-HUB" %} -
- {% elif timer.structure == "Raitaru" %} -
- {% elif timer.structure == "Azbel" %} -
- {% elif timer.structure == "Athanor" %} -
- {% elif timer.structure == "Tatara" %} -
- {% elif timer.structure == "Pharolux Cyno Beacon" %} -
- {% elif timer.structure == "Tenebrex Cyno Jammer" %} -
- {% elif timer.structure == "Ansiblex Jump Gate" %} -
- {% elif timer.structure == "Mercenary Den" %} -
- {% elif timer.structure == "Metenox Moon Drill" %} -
- - {% comment %} BG: Danger {% endcomment %} - {% elif timer.structure == "TCU" %} -
- {% elif timer.structure == "Astrahus" %} -
- {% elif timer.structure == "Fortizar" %} -
- {% elif timer.structure == "Keepstar" %} -
- {% elif timer.structure == "Sotiyo" %} -
- - {% comment %} BG: Secondary {% endcomment %} - {% elif timer.structure == "TCU" %} {% comment %} Pending Removal {% endcomment %} -
- {% elif timer.structure == "Other" %} -
- - {% comment %} BG: Success {% endcomment %} - {% elif timer.structure == "Moon Mining Cycle" %} -
- - {% comment %} BG: Primary (for all other timers) {% endcomment %} - {% else %} -
- {% endif %} - +
{{ timer.get_structure_display }}
diff --git a/allianceauth/timerboard/views.py b/allianceauth/timerboard/views.py index 93c877c8..bfee13ff 100644 --- a/allianceauth/timerboard/views.py +++ b/allianceauth/timerboard/views.py @@ -3,7 +3,8 @@ import logging from django.contrib import messages from django.contrib.auth.mixins import ( - LoginRequiredMixin, PermissionRequiredMixin, + LoginRequiredMixin, + PermissionRequiredMixin, ) from django.db.models import Q from django.shortcuts import get_object_or_404, redirect, render @@ -20,8 +21,8 @@ from allianceauth.timerboard.models import Timer logger = logging.getLogger(__name__) -TIMER_VIEW_PERMISSION = 'auth.timer_view' -TIMER_MANAGE_PERMISSION = 'auth.timer_management' +TIMER_VIEW_PERMISSION = "auth.timer_view" +TIMER_MANAGE_PERMISSION = "auth.timer_management" class BaseTimerView(LoginRequiredMixin, PermissionRequiredMixin, View): @@ -29,22 +30,112 @@ class BaseTimerView(LoginRequiredMixin, PermissionRequiredMixin, View): class TimerView(BaseTimerView): - template_name = 'timerboard/view.html' + template_name = "timerboard/view.html" permission_required = TIMER_VIEW_PERMISSION def get(self, request): + """ + Renders the timer view + + :param request: + :type request: + :return: + :rtype: + """ + + def get_bg_modifier(structure): + """ + Returns the bootstrap bg modifier for the given structure + + :param structure: + :type structure: + :return: + :rtype: + """ + + if structure in bg_info: + return "info" + elif structure in bg_waning: + return "warning" + elif structure in bg_danger: + return "danger" + elif structure in bg_secondary: + return "secondary" + + return "primary" + logger.debug(f"timer_view called by user {request.user}") char = request.user.profile.main_character + if char: corp = char.corporation else: corp = None - base_query = Timer.objects.select_related('eve_character') + + base_query = Timer.objects.select_related("eve_character") + + timers = [] + corp_timers = [] + future_timers = [] + past_timers = [] + + bg_info = [ + Timer.Structure.POCO.value, # POCO + Timer.Structure.POSS.value, # POS[S] + Timer.Structure.POSM.value, # POS[M] + Timer.Structure.POSL.value, # POS[L] + ] + bg_waning = [ + Timer.Structure.ANSIBLEX.value, # Ansiblex Jump Gate + Timer.Structure.ATHANOR.value, # Athanor + Timer.Structure.AZBEL.value, # Azbel + Timer.Structure.MERCDEN.value, # Mercenary Den + Timer.Structure.METENOX.value, # Metenox Moon Drill + Timer.Structure.ORBITALSKYHOOK.value, # Orbital Skyhook + Timer.Structure.PHAROLUX.value, # Pharolux Cyno Beacon + Timer.Structure.RAITARU.value, # Raitaru + "Station", # Legacy structure, remove in future update + Timer.Structure.TATARA.value, # Tatara + Timer.Structure.TENEBREX.value, # Tenebrex Cyno Jammer + ] + bg_danger = [ + Timer.Structure.ASTRAHUS.value, # Astrahus + Timer.Structure.FORTIZAR.value, # Fortizar + Timer.Structure.IHUB.value, # I-HUB + Timer.Structure.KEEPSTAR.value, # Keepstar + Timer.Structure.SOTIYO.value, # Sotiyo + Timer.Structure.TCU.value, # TCU (Legacy structure, remove in future update) + ] + bg_secondary = [ + Timer.Structure.MOONPOP.value, # Moon Mining Cycle + Timer.Structure.OTHER.value, # Other + ] + + # Timers + for timer in base_query.filter(corp_timer=False): + timer.bg_modifier = get_bg_modifier(timer.structure) + timers.append(timer) + + # Corp Timers + for timer in base_query.filter(corp_timer=True, eve_corp=corp): + timer.bg_modifier = get_bg_modifier(timer.structure) + corp_timers.append(timer) + + # Future Timers + for timer in base_query.filter(corp_timer=False, eve_time__gte=timezone.now()): + timer.bg_modifier = get_bg_modifier(timer.structure) + future_timers.append(timer) + + # Past Timers + for timer in base_query.filter(corp_timer=False, eve_time__lt=timezone.now()): + timer.bg_modifier = get_bg_modifier(timer.structure) + past_timers.append(timer) + render_items = { - 'timers': base_query.filter(corp_timer=False), - 'corp_timers': base_query.filter(corp_timer=True, eve_corp=corp), - 'future_timers': base_query.filter(corp_timer=False, eve_time__gte=timezone.now()), - 'past_timers': base_query.filter(corp_timer=False, eve_time__lt=timezone.now()), + "timers": timers, + "corp_timers": corp_timers, + "future_timers": future_timers, + "past_timers": past_timers, } return render(request, self.template_name, context=render_items) @@ -52,7 +143,7 @@ class TimerView(BaseTimerView): class TimerManagementView(BaseTimerView): permission_required = TIMER_MANAGE_PERMISSION - index_redirect = 'timerboard:view' + index_redirect = "timerboard:view" success_url = reverse_lazy(index_redirect) model = Timer @@ -66,12 +157,12 @@ class AddUpdateMixin: Inject the request user into the kwargs passed to the form """ kwargs = super().get_form_kwargs() - kwargs.update({'user': self.request.user}) + kwargs.update({"user": self.request.user}) return kwargs class AddTimerView(TimerManagementView, AddUpdateMixin, CreateView): - template_name_suffix = '_create_form' + template_name_suffix = "_create_form" form_class = TimerForm def form_valid(self, form): @@ -82,17 +173,18 @@ class AddTimerView(TimerManagementView, AddUpdateMixin, CreateView): ) messages.success( self.request, - _('Added new timer in %(system)s at %(time)s.') % {"system": timer.system, "time": timer.eve_time} + _("Added new timer in %(system)s at %(time)s.") + % {"system": timer.system, "time": timer.eve_time}, ) return result class EditTimerView(TimerManagementView, AddUpdateMixin, UpdateView): - template_name_suffix = '_update_form' + template_name_suffix = "_update_form" form_class = TimerForm def form_valid(self, form): - messages.success(self.request, _('Saved changes to the timer.')) + messages.success(self.request, _("Saved changes to the timer.")) return super().form_valid(form) @@ -107,21 +199,20 @@ def dashboard_timers(request): except (EveCorporationInfo.DoesNotExist, AttributeError): return "" - timers = Timer.objects.select_related( - 'eve_character' - ).filter( + timers = Timer.objects.select_related("eve_character").filter( (Q(corp_timer=True) & Q(eve_corp=corp)) | Q(corp_timer=False), - eve_time__gte=timezone.now() + eve_time__gte=timezone.now(), )[:5] if timers.count(): context = { - 'timers': timers, + "timers": timers, } return render_to_string( - template_name='timerboard/dashboard.timers.html', - context=context, request=request + template_name="timerboard/dashboard.timers.html", + context=context, + request=request, ) else: return ""