def create(request):
    """Creates an image."""
    context = RequestContext(request)
    logger.error(request.FILES['thumb'])
    if request.method == 'POST':
        if request.user.is_authenticated():
            profile = DocumentForm(request.POST, request.FILES)
            DocumentForm.createImage(profile, request.FILES['thumb'])
        else:
            return _render_error('login.html', 'Please log in.', context)

    return render_to_response("display.html")
def upload(request):
    """Uploads an image."""
    context = RequestContext(request)
    if request.user.is_authenticated():
        image_form = DocumentForm()
        return render(request, "uploadImage.html", {'imgForm': image_form})
    else:
        return _render_error('login.html', 'Please log in.', context)
Beispiel #3
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'))