Exemplo n.º 1
0
def editPost(request, post_uid=''):
    try:
        post = Post.objects.get(uid=post_uid)
        thread = post.thread
    except (Post.DoesNotExist, Thread.DoesNotExist):
        raise Http404
    if thread.locked or (request.user != post.created_by and not request.user.is_superuser):
        return HttpResponseForbidden()
    if request.method == 'POST':
        postForm = PostForm(request.POST, instance=post, prefix='post')
        threadForm = ThreadForm(request.POST, instance=thread, prefix='thread')
        if postForm.is_valid():
            postForm.save()
        if threadForm.is_valid():
            threadForm.save()
        return redirect('threadPage', thread.topic.urlTitle, thread.id)
    else:
        postForm = PostForm(instance=post, prefix='post')
        if request.user.is_superuser and thread.op == post:
            threadForm = ThreadForm(instance=thread, prefix='thread')
        else:
            threadForm = None
            postForm.fields['content'].label = ''
        context = dict(postForm=postForm, threadForm=threadForm, post_uid=post.uid, thread=thread, post=post)
        return render(request, 'djeddit/edit_post.html', context)
Exemplo n.º 2
0
def editPost(request, post_uid=''):
    try:
        post = Post.objects.get(uid=post_uid)
        thread = post.thread
    except (Post.DoesNotExist, Thread.DoesNotExist):
        raise Http404
    if thread.locked or (request.user != post.created_by and not request.user.is_superuser):
        return HttpResponseForbidden()
    if request.method == 'POST':
        postForm = PostForm(request.POST, instance=post, prefix='post')
        threadForm = ThreadForm(request.POST, instance=thread, prefix='thread')
        if postForm.is_valid():
            postForm.save()
        if threadForm.is_valid():
            threadForm.save()
        url = thread.relativeUrl

        if ('HTTP_REFERER' in request.META and 'pib_mobile' in request.META.get('HTTP_REFERER')):
            url += '?pib_mobile=true'
        return HttpResponseRedirect(url)
    else:
        postForm = PostForm(instance=post, prefix='post')
        if request.user.is_superuser and thread.op == post:
            threadForm = ThreadForm(instance=thread, prefix='thread')
        else:
            threadForm = None
            postForm.fields['content'].label = ''
        context = dict(postForm=postForm, threadForm=threadForm, post_uid=post.uid, thread=thread, post=post)
        return render(request, 'djeddit/edit_post.html', context)
Exemplo n.º 3
0
def createThread(request, topic_title=None):
    if topic_title:
        try:
            if request.method == 'POST':
                topic = Topic.getTopic(topic_title)
                threadForm = ThreadForm(request.POST, prefix='thread')
                postForm = PostForm(request.POST, prefix='post')
                if threadForm.is_valid() and postForm.is_valid():
                    thread = threadForm.save(commit=False)
                    post = postForm.save(commit=False)
                    thread.op = post
                    thread.topic = topic
                    thread.save()
                    if request.user.is_authenticated():
                        post.created_by = request.user
                    post.save()
                    return redirect('threadPage', topic.urlTitle, thread.id)
            else:
                threadForm = ThreadForm(prefix='thread')
                postForm = PostForm(prefix='post')
            context = dict(threadForm=threadForm, postForm=postForm)
            return render(request, 'djeddit/create_thread.html', context)
        except Topic.DoesNotExist:
            pass
    return redirect('topics')
Exemplo n.º 4
0
def createThread(request, topic_title=None):
    if request.method == 'POST':
        if topic_title == 'None':
            topic_id = request.POST['thread-topic']
            topic_title = Topic.objects.get(pk=topic_id)
        try:
            topic = Topic.getTopic(topic_title)
            threadForm = ThreadForm(request.POST, prefix='thread')
            postForm = PostForm(request.POST, prefix='post')
            if threadForm.is_valid() and postForm.is_valid():
                thread = threadForm.save(commit=False)
                post = postForm.save(commit=False)
                thread.op = post
                thread.topic = topic
                post.setMeta(request)
                post.save()
                thread.save()
                if is_authenticated(request):
                    post.created_by = request.user
                post.save()
                url = thread.relativeUrl
                if ('HTTP_REFERER' in request.META and 'pib_mobile' in request.META.get('HTTP_REFERER')):
                    url += '?pib_mobile=true'
                return HttpResponseRedirect(url)
        except Topic.DoesNotExist:
            pass
    # Else it's a GET request
    else:
        if topic_title:
            topic = Topic.getTopic(topic_title)
            threadForm = ThreadForm(prefix='thread', initial={'topic': topic.id})
        else:
            threadForm = ThreadForm(prefix='thread')
        postForm = PostForm(prefix='post')
        context = dict(threadForm=threadForm, postForm=postForm, topic=topic_title)
        return render(request, 'djeddit/create_thread.html', context)
Exemplo n.º 5
0
 def testIsValid(self):
     tf = ThreadForm(dict(title='Test Thread'))
     self.assertTrue(tf.is_valid())