Add API to list all templates

Remove newlines from the rendered templates.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2019-04-02 22:37:46 -04:00
parent 33a542b9d5
commit f45384e399
4 changed files with 41 additions and 6 deletions

View File

@@ -17,7 +17,7 @@ class APIContentTypeList(generics.ListAPIView):
queryset = ContentType.objects.order_by('app_label', 'model')
class APITemplateView(generics.RetrieveAPIView):
class APITemplateDetailView(generics.RetrieveAPIView):
"""
Returns the selected partial template details.
get: Retrieve the details of the partial template.
@@ -27,3 +27,14 @@ class APITemplateView(generics.RetrieveAPIView):
def get_object(self):
return Template.get(self.kwargs['name']).render(request=self.request)
class APITemplateListView(generics.ListAPIView):
"""
Returns a list of all the available templates.
"""
serializer_class = TemplateSerializer
permission_classes = (IsAuthenticated,)
def get_queryset(self):
return Template.all(rendered=True, request=self.request)

View File

@@ -371,6 +371,16 @@ class PropertyHelper(object):
class Template(object):
_registry = {}
@classmethod
def all(cls, rendered=False, request=None):
if not rendered:
return cls._registry.values()
else:
result = []
for template in cls._registry.values():
result.append(template.render(request=request))
return result
@classmethod
def get(cls, name):
return cls._registry[name]
@@ -381,7 +391,9 @@ class Template(object):
self.__class__._registry[name] = self
def get_absolute_url(self):
return reverse('rest_api:template-detail', args=(self.name,))
return reverse(
kwargs={'name': self.name}, viewname='rest_api:template-detail'
)
def render(self, request):
context = {
@@ -393,8 +405,10 @@ class Template(object):
context=context,
).render()
self.html = result.rendered_content
self.hex_hash = hashlib.sha256(result.content).hexdigest()
content = result.content.replace('\n', '')
self.html = content
self.hex_hash = hashlib.sha256(content).hexdigest()
return self

View File

@@ -15,3 +15,7 @@ class TemplateSerializer(serializers.Serializer):
hex_hash = serializers.CharField(read_only=True)
name = serializers.CharField(read_only=True)
html = serializers.CharField(read_only=True)
url = serializers.HyperlinkedIdentityField(
lookup_field='name', lookup_url_kwarg='name',
view_name='rest_api:template-detail'
)

View File

@@ -3,7 +3,9 @@ from __future__ import unicode_literals
from django.conf.urls import url
from django.views.i18n import javascript_catalog, set_language
from .api_views import APIContentTypeList, APITemplateView
from .api_views import (
APIContentTypeList, APITemplateDetailView, APITemplateListView
)
from .views import (
AboutView, CheckVersionView, CurrentUserDetailsView, CurrentUserEditView,
CurrentUserLocaleProfileDetailsView, CurrentUserLocaleProfileEditView,
@@ -78,7 +80,11 @@ api_urls = [
name='content-type-list'
),
url(
r'^templates/(?P<name>[-\w]+)/$', APITemplateView.as_view(),
r'^templates/$', APITemplateListView.as_view(),
name='template-list'
),
url(
r'^templates/(?P<name>[-\w]+)/$', APITemplateDetailView.as_view(),
name='template-detail'
),
]