Exemple #1
0
def vidly_media(request):
    events = Event.objects.filter(
        Q(template__name__contains='Vid.ly')
        | Q(pk__in=VidlySubmission.objects.all().values_list('event_id',
                                                             flat=True)))

    status = request.GET.get('status')
    repeated = request.GET.get('repeated') == 'event'
    repeats = {}

    if status:
        if status not in ('New', 'Processing', 'Finished', 'Error'):
            return http.HttpResponseBadRequest("Invalid 'status' value")

        # make a list of all tags -> events
        _tags = {}
        for event in events:
            environment = event.template_environment or {}
            if not environment.get('tag') or environment.get('tag') == 'None':
                continue
            _tags[environment['tag']] = event.id

        event_ids = []
        for tag in vidly.medialist(status):
            try:
                event_ids.append(_tags[tag])
            except KeyError:
                # it's on vid.ly but not in this database
                logging.debug("Unknown event with tag=%r", tag)

        events = events.filter(id__in=event_ids)
    elif repeated:
        repeats = dict(
            (x['event_id'], x['event__id__count'])
            for x in VidlySubmission.objects.values('event_id').annotate(
                Count('event__id')).filter(event__id__count__gt=1))
        events = Event.objects.filter(id__in=repeats.keys())

    def get_repeats(event):
        return repeats[event.id]

    events = events.order_by('-start_time')
    events = events.select_related('template')
    paged = paginate(events, request.GET.get('page'), 15)

    submissions = defaultdict(list)
    for submission in VidlySubmission.objects.filter(event__in=paged):
        submissions[submission.event_id].append(submission)

    vidly_resubmit_form = forms.VidlyResubmitForm()
    context = {
        'paginate': paged,
        'status': status,
        'vidly_resubmit_form': vidly_resubmit_form,
        'repeated': repeated,
        'get_repeats': get_repeats,
        'submissions': submissions,
    }
    return render(request, 'manage/vidly_media.html', context)
Exemple #2
0
def vidly_media_resubmit(request):
    if request.POST.get('cancel'):
        return redirect(reverse('manage:vidly_media') + '?status=Error')

    form = forms.VidlyResubmitForm(data=request.POST)
    if not form.is_valid():
        return http.HttpResponse(str(form.errors))
    event = get_object_or_404(Event, pk=form.cleaned_data['id'])
    environment = event.template_environment or {}
    if not environment.get('tag') or environment.get('tag') == 'None':
        raise ValueError("Not a valid tag in template")

    if event.privacy != Event.PRIVACY_PUBLIC:
        token_protection = True  # no choice
    else:
        token_protection = form.cleaned_data['token_protection']

    base_url = get_base_url(request)
    webhook_url = base_url + reverse('manage:vidly_media_webhook')

    old_tag = environment['tag']
    url = prepare_vidly_video_url(form.cleaned_data['url'])
    shortcode, error = vidly.add_media(
        url=url,
        hd=form.cleaned_data['hd'],
        token_protection=token_protection,
        notify_url=webhook_url,
    )
    VidlySubmission.objects.create(
        event=event,
        url=url,
        token_protection=token_protection,
        hd=form.cleaned_data['hd'],
        tag=shortcode,
        submission_error=error
    )

    if error:
        messages.warning(
            request,
            "Media could not be re-submitted:\n<br>\n%s" % error
        )
    else:
        messages.success(
            request,
            "Event re-submitted to use tag '%s'" % shortcode
        )
        vidly.delete_media(old_tag)
        event.template_environment['tag'] = shortcode
        event.save()

        cache_key = 'vidly-query-%s' % old_tag
        cache.delete(cache_key)

    return redirect(reverse('manage:vidly_media') + '?status=Error')