mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-04-14 20:10:59 +00:00
Refactored detail_url calculation into function
This commit is contained in:
parent
de689215b0
commit
5733f16002
1 changed files with 10 additions and 3 deletions
|
|
@ -16,6 +16,13 @@ from wagtail.wagtailcore import fields as wagtailcore_fields
|
|||
from .utils import get_full_url, pages_for_site
|
||||
|
||||
|
||||
def get_object_detail_url(context, model, pk):
|
||||
url_path = context['router'].get_object_detail_urlpath(model, pk)
|
||||
|
||||
if url_path:
|
||||
return get_full_url(context['request'], url_path)
|
||||
|
||||
|
||||
class MetaField(Field):
|
||||
"""
|
||||
Serializes the "meta" section of each object.
|
||||
|
|
@ -35,7 +42,7 @@ class MetaField(Field):
|
|||
def to_representation(self, obj):
|
||||
return OrderedDict([
|
||||
('type', type(obj)._meta.app_label + '.' + type(obj).__name__),
|
||||
('detail_url', get_full_url(self.context['request'], self.context['router'].get_object_detail_urlpath(type(obj), obj.pk))),
|
||||
('detail_url', get_object_detail_url(self.context, type(obj), obj.pk)),
|
||||
])
|
||||
|
||||
|
||||
|
|
@ -55,7 +62,7 @@ class PageMetaField(MetaField):
|
|||
def to_representation(self, page):
|
||||
return OrderedDict([
|
||||
('type', page.specific_class._meta.app_label + '.' + page.specific_class.__name__),
|
||||
('detail_url', get_full_url(self.context['request'], self.context['router'].get_object_detail_urlpath(type(page), page.pk))),
|
||||
('detail_url', get_object_detail_url(self.context, type(page), page.pk)),
|
||||
])
|
||||
|
||||
|
||||
|
|
@ -74,7 +81,7 @@ class DocumentMetaField(MetaField):
|
|||
def to_representation(self, document):
|
||||
data = OrderedDict([
|
||||
('type', "wagtaildocs.Document"),
|
||||
('detail_url', get_full_url(self.context['request'], self.context['router'].get_object_detail_urlpath(type(document), document.pk))),
|
||||
('detail_url', get_object_detail_url(self.context, type(document), document.pk)),
|
||||
])
|
||||
|
||||
# Add download url
|
||||
|
|
|
|||
Loading…
Reference in a new issue