First time here? Check out the FAQ!
0

seeding vote count for questions

In the process of rewriting the importer for Zendesk, I'm wondering if there's a generic way to seed the vote count for a particular question. I found the increase_view_count() method for increasing the view count but couldn't find a similar method for increasing the vote count as it seems they have to be specifically linked to the users who submitted the vote.

The Zendesk XML export used to import data doesn't have any specific information regarding who voted for a forum topic, it only has the overall vote count.

Is there a way to do this easily?

kporangehat's avatar
1
kporangehat
asked 2013-06-11 15:45:14 -0500
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

1 Answer

1

Maybe you could create a special user account and make it vote? There may be a limitation on the number of votes per day - if you hit that we might need to do something about this.

If you don't like to display that account in the future - just make it "blocked" - blocked accounts don't show anywhere.

To avoid hitting limitations of votes per post and votes per user per day - create a function in the importer vote() or something like that.

On post - save points, vote_up_count, vote_down_count - denormalized values. You might get away with just maintaining these, but for the better DB consistency I would for each vote also create a vote object: Vote(user = user, voted_post=post, vote=Vote.VOTE_UP, voted_at=timestamp).save().

Since the account will be throwaway - don't worry about tracking users karma.

Evgeny's avatar
13.2k
Evgeny
answered 2013-06-11 15:47:39 -0500, updated 2013-06-11 16:21:05 -0500
edit flag offensive 0 remove flag delete link

Comments

Can't a user only vote up a question once?

kporangehat's avatar kporangehat (2013-06-11 16:06:42 -0500) edit

Yes, you're right! I'll see what else I can suggest.

Evgeny's avatar Evgeny (2013-06-11 16:08:13 -0500) edit

Brilliant. I think that will work. I'll give it a shot. Thanks!

kporangehat's avatar kporangehat (2013-06-11 16:19:25 -0500) edit

Couple of issues: 1) When I run post.points = n, post.save(), the question list doesn't show the votes. However if I click into the Question, the votes are displayed and then the next time I go to the question list, I see the votes listed. Even setting the post.vote_up_count = n as well doesn't display the vote count in the question list. 2) Once I have clicked into the question and navigate back to the question list, the sorting by vote count doesn't seem to work.

I can probably figure this all out, but can you explain the relationship between Vote, and Post.points/vote_up_count/vote_down_count?

kporangehat's avatar kporangehat (2013-06-12 12:12:42 -0500) edit

It's because of the cache. Just delete all cache after the import:

python manage.py shell
from django.core.cache import cache
cache.clear()

There is cache for the question summary snippet for the front page.

Evgeny's avatar Evgeny (2013-06-12 12:16:50 -0500) edit

Points is 10*(vote_up_count - vote_down_count), sorry I should have explained before.

Evgeny's avatar Evgeny (2013-06-12 12:23:34 -0500) edit

Thanks for the clarification. However, clearing the cache doesn't seem to have any effect on the previous behavior. Here's the complete example of what I'm doing to try and seed a vote count of 10 on a particular Post:

post.vote_up_count = 10
post.points = 100
post.save()
Vote(user=PHANTOM_VOTER_USER, voted_post=post, vote=100).save()
cache.clear()

I was going to look into the query that is generated for the question summary front page to investigate further, but I'm not sure where it's being generated.

kporangehat's avatar kporangehat (2013-06-12 12:56:37 -0500) edit

Okay so it looks like I also need to set the Thread.points value as well to get this to work completely. Also, I don't see the points value computed the way you describe above. If I up-vote a post, the database shows the point value as +1 as well.

kporangehat's avatar kporangehat (2013-06-12 14:01:25 -0500) edit

Oh, sorry missed that. Btw the vote value of Vote should be either Vote.VOTE_UP or Vote.VOTE_DOWN (which are +1 and -1). You will need the Vote records if you ever decide to recalculate the votes for some reason.

Evgeny's avatar Evgeny (2013-06-12 14:07:31 -0500) edit

No worries. As for the Vote.VOTE_UP and Vote.VOTE_DOWN, this won't work for seeding a Vote count of more than one when we do the import since we're still limited by a unique constraint on (user_id, voted_post_id). For us it's not a huge issue. In the importer, I'll make a note about this if Votes need to be recalculated.

I don't see any management command for recalculating votes currently, is there one? Not sure what exactly would go wrong though, would it just count that as a single vote up or down? Or would it barf?

kporangehat's avatar kporangehat (2013-06-12 14:19:52 -0500) edit
add a comment see more comments