コード例 #1
0
def public_posts(request, post_id=None):
    """Return all posts marked as public on the server.
    If a post_id is specified, only return a single post with the provided id.
    """
    if request.user.is_authenticated():
        if request.method == 'GET':
            try:
                response = utils._get_posts(request, post_id, POST)

                return HttpResponse(json.dumps(response),
                                    content_type='application/json',
                                    status=200)
            except Exception as e:
                return HttpResponse(e.message,
                                    content_type='text/plain',
                                    status=500)

        elif request.method == 'POST' or request.method == 'PUT':
            try:
                post_data = json.loads(request.body)
                response = post_utils.updatePost(post_data)

                return HttpResponse(json.dumps(response),
                                    content_type='application/json',
                                    status=200)
            except Exception as e:
                return HttpResponse(e.message,
                                    content_type='text/plain',
                                    status=500)

    return HttpResponse(status=403)
コード例 #2
0
def post(request, post_id):
    context = RequestContext(request)

    if request.method == 'GET' or request.method == 'POST':
        if 'application/json' in request.META['HTTP_ACCEPT']:
            return HttpResponseRedirect('/api/posts/%s' % post_id, status=302)
        else:
            try:
                if request.user.is_authenticated():
                    viewer = Author.objects.get(user=request.user)
                else:
                    viewer = None
                post = post_utils.getPostById(post_id, viewer)

                context['posts'] = _getDetailedPosts([post])
                # context indicating that we are seeing a specific user stream
                context['specific'] = True
                context['page_header'] = 'Posts with ID %s' % post_id

                return render_to_response('index.html', context)
            except Exception as e:
                print "Error in posts: %s" % e

    elif request.method == 'PUT':
        try:
            jsonData = json.load(request.body)
            post = post_utils.updatePost(jsonData)
            return HttpResponse(json.dumps(post_utils.get_post_json(post)),
                                content_type='application/json',
                                status=200)
        except Author.DoesNotExist:
            return HttpResponse(status=403)

    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
コード例 #3
0
def public_posts(request, post_id=None):
    """Return all posts marked as public on the server.
    If a post_id is specified, only return a single post with the provided id.
    """
    if request.user.is_authenticated():
        if request.method == 'GET':
            try:
                response = utils._get_posts(request, post_id, POST)

                return HttpResponse(json.dumps(response),
                                    content_type='application/json',
                                    status=200)
            except Exception as e:
                return HttpResponse(e.message,
                                    content_type='text/plain',
                                    status=500)

        elif request.method == 'POST' or request.method == 'PUT':
            try:
                post_data = json.loads(request.body)
                response = post_utils.updatePost(post_data)

                return HttpResponse(json.dumps(response),
                                    content_type='application/json',
                                    status=200)
            except Exception as e:
                return HttpResponse(e.message,
                                    content_type='text/plain',
                                    status=500)

    return HttpResponse(status=403)