Commit b4c07052 authored by Michal Čihař's avatar Michal Čihař

Include self-links in all API objects

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent ae4582b9
...@@ -24,6 +24,29 @@ from weblate.trans.models import Project, SubProject, Translation ...@@ -24,6 +24,29 @@ from weblate.trans.models import Project, SubProject, Translation
from weblate.lang.models import Language from weblate.lang.models import Language
class MultiFieldHyperlinkedIdentityField(serializers.HyperlinkedIdentityField):
def get_url(self, obj, view_name, request, format):
"""
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.
"""
# Unsaved objects will not yet have a valid URL.
if hasattr(obj, 'pk') and obj.pk is None:
return None
kwargs = {}
for lookup in self.lookup_field:
value = obj
for key in lookup.split('__'):
value = getattr(value, key)
kwargs[lookup] = value
return self.reverse(
view_name, kwargs=kwargs, request=request, format=format
)
class LanguageSerializer(serializers.ModelSerializer): class LanguageSerializer(serializers.ModelSerializer):
web_url = serializers.CharField(source='get_absolute_url', read_only=True) web_url = serializers.CharField(source='get_absolute_url', read_only=True)
...@@ -62,25 +85,45 @@ class ComponentSerializer(serializers.ModelSerializer): ...@@ -62,25 +85,45 @@ class ComponentSerializer(serializers.ModelSerializer):
web_url = serializers.CharField(source='get_absolute_url', read_only=True) web_url = serializers.CharField(source='get_absolute_url', read_only=True)
project = ProjectSerializer(read_only=True) project = ProjectSerializer(read_only=True)
serializer_url_field = MultiFieldHyperlinkedIdentityField
class Meta(object): class Meta(object):
model = SubProject model = SubProject
fields = ( fields = (
'id', 'name', 'slug', 'project', 'vcs', 'repo', 'git_export', 'name', 'slug', 'project', 'vcs', 'repo', 'git_export',
'branch', 'filemask', 'template', 'file_format', 'license', 'branch', 'filemask', 'template', 'file_format', 'license',
'license_url', 'web_url', 'license_url', 'web_url', 'url',
) )
extra_kwargs = {
'url': {
'view_name': 'api:component-detail',
'lookup_field': ('project__slug', 'slug'),
}
}
class TranslationSerializer(serializers.ModelSerializer): class TranslationSerializer(serializers.ModelSerializer):
web_url = serializers.CharField(source='get_absolute_url', read_only=True) web_url = serializers.CharField(source='get_absolute_url', read_only=True)
subproject = ComponentSerializer(read_only=True) component = ComponentSerializer(read_only=True, source='subproject')
language = LanguageSerializer(read_only=True) language = LanguageSerializer(read_only=True)
serializer_url_field = MultiFieldHyperlinkedIdentityField
class Meta(object): class Meta(object):
model = Translation model = Translation
fields = ( fields = (
'id', 'language', 'subproject', 'translated', 'fuzzy', 'total', 'language', 'component', 'translated', 'fuzzy', 'total',
'translated_words', 'fuzzy_words', 'failing_checks_words', 'translated_words', 'fuzzy_words', 'failing_checks_words',
'total_words', 'failing_checks', 'have_suggestion', 'have_comment', 'total_words', 'failing_checks', 'have_suggestion', 'have_comment',
'language_code', 'filename', 'revision', 'web_url', 'language_code', 'filename', 'revision', 'web_url', 'url',
) )
extra_kwargs = {
'url': {
'view_name': 'api:translation-detail',
'lookup_field': (
'subproject__project__slug',
'subproject__slug',
'language__code',
),
}
}
...@@ -35,7 +35,8 @@ router.register( ...@@ -35,7 +35,8 @@ router.register(
) )
router.register( router.register(
r'components/(?P<project__slug>[^/]+)', r'components/(?P<project__slug>[^/]+)',
ComponentViewSet ComponentViewSet,
'component',
) )
router.register( router.register(
r'translations/' r'translations/'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment