Ejemplo n.º 1
0
def get_upload(request):
    if request.POST:
        missing_keys = filter(lambda i: i not in request.POST.keys(),
                              [u'md5sum', u'size'])

        if missing_keys:
            return render_to_json({
                'success': False, 'msg': _('md5sum and size required.')},
                status=400)

        if int(request.POST['size']) > settings.MAXIMUM_UPLOAD_SIZE:
            return render_to_json({
                'success': False, 'msg': _('File is too big to upload.')},
                status=400)

        for model in [Image, Video]:
            try:
                obj = model.objects.get(md5sum=request.POST['md5sum'])
                return render_to_json({'status': 'uploaded',
                                       'object': obj.serialize()})
            except model.DoesNotExist:
                pass

        upload = Upload.objects.create(md5sum=request.POST['md5sum'],
                                       size=int(request.POST['size']),
                                       owner=request.user)

        return render_to_json({'status': 'uploading',
                               'upload_id': upload.upload_id})
    else:
        return render_to_json({
            'success': False, 'msg': _('post method required.')},
            status=400)
Ejemplo n.º 2
0
def like(request):
    if not request.user.is_authenticated():
        return render_to_json(
            {'errMsg': _('You have to login to complete this action.')},
            HttpResponse, 401)

    # TODO: Make it decorator
    if not request.POST:
        return render_to_json(
            {'errMsg': _('You have send bad data.')},
            HttpResponse, 400)

    sid, val = request.POST.get('sid'), request.POST.get('val')

    if not sid:
        return HttpResponse(status=400)

    story = get_object_or_404(Story, id=int(sid))
    is_liked = story.set_like(request.user, liked=bool(int(val)))
    notify(ntype_slug='user_liked_my_answer',
           sub=request.user,
           obj=story,
           recipient=story.owner,
           ignored_recipients=[request.user],
           url=story.get_absolute_url())
    like_count = story.get_like_count_from_redis()
    return HttpResponse(json.dumps(
        {'like_count': like_count,
         'is_liked': is_liked}))
Ejemplo n.º 3
0
def like(request):
    if not request.user.is_authenticated():
        return render_to_json(
            {'errMsg': _('You have to login to complete this action.')},
            HttpResponse, 401)

    # TODO: Make it decorator
    if not request.POST:
        return render_to_json({'errMsg': _('You have send bad data.')},
                              HttpResponse, 400)

    sid, val = request.POST.get('sid'), request.POST.get('val')

    if not sid:
        return HttpResponse(status=400)

    story = get_object_or_404(Story, id=int(sid))
    is_liked = story.set_like(request.user, liked=bool(int(val)))
    notify(ntype_slug='user_liked_my_answer',
           sub=request.user,
           obj=story,
           recipient=story.owner,
           url=story.get_absolute_url())
    like_count = story.get_like_count_from_redis()
    return HttpResponse(
        json.dumps({
            'like_count': like_count,
            'is_liked': is_liked
        }))
Ejemplo n.º 4
0
def get_upload(request):
    if request.POST:
        missing_keys = filter(lambda i: i not in request.POST.keys(),
                              [u'md5sum', u'size', u'model'])
        if missing_keys:
            return render_to_json(
                {
                    'success': False,
                    'msg': _('md5sum and size required.')
                }, HttpResponseBadRequest)
        model = Upload.MODEL_MAPPING[request.POST['model']]
        try:
            obj = model.objects.get(md5sum=request.POST['md5sum'])
            return render_to_json({
                'status': 'uploaded',
                'object': obj.serialize()
            })
        except model.DoesNotExist:
            pass
        upload = Upload.objects.create(md5sum=request.POST['md5sum'],
                                       size=int(request.POST['size']),
                                       model=request.POST['model'],
                                       owner=request.user)
        return render_to_json({
            'status': 'uploading',
            'upload_id': upload.upload_id
        })
    else:
        return render_to_json(
            {
                'success': False,
                'msg': _('post method required.')
            }, HttpResponseBadRequest)
Ejemplo n.º 5
0
def upload(request, upload_id):
    """
    Get upload_id, content-range.

    IF Content Range start is not upload offset raise Error.
    Append chunk.

    IF file finished return obj.
    """
    upload = get_object_or_404(Upload,
                               upload_id=upload_id,
                               owner=request.user,
                               expires_at__gte=datetime.now())
    chunk = request.body
    content_range = request.META.get('HTTP_CONTENT_RANGE', '')
    match = CONTENT_RANGE_PATTERN.match(content_range)

    if not match:
        return HttpResponseBadRequest('Content Range header required.')

    start = int(match.group('start'))
    end = int(match.group('end'))

    if upload.offset != start:
        return HttpResponseBadRequest('Offsets does not match.')

    chunk_size = end - start
    try:
        upload.append_data(chunk, size=chunk_size)
    except Exception as err:
        return render_to_json({
            'status': 'failed',
            'msg': err.status
        },
                              status=400)

    if end >= upload.size:
        obj = upload.convert_to_model()
        return render_to_json({
            'status': 'uploaded',
            'object': obj.serialize()
        })
    return render_to_json({
        'upload_id': upload.upload_id,
        'status': 'uploading',
        'offset': upload.offset
    })
Ejemplo n.º 6
0
def get_upload(request):
    if request.POST:
        missing_keys = filter(lambda i: i not in request.POST.keys(),
                              [u'md5sum', u'size'])

        if missing_keys:
            return render_to_json(
                {
                    'success': False,
                    'msg': _('md5sum and size required.')
                },
                status=400)

        if int(request.POST['size']) > settings.MAXIMUM_UPLOAD_SIZE:
            return render_to_json(
                {
                    'success': False,
                    'msg': _('File is too big to upload.')
                },
                status=400)

        for model in [Image, Video]:
            try:
                obj = model.objects.get(md5sum=request.POST['md5sum'])
                return render_to_json({
                    'status': 'uploaded',
                    'object': obj.serialize()
                })
            except model.DoesNotExist:
                pass

        upload = Upload.objects.create(md5sum=request.POST['md5sum'],
                                       size=int(request.POST['size']),
                                       owner=request.user)

        return render_to_json({
            'status': 'uploading',
            'upload_id': upload.upload_id
        })
    else:
        return render_to_json(
            {
                'success': False,
                'msg': _('post method required.')
            }, status=400)
Ejemplo n.º 7
0
def pending_follow_request_action(request):
    if request.method == 'POST':

        frpk = request.POST.get('pk')
        try:
            frpk = int(frpk)
        except ValueError:
            return render_to_json({'errMsg': 'Invalida data'},
                                  HttpResponseBadRequest)

        action = request.POST['action']

        follow_request = get_object_or_404(UserFollow,
                                           pk=frpk,
                                           target=request.user)

        if action == 'accept':
            follow_request.status = UserFollow.FOLLOWING
            follow_request.save()
            follower_count_changed.send(sender=request.user)
            notify(ntype_slug='user_accepted_my_follow_request',
                   sub=follow_request.target,
                   obj=follow_request,
                   recipient=follow_request.follower,
                   url=follow_request.target.get_absolute_url())
            return render_to_json({'success': True})
        elif action == 'accept-restricted':
            follow_request.status = UserFollow.FOLLOWING_RESTRICTED
            follow_request.save()
            follower_count_changed.send(sender=request.user)
            notify(ntype_slug='user_accepted_my_follow_request',
                   sub=follow_request.target,
                   obj=follow_request,
                   recipient=follow_request.follower,
                   url=follow_request.target.get_absolute_url())
        if action == 'decline':
            follow_request.delete()
            return render_to_json({'success': True})
    return render_to_json({'success': False, 'message': 'Invalida data'})
Ejemplo n.º 8
0
def pending_follow_request_action(request):
    if request.method == 'POST':

        frpk = request.POST.get('pk')
        try:
            frpk = int(frpk)
        except ValueError:
            return render_to_json(
                {'errMsg': 'Invalida data'}, HttpResponseBadRequest)

        action = request.POST['action']

        follow_request = get_object_or_404(
            UserFollow, pk=frpk, target=request.user)

        if action == 'accept':
            follow_request.status = UserFollow.FOLLOWING
            follow_request.save()
            follower_count_changed.send(sender=request.user)
            notify(ntype_slug='user_accepted_my_follow_request',
                   sub=follow_request.target,
                   obj=follow_request,
                   recipient=follow_request.follower,
                   url=follow_request.target.get_absolute_url())
            return render_to_json({'success': True})
        elif action == 'accept-restricted':
            follow_request.status = UserFollow.FOLLOWING_RESTRICTED
            follow_request.save()
            follower_count_changed.send(sender=request.user)
            notify(ntype_slug='user_accepted_my_follow_request',
                   sub=follow_request.target,
                   obj=follow_request,
                   recipient=follow_request.follower,
                   url=follow_request.target.get_absolute_url())
        if action == 'decline':
            follow_request.delete()
            return render_to_json({'success': True})
    return render_to_json({'success': False,
                           'message': 'Invalida data'})
Ejemplo n.º 9
0
def upload(request, upload_id):
    """
    Get upload_id, content-range.

    IF Content Range start is not upload offset raise Error.
    Append chunk.

    IF file finished return obj.
    """
    upload = get_object_or_404(Upload, upload_id=upload_id,
                               owner=request.user,
                               expires_at__gte=datetime.now())
    chunk = request.body
    content_range = request.META.get('HTTP_CONTENT_RANGE', '')
    match = CONTENT_RANGE_PATTERN.match(content_range)

    if not match:
        return HttpResponseBadRequest('Content Range header required.')

    start = int(match.group('start'))
    end = int(match.group('end'))

    if upload.offset != start:
        return HttpResponseBadRequest('Offsets does not match.')

    chunk_size = end - start
    try:
        upload.append_data(chunk, size=chunk_size)
    except Exception as err:
        return render_to_json({'status': 'failed',
                               'msg': err.status}, status=400)

    if end >= upload.size:
        obj = upload.convert_to_model()
        return render_to_json({'status': 'uploaded',
                               'object': obj.serialize()})
    return render_to_json({'upload_id': upload.upload_id,
                           'status': 'uploading',
                           'offset': upload.offset})
Ejemplo n.º 10
0
def pending_question_action(request):
    def _reject(question):
        question.status = Question.REJECTED
        question.save()
        return render_to_json({'qpk': question.pk, 'status': question.status})

    qpk, action = request.POST.get('qpk'), request.POST.get('action')
    question = get_object_or_404(Question, pk=qpk, questionee=request.user)
    action_method = {'reject': _reject}.get(action)
    if action_method:
        return action_method(question)
    else:
        return render_to_json({'errMsg': _('Action not found')},
                              HttpResponseBadRequest)
Ejemplo n.º 11
0
def pending_question_action(request):
    def _reject(question):
        question.status = Question.REJECTED
        question.save()
        return render_to_json({'qpk': question.pk,
                               'status': question.status})

    qpk, action = request.POST.get('qpk'), request.POST.get('action')
    question = get_object_or_404(Question, pk=qpk, questionee=request.user)
    action_method = {'reject': _reject}.get(action)
    if action_method:
        return action_method(question)
    else:
        return render_to_json({'errMsg': _('Action not found')},
                              HttpResponseBadRequest)
Ejemplo n.º 12
0
 def _reject(question):
     question.status = Question.REJECTED
     question.save()
     return render_to_json({'qpk': question.pk,
                            'status': question.status})
Ejemplo n.º 13
0
def delete_comment(request, cid):
    comment = get_object_or_404(Comment, id=cid, owner=request.user)
    comment.status = Comment.DELETED_BY_OWNER
    comment.save()
    comment.story.update_comment_count(save=True)
    return render_to_json({'status': 'deleted'})
Ejemplo n.º 14
0
 def _reject(question):
     question.status = Question.REJECTED
     question.save()
     return render_to_json({'qpk': question.pk, 'status': question.status})
Ejemplo n.º 15
0
def delete_comment(request, cid):
    comment = get_object_or_404(Comment, id=cid, owner=request.user)
    comment.status = Comment.DELETED_BY_OWNER
    comment.save()
    comment.story.update_comment_count(save=True)
    return render_to_json({'status': 'deleted'})