Fix sort names getting mangled

This commit is contained in:
Lukáš Kucharczyk 2023-11-09 15:41:46 +01:00
parent 55ff3cd1a9
commit fa2b9b5c88
1 changed files with 6 additions and 12 deletions

View File

@ -18,20 +18,17 @@ class Game(models.Model):
return self.name return self.name
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
# Logic to create sort_name from name
def get_sort_name(name): def get_sort_name(name):
articles = ["a", "an", "the"] articles = ["a", "an", "the"]
name_parts = name.split() name_parts = name.split()
first_word = name_parts[0].lower() first_word = name_parts[0].lower()
if first_word in articles: if first_word in articles:
# Move the first word to the end return f"{' '.join(name_parts[1:])}, {name_parts[0]}"
name_parts.append(name_parts.pop(0))
return ", ".join(name_parts).capitalize()
else: else:
return name.capitalize() return name
self.sort_name = get_sort_name(self.name) self.sort_name = get_sort_name(self.name)
super().save(*args, **kwargs) # Call the "real" save() method. super().save(*args, **kwargs)
class Edition(models.Model): class Edition(models.Model):
@ -51,20 +48,17 @@ class Edition(models.Model):
return self.sort_name return self.sort_name
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
# Logic to create sort_name from name
def get_sort_name(name): def get_sort_name(name):
articles = ["a", "an", "the"] articles = ["a", "an", "the"]
name_parts = name.split() name_parts = name.split()
first_word = name_parts[0].lower() first_word = name_parts[0].lower()
if first_word in articles: if first_word in articles:
# Move the first word to the end return f"{' '.join(name_parts[1:])}, {name_parts[0]}"
name_parts.append(name_parts.pop(0))
return ", ".join(name_parts).capitalize()
else: else:
return name.capitalize() return name
self.sort_name = get_sort_name(self.name) self.sort_name = get_sort_name(self.name)
super().save(*args, **kwargs) # Call the "real" save() method. super().save(*args, **kwargs)
class PurchaseQueryset(models.QuerySet): class PurchaseQueryset(models.QuerySet):