Example #1
0
def add_topic(request, section_id):
    context = RequestContext(request)

    if request.method == 'POST':
        form = TopicForm(request.POST)

        if form.is_valid():
            form.save(commit=True)
            return index(request)
        else:
            print(form.errors)
    else:
        form = TopicForm()

    return render_to_response('forum/add_topic.html', {'form': form, 'section_id': section_id}, context)
Example #2
0
def topic_create(request):
    if request.method == 'POST':
        topicForm = TopicForm(request.POST, prefix="topic")
        messageForm = MessageForm(request.POST, prefix="message")
        if topicForm.is_valid() and messageForm.is_valid():
            topic = topicForm.save(commit=False)
            topic.author = request.user
            topic.creation_date = datetime.datetime.now()
            topic.save()
            message = messageForm.save(commit=False)
            message.author = request.user
            message.creation_date = datetime.datetime.now()
            message.topic = topic
            message.save()

            domain = Site.objects.get_current().domain
            mail_subject = _('A new topic has been posted')
            mail_message = _('%(topic)s : %(topic_url)s') % {'topic': topic.title,
                                                             'topic_url': 'https://%s%s' % (domain, reverse('topic_detail', kwargs={'id': topic.id}))}
            mail_admins(mail_subject, mail_message, fail_silently=(not settings.DEBUG))

            messages.success(request, _('The topic has been saved successfully'))
            return redirect(reverse('topic_detail', kwargs={'id': topic.id}))
        else:
            messages.error(request, _('Data provided are invalid'))
    else:
        topicForm = TopicForm(prefix="topic")
        messageForm = MessageForm(prefix="message")

    return render_to_response('topic/create.html',
                              {'active': 'help',
                               'title': _('New topic'),
                               'topicForm': topicForm,
                               'messageForm': messageForm},
                               context_instance=RequestContext(request))
Example #3
0
def _topic_create_POST(request):
    commit = True
    try:
        post = Post(author=request.forum_user)
        post_form = PostForm(request.POST, instance=post)
        if post_form.is_valid():
            post = post_form.save()
            topic = Topic(author=request.forum_user, first_post=post,
                    last_post=post)
            topic_form = TopicForm(request.POST, instance=topic)
            if topic_form.is_valid():
                topic = topic_form.save()
                post.topic = topic
                post.save()
                for category in topic.categories.all():
                    category.topic_count = category.topic_set.all().count()
                    category.post_count = Post.objects\
                            .filter(topic__in=category.topic_set.all()).count()
                    category.save()
                return redirect(post.get_absolute_url())
        else:
            post_form = PostForm(request.POST)
        ctx = {
            'forum_user': request.forum_user,
            'topic_form': topic_form,
            'post_form': post_form,
        }
        return render(request, 'forum/topic/create.html', ctx)

    finally:
        if commit:
            transaction.commit()
        else:
            transaction.rollback()
Example #4
0
def topics(request):
    all_topic_list = Topic.objects.all().order_by('title')
    if request.method == 'POST': 
        user = request.user
        form = TopicForm(request.POST)
        if form.is_valid():
            form.save(user)
        else:
            return render_to_response('forum/topics.html', 
                              {'all_topic_list': all_topic_list, 'form': form},
                              context_instance=RequestContext(request))
    form = TopicForm()
    all_topic_list = Topic.objects.all().order_by('title')
    return render_to_response('forum/topics.html', 
                              {'all_topic_list': all_topic_list, 'form': form},
                              context_instance=RequestContext(request))
Example #5
0
def add_topic(request, section_id):
    context = RequestContext(request)

    if request.method == 'POST':
        form = TopicForm(request.POST)

        if form.is_valid():
            form.save(commit=True)
            return index(request)
        else:
            print(form.errors)
    else:
        form = TopicForm()

    return render_to_response('forum/add_topic.html', {
        'form': form,
        'section_id': section_id
    }, context)
Example #6
0
def topic_list(request, template='topic/list.html'):
    d = {}
    d['form'] = TopicForm()
    if request.method == 'POST':
        form = TopicForm(request.POST)
        if form.is_valid():
            item = form.save()
            return JsonResponse(data={'id': item.id, 'name': str(item), 'form': TopicForm().as_p(), 'token': get_token(request)})
        else:
            d['form'] = form
            return JsonResponse(data={'form': d['form'].as_p(), 'token': get_token(request)}, success=False)
    d['topic_list'] = Topic.objects.all()
    return render(request, template, d)
Example #7
0
def new_topic(request):
    if request.method != 'POST':
        form = TopicForm()
    else:
        form = TopicForm(request.POST)
        if form.is_valid():
            new_topic = form.save(commit=False)
            new_topic.author = request.user
            new_topic.last_poster = new_topic.author
            new_topic.save()
            return redirect('../')

    return render(request, 'forum/new_topic.html', {'form': form})
Example #8
0
    def add_topic(self, request, *args, **kwargs):
        group = self.get_object()
        check_or_business_error(group in request.user.member_groups.all(),
                                '1010')
        form = TopicForm(data=request.data)
        check_or_business_error(form.is_valid(),
                                '1000',
                                error_data=form.errors)
        topic = form.save(group=group, create_user=request.user, is_topic=True)
        data = TopicDetailSerializer(topic).data

        raise_heat.delay(group.id, 'UserGroup', 2)
        return success_rsp(data)
Example #9
0
def topic_details(request, id, template='topic/details.html'):
    d = {}
    item = get_object_or_404(Topic, pk=id)
    d['form'] = TopicForm(instance=item)
    if request.method == 'POST':
        form = TopicForm(request.POST, instance=item)
        if form.is_valid():
            item = form.save()
            return JsonResponse(data={'form': TopicForm(instance=item).as_p(), 'token': get_token(request)})
        else:
            d['form'] = form
            return JsonResponse(data={'form': d['form'].as_p(), 'token': get_token(request)}, success=False)
    d['topic'] = Topic.objects.get(pk=id)
    return render(request, template, d)
Example #10
0
def addtopic(request, section_id):

    form = TopicForm(request.POST or None)

    if form.is_valid():
        topic = form.save(commit=False)
        topic.section = get_object_or_404(Section, pk=section_id)
        topic.timestamp = datetime.today()
        topic.author = request.user.username
        topic.save()
        
    return redirect(topics, section_id)
  

        
Example #11
0
def edit_topic(request, topic_id):
    topic = get_object_or_404(Topic, id=topic_id)
    posts = topic.post_set.order_by('date_added')
    if topic.author != request.user:
        raise Http404

    if request.method != 'POST':
        form = TopicForm(instance=topic)
    else:
        form = TopicForm(request.POST, instance=topic)
        if form.is_valid():
            new_topic = form.save(commit=False)
            new_topic.edit_date = timezone.now()
            new_topic.save()
            return redirect('../')

    return render(request, 'forum/edit_topic.html', {'topic': topic, 'posts': posts, 'form': form})
Example #12
0
def forum_details(request, id, template="forum/details.html"):
    """View for a forum.
    Autogenerated initially."""
    d = {}
    d["form"] = TopicForm()
    if request.method == "POST":
        form = TopicForm(request.POST)
        if form.is_valid():
            topic = form.save(commit=False)
            topic.forum = Forum.objects.get(pk=id)
            topic.save()
        #             return JsonResponse(data={'id': item.id, 'name': str(item), 'form': TopicForm().as_p(), 'token': get_token(request)})
        else:
            d["form"] = form
    #             return JsonResponse(data={'form': d['form'].as_p(), 'token': get_token(request)}, success=False)
    d["forum"] = Forum.objects.get(pk=id)
    d["topics"] = Topic.objects.filter(forum_id=id)
    return render(request, template, d)
Example #13
0
def topic_create(request):
    if request.method == 'POST':
        topicForm = TopicForm(request.POST, prefix="topic")
        messageForm = MessageForm(request.POST, prefix="message")
        if topicForm.is_valid() and messageForm.is_valid():
            topic = topicForm.save(commit=False)
            topic.author = request.user
            topic.creation_date = datetime.datetime.now()
            topic.save()
            message = messageForm.save(commit=False)
            message.author = request.user
            message.creation_date = datetime.datetime.now()
            message.topic = topic
            message.save()

            domain = Site.objects.get_current().domain
            mail_subject = _('A new topic has been posted')
            mail_message = _('%(topic)s : %(topic_url)s') % {
                'topic':
                topic.title,
                'topic_url':
                'https://%s%s' %
                (domain, reverse('topic_detail', kwargs={'id': topic.id}))
            }
            mail_admins(mail_subject,
                        mail_message,
                        fail_silently=(not settings.DEBUG))

            messages.success(request,
                             _('The topic has been saved successfully'))
            return redirect(reverse('topic_detail', kwargs={'id': topic.id}))
        else:
            messages.error(request, _('Data provided are invalid'))
    else:
        topicForm = TopicForm(prefix="topic")
        messageForm = MessageForm(prefix="message")

    return render_to_response('topic/create.html', {
        'active': 'help',
        'title': _('New topic'),
        'topicForm': topicForm,
        'messageForm': messageForm
    },
                              context_instance=RequestContext(request))