Skip to content

Commit

Permalink
feat(models): add full_title method
Browse files Browse the repository at this point in the history
Add method for creating full titles from
title + subtitle fields in model classes.
Also remove now redundant render_full_title
method from tables.py.
  • Loading branch information
koeaw committed Mar 18, 2024
1 parent 103bcd6 commit 9912540
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
14 changes: 14 additions & 0 deletions apis_ontology/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
from multiselectfield import MultiSelectField
import re


@reversion.register
Expand Down Expand Up @@ -143,6 +144,19 @@ class TitlesMixin(models.Model):
class Meta:
abstract = True

def full_title(self):
full_title = self.title
subtitle = self.subtitle
letter_or_digit = re.compile(r"[\W\d]", re.U)

if subtitle:
if letter_or_digit.match(subtitle[0]):
full_title += f" {subtitle}"
else:
full_title += f". {subtitle}"

return full_title


@reversion.register
class DataSource(models.Model):
Expand Down
11 changes: 1 addition & 10 deletions apis_ontology/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self, *args, **kwargs):

class FullTitleMixin(tables.Table):
full_title = GenericEditLinkColumn(
accessor="title",
accessor="full_title",
verbose_name=_("Titel (gesamt)"),
order_by=("title", "subtitle"),
)
Expand All @@ -46,15 +46,6 @@ class Meta:
sequence = ("full_title", "title", "subtitle")
exclude = ["title", "subtitle"]

def render_full_title(self, record):
full_title = record.title
subtitle = record.subtitle

if subtitle:
full_title += f". {subtitle}"

return full_title


class FullNameMixin(tables.Table):
full_name = GenericEditLinkColumn(
Expand Down

0 comments on commit 9912540

Please sign in to comment.