| |
Django developers |
Hi, I know it's going to be rewritten soon. And I know I should launch the When using the karma function for the first time I got the following errors: So i just had a look at the code :-P Here is a list of the problems I found and their corrections (patch In django/contrib/comments/models.py, I change the line 212: I also identified this in line 157: In the line 136: Finally, in line 157: Ok, hope it helps, and please tell me if something is wrong here. G The patch: =================================================================== --- contrib/comments/models.py (revision 3910) +++ contrib/comments/models.py (working copy) @@ -133,7 +133,7 @@ def _fill_karma_cache(self): return self._karma_total_bad def get_karma_total(self): def get_as_text(self): class KarmaScoreManager(models.Manager):
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.
'KarmaScoreManager' object has no attribute 'objects'
with all errors at the end):
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.
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.
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.
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.
*************************************************************************** ********************************
BEGIN PATCH
*************************************************************************** ********************************
Index: contrib/comments/models.py
"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 @@
- 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
return _('Posted by %(user)s at
%(date)s\n\n%(comment)s\n\nhttp://%(domain)s%(url)s') % \
@@ -209,7 +208,7 @@
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
*************************************************************************** ********************************