Пример #1
0
def create_quest(request, title, content_id=None, invite_followees=False,
                 facebook_share=False, facebook_access_token=None,
                 twitter_share=False, twitter_access_token=None, twitter_access_token_secret=None,
                 email_share=False, email_recipients=[],
                 resolve_share_ids=[]):
    if not request.user.is_staff and not settings.STAGING:
        prefix = 'user:{}:create_quest_limit:'.format(request.user.id)
        if not RateLimit(prefix+'h', 60, 60*60).allowed() or not RateLimit(prefix+'d', 100, 8*60*60).allowed():
            raise ServiceError("Attempting to create quests too quickly.")

    _, _, content, _, title = validate_and_clean_comment(
        request.user,
        parent_comment=None,
        reply_content=content_id,
        title=title,
    )

    if facebook_share:
        facebook_share = sns_publishing.facebook_share_pre_post(request, facebook_access_token)

    if twitter_share:
        twitter_share = sns_publishing.twitter_share_pre_post(request, twitter_access_token, twitter_access_token_secret)

    quest = Quest.create_and_post(request, request.user, title, content=content, ugq=True)

    models.autocurate_for_flag_words(quest)

    if invite_followees:
        quest.invited_users.invite(request.user, request.user.followers(), ignore_errors=True)

    if facebook_share:
        sns_publishing.facebook_share_post_post(request, facebook_access_token, quest)

    if twitter_share:
        sns_publishing.twitter_share_post_post(request, twitter_access_token, twitter_access_token_secret, quest)

    for share_id in resolve_share_ids:
        share = ShareTrackingUrl.objects.get(id=share_id)

        if share.redirect_url:
            pass #TODO log some error here without failing this request
        
        share.redirect_url = quest.get_share_page_url()
        share.save()

    if email_share:
        @bgwork.defer
        def defer_email_share():
            sns_publishing.share_quest_by_email(quest, request.user, email_recipients)

    @bgwork.defer
    def alert_followers():
        for follower_id in request.user.redis.new_followers.zrange(0, -1):
            RealtimeChannel('user:{}:rt_tab_badges'.format(follower_id), 1).publish({'tab_badge_update': 'draw'})

    return {
        'quest': quest.details(),
    }
Пример #2
0
def post_quest_comment(request, quest_id, content_id, fact_metadata={},
                       facebook_share=False, facebook_access_token=None,
                       twitter_share=False, twitter_access_token=None, twitter_access_token_secret=None,
                       email_share=False, email_recipients=[],
                       resolve_share_ids=[],
                       uuid=None):
    if not request.user.is_staff and not settings.STAGING:
        prefix = 'user:{}:post_limit:'.format(request.user.id)
        if not RateLimit(prefix+'h', 60, 60*60).allowed() or not RateLimit(prefix+'d', 100, 8*60*60).allowed():
            raise ServiceError("Attempting to post drawings too quickly.")

    _, parent_comment, content, _, _ = validate_and_clean_comment(
        request.user,
        parent_comment=quest_id,
        reply_content=content_id,
    )

    if facebook_share:
        facebook_share = sns_publishing.facebook_share_pre_post(request, facebook_access_token)

    if twitter_share:
        twitter_share = sns_publishing.twitter_share_pre_post(request, twitter_access_token, twitter_access_token_secret)

    comment = QuestComment.create_and_post(request, request.user, content, parent_comment,
                                           uuid=uuid, fact_metadata=fact_metadata, debug_content_id=content_id)

    if facebook_share:
        sns_publishing.facebook_share_post_post(request, facebook_access_token, comment)

    if twitter_share:
        sns_publishing.twitter_share_post_post(request, twitter_access_token, twitter_access_token_secret, comment)

    for share_id in resolve_share_ids:
        share = ShareTrackingUrl.objects.get(id=share_id)

        if share.redirect_url:
            pass #TODO log some error here without failing this request

        share.redirect_url = comment.get_share_page_url()
        share.save()

    if email_share:
        @bgwork.defer
        def defer_email_share():
            sns_publishing.share_comment_by_email(comment, request.user, email_recipients)

    return {
        'comment': comment.details(),
        'balance': economy.balance(request.user),
    }
Пример #3
0
def create_quest(request,
                 title,
                 content_id=None,
                 invite_followees=False,
                 facebook_share=False,
                 facebook_access_token=None,
                 twitter_share=False,
                 twitter_access_token=None,
                 twitter_access_token_secret=None,
                 email_share=False,
                 email_recipients=[],
                 resolve_share_ids=[]):
    if not request.user.is_staff and not settings.STAGING:
        prefix = 'user:{}:create_quest_limit:'.format(request.user.id)
        if not RateLimit(prefix + 'h', 60, 60 * 60).allowed() or not RateLimit(
                prefix + 'd', 100, 8 * 60 * 60).allowed():
            raise ServiceError("Attempting to create quests too quickly.")

    _, _, content, _, title = validate_and_clean_comment(
        request.user,
        parent_comment=None,
        reply_content=content_id,
        title=title,
    )

    if facebook_share:
        facebook_share = sns_publishing.facebook_share_pre_post(
            request, facebook_access_token)

    if twitter_share:
        twitter_share = sns_publishing.twitter_share_pre_post(
            request, twitter_access_token, twitter_access_token_secret)

    quest = Quest.create_and_post(request,
                                  request.user,
                                  title,
                                  content=content,
                                  ugq=True)

    models.autocurate_for_flag_words(quest)

    if invite_followees:
        quest.invited_users.invite(request.user,
                                   request.user.followers(),
                                   ignore_errors=True)

    if facebook_share:
        sns_publishing.facebook_share_post_post(request, facebook_access_token,
                                                quest)

    if twitter_share:
        sns_publishing.twitter_share_post_post(request, twitter_access_token,
                                               twitter_access_token_secret,
                                               quest)

    for share_id in resolve_share_ids:
        share = ShareTrackingUrl.objects.get(id=share_id)

        if share.redirect_url:
            pass  #TODO log some error here without failing this request

        share.redirect_url = quest.get_share_page_url()
        share.save()

    if email_share:

        @bgwork.defer
        def defer_email_share():
            sns_publishing.share_quest_by_email(quest, request.user,
                                                email_recipients)

    @bgwork.defer
    def alert_followers():
        for follower_id in request.user.redis.new_followers.zrange(0, -1):
            RealtimeChannel('user:{}:rt_tab_badges'.format(follower_id),
                            1).publish({'tab_badge_update': 'draw'})

    return {
        'quest': quest.details(),
    }