Ejemplo n.º 1
0
    def forwards(self, orm):
        db.alter_column("core_reference", "score", models.FloatField())

        # Add the generic foreign key back onto Vote.
        generic_foreign_key = generic.GenericForeignKey()
        generic_foreign_key.contribute_to_class(orm["votes.vote"], "content_object")

        # The reference content type.
        content_type = orm["contenttypes.ContentType"]
        content_type = content_type.objects.get_or_create(app_label="core", model="reference")[0]

        for reference in orm.Reference.objects.all():
            votes = orm["votes.vote"].objects.filter(content_type=content_type,
                    is_archived=False, object_id=reference.pk)

            if len(votes) > 0:
                Vote.refresh_score(votes[0], votes=votes)
Ejemplo n.º 2
0
    def test_valid_reference(self):
        """A view's stance should be equal to that of its most recently
            published, valid reference (valid if its score is >= 0.5)."""
        for reference_pk in (1, 2):
            reference = Reference.objects.get(pk=reference_pk)

            for user_pk in (1, 2, 3):
                user = User.objects.get(pk=user_pk)
                vote = Vote(author=user, content_object=reference, type=Vote.UP)

                # We attempt to create a couple of votes that are already in the
                # fixture. Catch the error and continue because they're there.
                try:
                    vote.save()
                except IntegrityError:
                    pass

        # Both references are valid, but the oppose one was published later.
        view = View.objects.get(pk=1)
        self.assertEqual(View.OPPOSE, view.stance)
        self.assertEqual(Reference.objects.get(pk=2),
                view.get_current_reference())

        # If the score of the currently winning reference dips back below 0.5
        # (making it "invalid"), the view's stance should update accordingly.
        Vote.objects.filter(object_id=2).delete()
        view = View.objects.get(pk=1)
        self.assertEqual(View.SUPPORT, view.stance)
        self.assertEqual(Reference.objects.get(pk=1),
                view.get_current_reference())

        # ...and if all the references become invalid, it should become unknown.
        Vote.objects.all().delete()
        view = View.objects.get(pk=1)
        self.assertEqual(View.UNKNOWN, view.stance)
        self.assertEqual(None, view.get_current_reference())