|
List Info
Thread: Just when I think I've got Karma working
|
|
| Just when I think I've got Karma working |

|
2006-10-30 22:43:23 |
With help from Malcolm, Guillermo and others, I thought I
had this
karma thing working. Apparently I do not.
I'm attempting to build my own karma system that will let me
allow
voting on anything, not just comments. The problem is, any
votes after
the first don't appear to do anything. The previous score is
getting
overwritten rather than incremented. This should look pretty
familiar
to anyone who's looked at comments:
class KarmaScoreManager(models.Manager):
def vote(self, user_id, content_type, object_id,
rating):
try:
karma = self.get(content_type=content_type,
object_id=object_id)
except self.model.DoesNotExist:
karma = self.model(None, user_id=user_id,
content_type_id=content_type, object_id=object_id,
score=rating,
scored_date=datetime.datetime.now())
karma.save()
else:
karma.score = rating
karma.scored_date = datetime.datetime.now()
karma.save()
I think the problem is karma.score = rating, but that's
pulled straight
from Comments, which works.
I've tried adding it to itself, both as karma.score =
karma.score +
rating or karma.score += rating, but that returns 0
I also tried karma.score += int(rating), but still didn't
go.
As with the comment karma system, rating must be 1 or -1
Any ideas?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| Just when I think I've got Karma working |

|
2006-10-30 23:31:45 |
Hi,
Just guessing, but if I understood well the error you get,
that's normal.
The KarmaScoreManager is not the amount of karma of a coment
(or in
your case an object). It keeps in the database the karma
score that a
given user gave to a specific comment (or object). As there
is a
single score per user and per object, you can only override
the total
karma.
The key point of the calculation of the karma is in the
Comment model:
def _fill_karma_cache(self):
"Helper function that populates good/bad karma
caches"
good, bad = 0, 0
for k in self.karmascore_set.all():
if k.score == -1:
bad +=1
elif k.score == 1:
good +=1
self._karma_total_good, self._karma_total_bad =
good, bad
For a given Comment, the function of the model will take all
the
KarmaScore for the given comment (self.karmascore_set.all())
and
iterate over them to calculate the total karma of the
Comment (self).
If you want to do a generic karma, you might want to add a
function to
the manipulator that will do just this, take an id and
content_type,
get() the object, find all the KarmaScores that are related
to this
object (self.karmascore_set.all()) and calculate the total
karma by
iterating over those.
Hope it helps,
G
On 10/30/06, baxter gretschpages.com <mail.baxter gmail.com> wrote:
>
> With help from Malcolm, Guillermo and others, I thought
I had this
> karma thing working. Apparently I do not.
>
> I'm attempting to build my own karma system that will
let me allow
> voting on anything, not just comments. The problem is,
any votes after
> the first don't appear to do anything. The previous
score is getting
> overwritten rather than incremented. This should look
pretty familiar
> to anyone who's looked at comments:
>
> class KarmaScoreManager(models.Manager):
> def vote(self, user_id, content_type, object_id,
rating):
> try:
> karma = self.get(content_type=content_type,
> object_id=object_id)
> except self.model.DoesNotExist:
> karma = self.model(None, user_id=user_id,
> content_type_id=content_type, object_id=object_id,
score=rating,
> scored_date=datetime.datetime.now())
> karma.save()
> else:
> karma.score = rating
> karma.scored_date = datetime.datetime.now()
> karma.save()
>
> I think the problem is karma.score = rating, but that's
pulled straight
> from Comments, which works.
>
> I've tried adding it to itself, both as karma.score =
karma.score +
> rating or karma.score += rating, but that returns 0
>
> I also tried karma.score += int(rating), but still
didn't go.
>
> As with the comment karma system, rating must be 1 or
-1
>
> Any ideas?
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| Just when I think I've got Karma working |

|
2006-10-31 14:29:15 |
Guillermo Fernandez Castellanos wrote:
> Hi,
>
> Just guessing, but if I understood well the error you
get, that's normal.
>
> The KarmaScoreManager is not the amount of karma of a
coment (or in
> your case an object). It keeps in the database the
karma score that a
> given user gave to a specific comment (or object). As
there is a
> single score per user and per object, you can only
override the total
> karma.
>
> The key point of the calculation of the karma is in the
Comment model:
> def _fill_karma_cache(self):
> "Helper function that populates good/bad
karma caches"
> good, bad = 0, 0
> for k in self.karmascore_set.all():
> if k.score == -1:
> bad +=1
> elif k.score == 1:
> good +=1
> self._karma_total_good, self._karma_total_bad =
good, bad
>
> For a given Comment, the function of the model will
take all the
> KarmaScore for the given comment
(self.karmascore_set.all()) and
> iterate over them to calculate the total karma of the
Comment (self).
>
> If you want to do a generic karma, you might want to
add a function to
> the manipulator that will do just this, take an id and
content_type,
> get() the object, find all the KarmaScores that are
related to this
> object (self.karmascore_set.all()) and calculate the
total karma by
> iterating over those.
>
> Hope it helps,
>
> G
>
I thought I had taken care of that. Apparently not. I have:
def _fill_karma_cache(self):
good, bad = 0, 0
from django.contrib.contenttypes.models import
ContentType
from gretschpages.karma.models import KarmaScore
ctype = ContentType.objects.get(model='gpuser')
for k in
KarmaScore.objects.filter(content_type=ctype.id,
object_id=self.id):
if k.score == -1:
bad +=1
elif k.score == 1:
good +=1
self._karma_total_good, self._karma_total_bad =
good, bad
But something's going awry.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| Just when I think I've got Karma working |

|
2006-10-31 14:29:15 |
Guillermo Fernandez Castellanos wrote:
> Hi,
>
> Just guessing, but if I understood well the error you
get, that's normal.
>
> The KarmaScoreManager is not the amount of karma of a
coment (or in
> your case an object). It keeps in the database the
karma score that a
> given user gave to a specific comment (or object). As
there is a
> single score per user and per object, you can only
override the total
> karma.
>
> The key point of the calculation of the karma is in the
Comment model:
> def _fill_karma_cache(self):
> "Helper function that populates good/bad
karma caches"
> good, bad = 0, 0
> for k in self.karmascore_set.all():
> if k.score == -1:
> bad +=1
> elif k.score == 1:
> good +=1
> self._karma_total_good, self._karma_total_bad =
good, bad
>
> For a given Comment, the function of the model will
take all the
> KarmaScore for the given comment
(self.karmascore_set.all()) and
> iterate over them to calculate the total karma of the
Comment (self).
>
> If you want to do a generic karma, you might want to
add a function to
> the manipulator that will do just this, take an id and
content_type,
> get() the object, find all the KarmaScores that are
related to this
> object (self.karmascore_set.all()) and calculate the
total karma by
> iterating over those.
>
> Hope it helps,
>
> G
>
I thought I had taken care of that. Apparently not. I have:
def _fill_karma_cache(self):
good, bad = 0, 0
from django.contrib.contenttypes.models import
ContentType
from gretschpages.karma.models import KarmaScore
ctype = ContentType.objects.get(model='gpuser')
for k in
KarmaScore.objects.filter(content_type=ctype.id,
object_id=self.id):
if k.score == -1:
bad +=1
elif k.score == 1:
good +=1
self._karma_total_good, self._karma_total_bad =
good, bad
But something's going awry.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
| Just when I think I've got Karma working |

|
2006-10-31 17:02:09 |
Maybe the bug is in KarmaScoreManager.vote():
karma =
self.get(content_type=content_type,object_id=object_id)
might need to be:
karma =
self.get(content_type__pk=content_type,object_id=object_id,
user__pk=user_id)
otherwise, you'll get a set of karma votes for a given
object, instead
of the vote of a given person for a given object. Thus, you
will
always go to the except self.model.DoesNotExist part of the
code.
And it seems that the KarmaScore class should have the
fields
content_type and object_id named as such
(http://www.djangoproject.com/documenta
tion/models/generic_relations/#c1876),
while you seems to call them content_type_id in here:
karma = self.model(None, user_id=user_id,
content_type_id=content_type, object_id=object_id,
score=rating,
scored_date=datetime.datetime.now())
As well, if you add this field to the KarmaScore:
content_object = models.GenericForeignKey()
and pass the objecto to vote on in your vote function, you
would be
able to create your karma objects like this:
karma = self.model(None, user_id=user_id,
content_object=object,
score=rating, scored_date=datetime.datetime.now())
without needing to explicitely pass the content_type and
object_id.
Oh... and in your KarmaScore model,
scored_date=datetime.datetime.now(), you might want to add
the
auto_add=True option to DateTimeField.
Disclaimer: I have never used the Generic relations, and all
my
conclusions come from the docs:
http://www.djangoproject.com/documentation/m
odels/generic_relations/
and is sometimes difficult to answer such questions without
the whole
code and some testing.
Hope it helps,
G
On 10/31/06, baxter gretschpages.com <mail.baxter gmail.com> wrote:
>
> Guillermo Fernandez Castellanos wrote:
> > Hi,
> >
> > Just guessing, but if I understood well the error
you get, that's normal.
> >
> > The KarmaScoreManager is not the amount of karma
of a coment (or in
> > your case an object). It keeps in the database the
karma score that a
> > given user gave to a specific comment (or object).
As there is a
> > single score per user and per object, you can only
override the total
> > karma.
> >
> > The key point of the calculation of the karma is
in the Comment model:
> > def _fill_karma_cache(self):
> > "Helper function that populates
good/bad karma caches"
> > good, bad = 0, 0
> > for k in self.karmascore_set.all():
> > if k.score == -1:
> > bad +=1
> > elif k.score == 1:
> > good +=1
> > self._karma_total_good,
self._karma_total_bad = good, bad
> >
> > For a given Comment, the function of the model
will take all the
> > KarmaScore for the given comment
(self.karmascore_set.all()) and
> > iterate over them to calculate the total karma of
the Comment (self).
> >
> > If you want to do a generic karma, you might want
to add a function to
> > the manipulator that will do just this, take an id
and content_type,
> > get() the object, find all the KarmaScores that
are related to this
> > object (self.karmascore_set.all()) and calculate
the total karma by
> > iterating over those.
> >
> > Hope it helps,
> >
> > G
> >
> I thought I had taken care of that. Apparently not. I
have:
>
> def _fill_karma_cache(self):
> good, bad = 0, 0
> from django.contrib.contenttypes.models import
ContentType
> from gretschpages.karma.models import KarmaScore
> ctype = ContentType.objects.get(model='gpuser')
>
> for k in
KarmaScore.objects.filter(content_type=ctype.id,
> object_id=self.id):
> if k.score == -1:
> bad +=1
> elif k.score == 1:
> good +=1
> self._karma_total_good, self._karma_total_bad =
good, bad
>
> But something's going awry.
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|
|
[1-5]
|
|