Example #1
0
def update_rankings(sender, cons):

    #update reporting percentage
    upvotes = UpDownVote.objects.filter(object_pk=cons.object_pk, vote__gt=6)
    downvotes = UpDownVote.objects.filter(object_pk=cons.object_pk, vote__lt=6)
    try:
        topic = Topic.objects.get(pk=cons.content_object.parent.pk)
        groups = MyGroup.objects.filter(topic=topic)

        try:
            cons.reporting_percent = (upvotes.count() + downvotes.count()) / float(groups.count())
        except:
            cons.reporting_percent = 0.0

        try:
            cons.consensus_percent = upvotes.count() / float(upvotes.count() + downvotes.count())
        except:
            cons.consensus_percent = 0.0
    except:
        pass
    #update the various ranking algorithms
    pis = cons.content_object
    spectrum = cons.spectrum.get_list()
    try:
        rating = cons.rating.get_list()
    except:
        #add rating to consensus if first rating
        rt = Rating()
        cons.rating = rt
        rt.save()
        rating = cons.rating.get_list()
    cons.save()
    dt = cons.submit_date
    timeDiff = (dt - datetime.datetime(2010, 7, 5, 20, 16, 19, 539498)).seconds
    timeNormFactor = (dt - datetime.datetime.now()).seconds

    dt = timeDiff / float(timeNormFactor)

    sols = [(('hot', calc_hot(dt, spectrum, rating)), ('cont', calc_controversial(dt, spectrum, rating)), ('best', calc_best(dt, spectrum, rating)))]

    ###I don't know why, but this fails if it's where it should be up above...
    from pirate_ranking.models import Ranking
    for scores in sols:
        for dim, sc in scores:
            try:
                obj = Ranking.objects.get(object_pk=pis.id, dimension=dim)
                obj.score = sc
                obj.save()
            except:
                contype = ContentType.objects.get_for_model(pis)
                newrank = Ranking(content_object=pis, dimension=dim, score=sc,
                            consensus_pk=cons.id, content_type=contype, object_pk=pis.id)
                newrank.save()
            if dim == 'hot':
                setattr(cons, 'interest', sc)
            if dim == 'cont':
                setattr(cons, 'controversy', sc)
            if dim == 'best':
                setattr(cons, 'best', sc)
            cons.save()
    ##Now we want to publish the vote event in the redis for nodejs to update
    print '*' * 50
    print 'publishing to: ' + str(cons.content_object.user.username)
    if sender != 'null':
        redis_client().publish(cons.content_object.user.username, json.dumps({'message': 'Someone voted on', 'object': str(cons.content_object.summary), 'type': 'vote', 'object_pk': str(cons.content_object.pk)}))
Example #2
0
def create_notice_email(obj_pk, ctype_pk, reply_to, link, text):
    ctype = ContentType.objects.get(pk=ctype_pk)
    obj = ctype.get_object_for_this_type(pk=obj_pk)
    try:
        profile = Profile.objects.get(user=reply_to.user)
        send_email = profile.receive_emails
    except:
        send_email = True
    content_type = ContentType.objects.get_for_model(obj)
    if reply_to is not None:
        rep_type = ContentType.objects.get_for_model(reply_to)
    else:
        rep_type = None
    user_type = ContentType.objects.get_for_model(User)
    if content_type is not user_type and rep_type is not user_type:
        path = obj.get_absolute_url()
        if str(content_type) == 'message':
            if send_email:
                    notification.send([obj.receiver], "message_received", {"from_user": obj.sender, "user_url": settings.DOMAIN + obj.sender.get_absolute_url(),
                    "notice_message": obj.sender.username  + "said<br>" + obj.description,
                    "path": settings.DOMAIN + path})
            redis_client().publish(obj.receiver, json.dumps({'message': obj.sender.username + ' said',
                'object': str(obj.description), 'type': 'message', 'object_pk': str(obj.sender.pk)}))
            text = obj.description
            link = obj.get_absolute_url()
        elif obj.user != reply_to.user:
            #redis_client().publish(cons.content_object.user.username, json.dumps({'message': 'Someone voted on', 'object': str(cons.content_object.summary), 'type': 'vote', 'object_pk': str(cons.content_object.pk)}))
            #if this notification is a comment_reply
            if str(content_type) == 'comment':
                if str(rep_type) == 'comment':
                    summ = str(reply_to.text)
                else:
                    summ = str(reply_to.summary)
                if send_email:
                        print reply_to.user.email
                        notification.send([reply_to.user], "comment_reply", {"from_user": obj.user, "user_url": settings.DOMAIN + obj.user.get_absolute_url(),
                        "notice_message": "New comment received for your " + str(rep_type) + " '" + summ + "':",
                        "reply": str(obj.text), "path": path})
                tt = str(obj.text)
                text = str(obj.user.username) + " replied to your " + str(rep_type) + "<br>" + tt
                link = obj.get_absolute_url()
                redis_client().publish(reply_to.user.username, json.dumps({'message': obj.user.username + " commented on ",
                    'object': str(summ), 'type': 'comment', 'object_pk': str(obj.pk)}))

            #if notification is an action_reply
            elif str(content_type) == 'action taken':
                if send_email:
                        notification.send([reply_to.user], "action_reply", {"from_user": obj.user, "user_url": settings.DOMAIN + obj.user.get_absolute_url(),
                        "notice_message": str(obj.user.username) + " acted on your " + str(rep_type) + " : " + str(reply_to.summary),
                        "path": settings.DOMAIN + path})

                text = str(obj.user.username) + " acted on your " + str(rep_type)
                link = reply_to.get_absolute_url()

            elif str(content_type) == 'argument':
                if send_email:
                        notification.send([reply_to.user], "argument_reply", {"from_user": obj.user, "user_url": settings.DOMAIN + obj.user.get_absolute_url(),
                        "notice_message": "New argument received for your " + str(rep_type) + " " + str(reply_to.summary),
                        "path": settings.DOMAIN + path})
                text = str(obj.user.username) + " created an argument for your " + str(rep_type)
                link = reply_to.get_absolute_url()
                #push to nodejs through redis
                redis_client().publish(reply_to.user.username, json.dumps({'message': obj.user.username + ' made an argument on',
                        'object': str(reply_to.summary), 'type': 'argument', 'object_pk': str(obj.pk)}))

            #if notification is badge_received

        #elif str(content_type) == 'badge':
        #    content_type = ContentType.objects.get_for_model(User)
        #    path = "/index.html#user/t-" + str(content_type.pk) + "/o-" + str(obj.user.pk)
        #    if send_email:
        #            notification.send([reply_to.user], "badge_received", {"from_user": obj.user,
        #        "notice_message": str(obj.user.username) + ", you've received a " + str(rep_type) + " '" + str(reply_to.dimension.name) + "':",
        #        "path": settings.DOMAIN_NAME + path})
        #    text = "you received a " + str(reply_to.dimension.name) + " badge"
        #    link = reply_to.get_absolute_url()

            #if notification is a child_reply

            #if notification is a message_received

            #if notification is support_Created

            #if notification is an argument_reply
  
    if link is not None and text is not None:
        if user_type != rep_type:
            if reply_to is None:

                notif = Notification(receiver=obj.receiver, sender=obj.sender, text=text,
                    link=link, content_type=content_type, object_pk=obj.pk, is_read=False, submit_date=datetime.datetime.now())
            else:
                notif = Notification(receiver=reply_to.user, sender=obj.user, text=text,
                    link=link, content_type=rep_type, object_pk=reply_to.pk, is_read=False, submit_date=datetime.datetime.now())
        else:
            root = get_root(obj)
            notif = Notification(receiver=reply_to, sender=obj, text=text, group=root,
                link=link, content_type=rep_type, object_pk=reply_to.pk, is_read=False, submit_date=datetime.datetime.now())
        notif.save()
Example #3
0
def update_rankings(sender, cons):

    #update reporting percentage
    upvotes = UpDownVote.objects.filter(object_pk=cons.object_pk, vote__gt=6)
    downvotes = UpDownVote.objects.filter(object_pk=cons.object_pk, vote__lt=6)
    try:
        topic = Topic.objects.get(pk=cons.content_object.parent.pk)
        groups = MyGroup.objects.filter(topic=topic)

        try:
            cons.reporting_percent = (
                upvotes.count() + downvotes.count()) / float(groups.count())
        except:
            cons.reporting_percent = 0.0

        try:
            cons.consensus_percent = upvotes.count() / float(upvotes.count() +
                                                             downvotes.count())
        except:
            cons.consensus_percent = 0.0
    except:
        pass
    #update the various ranking algorithms
    pis = cons.content_object
    spectrum = cons.spectrum.get_list()
    try:
        rating = cons.rating.get_list()
    except:
        #add rating to consensus if first rating
        rt = Rating()
        cons.rating = rt
        rt.save()
        rating = cons.rating.get_list()
    cons.save()
    dt = cons.submit_date
    timeDiff = (dt - datetime.datetime(2010, 7, 5, 20, 16, 19, 539498)).seconds
    timeNormFactor = (dt - datetime.datetime.now()).seconds

    dt = timeDiff / float(timeNormFactor)

    sols = [(('hot', calc_hot(dt, spectrum, rating)),
             ('cont', calc_controversial(dt, spectrum, rating)),
             ('best', calc_best(dt, spectrum, rating)))]

    ###I don't know why, but this fails if it's where it should be up above...
    from pirate_ranking.models import Ranking
    for scores in sols:
        for dim, sc in scores:
            try:
                obj = Ranking.objects.get(object_pk=pis.id, dimension=dim)
                obj.score = sc
                obj.save()
            except:
                contype = ContentType.objects.get_for_model(pis)
                newrank = Ranking(content_object=pis,
                                  dimension=dim,
                                  score=sc,
                                  consensus_pk=cons.id,
                                  content_type=contype,
                                  object_pk=pis.id)
                newrank.save()
            if dim == 'hot':
                setattr(cons, 'interest', sc)
            if dim == 'cont':
                setattr(cons, 'controversy', sc)
            if dim == 'best':
                setattr(cons, 'best', sc)
            cons.save()
    ##Now we want to publish the vote event in the redis for nodejs to update
    print '*' * 50
    print 'publishing to: ' + str(cons.content_object.user.username)
    if sender != 'null':
        redis_client().publish(
            cons.content_object.user.username,
            json.dumps({
                'message': 'Someone voted on',
                'object': str(cons.content_object.summary),
                'type': 'vote',
                'object_pk': str(cons.content_object.pk)
            }))