def approve_all(request):
    sitelocation = SiteLocation.objects.get_current()
    video_paginator = get_video_paginator(sitelocation)
    try:
        page = video_paginator.page(int(request.GET.get('page', 1)))
    except ValueError:
        return HttpResponseBadRequest('Not a page number')
    except EmptyPage:
        return HttpResponseBadRequest(
            'Page number request exceeded available pages')

    if SiteLocation.enforce_tiers():
        tier_remaining_videos = sitelocation.get_tier().remaining_videos()
        if len(page.object_list) > tier_remaining_videos:
            remaining = str(tier_remaining_videos)
            need = str(len(page.object_list))
            return HttpResponse(content=(
                    ("You are trying to approve %s videos at a time. " % need) +
                    ("However, you can approve only %s more videos under your video limit. " % remaining) +
                    ("Please upgrade your account to increase your limit, or unapprove some older videos to make space for newer ones.")), status=402)

    for video in page.object_list:
        video.status = Video.ACTIVE
        video.when_approved = datetime.datetime.now()
        video.save()

    return HttpResponse('SUCCESS')
def feature_video(request):
    video_id = request.GET.get('video_id')
    sitelocation = SiteLocation.objects.get_current()
    current_video = get_object_or_404(
        Video, pk=video_id, site=sitelocation.site)
    if not current_video.status == Video.ACTIVE:
        if (SiteLocation.enforce_tiers() and
            sitelocation.get_tier().remaining_videos() < 1):
            return HttpResponse(content="You are over the video limit. You will need to upgrade to feature that video.", status=402)
        current_video.status = Video.ACTIVE
        current_video.when_approved = datetime.datetime.now()
    current_video.last_featured = datetime.datetime.now()
    current_video.save()

    return HttpResponse('SUCCESS')
def approve_video(request):
    sitelocation = SiteLocation.objects.get_current()
    current_video = get_object_or_404(
        Video,
        id=request.GET['video_id'],
        site=sitelocation.site)

    # If the site would exceed its video allotment, then fail
    # with a HTTP 402 and a clear message about why.
    if (SiteLocation.enforce_tiers() and
        sitelocation.get_tier().remaining_videos() < 1):
        return HttpResponse(content="You are over the video limit. You will need to upgrade to approve that video.", status=402)

    current_video.status = Video.ACTIVE
    current_video.when_approved = datetime.datetime.now()

    if request.GET.get('feature'):
        current_video.last_featured = datetime.datetime.now()

    current_video.save()

    if current_video.user and current_video.user.email:
        video_approved = notification.NoticeType.objects.get(
            label="video_approved")
        if notification.should_send(current_video.user, video_approved, "1"):
            subject = '[%s] "%s" was approved!' % (
                current_video.site.name,
                current_video)
            t = loader.get_template(
                'localtv/submit_video/approval_notification_email.txt')
            c = Context({'current_video': current_video})
            message = t.render(c)
            EmailMessage(subject, message, settings.DEFAULT_FROM_EMAIL,
                         [current_video.user.email]).send(fail_silently=True)

    return HttpResponse('SUCCESS')
示例#4
0
def mark_import_pending(import_app_label,
                        import_model,
                        import_pk,
                        using='default'):
    """
    Checks whether an import's first stage is complete. If it's not, retries
    the task with a countdown of 30.

    """
    import_class = get_model(import_app_label, import_model)
    try:
        source_import = import_class._default_manager.using(using).get(
            pk=import_pk, status=import_class.STARTED)
    except import_class.DoesNotExist:
        logging.debug('Expected %s instance (pk=%r) missing.',
                      import_class.__name__, import_pk)
        # If this is the problem, don't retry indefinitely.
        if mark_import_pending.request.retries > 10:
            raise MaxRetriesExceededError
        mark_import_pending.retry()
    source_import.last_activity = datetime.datetime.now()
    if source_import.total_videos is None:
        source_import.save()
        mark_import_pending.retry()
    # get the correct counts from the database, rather than the race-condition
    # prone count fields
    import_count = source_import.indexes.count()
    skipped_count = source_import.errors.filter(is_skip=True).count()
    if import_count != source_import.videos_imported:
        source_import.videos_imported = import_count
    if skipped_count != source_import.videos_skipped:
        source_import.videos_skipped = skipped_count
    if (source_import.videos_imported + source_import.videos_skipped <
            source_import.total_videos):
        # Then the import is incomplete. Requeue it.
        source_import.save()
        # Retry raises an exception, ending task execution.
        mark_import_pending.retry()

    # Otherwise the first stage is complete. Check whether they can take all the
    # videos.
    active_set = None
    unapproved_set = source_import.get_videos(using).filter(
        status=Video.PENDING)
    if source_import.auto_approve:
        if not SiteLocation.enforce_tiers(using=using):
            active_set = unapproved_set
            unapproved_set = None
        else:
            remaining_videos = (
                Tier.get(using=using).videos_limit() -
                Video.objects.using(using).filter(status=Video.ACTIVE).count())
            if remaining_videos > source_import.videos_imported:
                active_set = unapproved_set
                unapproved_set = None
            elif remaining_videos > 0:
                unapproved_set = unapproved_set.order_by('when_submitted')
                # only approve `remaining_videos` videos
                when_submitted = unapproved_set[
                    remaining_videos].when_submitted
                active_set = unapproved_set.filter(
                    when_submitted__lt=when_submitted)
                unapproved_set = unapproved_set.filter(
                    when_submitted__gte=when_submitted)
    if unapproved_set is not None:
        unapproved_set.update(status=Video.UNAPPROVED)
    if active_set is not None:
        active_set.update(status=Video.ACTIVE)

    source_import.status = import_class.PENDING
    source_import.save()

    active_pks = source_import.get_videos(using).filter(
        status=Video.ACTIVE).values_list('pk', flat=True)
    if active_pks:
        opts = Video._meta
        haystack_batch_update.delay(opts.app_label,
                                    opts.module_name,
                                    pks=list(active_pks),
                                    remove=False,
                                    using=using)

    mark_import_complete.delay(import_app_label,
                               import_model,
                               import_pk,
                               using=using)
示例#5
0
def mark_import_pending(import_app_label, import_model, import_pk, using="default"):
    """
    Checks whether an import's first stage is complete. If it's not, retries
    the task with a countdown of 30.

    """
    import_class = get_model(import_app_label, import_model)
    try:
        source_import = import_class._default_manager.using(using).get(pk=import_pk, status=import_class.STARTED)
    except import_class.DoesNotExist:
        logging.debug("Expected %s instance (pk=%r) missing.", import_class.__name__, import_pk)
        # If this is the problem, don't retry indefinitely.
        if mark_import_pending.request.retries > 10:
            raise MaxRetriesExceededError
        mark_import_pending.retry()
    source_import.last_activity = datetime.datetime.now()
    if source_import.total_videos is None:
        source_import.save()
        mark_import_pending.retry()
    # get the correct counts from the database, rather than the race-condition
    # prone count fields
    import_count = source_import.indexes.count()
    skipped_count = source_import.errors.filter(is_skip=True).count()
    if import_count != source_import.videos_imported:
        source_import.videos_imported = import_count
    if skipped_count != source_import.videos_skipped:
        source_import.videos_skipped = skipped_count
    if source_import.videos_imported + source_import.videos_skipped < source_import.total_videos:
        # Then the import is incomplete. Requeue it.
        source_import.save()
        # Retry raises an exception, ending task execution.
        mark_import_pending.retry()

    # Otherwise the first stage is complete. Check whether they can take all the
    # videos.
    active_set = None
    unapproved_set = source_import.get_videos(using).filter(status=Video.PENDING)
    if source_import.auto_approve:
        if not SiteLocation.enforce_tiers(using=using):
            active_set = unapproved_set
            unapproved_set = None
        else:
            remaining_videos = (
                Tier.get().videos_limit() - Video.objects.using(using).filter(status=Video.ACTIVE).count()
            )
            if remaining_videos > source_import.videos_imported:
                active_set = unapproved_set
                unapproved_set = None
            else:
                unapproved_set = unapproved_set.order_by("when_submitted")
                # only approve `remaining_videos` videos
                when_submitted = unapproved_set[remaining_videos].when_submitted
                active_set = unapproved_set.filter(when_submitted__lt=when_submitted)
                unapproved_set = unapproved_set.filter(when_submitted__gte=when_submitted)
    if unapproved_set is not None:
        unapproved_set.update(status=Video.UNAPPROVED)
    if active_set is not None:
        active_set.update(status=Video.ACTIVE)

    source_import.status = import_class.PENDING
    source_import.save()

    active_pks = source_import.get_videos(using).filter(status=Video.ACTIVE).values_list("pk", flat=True)
    if active_pks:
        opts = Video._meta
        for pk in active_pks:
            haystack_update_index.delay(opts.app_label, opts.module_name, pk, is_removal=False, using=using)

    mark_import_complete.delay(import_app_label, import_model, import_pk, using=using)