Hi,
I know it's going to be rewritten soon. And I know I should launch the
basic test cases before sending a patch. But to be honest, I've tried
to launch the tests without much success (i.e. I could not launch
them...). Furthermore I would like to be sure I'm not wrong before
contributing a patch.
When using the karma function for the first time I got the following errors:
'KarmaScoreManager' object has no attribute 'objects'
So i just had a look at the code :-P
Here is a list of the problems I found and their corrections (patch
with all errors at the end):
In django/contrib/comments/models.py, I change the line 212:
karma = self.objects.get(comment__pk=comment_id, user__pk=user_id)
to:
karma = self.get(comment__pk=comment_id, user__pk=user_id)
and it works perfectly.
I also identified this in line 157:
return self._karma_total_good + self._karma_total_bad
that should be:
return self._karma_total_good - self._karma_total_bad
indee, self._karma_total_bad is always a positive number, so the
total, to be meagninful, should be with the minus. Otherwise we get
the total number of karma votes, instead of the total karma.
In the line 136:
for k in self.karmascore_set:
should be:
for k in self.karmascore_set.all():
Otherwise I have a related manager over which I can not iterate.
Finally, in line 157:
if not hasattr(self, "_karma_total_bad") or not hasattr(self,
"_karma_total_good"):
self._fill_karma_cache()
should be:
self._fill_karma_cache()
Otherwise, the karma count does not actualise itself for each comment
and all comments have the same karma count.
Ok, hope it helps, and please tell me if something is wrong here.
G
The patch:
***
BEGIN PATCH
***
Index: contrib/comments/models.py
===
--- contrib/comments/models.py (revision 3910)
+++ contrib/comments/models.py (working copy)
@@ -133,7 +133,7 @@
def _fill_karma_cache(self):
"Helper function that populates good/bad karma caches"
good, bad = 0, 0
-for k in self.karmascore_set:
+for k in self.karmascore_set.all():
if k.score == -1:
bad +=1
elif k.score == 1:
@@ -151,9 +151,8 @@
return self._karma_total_bad
def get_karma_total(self):
-if not hasattr(self, "_karma_total_good") or not
hasattr(self, "_karma_total_bad"):
-self._fill_karma_cache()
-return self._karma_total_good + self._karma_total_bad
+self._fill_karma_cache()
+return self._karma_total_good - self._karma_total_bad
def get_as_text(self):
return _('Posted by %(user)s at
%(date)s\n\n%(comment)s\n\nhttp://%(domain)s%(url)s') % \
@@ -209,7 +208,7 @@
class KarmaScoreManager(models.Manager):
def vote(self, user_id, comment_id, score):
try:
-karma = self.objects.get(comment__pk=comment_id, user__pk=user_id)
+karma = self.get(comment__pk=comment_id, user__pk=user_id)
except self.model.DoesNotExist:
karma = self.model(None, user_id=user_id,
comment_id=comment_id, score=score,
scored_date=datetime.datetime.now())
karma.save()
***
END PATCH
***
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-developers
-~--~~~~--~~--~--~---