Exemplo n.º 1
0
    def record(self, request_or_user, **metadata):
        from canvas import fact
        # A unique key per day.

        if hasattr(request_or_user, 'user'):
            request = request_or_user
            user = request.user
        else:
            request = None
            user = request_or_user

        def _record(timestamp_key):
            if request:
                RedisSet(timestamp_key + ":unique_ips").sadd(
                    util.ip_to_int(request.META.get('REMOTE_ADDR')))
            if user:
                RedisSet(timestamp_key + ":uniques").sadd(user.id)
            RedisKey(timestamp_key + ":count").incr(1)

        _record(self.basekey + ":" + Services.time.strftime("%Y.%m.%d"))
        _record(self.basekey + ":" + Services.time.strftime("%Y.%m.%d.%H"))
        self.timestamp_key.set(str(Services.time.time()))

        if self.threshold:
            ThresholdMetric(self.basekey,
                            threshold=self.threshold,
                            minutes=self.alarm_minutes).increment()

        if metadata.get('record_fact', True):
            fact.record('metric', request_or_user,
                        dict(metadata, metric=self.name))
Exemplo n.º 2
0
def _follow_or_unfollow_group(request, category_id, follow):
    #TODO why does this default to 0?
    category_id = int(category_id or 0)
    category = get_object_or_404(models.Category, id=category_id)
    existing = models.FollowCategory.objects.get_or_none(category=category, user=request.user)

    if follow:
        if not existing:
            try:
                models.FollowCategory(
                    category=category,
                    user=request.user
                ).save()
            except model.FollowCategory.IntegrityError:
                pass
        Metrics.follow.record(request, category=category.id)
    else:
        if existing:
            existing.delete()
            fact.record("unfollow", request, {'category': category.id})

    # Update following count.
    category.details.force()

    return {'follows': [follow.category.details() for follow in request.user.following.all()]}
Exemplo n.º 3
0
def _follow_or_unfollow_group(request, category_id, follow):
    #TODO why does this default to 0?
    category_id = int(category_id or 0)
    category = get_object_or_404(models.Category, id=category_id)
    existing = models.FollowCategory.objects.get_or_none(category=category,
                                                         user=request.user)

    if follow:
        if not existing:
            try:
                models.FollowCategory(category=category,
                                      user=request.user).save()
            except model.FollowCategory.IntegrityError:
                pass
        Metrics.follow.record(request, category=category.id)
    else:
        if existing:
            existing.delete()
            fact.record("unfollow", request, {'category': category.id})

    # Update following count.
    category.details.force()

    return {
        'follows':
        [follow.category.details() for follow in request.user.following.all()]
    }
Exemplo n.º 4
0
    def record(self, request_or_user, **metadata):
        from canvas import fact
        # A unique key per day.

        if hasattr(request_or_user, 'user'):
            request = request_or_user
            user = request.user
        else:
            request = None
            user = request_or_user

        def _record(timestamp_key):
            if request:
                RedisSet(timestamp_key + ":unique_ips").sadd(util.ip_to_int(request.META.get('REMOTE_ADDR')))
            if user:
                RedisSet(timestamp_key + ":uniques").sadd(user.id)
            RedisKey(timestamp_key + ":count").incr(1)

        _record(self.basekey + ":" + Services.time.strftime("%Y.%m.%d"))
        _record(self.basekey + ":" + Services.time.strftime("%Y.%m.%d.%H"))
        self.timestamp_key.set(str(Services.time.time()))

        if self.threshold:
            ThresholdMetric(self.basekey, threshold=self.threshold, minutes=self.alarm_minutes).increment()

        if metadata.get('record_fact', True):
            fact.record('metric', request_or_user, dict(metadata, metric=self.name))
Exemplo n.º 5
0
def new_group(request, group_name, group_description):
    name = group_name
    description = group_description

    if models.Category.all_objects.get_or_none(name=name):
        raise ServiceError('A group already exists with that name.')
    if models.Category.objects.filter(
            founder=request.user).count() >= models.Category.FOUND_LIMIT:
        raise ServiceError(
            'Sorry, you can only found up to %s groups at this time.' %
            models.Category.FOUND_LIMIT)

    group = models.Category(
        founder=request.user,
        founded=time.time(),
        name=name,
        description=description,
    )

    problem = group.validate()
    if problem:
        raise ServiceError(problem)

    group.save()

    request.user.userinfo.details.force()

    # A founder auto-follows their group.
    models.FollowCategory(category=group, user=request.user).save()

    group_info = {'name': name, 'description': description}
    fact.record('new_group', request, group_info)
    Metrics.new_group.record(request, group_info=group_info)
Exemplo n.º 6
0
def new_group(request, group_name, group_description):
    name = group_name
    description = group_description

    if models.Category.all_objects.get_or_none(name=name):
        raise ServiceError('A group already exists with that name.')
    if models.Category.objects.filter(founder=request.user).count() >= models.Category.FOUND_LIMIT:
        raise ServiceError('Sorry, you can only found up to %s groups at this time.' % models.Category.FOUND_LIMIT)

    group = models.Category(
        founder=request.user,
        founded=time.time(),
        name=name,
        description=description,
    )

    problem = group.validate()
    if problem:
        raise ServiceError(problem)

    group.save()

    request.user.userinfo.details.force()

    # A founder auto-follows their group.
    models.FollowCategory(
        category=group,
        user=request.user
    ).save()

    group_info = {'name': name, 'description': description}
    fact.record('new_group', request, group_info)
    Metrics.new_group.record(request, group_info=group_info)
Exemplo n.º 7
0
    def get_share_page_url_with_tracking(self, sharer, channel, request=None, absolute=False):
        url = self.get_share_page_url(absolute=absolute)
        share = ShareTrackingUrl.create(sharer, url, channel)

        if request and channel != 'testing':
            fact.record('create_share_url', request, dict(url=url, channel=channel, share=share.id))

        return share.url_for_channel()
Exemplo n.º 8
0
def share_create(request, url, channel):
    share = ShareTrackingUrl.create(request.user, url=url, channel=channel)
    fact.record('create_share_url', request, dict(url=url, channel=channel, share=share.id))

    return {
        'share_url': share.url,
        'share_get_arg': share.get_arg,
        'share_id': share.id,
    }
Exemplo n.º 9
0
def logged_out_thread_view(request, short_id, page=None, gotoreply=None):
    view_data = CommentViewData(request, short_id, page=page, gotoreply=gotoreply)

    ctx = view_data.thread_context()

    if request.is_mobile:
        return r2r_jinja('mobile/thread.html', ctx)

    fact.record('flow_start', request, {})

    return render_to_response('threads/new_thread.django.html', ctx, context_instance=RequestContext(request))
Exemplo n.º 10
0
    def _record_add(self, experiment, branch):
        identifier = self._request
        if not identifier and self._user_id:
            from apps.canvas_auth.models import User
            identifier = User.objects.get(id=self._user_id)

        if identifier:
            from canvas import fact
            fact.record('experiment_add', identifier, {
                'experiment': experiment.name,
                'branch': branch.name,
            })
Exemplo n.º 11
0
    def get_share_page_url_with_tracking(self,
                                         sharer,
                                         channel,
                                         request=None,
                                         absolute=False):
        url = self.get_share_page_url(absolute=absolute)
        share = ShareTrackingUrl.create(sharer, url, channel)

        if request and channel != 'testing':
            fact.record('create_share_url', request,
                        dict(url=url, channel=channel, share=share.id))

        return share.url_for_channel()
Exemplo n.º 12
0
def unflag_comment(request, flag_id):
    flag = get_object_or_404(models.CommentFlag, id=flag_id)
    comment = flag.comment

    if flag.user != request.user:
        raise InsufficientPrivileges()

    flag.undone = True
    flag.save()
    comment.details.force()

    fact.record("unflag", request, {'comment': comment.id})

    return {'flag_counts': comment.details().flag_counts, 'flag_id': flag.id}
Exemplo n.º 13
0
def unflag_comment(request, flag_id):
    flag = get_object_or_404(models.CommentFlag, id=flag_id)
    comment = flag.comment

    if flag.user != request.user:
        raise InsufficientPrivileges()

    flag.undone = True
    flag.save()
    comment.details.force()

    fact.record("unflag", request, {"comment": comment.id})

    return {"flag_counts": comment.details().flag_counts, "flag_id": flag.id}
Exemplo n.º 14
0
def view(request, short_id, option=None):
    from apps.monster.jinja_tags import monster_image_tile

    view_data = CommentViewData(request, short_id)
    main_comment = view_data.op_comment
    replies = [Comment.details_by_id(cid) for cid in view_data.reply_ids]
    has_replies = len(replies) > 0
    complete_link = option and (option == 'complete')
    if complete_link and request.user.is_anonymous():
        fact.record('monster_start_flow', request, {'monster_id': short_id})
    reply_id = None

    if option:
        try:
            reply_id = int(option)
        except ValueError:
            pass

    (
        (main_comment,),
        replies
    ) = CachedCall.many_multicall(
        [main_comment],
        replies,
    )

    replies = [reply for reply in replies if not reply.is_collapsed]

    monster_part = MonsterPart.get_by_comment(main_comment)
    main_comment_details = main_comment
    main_comment = TileDetails(main_comment)

    made_bottom = False
    made_top = main_comment.comment.real_author == request.user.username

    linked_monster_footer_image = ""
    current_monster_index = 0

    for i in range(len(replies)):
        reply = replies[i]
        if reply_id is not None and reply.id == int(reply_id):
            current_monster_index = i
        elif reply.real_author == request.user.username and reply_id is None:
            current_monster_index = i
            made_bottom = True

    try:
        if (has_replies
                and replies[current_monster_index].reply_content
                and replies[current_monster_index].reply_content.footer):
            linked_monster_footer_image = replies[current_monster_index].reply_content.footer['name']
    except (AttributeError, IndexError):
        pass

    made_part = made_top or made_bottom

    if made_part:
        CompletedMonsterSet(request.user).sadd(main_comment.comment.id)

    can_make_bottom = (not made_part) and complete_link
    can_invite = made_top

    # incomplete monster without an invite link, send to monster index
    if not has_replies and not complete_link and not can_invite:
        return HttpResponseRedirect('/monster')

    ctx = {
        'can_invite': can_invite,
        'can_make_bottom': can_make_bottom,
        'current_monster_index': current_monster_index,
        'domain': settings.DOMAIN,
        'made_bottom': made_bottom,
        'made_part': made_part,
        'made_top': made_top,
        'main_comment': main_comment,
        'monster_content': main_comment.comment.reply_content,
        'og_image_url': linked_monster_footer_image.replace("https", "http", 1),
        'monster_group': MONSTER_GROUP,
        'monster_name': main_comment.comment.title,
        'replies': MonsterTileDetails.from_shared_op_details_with_viewer_stickers(request.user, main_comment_details, replies),
        'request': request,
        'short_id': main_comment.comment.short_id(),
        'start_content': Content.all_objects.get(id=Content.SMALL_DRAW_FROM_SCRATCH_PK).details(),
    }

    return r2r_jinja('monster/view.html', ctx)
Exemplo n.º 15
0
def record_fact(request, type, info={}):
    fact.record(type, request, info)
Exemplo n.º 16
0
def unpin_comment(request, comment_id):
    comment = get_object_or_404(models.Comment, pk=comment_id)
    comment.remove_pin(request.user)
    fact.record("unpin", request, {'comment': comment.id})
Exemplo n.º 17
0
def record_fact(request, type, info={}):
    fact.record(type, request, info)
Exemplo n.º 18
0
def share_create(request, url, channel):
    share = ShareTrackingUrl.create(request.user, url=url, channel=channel)
    fact.record("create_share_url", request, dict(url=url, channel=channel, share=share.id))

    return {"share_url": share.url, "share_get_arg": share.get_arg, "share_id": share.id}
Exemplo n.º 19
0
def unpin_comment(request, comment_id):
    comment = get_object_or_404(models.Comment, pk=comment_id)
    comment.remove_pin(request.user)
    fact.record("unpin", request, {"comment": comment.id})