From 4a6c6f614ff6dae7a6d728f9a458d8fa41621282 Mon Sep 17 00:00:00 2001 From: zhijiasun Date: Fri, 24 Jan 2014 07:00:56 +0800 Subject: [PATCH] improve snippets app --- polls/urls.py | 4 ++-- snippets/models.py | 6 +++--- snippets/urls.py | 4 ++-- snippets/views.py | 34 +++++++++++++++++++++++++++++++++- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/polls/urls.py b/polls/urls.py index 0f466cf..ce1e72d 100644 --- a/polls/urls.py +++ b/polls/urls.py @@ -3,6 +3,6 @@ urlpatterns = patterns('', url(r'^$',views.index,name='index'), - url(r'^(?P\d+)/$', view.detail,name='detail'), - url(r'^(?P\d+)/results/$',view.results,name='results'), + url(r'^(?P\d+)/$', views.detail,name='detail'), + url(r'^(?P\d+)/results/$',views.results,name='results'), ) \ No newline at end of file diff --git a/snippets/models.py b/snippets/models.py index 567a674..ce880bd 100644 --- a/snippets/models.py +++ b/snippets/models.py @@ -2,7 +2,7 @@ from pygments.lexers import get_all_lexers from pygments.styles import get_all_styles -from django.contrib.auth.models import User +#from django.contrib.auth.models import User LEXERS = [item for item in get_all_lexers() if item[1]] LANGUAGE_CHOICES = sorted([(item[1][0],item[0]) for item in LEXERS]) @@ -18,6 +18,6 @@ class Snippet(models.Model): language = models.CharField(choices=LANGUAGE_CHOICES,default='python',max_length=100) style = models.CharField(choices=STYLE_CHOICES,default='friendly',max_length=100) -class Meta: - ordering = ('created',) + class Meta: + ordering = ('created',) diff --git a/snippets/urls.py b/snippets/urls.py index ca50a4b..c70d24c 100644 --- a/snippets/urls.py +++ b/snippets/urls.py @@ -1,7 +1,7 @@ from django.conf.urls import patterns,url urlpatterns = patterns('snippets.views', - url(r'^index/$','snippet_index'), - url(r'^snippets/$','snippet_list'), + #url(r'^index/$','snippet_index'), + url(r'^snippets/$','snippet_list2'), url(r'snippets/(?P[0-9]+)/$','snippet_detail'), ) diff --git a/snippets/views.py b/snippets/views.py index 6f11c29..1578410 100644 --- a/snippets/views.py +++ b/snippets/views.py @@ -21,7 +21,7 @@ def __init__(self,data,**kwargs): @csrf_exempt -def snippet_list(request): +def snippet_list2(request): if request.method == 'GET': snippets = Snippet.objects.all() serializer = SnippetSerializer(snippets,many=True) @@ -36,6 +36,7 @@ def snippet_list(request): return JSONResponse(serializer.error,status=400) +""" @csrf_exempt def snippet_detail(request,pk): print '!!!!' @@ -66,3 +67,34 @@ def snippet_detail(request,pk): snippet.delete() return HttpResponse(status=204) +""" + +""" +refactor the views.py +""" +from rest_framework import status +from rest_framework.decorators import api_view +from rest_framework.response import Response +from snippets.models import Snippet +from snippets.serializers import SnippetSerializer + + +@api_view(['GET','POST']) +def snippet_list(request): + if request.method == 'GET': + snippets = Snippet.objects.all() + serializer = SnippetSerializer(snippets,many=True) + return Response(serializer.data) + elif request.method == 'POST': + serializer = SnippetSerializer(data=request.DATA) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data,status=status.HTTP_201_CREATED) + return Response(serializer.errors,status=status.HTTP_404_BAD_REQUEST) + + + + +@api_view(['GET','PUT','DELETE']) +def snippet_detail(request,pk): + pass \ No newline at end of file