本文整理汇总了Python中forms.TopicForm类的典型用法代码示例。如果您正苦于以下问题:Python TopicForm类的具体用法?Python TopicForm怎么用?Python TopicForm使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TopicForm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create
def create():
data = request.get_json()
form = TopicForm(**data)
if form.validate():
form_data = form.data
form_data['ip'] = request.remote_addr
try:
topic = g.user.create_topic(**form_data)
alert = dict(
type='success',
messages=_("Your topic has been created successfully. You will be redirected to it shortly")
)
redirect = topic.get_url('view')
cache.update_sorted_topics(topic, 'date_created')
return jsonify({"data": topic.json_data(), "alert": alert, "redirect": redirect})
except ModelException, me:
db_session.rollback()
return json_error(type=me.type, messages=me.message)
except Exception, e:
logging.error(e)
db_session.rollback()
return json_error_database()
示例2: topic_create
def topic_create(project_id):
project = Project.query.get(project_id)
if not current_user.in_team(project.team_id):
abort(404)
form = TopicForm(current_user, project, request.form.getlist('attachment'))
if form.validate_on_submit():
topic = form.saveTopic()
if topic is not None:
flash(u'保存成功')
return redirect(url_for('topic_detail', topic_id=topic.id))
else:
flash(u'保存失败,请联系管理员', 'error')
return render_template('project/topic_create.html', project=project, form=form)
示例3: new
def new():
form = TopicForm()
flash(form.board_id.data)
if form.validate_on_submit():
u = User.query.filter_by(id = g.user.id).first()
topic = Topic(board_id=form.board_id.data, category=form.category.data, author=u, title=form.title.data, content=form.content.data)
db.session.add(topic)
db.session.commit()
flash("发表成功")
return redirect(request.args.get("next") or url_for("index"))
form.board_id.data = request.args.get('board_id', '')
return render_template('new.html',
form = form,
title = '新建')
示例4: forum
def forum(page=1):
# Нужная страница
if request.args.get('page'):
page = int(request.args.get('page'))
# Разбиение на страницы
pagination = Pagination(page, TOPIC_PER_PAGE, ForumTopic.query.count())
# Форма для постинга сообщений
form_topic = TopicForm()
form_message = MessageForm()
# Если отправлена форма постинга
if request.method == 'POST' and form_topic.validate_on_submit() and form_message.validate_on_submit():
# Данные из формы c применением форматирования
data_topic = form_topic.topic.data
data_message = message_format(form_message.message.data, True)
# Создание темы и обновление счётчиков у пользователя
new_topic = ForumTopic(name=data_topic, author_id=current_user.id)
current_user.message_count += 1
current_user.topic_count += 1
db.session.add(new_topic)
# Коммит в этом месте нужен, чтобы появился ID
db.session.commit()
# Создание сообщения
new_mes = ForumMessage(topic_id=new_topic.id, author_id=current_user.id, text=data_message)
db.session.add(new_mes)
db.session.commit()
return(redirect(url_for('topic', topic_id=str(new_topic.id))))
# Выборка всех тем с счётчиком сообщений для каждой
# Подзапрос
all_topics_subq = db.session.query(
ForumMessage.topic_id, func.count(ForumMessage.id).label('mes_count')).\
group_by(ForumMessage.topic_id).\
subquery()
# Основной запрос
all_topics = db.session.query(
ForumTopic, all_topics_subq.c.mes_count).\
join(all_topics_subq, ForumTopic.id == all_topics_subq.c.topic_id).\
order_by(ForumTopic.time_last.desc()).all()
# Вернуть страницу
return(render_template('forum.html',
user=current_user,
all_topics=all_topics,
form_topic=form_topic,
form_message=form_message,
pagination=pagination))
示例5: new_topic
def new_topic():
"""
Starts a new thread creating a new Topic and corresponding first Post in the database.
"""
form = TopicForm(request.form)
if form.validate_on_submit():
topic = Topic(subject=form.subject.data)
post = Post(message=form.message.data, ip_address=request.remote_addr, name=form.name.data,
email=form.email.data)
topic.first_post = post
post.topic = topic
db.session.add(topic)
db.session.commit()
return redirect(url_for('view_topic', topic_id=topic.id))
return render_template('new_topic.html', form=form)
示例6: add_topic
def add_topic(request):
'''Add a new topic'''
tform = None
if request.method == 'POST':
tform = TopicForm(request.POST)
if tform.is_valid():
t = tform.save()
'If topic was added successfully, send them to that topic page'
return goto_topic(t, 'Now you can start adding options')
if not tform:
tform = TopicForm()
return render_to_response('dovote/add_topic.html',
dict(tform=tform),
context_instance=RequestContext(request))
示例7: post
def post(self):
node_id = force_int(self.get_argument('node_id', 0), 0)
node = Node.get(id=node_id)
user = self.current_user
form = TopicForm(self.request.arguments)
if form.validate():
topic = form.save(user=user)
topic.put_notifier()
result = {'status': 'success', 'message': '主题创建成功',
'topic_url': topic.url}
if self.is_ajax:
return self.write(result)
self.flash_message(**result)
return self.redirect(topic.url)
if self.is_ajax:
return self.write(form.result)
return self.render("topic/create.html", form=form, node=node)
示例8: chat
def chat(user):
# Creating new topics.
if request.method == 'POST':
if user.topics_full():
return render_template('/index.html', topics=get_topics(), full=True,
user=user.user)
form = TopicForm(request.form)
if form.validate():
form.add(user.user)
return render_template('/index.html', topics=get_topics(), form=form,
user=user.user)
# Deleting topics.
if request.method == 'GET' and request.args.get('del_topic', False):
user.del_topic(request.args['del_topic'])
form = TopicForm()
return render_template('/index.html', topics=get_topics(), form=form,
user=user.user)
示例9: post
def post(self, topic_id):
if not self.has_permission:
return
topic = Topic.get(id=topic_id)
if not topic:
return self.redirect_next_url()
user = self.current_user
form = TopicForm(self.request.arguments)
if form.validate():
topic = form.save(user=user, topic=topic)
result = {'status': 'success', 'message': '主题修改成功',
'topic_url': topic.url}
if self.is_ajax:
return self.write(result)
self.flash_message(result)
return self.redirect(topic.url)
if self.is_ajax:
return self.write(form.result)
return self.render("topic/create.html", form=form, node=topic.node)
示例10: get
def get(self):
node_id = force_int(self.get_argument('node_id', 0), 0)
node = Node.get(id=node_id)
if node:
selected = node.name
else:
selected = None
choices = Node.get_node_choices()
form = TopicForm.init(choices=choices, selected=selected)
return self.render("topic/create.html", form=form, node=node)
示例11: get
def get(self, topic_id):
topic = Topic.get(id=topic_id)
if topic and (topic.author == self.current_user or self.current_user.is_admin):
selected = topic.node.name
else:
return self.redirect_next_url()
choices = Node.get_node_choices()
args = {'node_name': [selected], 'title': [topic.title], 'content':
[topic.content]}
form = TopicForm.init(choices=choices, selected=selected, args=args)
return self.render("topic/create.html", form=form, node=topic.node)
示例12: add_topic
def add_topic(request, event_id):
e = Event.objects.get(id=event_id)
if request.method == 'POST':
f = TopicForm(request.POST)
if f.is_valid():
t = f.save(commit=false)#not push yet.
#more values... to event yes.
t.event = e
t.save()
return HttpResponseRedirect('/event/get/%s' % event_id)
else:
f=TopicForm()
args ={}
args.update(csrf(request))
args['article']=a
args['form']=f
return render_to_response('add_topic.html',args)
示例13: bbs_add_topic
def bbs_add_topic():
node_str = urllib.unquote(request.args.get('node', "").strip())
node = None
if node_str:
node = Node.query.filter(Node.name==node_str).first()
if not node:
return render_template("node_not_found.html", next=url_for('.bbs_add_topic'), name=node_str)
form = TopicForm(request.form)
form.node.choices = Section.query.get_all_nodes()
if node_str:
form.node.data=node.title
if request.method == 'POST' and form.validate():
if not node:
node = Node.query.filter(Node.title==form.data["node"].strip()).first()
if not node:
form.node.errors.append(u"节点不存在!")
return render_template("add_topic.html", form=form)
section = node.section
topic = Topic(user=current_user, title=form.data['title'].strip(), content=form.data["content"],
tags=form.data["tags"], created=datetime.now(), last_modified=datetime.now(),
last_reply_at=datetime.now())
topic.section = section
topic.node = node
#db.session.add(topic)
node.topic_count += 1
#db.session.add(node)
section.topic_count += 1
#db.session.add(section)
current_user.profile.topics += 1
db.session.add_all([topic, node, section, current_user.profile])
db.session.commit()
return redirect(url_for(".bbs_topic", id=topic.id))
if node_str:
action_url = url_for(".bbs_add_topic", node=node_str)
else:
action_url = url_for(".bbs_add_topic")
return render_template("add_topic.html", form=form, action_url=action_url)
示例14: get
def get(self, topic_id):
if not self.has_permission:
return
topic = Topic.get(id=topic_id)
if topic:
selected = topic.node.name
else:
return self.redirect_next_url()
choices = Node.get_node_choices()
args = {'node_name': [selected], 'title': [topic.title], 'content':
[topic.content]}
form = TopicForm.init(choices=choices, selected=selected, args=args)
return self.render("topic/create.html", form=form, node=topic.node)
示例15: new
def new(request):
if request.method == 'POST':
topic_form = TopicForm(request.POST)
post_form = PostForm(request.POST)
if topic_form.is_valid() and post_form.is_valid():
topic = topic_form.save()
post = post_form.save(commit=False)
post.topic = topic
post.author = request.user
post.save()
return redirect('/forum/%d' % topic.id)
else:
topic_form = TopicForm()
post_form = PostForm()
return render_to_response('forum/new.html',
{
'topic_form': topic_form,
'post_form': post_form
},
context_instance=RequestContext(request))