Example #1
0
def like_by_url(request):
    if not request.user.is_authenticated():
        raise Unauthorized()

    querydict = request.GET if request.method == 'GET' else request.POST
    try:
        normalized_url = url_fix(querydict['url'])
    except KeyError:
        raise BadRequest('Parameter:url missing')
    except MalformedURLException:
        raise BadRequest('Malformed URL:%s' % url)

    try:
        video = Video.objects.get(url=normalized_url)

        try:
            UserVideo.objects.get(user=request.user, video=video, liked_timestamp__isnull=False)
        except UserVideo.DoesNotExist:
            push_like_to_fb.delay(video, request.user)

        user_video = request.user.like_video(video)
        task_id = video.task_id

    except Video.DoesNotExist:
        video = Video(url=normalized_url)
        video.save()

        user_video = UserVideo(user=request.user,
                               video=video,
                               host=request.META.get('HTTP_REFERER'),
                               liked=True,
                               liked_timestamp=datetime.utcnow())
        user_video.save()

        # Fetch video metadata in background
        task = fetch.delay(request.user.id,
                           normalized_url,
                           user_video.host,
                           callback=push_like_to_fb.subtask((request.user, )))

        task_id = task.task_id

    info = as_dict(user_video)
    info['task_id'] = task_id
    info['first'] = request.user.notifications()['firstlike']
    return info
Example #2
0
def add(request):
    if not request.user.is_authenticated():
        raise Unauthorized()

    querydict = request.GET if request.method == 'GET' else request.POST
    try:
        normalized_url = url_fix(querydict['url'])
    except KeyError:
        raise BadRequest('Parameter:url missing')
    except MalformedURLException:
        raise BadRequest('Malformed URL:%s' % url)

    try:
        user_video = UserVideo.objects.get(user=request.user, video__url=normalized_url)

        if user_video.saved:
            raise BadRequest('Video:%s already saved with id:%s' % (user_video.video.url, user_video.video.id))

    except UserVideo.DoesNotExist:
        try:
            video = Video.objects.get(url=normalized_url)
        except Video.DoesNotExist:
            video = Video(url=normalized_url)
            video.save()

        user_video = UserVideo(user=request.user, video=video)

    user_video.saved = True
    user_video.saved_timestamp = datetime.utcnow()
    user_video.host = request.META.get('HTTP_REFERER')
    user_video.save()

    # Fetch video metadata in background
    task = fetch.delay(request.user.id, normalized_url, user_video.host)

    info = as_dict(user_video)
    info.update({'first': request.user.saved_videos().count() == 1,
                 'unwatched': request.user.unwatched_videos().count(),
                 'task_id': task.task_id})

    return info