From 628764a338e940a650bc26d265afa4090d95214e Mon Sep 17 00:00:00 2001 From: Corban-Lee <77944149+XordK@users.noreply.github.com> Date: Thu, 11 May 2023 15:43:33 +0100 Subject: [PATCH] Adding Teams & Sections is functional-ish --- src/mainapp/models.py | 108 +++++++++++++++----------- src/mainapp/templates/index.html | 2 +- src/mainapp/templates/teams.html | 92 +++++++++++++--------- src/mainapp/urls.py | 5 +- src/mainapp/views.py | 104 ++++++++++++++++++++++++- src/static/img/logo.webp | Bin 0 -> 28508 bytes src/static/js/teams.js | 126 ++++++++++++++++++++++++++++--- 7 files changed, 343 insertions(+), 94 deletions(-) create mode 100644 src/static/img/logo.webp diff --git a/src/mainapp/models.py b/src/mainapp/models.py index 14a4ffd..a74288c 100644 --- a/src/mainapp/models.py +++ b/src/mainapp/models.py @@ -1,5 +1,7 @@ """Models for the mainapp.""" +from string import ascii_uppercase + from django.db import models from django.core.exceptions import ValidationError @@ -96,12 +98,65 @@ class SectionValidator: return value + +class SectionManager(models.Manager): + + @staticmethod + def get_max_section(): + max_section = None + max_number = -1 + + # Iterate through all sections in the database + for section in Section.objects.all(): + section_name = section.name + section_number = 0 + + # Calculate the section number based on the section name + for i, char in enumerate(section_name): + section_number += (ord(char) - ord('A') + 1) * (26 ** (len(section_name) - i - 1)) + + # Check if this section has a higher number than the current maximum + if section_number > max_number: + max_number = section_number + max_section = section_name + + return max_section + + @staticmethod + def find_next_section(current_section): + if not current_section: + return 'A' + + # Split current section name into a list of characters + chars = list(current_section) + + # Increment the last character + chars[-1] = chr(ord(chars[-1]) + 1) + + # Check if the last character is "Z", and carry over to the next character if necessary + for i in range(len(chars) - 1, -1, -1): + if chars[i] > 'Z': + chars[i] = 'A' + if i == 0: + # If the first character needs to be incremented, add a new character "A" + chars.insert(0, 'A') + else: + # Increment the previous character + chars[i - 1] = chr(ord(chars[i - 1]) + 1) + else: + break + + # Join the characters back into a string and return the result + return ''.join(chars) + class Section(models.Model): """Represents a fishing area. Members can be assigned to a section, but no 2 teammates can be in the same or adjacent section.""" name = models.CharField(max_length=3, unique=True, null=False) + objects = SectionManager() + def clean(self): super().clean() @@ -118,52 +173,6 @@ class Section(models.Model): def get_members(self): return Member.objects.filter(section=self) - @property - def section_after(self): - """Returns the next section after this one, or none if not found.""" - sections = [sec for sec in Section.objects.all()] - sections.sort(key=lambda sec: sec.name) - try: - return sections[sections.index(self) + 1] - except IndexError: - return None - - @property - def section_before(self): - """Returns the last section before this one, or none if not found.""" - sections = [sec for sec in Section.objects.all()] - sections.sort(key=lambda sec: sec.name) - try: - return sections[sections.index(self) - 1] - except IndexError: - return None - - def is_joinable(self, member, /) -> bool: - """Returns boolean if a member is able to join this section.""" - - if not member.team: - return True - - teammates = [teammate for teammate in Member.objects.filter(team=member.team)] - - section_after = self.section_after.get_members if self.section_after else [] - section_before = self.section_before.get_members if self.section_before else [] - - for teammate in teammates: - if teammate in section_after: - return False - - if teammate in section_before: - return False - - return True - - return any( - teammate in section_after or teammate in section_before - for teammate in teammates - ) - - class Member(models.Model): """Represents a member of a team""" @@ -192,11 +201,18 @@ class Member(models.Model): def fullname(self) -> str: return f"{self.first_name} {self.last_name}" + +class TeamManager(models.Manager): + pass + + class Team(models.Model): """Represents a team""" name = ReusableAutoField(primary_key=True, default=ReusableAutoField.get_default, editable=False) + objects = TeamManager() + def __str__(self): return f"Team {self.name}" diff --git a/src/mainapp/templates/index.html b/src/mainapp/templates/index.html index caf0414..d586ac6 100644 --- a/src/mainapp/templates/index.html +++ b/src/mainapp/templates/index.html @@ -24,7 +24,7 @@ -->
diff --git a/src/mainapp/templates/teams.html b/src/mainapp/templates/teams.html index 0cc482e..ee4b7aa 100644 --- a/src/mainapp/templates/teams.html +++ b/src/mainapp/templates/teams.html @@ -1,7 +1,8 @@ {% extends "base.html" %} +{% load static %} {% block title %} - Member Management | + Manage Anglers | {% endblock title %} {% block style %} @@ -10,21 +11,22 @@ {% endblock style %} {% block content %} -