1. Stephen McDonald
  2. drum

Commits

Stephen McDonald  committed 6071630

Cleanups that came out of the blog post.

  • Participants
  • Parent commits 3375640
  • Branches default

Comments (0)

Files changed (4)

File main/models.py

View file
  • Ignore whitespace
 
 class Profile(models.Model):
 
-    user = models.OneToOneField("auth.user")
+    user = models.OneToOneField("auth.User")
     website = models.URLField(blank=True)
     bio = models.TextField(blank=True)
     karma = models.IntegerField(default=0, editable=False)
 
 
 @receiver(post_save, sender=Rating)
-def karma(sender, instance, **kwargs):
+def karma(sender, rating, **kwargs):
     """
     Each time a rating is saved, check its value and modify the
     profile karma for the related object's user accordingly.
     we can assume that the existing rating is in the other direction,
     so we multiply the karma modifier by 2.
     """
-    value = int(instance.value)
+    value = int(rating.value)
     if not kwargs["created"]:
         value *= 2
-    content_object = instance.content_object
-    if instance.user != content_object.user:
+    content_object = rating.content_object
+    if rating.user != content_object.user:
         queryset = Profile.objects.filter(user=content_object.user)
         queryset.update(karma=models.F("karma") + value)

File main/static/js/drum.js

View file
  • Ignore whitespace
     // value to redirect to, otherwise it will contain the new
     // rating score, which we update the page with.
     $('.arrows a').click(function() {
+
         var arrow = $(this);
         var index = arrow.find('i').hasClass('icon-arrow-up') ? 1 : 0;
         var container = arrow.parent().parent();
         var form = container.find('form');
+
         form.find('input:radio')[index].checked = true;
+
         $.post(form.attr('action'), form.serialize(), function(data) {
             if (data.location) {
                 location = data.location;
                 container.find('.score').text(data.rating_sum);
             }
         }, 'json');
+
         return false;
     });
 };

File main/templatetags/drum_tags.py

View file
  • Ignore whitespace
 
 from collections import defaultdict
 from django import template
-from mezzanine.generic.models import ThreadedComment
 
 from ..utils import order_by_score
 from ..views import CommentList
 
 
 @register.simple_tag(takes_context=True)
-def order_comments_by_score_for(context, parent):
+def order_comments_by_score_for(context, link):
     """
     Preloads threaded comments in the same way Mezzanine initially does,
     but here we order them by score.
     """
     comments = defaultdict(list)
-    qs = parent.comments.visible().select_related("user", "user__profile")
+    qs = link.comments.visible().select_related("user", "user__profile")
     for comment in order_by_score(qs, CommentList.score_fields, "submit_date"):
         comments[comment.replied_to_id].append(comment)
     context["all_comments"] = comments

File main/utils.py

View file
  • Ignore whitespace
 
 def order_by_score(queryset, score_fields, date_field, reverse=True):
     """
-    Take some queryset (links or comments) and orders them by score,
+    Take some queryset (links or comments) and order them by score,
     which is basically "rating_sum / age_in_seconds ^ scale", where
     scale is a constant that can be used to control how quickly scores
     reduce over time. To perform this in the database, it needs to
     scale = getattr(settings, "SCORE_SCALE_FACTOR", 2)
 
     # Timestamp SQL function snippets mapped to DB backends.
-    # Defining these assumes the SQL fucntions POW() and NOW()
-    # are available for the db backend.
-    db_engine = settings.DATABASES[queryset.db]["ENGINE"].rsplit(".", 1)[1]
-    timestamp_sql = {
+    # Defining these assumes the SQL functions POW() and NOW()
+    # are available for the DB backend.
+    timestamp_sqls = {
         "mysql": "UNIX_TIMESTAMP(%s)",
         "postgresql_psycopg2": "EXTRACT(EPOCH FROM %s)" ,
-    }.get(db_engine)
+    }
+    db_engine = settings.DATABASES[queryset.db]["ENGINE"].rsplit(".", 1)[1]
+    timestamp_sql = timestamp_sqls.get(db_engine)
 
     if timestamp_sql:
         score_sql = "(%s) / POW(%s - %s, %s)" % (