Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Bugs in karma code
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Guillermo Fernandez Castellanos  
View profile  
 More options Oct 21 2006, 1:07 pm
From: "Guillermo Fernandez Castellanos" <guillermo.fernandez.castella...@gmail.com>
Date: Sat, 21 Oct 2006 19:07:00 +0200
Subject: Bugs in karma code
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
*************************************************************************** ********************************


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google