diff --git a/server/vb/migrations/0001_initial.py b/server/vb/migrations/0001_initial.py index 85cd620..2369612 100644 --- a/server/vb/migrations/0001_initial.py +++ b/server/vb/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 5.0.3 on 2024-04-02 20:54 +# Generated by Django 5.0.3 on 2024-04-02 21:24 import django.core.validators import django.db.models.deletion @@ -43,7 +43,7 @@ class Migration(migrations.Migration): ('slug', models.SlugField(max_length=255, unique=True)), ('start_at', models.DateTimeField()), ('end_at', models.DateTimeField()), - ('description', models.TextField(default='{{ school.short_name }} students: check your voter registration for a 1 in 10 chance to win a $25 Amazon gift card.', help_text="A description of the contest. Can use {{ school.name }} to insert the school's name, etc.")), + ('template', models.TextField(default='{{ school.short_name }} students: check your voter registration for a 1 in 10 chance to win a $25 Amazon gift card.', help_text="A description of the contest. Can use {{ school.name }} to insert the school's name, etc.")), ('school', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='contests', to='vb.school')), ], ), diff --git a/server/vb/models.py b/server/vb/models.py index 8e160ee..e844ebd 100644 --- a/server/vb/models.py +++ b/server/vb/models.py @@ -3,6 +3,7 @@ import typing as t from django.db import models +from django.template import Context, Template from django.utils.timezone import now as django_now from server.utils.contrast import HEX_COLOR_VALIDATOR, get_text_color @@ -110,10 +111,10 @@ class Contest(models.Model): slug = models.SlugField(max_length=255, blank=False, unique=True) start_at = models.DateTimeField(blank=False) end_at = models.DateTimeField(blank=False) - description = models.TextField( + template = models.TextField( blank=False, help_text="A description of the contest. Can use {{ school.name }} to insert the school's name, etc.", # noqa - default="{{ school.short_name }} students: check your voter registration for a 1 in 10 chance to win a $25 Amazon gift card.", + default="{{ school.short_name }} students: check your voter registration for a 1 in 10 chance to win a $25 Amazon gift card.", # noqa ) # For now, we assume that each contest is associated with a single school. @@ -146,6 +147,11 @@ def seconds_until_end(self, when: datetime.datetime | None = None) -> float: when = when or django_now() return (self.end_at - when).total_seconds() + def description(self): + """Render the contest template.""" + context = {"school": self.school, "contest": self} + return Template(self.template).render(Context(context)) + def __str__(self): """Return the contest model's string representation.""" return f"Contest: {self.name} for {self.school.name}" diff --git a/server/vb/templates/school.dhtml b/server/vb/templates/school.dhtml index 40bbaa2..2f3ba4b 100644 --- a/server/vb/templates/school.dhtml +++ b/server/vb/templates/school.dhtml @@ -56,9 +56,11 @@
- {{ school.short_name }} students: check your voter registration status for a 1 in 10 chance to win a $25 Amazon gift card. -
+ {% if current_contest %} +{{ current_contest.description }}
+ {% else %} +There is no contest at this time. TODO: show something useful in this state?
+ {% endif %}