From 59e783a4f7f3cbc7a73ad750a5577fc7a115fbc3 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Thu, 22 Jun 2023 20:41:31 +0200 Subject: [PATCH] save play session summary on each change --- guild/admin.py | 16 +++++++ .../0010_playsessionsummaryversion.py | 46 +++++++++++++++++++ guild/models.py | 30 ++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 guild/migrations/0010_playsessionsummaryversion.py diff --git a/guild/admin.py b/guild/admin.py index 8c38f3f..d9bd97c 100644 --- a/guild/admin.py +++ b/guild/admin.py @@ -1,3 +1,19 @@ from django.contrib import admin +from guild.models import ( + Adventure, + Character, + Player, + Reward, + Resource, + PlaySession, + PlaySessionSummaryVersion, +) # Register your models here. +admin.site.register(Adventure) +admin.site.register(Character) +admin.site.register(Player) +admin.site.register(Reward) +admin.site.register(Resource) +admin.site.register(PlaySession) +admin.site.register(PlaySessionSummaryVersion) diff --git a/guild/migrations/0010_playsessionsummaryversion.py b/guild/migrations/0010_playsessionsummaryversion.py new file mode 100644 index 0000000..9e03da1 --- /dev/null +++ b/guild/migrations/0010_playsessionsummaryversion.py @@ -0,0 +1,46 @@ +# Generated by Django 4.2.1 on 2023-06-22 18:40 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + ("guild", "0009_rename_resourceearned_reward_alter_reward_options"), + ] + + operations = [ + migrations.CreateModel( + name="PlaySessionSummaryVersion", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("summary", models.TextField()), + ( + "created_at", + models.DateTimeField(auto_now_add=True, verbose_name="created at"), + ), + ( + "playsession", + models.ForeignKey( + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to="guild.playsession", + verbose_name="playsession", + ), + ), + ], + options={ + "verbose_name": "playsession summary version", + "verbose_name_plural": "playsession summary versions", + "ordering": ["-created_at"], + }, + ), + ] diff --git a/guild/models.py b/guild/models.py index 24e9db3..5e52c18 100644 --- a/guild/models.py +++ b/guild/models.py @@ -132,6 +132,36 @@ class PlaySession(models.Model): + str(self.pk) ) + def save(self, *args, **kwargs): + super().save(*args, **kwargs) + PlaySessionSummaryVersion.objects.create( + playsession=self, + summary=self.summary, + ) + + +class PlaySessionSummaryVersion(models.Model): + playsession = models.ForeignKey( + "PlaySession", + verbose_name=_("playsession"), + on_delete=models.SET_NULL, + null=True, + ) + summary = models.TextField() + + created_at = models.DateTimeField(_("created at"), auto_now_add=True) + + class Meta: + verbose_name = _("playsession summary version") + verbose_name_plural = _("playsession summary versions") + ordering = ["-created_at"] + + def __str__(self): + return f"{self.playsession} - {self.created_at}" + + def get_absolute_url(self): + return reverse("guild:playsession_detail", kwargs={"pk": self.playsession.pk}) + class Reward(models.Model): resource = models.ForeignKey(