Skip to content

Commit

Permalink
improve snippets app
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijiasun committed Jan 23, 2014
1 parent 25826bb commit 4a6c6f6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
4 changes: 2 additions & 2 deletions polls/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

urlpatterns = patterns('',
url(r'^$',views.index,name='index'),
url(r'^(?P<poll_id>\d+)/$', view.detail,name='detail'),
url(r'^(?P<poll_id>\d+)/results/$',view.results,name='results'),
url(r'^(?P<poll_id>\d+)/$', views.detail,name='detail'),
url(r'^(?P<poll_id>\d+)/results/$',views.results,name='results'),
)
6 changes: 3 additions & 3 deletions snippets/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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',)

4 changes: 2 additions & 2 deletions snippets/urls.py
Original file line number Diff line number Diff line change
@@ -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<pk>[0-9]+)/$','snippet_detail'),
)
34 changes: 33 additions & 1 deletion snippets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -36,6 +36,7 @@ def snippet_list(request):
return JSONResponse(serializer.error,status=400)


"""
@csrf_exempt
def snippet_detail(request,pk):
print '!!!!'
Expand Down Expand Up @@ -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

0 comments on commit 4a6c6f6

Please sign in to comment.