From 3bfec8a5298c425b18581fa889dadfca55eb5035 Mon Sep 17 00:00:00 2001
From: Konrad Mohrfeldt <konrad.mohrfeldt@farbdev.org>
Date: Thu, 17 Mar 2022 13:17:15 +0100
Subject: [PATCH] feat: add string representations for models
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This adds a string representation for all program models except the
schedule so that it is easier to identify objects in DRF’s filter,
create, and update forms.
---
 program/models.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/program/models.py b/program/models.py
index 511ba383..6a4762df 100644
--- a/program/models.py
+++ b/program/models.py
@@ -47,6 +47,9 @@ class Type(models.Model):
     class Meta:
         ordering = ("name",)
 
+    def __str__(self):
+        return self.name
+
 
 class Category(models.Model):
     name = models.CharField(max_length=32)
@@ -58,6 +61,9 @@ class Category(models.Model):
     class Meta:
         ordering = ("name",)
 
+    def __str__(self):
+        return self.name
+
 
 class Topic(models.Model):
     name = models.CharField(max_length=32)
@@ -68,6 +74,9 @@ class Topic(models.Model):
     class Meta:
         ordering = ("name",)
 
+    def __str__(self):
+        return self.name
+
 
 class MusicFocus(models.Model):
     name = models.CharField(max_length=32)
@@ -78,6 +87,9 @@ class MusicFocus(models.Model):
     class Meta:
         ordering = ("name",)
 
+    def __str__(self):
+        return self.name
+
 
 class FundingCategory(models.Model):
     name = models.CharField(max_length=32)
@@ -88,6 +100,9 @@ class FundingCategory(models.Model):
     class Meta:
         ordering = ("name",)
 
+    def __str__(self):
+        return self.name
+
 
 class Language(models.Model):
     name = models.CharField(max_length=32)
@@ -96,6 +111,9 @@ class Language(models.Model):
     class Meta:
         ordering = ("language",)
 
+    def __str__(self):
+        return self.name
+
 
 class Host(models.Model):
     name = models.CharField(max_length=128)
@@ -118,6 +136,9 @@ class Host(models.Model):
     class Meta:
         ordering = ("name",)
 
+    def __str__(self):
+        return self.name
+
     def save(self, *args, **kwargs):
         super(Host, self).save(*args, **kwargs)
 
@@ -132,6 +153,9 @@ class Link(models.Model):
     description = models.CharField(max_length=8)
     url = models.URLField()
 
+    def __str__(self):
+        return self.url
+
 
 class Show(models.Model):
     predecessor = models.ForeignKey(
@@ -181,6 +205,9 @@ class Show(models.Model):
     class Meta:
         ordering = ("slug",)
 
+    def __str__(self):
+        return self.name
+
 
 class RRule(models.Model):
     name = models.CharField(max_length=32, unique=True)
@@ -192,6 +219,9 @@ class RRule(models.Model):
     class Meta:
         ordering = ("pk",)
 
+    def __str__(self):
+        return self.name
+
 
 class Schedule(models.Model):
     rrule = models.ForeignKey(RRule, on_delete=models.CASCADE, related_name="schedules")
@@ -979,6 +1009,20 @@ class TimeSlot(models.Model):
     class Meta:
         ordering = ("start", "end")
 
+    def __str__(self):
+        if self.start.date() == self.end.date():
+            time_span = "{0}, {1} - {2}".format(
+                self.start.strftime("%x"),
+                self.start.strftime("%X"),
+                self.end.strftime("%X"),
+            )
+        else:
+            time_span = "{0} - {1}".format(
+                self.start.strftime("%X %x"),
+                self.end.strftime("%X %x"),
+            )
+        return f"{str(self.show)} ({time_span})"
+
     def save(self, *args, **kwargs):
         self.show = self.schedule.show
         super(TimeSlot, self).save(*args, **kwargs)
@@ -1029,6 +1073,9 @@ class Note(models.Model):
     class Meta:
         ordering = ("timeslot",)
 
+    def __str__(self):
+        return self.title
+
     def save(self, *args, **kwargs):
         self.start = self.timeslot.start
         self.show = self.timeslot.schedule.show
-- 
GitLab