예제 #1
0
def add_forum(request):
    # 'belong','parent_id','name','tag','path_list','allow_topic','icon','topic_credit',
    # 'post_credit','visit_level','topic_level','post_level']
    if request.method == 'GET':
        form = ForumForm()
    else:
        if not request.is_ajax():
            pass
        form = ForumForm(request.POST, request.FILES)
        if form.is_valid():
            forum = Forum()
            data = form.cleaned_data
            forum.belong = data['belong']
            forum.parent_id = data['parent_id']
            forum.name = data['name']
            forum.tag = data['tag']
            forum.descr = data['descr']
            forum.content = data['content']
            count = len(forum.parent_id)
            if count == 32:
                forum.count = count
            if forum.parent_id == '0' * 32:
                forum.path_list = ''
            else:
                parent = Forum.objects.get(id=forum.parent_id)
                forum.path_list = parent.path_list + parent.tag + ','
            parents = Forum.objects.filter(
                parent_id=forum.parent_id).aggregate(Max('display_order'))
            if parents['display_order__max']:
                forum.display_order = parents['display_order__max'] + 1
            else:
                forum.display_order = 1
            forum.allow_topic = data['allow_topic']
            forum.icon = data['icon']
            forum.topic_credit = data['topic_credit']
            forum.post_credit = data['post_credit']
            forum.visit_level = data['visit_level']
            forum.topic_level = data['topic_level']
            forum.post_level = data['post_level']
            forum.save()
            # 同时添加默认主题
            subjects = [Subject(name='', color='#000000', forum=forum)]
            subject_list = json.loads(request.POST['subject_list_str'])
            for subject in subject_list:
                subjects.append(
                    Subject(name=subject['name'],
                            color=subject['color'],
                            forum=forum))
            Subject.objects.bulk_create(subjects)
            return HttpResponseRedirect(reverse('choose_bbs'))

    return render_to_response('admin/add_forum.html', {
        'config': config,
        'form': form
    },
                              context_instance=RequestContext(request))
예제 #2
0
파일: views.py 프로젝트: levvli/ws
def add_forum(request):
    '''
    新增帖子
    :param request:
    :return:
    '''
    category = Forum_plate.objects.all()
    if request.method == 'POST':
        form = Forum_form(request.POST)
        if form.is_valid():
            forum = Forum()
            forum.title = form.cleaned_data.get('title')
            forum.category_id = form.cleaned_data.get('category')
            forum.keywords = form.cleaned_data.get('keywords')
            forum.content = form.cleaned_data.get('content')
            forum.authors = form.cleaned_data.get('authors')
            try:
                forum.save()
                return JsonResponse({"code": 200, "data": "发布成功"})
            except Exception:
                return JsonResponse({"code": 400, "data": "发布失败"})
        return JsonResponse({"code": 400, "data": "发布失败"})
    return render(request, 'pc/forum_add.html', locals())