File tree Expand file tree Collapse file tree 8 files changed +27
-30
lines changed
Expand file tree Collapse file tree 8 files changed +27
-30
lines changed Original file line number Diff line number Diff line change 11language : python
22python :
3- - " 2.7"
4- - " 3.8"
3+ - " 3.7"
54env :
6- - DJANGO_VERSION=1.7
7- - DJANGO_VERSION=1.8
8- - DJANGO_VERSION=1.9
9- - DJANGO_VERSION=1.10
105 - DJANGO_VERSION=2.0
116 - DJANGO_VERSION=3.0
127install :
13- - pip install -q Django==$DJANGO_VERSION flake8 coverage
8+ - pip install -q Django==$DJANGO_VERSION flake8 coverage djangorestframework
149script :
1510 - flake8 --exclude vote/migrations/* vote
1611 - coverage run runtests.py
17- matrix :
18- exclude :
19- - python : " 3.8"
20- env : DJANGO_VERSION=1.7
21- - python : " 2.7"
22- env : DJANGO_VERSION=2.0
23- - python : " 2.7"
24- env : DJANGO_VERSION=3.0
2512
2613after_success :
2714 - bash <(curl -s https://codecov.io/bash)
Original file line number Diff line number Diff line change @@ -71,4 +71,16 @@ Review.votes.all(user_id)
7171
7272```
7373
74- `` django-vote `` now requires Django 1.7 or greater. (for Django < 1.7, please install previous release ` django-vote==1.1.3 ` )
74+ #### Use ` VoteMixin ` for REST API
75+
76+ ``` python
77+ class CommentViewSet (ModelViewSet , VoteMixin ):
78+ queryset = Comment.objects.all()
79+ serializer_class = CommentSerializer
80+ ```
81+
82+ ``` sh
83+ POST /api/comments/{id}/vote/
84+ POST /api/comments/{id}/vote/ {" action" :" down" }
85+ DELETE /api/comments/{id}/vote/
86+ ```
Original file line number Diff line number Diff line change @@ -5,7 +5,8 @@ Changelog
55------------------
66* Add post_vote signal
77* Add VoteMixin for easily write vote api
8- * Support Django 3.0
8+ * Drop support for Django < 2.0
9+ * Add Django 3.0 test
910
10112.1.7(2018.05.08)
1112-----------------
Original file line number Diff line number Diff line change 11{% load vote %}
22{% for comment in comments %}
33{{comment.content}} {% vote_exists comment user %}
4- {% endfor %}
4+ {% endfor %}
Original file line number Diff line number Diff line change @@ -17,6 +17,9 @@ def setUp(self):
1717 '111111' )
1818 self .
user3 = User .
objects .
create_user (
"test3" ,
"[email protected] " ,
1919 '111111' )
20+ print ('\n %s.%s.%s' % (self .__class__ .__module__ ,
21+ self .__class__ .__name__ ,
22+ self ._testMethodName ))
2023
2124 def tearDown (self ):
2225 self .model .objects .all ().delete ()
@@ -210,7 +213,9 @@ def test_vote_templatetag(self):
210213
211214 comment1 = comments [0 ]
212215 self .call_api ('up' , comment1 , self .user1 .pk )
213- res = self .client .get ('/comments/' )
216+ # test with anonymous user
217+ self .client .get ('/comments/' )
218+ # test with authenticated user
214219 self .client .login (username = self .user1 .username , password = '111111' )
215220 self .client .get ('/comments/' )
216221
@@ -220,8 +225,6 @@ def test_votemixin_api(self):
220225 content = "I'm a comment" )
221226 comment .save ()
222227 self .assertEqual (comment .num_vote_up , 0 )
223- import pdb
224- pdb .set_trace ()
225228 self .client .login (
226229 username = self .user1 .username , password = '111111' )
227230
Original file line number Diff line number Diff line change 66router .register (r'comments' , views .CommentViewSet )
77
88urlpatterns = [
9- path ('comments' , views .comments ),
9+ path ('comments/ ' , views .comments ),
1010 path ('api/' , include (router .urls )),
1111]
Original file line number Diff line number Diff line change 22
33from django import template
44from django .contrib .auth .models import AnonymousUser
5- from django import get_version
65
76from vote .models import UP
87
1110
1211@register .simple_tag
1312def vote_exists (model , user = AnonymousUser (), action = UP ):
14- if get_version () >= '2.0' :
15- if user .is_anonymous :
16- return False
17- elif user .is_anonymous ():
13+ if user .is_anonymous :
1814 return False
1915 return model .votes .exists (user .pk , action = action )
Original file line number Diff line number Diff line change 1- from django .db import IntegrityError
21from rest_framework .decorators import action
3- from rest_framework .views import APIView , Response
4- from rest_framework .viewsets import ModelViewSet
2+ from rest_framework .views import Response
53
64from vote .signals import post_voted
75
You can’t perform that action at this time.
0 commit comments