Skip to content

Instantly share code, notes, and snippets.

@firm1
Created November 12, 2014 16:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save firm1/0ad421327db706e8f8a1 to your computer and use it in GitHub Desktop.
Save firm1/0ad421327db706e8f8a1 to your computer and use it in GitHub Desktop.
fonctions de scoring d'un sujet de forum
from zds.forum.models import TopicFollowed, Topic, Post
from zds.utils.models import CommentLike, CommentDislike
unit_mark_activity = {
"follow": { # points lié au suivi du topic
"site": 1, # suivre un topic sur le site
"email": 2, # suivre un topic par email
}
"post" : { # points liés a tous les messages sauf au premier post
"new": 2, # nouveau message
"like": 1, # like sur un message
"dislike": -1, # dislike sur un message
"alert": -1, # alerter un message
}
"po": { # points liés au message principal
"like" : 1, # like sur le po
"dislike" : -1, # dislike sur le po
"alert": -1, # alerter un message
}
}
unit_mark_advice = {
"follow": { # points lié au suivi du topic
"site": 1, # suivre un topic sur le site
"email": 2, # suivre un topic par email
}
"post" : { # points liés a tous les messages sauf au premier post
"new": -1, # nouveau message
"like": +2, # like sur un message
"dislike": -2, # dislike sur un message
"alert": -2, # alerter un message
}
"po": { # points liés au message principal
"like" : 2, # like sur le po
"dislike" : -2, # dislike sur le po
"alert": -2, # alerter un message
"content_h1" : 1 # contient une balise de type h1
"content_h2" : 1 # contient une balise de type h2
}
}
def scoring_activity(topic):
score = 0
# calculate number of follow
followed_by_site = TopicFollowed.objects.filter(topic__pk=topic.pk, email=False).count()
followed_by_email = TopicFollowed.objects.filter(topic__pk=topic.pk, email=True).count()
score += followed_by_site*unit_mark_activity["follow"]["site"]
score += followed_by_email*unit_mark_activity["follow"]["email"]
# po's scoring
po_post = Post.objects.filter(position=1).first()
po_likes = po_post.like
po_dislikes = po_post.dislike
po_alerts = Alert.objects.filter(comment__pk=po_post.pk).count()
score += po_likes * unit_mark_activity["po"]["like"]
score += po_dislikes * unit_mark_activity["po"]["dislike"]
score += po_alerts * unit_mark_activity["po"]["alert"]
# post's scoring
posts = Post.objects.filter(position>1)
posts_new = posts.count()
posts_likes = posts.aggregate(total_likes=Sum('like'))
posts_dislikes = posts.aggregate(total_likes=Sum('dislike'))
posts_alerts = Alert.objects.filter(comment__pk__in=list(posts)).count()
score += posts_new * unit_mark_activity["post"]["new"]
score += posts_likes * unit_mark_activity["post"]["like"]
score += posts_dislikes * unit_mark_activity["post"]["dislike"]
score += posts_alerts * unit_mark_activity["post"]["alert"]
topic.activity_score = score
topic.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment