def _get_posts(request, id, type):
    if type == AUTHOR:
        viewer = []
        if (request.user.username != ''):
            viewer = Author.objects.filter(user=request.user)
        if len(viewer) > 0:
            viewer = viewer[0]
        else:
            viewer = None

        if id is not None:
            author = Author.objects.filter(uuid=id)
            if len(author) > 0:
                author = author[0]
            else:
                return {'posts': []}
            posts = post_utils.getVisibleToAuthor(viewer=viewer,
                                                  author=author,
                                                  localOnly=True)
        else:
            posts = post_utils.getVisibleToAuthor(viewer=viewer,
                                                  localOnly=True)

    else:
        if id is not None:
            post = post_utils.getPostById(id)
            if post is not None:
                return post.getJsonObj()
            else:
                return {}
        else:
            posts = post_utils.getVisibleToAuthor(localOnly=True)

    return {'posts': posts}
예제 #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'))
def _get_posts(request, id, type):
    if type == AUTHOR:
        viewer = []
        if(request.user.username != ''):
            viewer = Author.objects.filter(user=request.user)
        if len(viewer) > 0:
            viewer = viewer[0]
        else:
            viewer = None

        if id is not None:
            author = Author.objects.filter(uuid=id)
            if len(author) > 0:
                author = author[0]
            else:
                return {'posts': []}
            posts = post_utils.getVisibleToAuthor(viewer=viewer, author=author, localOnly=True)
        else:
            posts = post_utils.getVisibleToAuthor(viewer=viewer, localOnly=True)

    else:
        if id is not None:
            post = post_utils.getPostById(id)
            if post is not None:
                return post.getJsonObj()
            else:
                return {}
        else:
            posts = post_utils.getVisibleToAuthor(localOnly=True)

    return {'posts': posts}