Skip to content

Commit

Permalink
feat(models): add full_name method
Browse files Browse the repository at this point in the history
Add method for creating full names from
available name fields for person-like
entities in model classes.
Also remove now redundant method
render_full_name from tables.py.
  • Loading branch information
koeaw committed Mar 18, 2024
1 parent 9912540 commit f5ed5b1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
20 changes: 20 additions & 0 deletions apis_ontology/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,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 Down
29 changes: 4 additions & 25 deletions apis_ontology/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,14 @@ class Meta:

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

0 comments on commit f5ed5b1

Please sign in to comment.