aboutsummaryrefslogtreecommitdiffstats
path: root/examples/pygments_api
diff options
context:
space:
mode:
authorTom Christie2011-04-27 18:07:28 +0100
committerTom Christie2011-04-27 18:07:28 +0100
commit028851bfa1ee44b8e92808b18d32278d4a473cc8 (patch)
tree7a9497fef627d83bbc75dbd21294ff813216c828 /examples/pygments_api
parent762a52edde09297e87c640797219c9bb8255d50a (diff)
downloaddjango-rest-framework-028851bfa1ee44b8e92808b18d32278d4a473cc8.tar.bz2
Fix up tests and examples after refactoring
Diffstat (limited to 'examples/pygments_api')
-rw-r--r--examples/pygments_api/views.py24
1 files changed, 11 insertions, 13 deletions
diff --git a/examples/pygments_api/views.py b/examples/pygments_api/views.py
index 6fb9217a..f1a89702 100644
--- a/examples/pygments_api/views.py
+++ b/examples/pygments_api/views.py
@@ -41,26 +41,25 @@ 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."""
form = PygmentsForm
- allowed_methods = anon_allowed_methods = ('GET', 'POST',)
- def get(self, request, auth):
+ def get(self, request):
"""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, auth, content):
+ 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."""
unique_id = str(uuid.uuid1())
pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)
- lexer = get_lexer_by_name(content['lexer'])
- linenos = 'table' if content['linenos'] else False
- options = {'title': content['title']} if content['title'] else {}
- formatter = HtmlFormatter(style=content['style'], linenos=linenos, full=True, **options)
+ lexer = get_lexer_by_name(self.CONTENT['lexer'])
+ linenos = 'table' if self.CONTENT['linenos'] else False
+ options = {'title': self.CONTENT['title']} if self.CONTENT['title'] else {}
+ formatter = HtmlFormatter(style=self.CONTENT['style'], linenos=linenos, full=True, **options)
with open(pathname, 'w') as outfile:
- highlight(content['code'], lexer, formatter, outfile)
+ highlight(self.CONTENT['code'], lexer, formatter, outfile)
remove_oldest_files(HIGHLIGHTED_CODE_DIR, MAX_FILES)
@@ -70,20 +69,19 @@ class PygmentsRoot(Resource):
class PygmentsInstance(Resource):
"""Simply return the stored highlighted HTML file with the correct mime type.
This Resource only emits HTML and uses a standard HTML emitter rather than the emitters.DocumentingHTMLEmitter class."""
- allowed_methods = anon_allowed_methods = ('GET',)
emitters = (HTMLEmitter,)
- def get(self, request, auth, unique_id):
+ def get(self, request, unique_id):
"""Return the highlighted snippet."""
pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)
if not os.path.exists(pathname):
- return Resource(status.HTTP_404_NOT_FOUND)
+ return Response(status.HTTP_404_NOT_FOUND)
return open(pathname, 'r').read()
- def delete(self, request, auth, unique_id):
+ def delete(self, request, unique_id):
"""Delete the highlighted snippet."""
pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)
if not os.path.exists(pathname):
- return Resource(status.HTTP_404_NOT_FOUND)
+ return Response(status.HTTP_404_NOT_FOUND)
return os.remove(pathname)