Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rendering of custom table columns for full title, full name #36

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 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 @@ -119,6 +120,26 @@ class PersonNameMixin(AlternativeNameMixin, models.Model):
class Meta:
abstract = True

def full_name(self):
full_name = ""
surname = self.surname
forename = self.forename
fallback_name = self.fallback_name

if fallback_name != "":
full_name = fallback_name
else:
if forename != "" and surname != "":
full_name = f"{forename} {surname}"
elif surname != "":
full_name = surname
elif forename != "":
full_name = forename
else:
pass

return full_name


@reversion.register
class TitlesMixin(models.Model):
Expand All @@ -143,6 +164,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
40 changes: 5 additions & 35 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,47 +46,17 @@ 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(
accessor="surname",
accessor="full_name",
verbose_name=_("Name (voller)"),
order_by=("surname", "forename"),
order_by=("fallback_name", "surname", "forename"),
)

class Meta:
sequence = ("full_name", "surname", "forename")
exclude = ["surname", "forename"]

def render_full_name(self, record):
surname = record.surname
forename = record.forename
fallback_name = record.fallback_name

if fallback_name != "":
full_name = fallback_name
else:
if forename != "" and surname != "":
full_name = f"{forename} {surname}"
elif surname != "":
full_name = surname
elif forename != "":
full_name = forename
else:
# TODO log as error instead of exiting program
f"Missing name to look up or create Person, exiting."
exit(1)

return full_name
sequence = ("full_name", "fallback_name", "surname", "forename")
exclude = ["fallback_name", "surname", "forename"]


class BaseEntityTable(AbstractEntityTable, tables.Table):
Expand Down
Loading