From 1f93f9e7bdef573c49efa2dc221f0af459e6b0a4 Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Sat, 22 Jan 2022 19:11:47 +0100 Subject: [PATCH] Dynamically find currently active Contest Previously we had a lot of hardcoded contest information in the templates. Name, ruleset and number of contest are now all taken from the currently active contest and rendered into the templates. Instead of an URL containing the current contest name, we just use a generic /contest/. The API will no longer use "the contest with id=1", but the currently active contest as well. The currently active contest is - for now - the contest with the latest deadline. --- contest/context_processors.py | 7 +++++++ contest/migrations/0022_contest_contestno.py | 20 +++++++++++++++++++ .../migrations/0023_contest_rulesetlink.py | 19 ++++++++++++++++++ contest/models.py | 10 +++++++++- cqtu/settings.py | 2 ++ cqtu/urls.py | 2 +- templates/base.html | 6 +++--- templates/contest/contestPanel.html | 2 +- templates/index.html | 7 ++++++- 9 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 contest/context_processors.py create mode 100644 contest/migrations/0022_contest_contestno.py create mode 100644 contest/migrations/0023_contest_rulesetlink.py diff --git a/contest/context_processors.py b/contest/context_processors.py new file mode 100644 index 0000000..86b18b1 --- /dev/null +++ b/contest/context_processors.py @@ -0,0 +1,7 @@ +from .models import Contest + + +def current_contest(self): + return { + 'current_contest': Contest.get_current_contest(), + } diff --git a/contest/migrations/0022_contest_contestno.py b/contest/migrations/0022_contest_contestno.py new file mode 100644 index 0000000..04f65a4 --- /dev/null +++ b/contest/migrations/0022_contest_contestno.py @@ -0,0 +1,20 @@ +# Generated by Django 4.0.1 on 2022-01-22 17:52 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('contest', '0021_alter_qso_otherno_alter_qso_ownno_alter_user_dncall_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='contest', + name='contestNo', + field=models.IntegerField(default=1, help_text='Running number of contest (for vanity reasons)', validators=[django.core.validators.MinValueValidator(1)]), + preserve_default=False, + ), + ] diff --git a/contest/migrations/0023_contest_rulesetlink.py b/contest/migrations/0023_contest_rulesetlink.py new file mode 100644 index 0000000..4cbdca7 --- /dev/null +++ b/contest/migrations/0023_contest_rulesetlink.py @@ -0,0 +1,19 @@ +# Generated by Django 4.0.1 on 2022-01-22 17:56 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('contest', '0022_contest_contestno'), + ] + + operations = [ + migrations.AddField( + model_name='contest', + name='rulesetLink', + field=models.TextField(default='', help_text='URL to the ruleset pdf for this contest'), + preserve_default=False, + ), + ] diff --git a/contest/models.py b/contest/models.py index b7bc648..e746fbb 100644 --- a/contest/models.py +++ b/contest/models.py @@ -12,6 +12,9 @@ from .validators import CallUsernameValidator class Contest(models.Model): name = models.CharField(max_length=20) shortName = models.CharField(max_length=20, unique=True) + contestNo = models.IntegerField(validators=[MinValueValidator(1)], + help_text="Running number of contest (for vanity reasons)") + rulesetLink = models.TextField(help_text="URL to the ruleset pdf for this contest") callQrg = models.ForeignKey("Frequency", on_delete=models.SET_NULL, null=True, blank=True) deadline = models.DateTimeField() @@ -23,7 +26,12 @@ class Contest(models.Model): @classmethod def get_current_contest(cls): - return cls.objects.get(id=1) + # Currently the contest with the latest deadline is the active one + # This definitely has potential for improvement, but it's better than a hardcoded contest + contests = cls.objects.order_by("-deadline") + if len(contests) > 0: + return contests[0] + return None class Reference(models.Model): diff --git a/cqtu/settings.py b/cqtu/settings.py index 9573812..f4b008f 100644 --- a/cqtu/settings.py +++ b/cqtu/settings.py @@ -40,6 +40,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'django.contrib.humanize', 'crispy_forms', 'rest_framework', 'django_filters', @@ -72,6 +73,7 @@ TEMPLATES = [ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', + 'contest.context_processors.current_contest', ], }, }, diff --git a/cqtu/urls.py b/cqtu/urls.py index 72501c0..c0fb3fc 100644 --- a/cqtu/urls.py +++ b/cqtu/urls.py @@ -7,7 +7,7 @@ from contest.views import index, register, profile urlpatterns = [ path('', index, name="index"), - path('cqtufm2019/', include('contest.urls', namespace='contest')), + path('contest/', include('contest.urls', namespace='contest')), path('admin/', admin.site.urls), path('login/', auth_views.LoginView.as_view(), name='login'), diff --git a/templates/base.html b/templates/base.html index 3cdcdca..0b20c2e 100644 --- a/templates/base.html +++ b/templates/base.html @@ -10,7 +10,7 @@ {% load static %} - CQTUFM2019 - CQ TU FM Contest 2019 + {{ current_contest.name }} @@ -35,7 +35,7 @@ - CQ TU FM 2019 + {{ current_contest.name | default:"NO CONTEST" }}