diff options
| author | Tom Christie | 2011-05-16 14:11:36 +0100 | 
|---|---|---|
| committer | Tom Christie | 2011-05-16 14:11:36 +0100 | 
| commit | 1e04790d505a1174f9e3c4481288982f9e7fd6c0 (patch) | |
| tree | 09c5b29acdc820dc3fc1108f3503cde03d725eb9 /examples/pygments_api | |
| parent | e92002ddde31fcc4ba3dee0f4a92a114c3c3a959 (diff) | |
| download | django-rest-framework-1e04790d505a1174f9e3c4481288982f9e7fd6c0.tar.bz2 | |
Fixing some of the last blocking issues
Diffstat (limited to 'examples/pygments_api')
| -rw-r--r-- | examples/pygments_api/views.py | 66 | 
1 files changed, 47 insertions, 19 deletions
diff --git a/examples/pygments_api/views.py b/examples/pygments_api/views.py index 253b0907..e6bfae48 100644 --- a/examples/pygments_api/views.py +++ b/examples/pygments_api/views.py @@ -2,9 +2,10 @@ from __future__ import with_statement  # for python 2.5  from django.conf import settings  from django.core.urlresolvers import reverse -from djangorestframework.resource import Resource +from djangorestframework.resources import FormResource  from djangorestframework.response import Response  from djangorestframework.renderers import BaseRenderer +from djangorestframework.views import BaseView  from djangorestframework import status  from pygments.formatters import HtmlFormatter @@ -17,39 +18,60 @@ import os  import uuid  import operator -# We need somewhere to store the code that we highlight +# We need somewhere to store the code snippets that we highlight  HIGHLIGHTED_CODE_DIR = os.path.join(settings.MEDIA_ROOT, 'pygments')  MAX_FILES = 10 +  def list_dir_sorted_by_ctime(dir): -    """Return a list of files sorted by creation time""" +    """ +    Return a list of files sorted by creation time +    """      filepaths = [os.path.join(dir, file) for file in os.listdir(dir) if not file.startswith('.')] -    return [item[0] for item in sorted([(path, os.path.getctime(path)) for path in filepaths], -                                                     key=operator.itemgetter(1), reverse=False)] +    return [item[0] for item in sorted( [(path, os.path.getctime(path)) for path in filepaths], +                                        key=operator.itemgetter(1), reverse=False) ] +  def remove_oldest_files(dir, max_files): -    """Remove the oldest files in a directory 'dir', leaving at most 'max_files' remaining. -    We use this to limit the number of resources in the sandbox.""" +    """ +    Remove the oldest files in a directory 'dir', leaving at most 'max_files' remaining. +    We use this to limit the number of resources in the sandbox. +    """      [os.remove(path) for path in list_dir_sorted_by_ctime(dir)[max_files:]]  class HTMLRenderer(BaseRenderer): -    """Basic renderer which just returns the content without any further serialization.""" +    """ +    Basic renderer which just returns the content without any further serialization. +    """      media_type = 'text/html' -class PygmentsRoot(Resource): -    """This example demonstrates a simple RESTful Web API aound the awesome pygments library. -    This top level resource is used to create highlighted code snippets, and to list all the existing code snippets.""" + +class PygmentsFormResource(FormResource): +    """ +    """      form = PygmentsForm + +class PygmentsRoot(BaseView): +    """ +    This example demonstrates a simple RESTful Web API aound the awesome pygments library. +    This top level resource is used to create highlighted code snippets, and to list all the existing code snippets. +    """ +    resource = PygmentsFormResource +      def get(self, request): -        """Return a list of all currently existing snippets.""" +        """ +        Return a list of all currently existing snippets. +        """          unique_ids = [os.path.split(f)[1] for f in list_dir_sorted_by_ctime(HIGHLIGHTED_CODE_DIR)]          return [reverse('pygments-instance', args=[unique_id]) for unique_id in unique_ids]      def post(self, request): -        """Create a new highlighed snippet and return it's location. -        For the purposes of the sandbox example, also ensure we delete the oldest snippets if we have > MAX_FILES.""" +        """ +        Create a new highlighed snippet and return it's location. +        For the purposes of the sandbox example, also ensure we delete the oldest snippets if we have > MAX_FILES. +        """          unique_id = str(uuid.uuid1())          pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id) @@ -66,20 +88,26 @@ class PygmentsRoot(Resource):          return Response(status.HTTP_201_CREATED, headers={'Location': reverse('pygments-instance', args=[unique_id])}) -class PygmentsInstance(Resource): -    """Simply return the stored highlighted HTML file with the correct mime type. -    This Resource only renders HTML and uses a standard HTML renderer rather than the renderers.DocumentingHTMLRenderer class.""" +class PygmentsInstance(BaseView): +    """ +    Simply return the stored highlighted HTML file with the correct mime type. +    This Resource only renders HTML and uses a standard HTML renderer rather than the renderers.DocumentingHTMLRenderer class. +    """      renderers = (HTMLRenderer,)      def get(self, request, unique_id): -        """Return the highlighted snippet.""" +        """ +        Return the highlighted snippet. +        """          pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)          if not os.path.exists(pathname):              return Response(status.HTTP_404_NOT_FOUND)          return open(pathname, 'r').read()      def delete(self, request, unique_id): -        """Delete the highlighted snippet.""" +        """ +        Delete the highlighted snippet. +        """          pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)          if not os.path.exists(pathname):              return Response(status.HTTP_404_NOT_FOUND)  | 
