reddit style voting with django

Posted by dotty on Stack Overflow See other posts from Stack Overflow or by dotty
Published on 2010-06-01T13:30:46Z Indexed on 2010/06/01 13:33 UTC
Read the original article Hit count: 328

Filed under:
|
|

Hay i need to hand implemeneting a voting system into a model.

I've had a huge helping hand from Mike DeSimone making this work in the first place, but i need to expand upon his work.

Here is my current code

View

def show_game(request):
    game = Game.objects.get(pk=1)
    discussions = game.gamediscussion_set.filter(reply_to=None)
    d = {
        'game':game,
        'discussions':discussions
    }
    return render_to_response('show_game', d)

Template

<ul>
    {% for discussion in discussions %}
    {{ discussion.html }}
    {% endfor %}
</ul>

Model

class GameDiscussion(models.Model):
    game = models.ForeignKey(Game)
    message = models.TextField()
    reply_to = models.ForeignKey('self', related_name='replies', null=True, blank=True)
    created_on = models.DateTimeField(blank=True, auto_now_add=True)
    userUpVotes = models.ManyToManyField(User, blank=True, related_name='threadUpVotes')
    userDownVotes = models.ManyToManyField(User, blank=True, related_name='threadDownVotes')

    def html(self):
        DiscussionTemplate = loader.get_template("inclusions/discussionTemplate")
        return DiscussionTemplate.render(Context({
            'discussion': self,
            'replies': [reply.html() for reply in self.replies.all()]
    }))

DiscussionTemplate

<li>
    {{ discussion.message }}
    {% if replies %}
        <ul>
            {% for reply in replies %}
                {{ reply }}
            {% endfor %}
        </ul>
    {% endif %}
</li>

As you can see we have 2 fields userUpVotes and userDownVotes on the model, these will calculate how to order the discussions and replies.

How would i implement these 2 fields to order the replies and discussions based on votes?

Any help would be great!

© Stack Overflow or respective owner

Related posts about django

Related posts about reddit