Пример #1
0
def taggedPosts(request, tag):
    context = RequestContext(request)

    if request.method == 'GET':
        try:
            if request.user.is_authenticated():
                postList = []
                viewer = Author.objects.get(user=request.user)
                posts = post_utils.getVisibleToAuthor(viewer)
                taggedPosts = PostCategory.getPostWithCategory(tag)

                for categorizedPost in taggedPosts:
                    jsonPost = post_utils.get_post_json(categorizedPost.post)
                    if jsonPost in posts:
                        postList.append(jsonPost)

                context['posts'] = _getDetailedPosts(postList)
                context['visibility'] = post_utils.getVisibilityTypes()
                context['page_header'] = 'Posts tagged as %s' % tag
                context['specific'] = True

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

    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
def index(request):
    context = RequestContext(request)
    if request.method == 'DELETE':
        if request.user.is_authenticated():
            category = QueryDict(request.body).get('category_name')
            post_id = QueryDict(request.body).get('post_id')

            try:
                post = Post.objects.get(guid=post_id)
                PostCategory.removeCategory(
                    post, category,
                    Author.get_author_with_username(request.user.username))

                return HttpResponse(json.dumps({'msg': 'category deleted'}),
                                    content_type="application/json",
                                    status=200)
            except Exception as e:
                return HttpResponse(e.message,
                                    content_type="text/plain",
                                    status=400)

        else:
            loginError(context)

    elif request.method == 'POST':
        if request.user.is_authenticated():
            category = QueryDict(request.body).get('category_name')
            post_id = QueryDict(request.body).get('post_id')

            try:
                post = Post.objects.get(guid=post_id)
                PostCategory.addCategoryToPost(post, category)

                return HttpResponse(json.dumps({'msg': 'category added'}),
                                    content_type="application/json")
            except Exception as e:
                return HttpResponse(e.message,
                                    content_type="text/plain",
                                    status=400)
        else:
            loginError(context)
def index(request):
    context = RequestContext(request)
    if request.method == 'DELETE':
        if request.user.is_authenticated():
            category = QueryDict(request.body).get('category_name')
            post_id = QueryDict(request.body).get('post_id')

            try:
                post = Post.objects.get(guid=post_id)
                PostCategory.removeCategory(post, category, Author.get_author_with_username(request.user.username))

                return HttpResponse(json.dumps({'msg': 'category deleted'}),
                                    content_type="application/json",
                                    status=200)
            except Exception as e:
                return HttpResponse(e.message,
                                    content_type="text/plain",
                                    status=400)

        else:
            loginError(context)

    elif request.method == 'POST':
        if request.user.is_authenticated():
            category = QueryDict(request.body).get('category_name')
            post_id = QueryDict(request.body).get('post_id')

            try:
                post = Post.objects.get(guid=post_id)
                PostCategory.addCategoryToPost(post, category)

                return HttpResponse(json.dumps({'msg': 'category added'}),
                                    content_type="application/json")
            except Exception as e:
                return HttpResponse(e.message,
                                    content_type="text/plain",
                                    status=400)
        else:
            loginError(context)
def get_post_json(post):

    post_json = post.getJsonObj()

    comment_list = []
    comments = Comment.getCommentsForPost(post)
    for comment in comments:
        comment_list.append(comment.getJsonObj())

    category_list = []
    categories = PostCategory.getCategoryForPost(post)
    if categories is not None:
        for category in categories:
            category_list.append(category.name)

    post_json['comments'] = comment_list
    post_json['categories'] = category_list

    return post_json
def get_post_json(post):

    post_json = post.getJsonObj()

    comment_list = []
    comments = Comment.getCommentsForPost(post)
    for comment in comments:
        comment_list.append(comment.getJsonObj())

    category_list = []
    categories = PostCategory.getCategoryForPost(post)
    if categories is not None:
        for category in categories:
            category_list.append(category.name)

    post_json['comments'] = comment_list
    post_json['categories'] = category_list

    return post_json
Пример #6
0
def index(request):
    context = RequestContext(request)
    if request.method == 'GET':
        if 'application/json' in request.META['HTTP_ACCEPT']:
            return HttpResponseRedirect('/api/author/posts', status=302)
        else:
            if request.user.is_authenticated():
                try:
                    # get only posts made by friends and followees
                    viewer = Author.objects.get(user=request.user)
                    return render_to_response('index.html', _getAllPosts(viewer=viewer, time_line=True), context)
                except Author.DoesNotExist:
                    return _render_error('login.html', 'Please log in.', context)
            else:
                return _render_error('login.html', 'Please log in.', context)

    elif request.method == 'DELETE':
        try:
            if request.user.is_authenticated():
                post_utils.deletePost(QueryDict(request.body).get('post_id'))

                return HttpResponse(json.dumps({'msg': 'post deleted'}),
                                    content_type="application/json")
            else:
                return _render_error('login.html', 'Please log in.', context)
        except Exception as e:
            print "Error in posts: %s" % e

    elif request.method == 'POST':
        try:
            if request.user.is_authenticated():
                title = request.POST.get("title", "")
                description = request.POST.get("description", "")
                content = request.POST.get("text_body", "")
                author = Author.objects.get(user=request.user)
                visibility = request.POST.get("visibility_type", "")
                content_type = Post.MARK_DOWN if request.POST.get(
                    "markdown_checkbox", False) else Post.PLAIN_TEXT
                categories = request.POST.get("categories")

                new_post = Post.objects.create(title=title,
                                               description=description,
                                               guid=uuid.uuid1(),
                                               content=content,
                                               content_type=content_type,
                                               visibility=visibility,
                                               author=author,
                                               publication_date=timezone.now())

                if visibility == Post.ANOTHER_AUTHOR:
                    try:
                        visible_author = request.POST.get("visible_author", "")
                        visible_author_obj = Author.get_author_with_username(
                            visible_author)

                        VisibleToAuthor.objects.create(
                            visibleAuthor=visible_author_obj, post=new_post)
                    except Author.DoesNotExist:
                        # TODO: not too sure if care about this enough to
                        # handle it
                        print("hmm")

                # TODO: handle multiple image upload
                if len(request.FILES) > 0 and 'thumb' in request.FILES:
                    profile = DocumentForm(request.POST, request.FILES)
                    image = DocumentForm.createImage(
                        profile, request.FILES['thumb'])
                    PostImage.objects.create(post=new_post, image=image)

                category_list = categories.split(',')
                for category in category_list:
                    if len(category.strip()) > 0:
                        PostCategory.addCategoryToPost(new_post, category)

                viewer = Author.objects.get(user=request.user)
                return render_to_response('index.html', _getAllPosts(viewer=viewer, time_line=True), context)
            else:
                return redirect('login.html', 'Please log in.', context)
        except Exception as e:
            print "Error in posts: %s" % e

    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
def updatePost(postJson):
    post = Post.objects.filter(guid=postJson['guid'])
    if len(post) > 0:
        post = post.first()
        if 'title' in postJson:
            post.title = postJson['title']

        if 'description' in postJson:
            post.description = postJson['description']

        if 'content-type' in postJson:
            post.content_type = postJson['content-type']

        if 'content' in postJson:
            post.content = postJson['content']

        if 'visibility' in postJson:
            post.visibility = postJson['visibility']

        post.save()
    else:
        if 'author' in postJson:
            authorJson = postJson['author']
            if authorJson['host'] == LOCAL_HOST:
                author = Author.objects.filter(uuid=authorJson['id'])
                if len(author) > 0:
                    author = author.first()
                else:
                    raise Exception("invalid author")
            else:
                raise Exception("invalid author")
        else:
            raise Exception("invalid author")

        post = Post.objects.create(title=postJson['title'],
                                   description=postJson['description'],
                                   content_type=postJson['content-type'],
                                   content=postJson['content'],
                                   guid=postJson['guid'],
                                   visibility=postJson['visibility'],
                                   author=author,
                                   publication_date=parser.parse(
                                       postJson['pubDate']))
    if 'comments' in postJson:

        # easiest way to keep comments updated is by deleting exsting comments and adding the new ones
        Comment.removeCommentsForPost(post)

        comments = postJson['comments']
        for comment in comments:
            authorJson = comment['author']
            comment_id = comment['guid']

            commentFilter = Comment.objects.filter(guid=comment_id)
            if len(commentFilter) == 0:
                # no comment exists with the id, add it
                author = Author.objects.filter(uuid=authorJson['id'])

                if len(author) > 0:
                    author = author.first()
                else:
                    author = author_utils.createRemoteUser(
                        displayName=authorJson['displayname'],
                        host=authorJson['host'],
                        uuid=authorJson['id'])
                comment_text = comment['comment']
                Comment.objects.create(guid=comment_id,
                                       comment=comment_text,
                                       pubDate=parser.parse(
                                           comment['pubDate']),
                                       author=author,
                                       post=post)

    if 'categories' in postJson:

        #remove all previous categories and add the new ones
        PostCategory.removeAllCategoryForPost(post)

        categories = postJson['categories']
        for category in categories:
            PostCategory.addCategoryToPost(post, category)

    return get_post_json(post)
def updatePost(postJson):
    post = Post.objects.filter(guid=postJson['guid'])
    if len(post) > 0:
        post = post.first()
        if 'title' in postJson:
            post.title = postJson['title']

        if 'description' in postJson:
            post.description = postJson['description']

        if 'content-type' in postJson:
            post.content_type = postJson['content-type']

        if 'content' in postJson:
            post.content = postJson['content']

        if 'visibility' in postJson:
            post.visibility = postJson['visibility']

        post.save()
    else:
        if 'author' in postJson:
            authorJson = postJson['author']
            if authorJson['host'] == LOCAL_HOST:
                author = Author.objects.filter(uuid=authorJson['id'])
                if len(author) > 0:
                    author = author.first()
                else:
                    raise Exception("invalid author")
            else:
                raise Exception("invalid author")
        else:
            raise Exception("invalid author")

        post = Post.objects.create(title=postJson['title'],
                                   description=postJson['description'],
                                   content_type=postJson['content-type'],
                                   content=postJson['content'],
                                   guid=postJson['guid'],
                                   visibility=postJson['visibility'],
                                   author=author,
                                   publication_date=parser.parse(postJson['pubDate']))
    if 'comments' in postJson:

        # easiest way to keep comments updated is by deleting exsting comments and adding the new ones
        Comment.removeCommentsForPost(post)

        comments = postJson['comments']
        for comment in comments:
            authorJson = comment['author']
            comment_id = comment['guid']

            commentFilter = Comment.objects.filter(guid=comment_id)
            if len(commentFilter) == 0:
                # no comment exists with the id, add it
                author = Author.objects.filter(uuid=authorJson['id'])

                if len(author) > 0:
                    author = author.first()
                else:
                    author = author_utils.createRemoteUser(displayName=authorJson['displayname'],
                                                         host=authorJson['host'],
                                                         uuid=authorJson['id'])
                comment_text = comment['comment']
                Comment.objects.create(guid=comment_id,
                                       comment=comment_text,
                                       pubDate=parser.parse(comment['pubDate']),
                                       author=author,
                                       post=post)

    if 'categories' in postJson:

        #remove all previous categories and add the new ones
        PostCategory.removeAllCategoryForPost(post)

        categories = postJson['categories']
        for category in categories:
            PostCategory.addCategoryToPost(post, category)

    return get_post_json(post)