Skip to content

Commit

Permalink
feat: expose full scope name on api
Browse files Browse the repository at this point in the history
  • Loading branch information
czosel committed Aug 31, 2021
1 parent 93fa605 commit 3465b97
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
11 changes: 7 additions & 4 deletions emeis/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,15 @@ class Scope(MPTTModel, UUIDModel):
related_name="children",
)

def full_name(self, sep="\u00bb"):
def full_name(self, sep="\u00bb", language=None):
"""Return full name of the scope, including parent scopes."""
own_name = str(self.name) if language is None else self.name[language]

if self.parent:
parent_name = self.parent.full_name(sep)
return f"{parent_name} {sep} {self.name}"
return str(self.name)
parent_name = self.parent.full_name(sep, language)
return f"{parent_name} {sep} {own_name}"

return own_name

def __str__(self):
name = self.full_name()
Expand Down
17 changes: 16 additions & 1 deletion emeis/core/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from generic_permissions.validation import ValidatorMixin
from rest_framework_json_api import serializers
Expand Down Expand Up @@ -73,9 +74,23 @@ class Meta:


class ScopeSerializer(BaseSerializer):
full_name = serializers.SerializerMethodField()

def get_full_name(self, instance):
return {
language: instance.full_name(language=language)
for language, _ in settings.LANGUAGES
}

class Meta:
model = Scope
fields = BaseSerializer.Meta.fields + ("name", "description", "parent", "level")
fields = BaseSerializer.Meta.fields + (
"name",
"description",
"parent",
"level",
"full_name",
)


class PermissionSerializer(BaseSerializer):
Expand Down
14 changes: 12 additions & 2 deletions emeis/core/tests/snapshots/snap_test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@
"en": "Ago get want middle. Whose scientist draw free property consider rather. Have director true force.",
"fr": "",
},
"full-name": {"de": "", "en": "Stephanie Porter", "fr": ""},
"level": 0,
"meta": {},
"modified-at": "2017-05-21T00:00:00Z",
Expand All @@ -330,6 +331,7 @@
"en": "Form leader fund task believe oil. Itself close again affect ok window church. Claim interview participant call strategy.",
"fr": "",
},
"full-name": {"de": "", "en": "Dr. Jose Campbell", "fr": ""},
"level": 0,
"meta": {},
"modified-at": "2017-05-21T00:00:00Z",
Expand All @@ -350,6 +352,7 @@
"en": "Beyond seek officer player possible issue. Trial population standard. After away control expert without assume grow back.",
"fr": "",
},
"full-name": {"de": "", "en": "Mary White", "fr": ""},
"level": 0,
"meta": {},
"modified-at": "2017-05-21T00:00:00Z",
Expand Down Expand Up @@ -567,6 +570,7 @@
Argue move appear catch toward help wind. Material minute ago get.""",
"fr": "",
},
"full-name": {"de": "", "en": "Pamela Horton", "fr": ""},
"level": 0,
"meta": {},
"modified-at": "2017-05-21T00:00:00Z",
Expand All @@ -588,6 +592,7 @@
Son success provide beyond. Officer player possible issue ahead suffer.""",
"fr": "",
},
"full-name": {"de": "", "en": "Rebecca Gonzalez", "fr": ""},
"level": 0,
"meta": {},
"modified-at": "2017-05-21T00:00:00Z",
Expand All @@ -610,6 +615,7 @@
Daughter single product trade.""",
"fr": "",
},
"full-name": {"de": "", "en": "Lorraine Reynolds", "fr": ""},
"level": 0,
"meta": {},
"modified-at": "2017-05-21T00:00:00Z",
Expand Down Expand Up @@ -814,6 +820,7 @@
"en": "Ago get want middle. Whose scientist draw free property consider rather. Have director true force.",
"fr": "",
},
"full-name": {"de": "", "en": "Stephanie Porter", "fr": ""},
"level": 0,
"meta": {},
"modified-at": "2017-05-21T00:00:00Z",
Expand Down Expand Up @@ -960,6 +967,7 @@
Argue move appear catch toward help wind. Material minute ago get.""",
"fr": "",
},
"full-name": {"de": "", "en": "Pamela Horton", "fr": ""},
"level": 0,
"meta": {},
"modified-at": "2017-05-21T00:00:00Z",
Expand Down Expand Up @@ -1158,7 +1166,7 @@
Argue move appear catch toward help wind. Material minute ago get.\',\'\',\'\']), "parent_id" = NULL WHERE "emeis_core_scope"."id" = \'9dd4e461-268c-8034-f5c8-564e155c67a6\'::uuid""",
],
"request": {
"CONTENT_LENGTH": "516",
"CONTENT_LENGTH": "567",
"CONTENT_TYPE": "application/vnd.api+json",
"PATH_INFO": "/api/v1/scopes/9dd4e461-268c-8034-f5c8-564e155c67a6",
"QUERY_STRING": "",
Expand All @@ -1175,6 +1183,7 @@
Argue move appear catch toward help wind. Material minute ago get.""",
"fr": "",
},
"full-name": {"de": "", "en": "Pamela Horton", "fr": ""},
"level": 0,
"meta": {},
"modified-at": "2017-05-21T00:00:00Z",
Expand Down Expand Up @@ -1482,7 +1491,7 @@
Argue move appear catch toward help wind. Material minute ago get.','','']), NULL, 1, 2, 1, 0)""",
],
"request": {
"CONTENT_LENGTH": "516",
"CONTENT_LENGTH": "567",
"CONTENT_TYPE": "application/vnd.api+json",
"PATH_INFO": "/api/v1/scopes",
"QUERY_STRING": "",
Expand All @@ -1499,6 +1508,7 @@
Argue move appear catch toward help wind. Material minute ago get.""",
"fr": "",
},
"full-name": {"de": "", "en": "Pamela Horton", "fr": ""},
"level": 0,
"meta": {},
"modified-at": "2017-05-21T00:00:00Z",
Expand Down
13 changes: 13 additions & 0 deletions emeis/core/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ def test_scope_search_filter(db, scope_factory, client):
assert len(result["data"]) == 3


def test_scope_full_name_api(db, scope_factory, client):
parent = scope_factory()
child = scope_factory(parent=parent)

url = reverse("scope-detail", args=[child.pk])
response = client.get(url)

full_name = response.json()["data"]["attributes"]["full-name"]
assert full_name["en"] == child.full_name(language="en")
assert full_name["en"].startswith(parent.name["en"])
assert full_name["en"].endswith(child.name["en"])


def test_cannot_write_level(db, client, settings, user):
data = {
"data": {
Expand Down

0 comments on commit 3465b97

Please sign in to comment.