Skip to content
Snippets Groups Projects
Commit ad5386b8 authored by Andreas Hubel's avatar Andreas Hubel
Browse files

chore(room-api): add public_url

parent fc7bcd9b
No related branches found
No related tags found
No related merge requests found
......@@ -51,6 +51,42 @@ class ParameterisedHyperlinkedIdentityField(HyperlinkedIdentityField):
return reverse(view_name, kwargs=kwargs, request=request, format=format)
class HubHyperlinkedIdentityField(HyperlinkedIdentityField):
"""
Represents the instance, or a property on the instance, using hyperlinking.
lookup_fields is a tuple of tuples of the form:
('model_field', 'url_parameter')
taken from https://github.com/encode/django-rest-framework/issues/1024
"""
lookup_fields = (('pk', 'pk'),)
def __init__(self, *args, **kwargs):
self.lookup_fields = kwargs.pop('lookup_fields', self.lookup_fields)
super().__init__(*args, **kwargs)
def get_url(self, obj, view_name, request, format): # pylint: disable=redefined-builtin
"""
Given an object, return the URL that hyperlinks to the object.
May raise a `NoReverseMatch` if the `view_name` and `lookup_field`
attributes are not configured to correctly match the URL conf.
"""
kwargs = {}
for model_field, url_param in self.lookup_fields:
attr = obj
for field in model_field.split('.'):
attr = getattr(attr, field)
kwargs[url_param] = attr
from core.templatetags.hub_absolute import hub_absolute # pylint: disable=import-outside-toplevel
# TODO: add request=request, format=format,
return hub_absolute(view_name, **kwargs, i18n=False)
class LinkRelatedField(serializers.RelatedField):
"""
A read only field that represents its targets using their
......@@ -237,6 +273,8 @@ class RoomSerializer(HubModelSerializer):
read_only=True,
)
public_url = HubHyperlinkedIdentityField(view_name='plainui:room', lookup_fields=(('slug', 'slug'),))
class Meta:
model = Room
read_only_fields = ['id']
......@@ -251,6 +289,7 @@ class RoomSerializer(HubModelSerializer):
'assembly',
'links',
'backend_link',
'public_url',
]
staff_only_fields = ['blocked', 'backend_link']
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment