Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Django 1.0 compatibility updates - tests work again

git-svn-id: https://django-voting.googlecode.com/svn/trunk@65 662f01ad-f42a-0410-a340-718c64ddaef4
  • Loading branch information...
commit b0d84c2c4499491a71830383398e142f3ac3d0b6 1 parent 6d03beb
@insin insin authored
View
4 voting/admin.py
@@ -0,0 +1,4 @@
+from django.contrib import admin
+from voting.models import Vote
+
+admin.site.register(Vote)
View
7 voting/managers.py
@@ -100,16 +100,15 @@ def get_top(self, Model, limit=10, reversed=False):
else:
having_score = 'SUM(vote)'
if reversed:
- having_sql = ' HAVING %(having_score)s < 0 ORDER BY %(having_score)s ASC %(limit_offset)s'
+ having_sql = ' HAVING %(having_score)s < 0 ORDER BY %(having_score)s ASC LIMIT %%s'
else:
- having_sql = ' HAVING %(having_score)s > 0 ORDER BY %(having_score)s DESC %(limit_offset)s'
+ having_sql = ' HAVING %(having_score)s > 0 ORDER BY %(having_score)s DESC LIMIT %%s'
query += having_sql % {
'having_score': having_score,
- 'limit_offset': connection.ops.limit_offset_sql(limit),
}
cursor = connection.cursor()
- cursor.execute(query, [ctype.id])
+ cursor.execute(query, [ctype.id, limit])
results = cursor.fetchall()
# Use in_bulk() to avoid O(limit) db hits.
View
3  voting/models.py
@@ -27,9 +27,6 @@ class Meta:
# One vote per user per object
unique_together = (('user', 'content_type', 'object_id'),)
- class Admin:
- pass
-
def __unicode__(self):
return u'%s: %s on %s' % (self.user, self.vote, self.object)
View
4 voting/tests/models.py
@@ -1,10 +1,10 @@
from django.db import models
class Item(models.Model):
- name = models.CharField(maxlength=50)
+ name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Meta:
- ordering = ['name']
+ ordering = ['name']
View
5 voting/tests/settings.py
@@ -1,7 +1,6 @@
import os
-DIRNAME = os.path.dirname(__file__)
-DEFAULT_CHARSET = 'utf-8'
+DIRNAME = os.path.dirname(__file__)
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = os.path.join(DIRNAME, 'database.db')
@@ -25,4 +24,4 @@
'django.contrib.contenttypes',
'voting',
'voting.tests',
-)
+)
View
1  voting/tests/tests.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
r"""
>>> from django.contrib.auth.models import User
>>> from voting.models import Vote
View
39 voting/views.py
@@ -34,9 +34,7 @@ def vote_on_object(request, model, direction, post_vote_redirect=None,
direction
The type of vote which will be registered for the object.
"""
- if (allow_xmlhttprequest and
- request.META.has_key('HTTP_X_REQUESTED_WITH') and
- request.META['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'):
+ if allow_xmlhttprequest and request.is_ajax():
return xmlhttprequest_vote_on_object(request, model, direction,
object_id=object_id, slug=slug,
slug_field=slug_field)
@@ -48,7 +46,7 @@ def vote_on_object(request, model, direction, post_vote_redirect=None,
try:
vote = dict(VOTE_DIRECTIONS)[direction]
except KeyError:
- raise AttributeError('\'%s\' is not a valid vote type.' % vote_type)
+ raise AttributeError("'%s' is not a valid vote type." % vote_type)
# Look up the object to be voted on
lookup_kwargs = {}
@@ -57,7 +55,8 @@ def vote_on_object(request, model, direction, post_vote_redirect=None,
elif slug and slug_field:
lookup_kwargs['%s__exact' % slug_field] = slug
else:
- raise AttributeError('Generic vote view must be called with either object_id slug/slug_field.')
+ raise AttributeError('Generic vote view must be called with either '
+ 'object_id or slug and slug_field.')
try:
obj = model._default_manager.get(**lookup_kwargs)
except ObjectDoesNotExist:
@@ -74,12 +73,17 @@ def vote_on_object(request, model, direction, post_vote_redirect=None,
else:
next = obj.get_absolute_url
else:
- raise AttributeError('Generic vote view must be called with either post_vote_redirect, a "next" parameter in the request, or the object being voted on must define a get_absolute_url method or property.')
+ raise AttributeError('Generic vote view must be called with either '
+ 'post_vote_redirect, a "next" parameter in '
+ 'the request, or the object being voted on '
+ 'must define a get_absolute_url method or '
+ 'property.')
Vote.objects.record_vote(obj, request.user, vote)
return HttpResponseRedirect(next)
else:
if not template_name:
- template_name = '%s/%s_confirm_vote.html' % (model._meta.app_label, model._meta.object_name.lower())
+ template_name = '%s/%s_confirm_vote.html' % (
+ model._meta.app_label, model._meta.object_name.lower())
t = template_loader.get_template(template_name)
c = RequestContext(request, {
template_object_name: obj,
@@ -93,7 +97,7 @@ def vote_on_object(request, model, direction, post_vote_redirect=None,
response = HttpResponse(t.render(c))
return response
-def json_error_response(error_message, *args, **kwargs):
+def json_error_response(error_message):
return HttpResponse(simplejson.dumps(dict(success=False,
error_message=error_message)))
@@ -114,14 +118,16 @@ def xmlhttprequest_vote_on_object(request, model, direction,
processed.
"""
if request.method == 'GET':
- return json_error_response('XMLHttpRequest votes can only be made using POST.')
+ return json_error_response(
+ 'XMLHttpRequest votes can only be made using POST.')
if not request.user.is_authenticated():
return json_error_response('Not authenticated.')
try:
vote = dict(VOTE_DIRECTIONS)[direction]
except KeyError:
- return json_error_response('\'%s\' is not a valid vote type.' % direction)
+ return json_error_response(
+ '\'%s\' is not a valid vote type.' % direction)
# Look up the object to be voted on
lookup_kwargs = {}
@@ -130,13 +136,18 @@ def xmlhttprequest_vote_on_object(request, model, direction,
elif slug and slug_field:
lookup_kwargs['%s__exact' % slug_field] = slug
else:
- return json_error_response('Generic XMLHttpRequest vote view must be called with either object_id or slug/slug_field.')
+ return json_error_response('Generic XMLHttpRequest vote view must be '
+ 'called with either object_id or slug and '
+ 'slug_field.')
try:
obj = model._default_manager.get(**lookup_kwargs)
except ObjectDoesNotExist:
- return json_error_response('No %s found for %s.' % (model._meta.verbose_name, lookup_kwargs))
+ return json_error_response(
+ 'No %s found for %s.' % (model._meta.verbose_name, lookup_kwargs))
# Vote and respond
Vote.objects.record_vote(obj, request.user, vote)
- return HttpResponse(simplejson.dumps({'success': True,
- 'score': Vote.objects.get_score(obj)}))
+ return HttpResponse(simplejson.dumps({
+ 'success': True,
+ 'score': Vote.objects.get_score(obj),
+ }))

0 comments on commit b0d84c2

Please sign in to comment.
Something went wrong with that request. Please try again.