def new(request, node_slug): context = {} try: node = Node.objects.get(slug=node_slug) except Node.DoesNotExist: raise Http404 if request.method == 'POST': form = TopicForm(request.POST) if form.is_valid(): last_topic = Topic.objects.filter( author=request.user).order_by('-created_on')[:1] last_topic = last_topic.first() if last_topic and last_topic.title == form.clean()['title'] and ( (timezone.now() - last_topic.created_on).seconds < 10): messages.error(request, '你是否正在尝试连续提交两次相同的内容?') return HttpResponseRedirect( reverse("bbs:topic", args=(last_topic.id, ))) else: topic = form.save(commit=False) topic.node = node request.user.topic_num += 1 request.user.calculate_au() request.user.save() topic.author = request.user topic.last_reply = request.user topic.updated_on = timezone.now() topic.save() node.num_topics += 1 node.save() # --- 解析@ --- team_name_pattern = re.compile('(?<=@)(\w+)', re.UNICODE) at_name_list = set(re.findall(team_name_pattern, topic.content)) if at_name_list: for at_name in at_name_list: if at_name != topic.author.username: try: at_user = User.objects.get(username=at_name) if at_user: notice = Notice(from_user=topic.author, to_user=at_user, topic=topic, content='') notice.save() except: pass # --- 解析@ --- return HttpResponseRedirect( reverse("bbs:topic", args=(topic.id, ))) else: form = TopicForm() return render(request, 'bbs/new.html', {'node': node, 'form': form})
def edit(request,topic_id): try: topic = Topic.objects.get(id=topic_id) if topic.author != request.user: raise Http404 except Node.DoesNotExist: raise Http404 if request.method == 'POST': form = TopicForm(request.POST) if form.is_valid(): topic.title = form.clean()['title'] topic.content = form.clean()['content'] topic.updated_on = timezone.now() topic.save() return HttpResponseRedirect(reverse("bbs:topic" ,args=(topic.id,))) else: form = TopicForm(instance=topic) return render(request,'bbs/edit.html',{'topic':topic,'form':form})
def new(request, node_slug): context = {} try: node = Node.objects.get(slug=node_slug) except Node.DoesNotExist: raise Http404 if request.method == 'POST': form = TopicForm(request.POST) if form.is_valid(): last_topic = Topic.objects.filter(author=request.user).order_by('-created_on')[:1] last_topic = last_topic.first() if last_topic and last_topic.title == form.clean()['title'] and ((timezone.now()-last_topic.created_on).seconds < 10): messages.error(request,'你是否正在尝试连续提交两次相同的内容?'); return HttpResponseRedirect(reverse("bbs:topic" ,args=(last_topic.id,))) else: topic = form.save(commit=False) topic.node = node request.user.topic_num += 1 request.user.calculate_au() request.user.save() topic.author = request.user topic.last_reply = request.user topic.updated_on = timezone.now() topic.save() node.num_topics += 1 node.save() # --- 解析@ --- team_name_pattern = re.compile('(?<=@)(\w+)', re.UNICODE) at_name_list = set(re.findall(team_name_pattern, topic.content)) if at_name_list: for at_name in at_name_list: if at_name != topic.author.username: try: at_user = User.objects.get(username=at_name) if at_user: notice = Notice(from_user=topic.author,to_user=at_user,topic=topic,content='') notice.save() except: pass # --- 解析@ --- return HttpResponseRedirect(reverse("bbs:topic" ,args=(topic.id,))) else: form = TopicForm() return render(request,'bbs/new.html',{'node':node,'form':form})