Example #1
def moderate_post(request, post):
    """Determines the moderation behavior for the request and asset posted.

    Returns True when the post has been handled (pre-moderated or blocked),
    and returns False when the post is approved for posting."""
    # save a copy of this content to our database

    # do this check first of all to avoid any possible spam filtering
    if request.typepad_user.is_superuser or request.typepad_user.is_featured_member:
        return False

    post_status = moderation_status(request, post)

    if post_status is None:
        # blocked; don't post to typepad
        return True

    if is_spam(request, post):
        # spammy posts get pre-moderated
        post_status = Queue.SPAM

    # if moderation_status says the asset can be published, let it be so
    # what to do about uploads though?
    if post_status == Queue.APPROVED:
        return False

    queue = Queue()
    queue.asset_type = post.type_id
    queue.user_id = request.typepad_user.url_id
    queue.user_display_name = request.typepad_user.display_name
    queue.user_userpic = request.typepad_user.userpic
    queue.summary = unicode(post)
    queue.status = post_status

    content = QueueContent()
    content.queue = queue
    content.data = json.dumps(post.to_dict())
    if request.FILES:
        content.attachment = request.FILES['file']
    content.user_token = request.oauth_client.token.to_string()
    content.ip_addr = request.META['REMOTE_ADDR']

    if post.type_id == 'comment':
        request.flash.add('notices', _('Thank you. Your comment is awaiting moderation by the Administrator.'))
        request.flash.add('notices', _('Thank you. Your post is awaiting moderation by the Administrator.'))

    return True
Example #2
def moderation_report(request):
    asset_id = request.POST['asset-id']
        reason_code = int(request.POST.get('reason', 0))
    except ValueError:
        reason_code = 0
    note = request.POST.get('note', None)
    return_to = request.POST.get('return_to', reverse('home'))
    return_to = re.sub('.*?/', '/', return_to)

    ip = request.META['REMOTE_ADDR']

    user = get_user(request)
    asset = typepad.Asset.get_by_url_id(asset_id)
    except typepad.Asset.NotFound:
        if request.is_ajax():
            return HttpResponse(_("The requested post was not found."), mimetype='text/plain')
            return HttpResponse('ERROR', mimetype='text/plain')

    # TODO: Should we behave differently if the user is an admin?

    queue = Queue.objects.filter(asset_id=asset_id)
    if not queue:
        queue = Queue()
        queue.asset_id = asset_id
        queue.summary = unicode(asset)
        queue.asset_type = asset.type_id
        queue.user_id = asset.user.url_id
        queue.user_display_name = asset.user.display_name
        queue.user_userpic = asset.user.userpic
        queue.flag_count = 1
        queue.status = Queue.FLAGGED
        queue.last_flagged = datetime.now()
        queue = queue[0]
        queue.flag_count += 1

    approved = Approved.objects.filter(asset_id=asset_id)

    if len(approved):
        if request.is_ajax():
            return HttpResponse(_("This post has been approved by the site moderator."), mimetype='text/plain')
            request.flash.add('notices', _('This post has been approved by the site moderator.'))
            return HttpResponseRedirect(return_to)

    # determine if this report is going to suppress the asset or not.
    if queue.status != Queue.SUPPRESSED:
        # count # of flags for this reason and asset:
        if len(settings.REPORT_OPTIONS[reason_code]) > 1:
            trigger = settings.REPORT_OPTIONS[reason_code][1]
            count = Flag.objects.filter(tp_asset_id=asset_id, reason_code=reason_code).count()
            if count + 1 >= trigger:
                queue.status = Queue.SUPPRESSED


    # to avoid having to hit typepad for viewing this content,
    # save a local copy to make moderation as fast as possible
    # this data is removed once the post is processed.
    if not queue.content:
        content = QueueContent()
        content.data = json.dumps(asset.to_dict())
        content.queue = queue
        content.user_token = 'none'
        content.ip_addr = ''

    flag = Flag.objects.filter(user_id=user.url_id, queue=queue)
    if not flag:
        # lets not allow a single user to repeat a report on the same asset
        flag = Flag()
        flag.queue = queue
        flag.tp_asset_id = asset_id
        flag.user_id = user.url_id
        flag.user_display_name = user.display_name
        if reason_code is not None:
            flag.reason_code = reason_code
        if note is not None:
            flag.note = note
        flag.ip_addr = ip
        flag = flag[0]
        if reason_code and flag.reason_code != reason_code:
            flag.reason_code = reason_code
            flag.ip_addr = ip
            if note is not None:
                flag.note = note

    if request.is_ajax():
        return HttpResponse('OK', mimetype='text/plain')
        request.flash.add('notices', _('Thank you for your report.'))
        if queue.status == Queue.SUPPRESSED:
            return HttpResponseRedirect(reverse('home'))
            return HttpResponseRedirect(return_to)