本文整理汇总了Python中forms.MessageForm.validate_on_submit方法的典型用法代码示例。如果您正苦于以下问题:Python MessageForm.validate_on_submit方法的具体用法?Python MessageForm.validate_on_submit怎么用?Python MessageForm.validate_on_submit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类forms.MessageForm
的用法示例。
在下文中一共展示了MessageForm.validate_on_submit方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: home
# 需要导入模块: from forms import MessageForm [as 别名]
# 或者: from forms.MessageForm import validate_on_submit [as 别名]
def home():
# return "Hello, World!" # return a string
error = None
print current_user.id
context = { 'title': "Login Page",
'creator': "Sahai Srichock",
}
#print "hello home"
form = MessageForm(request.form)
if form.validate_on_submit():
new_message = BlogPost(
form.title.data,
form.description.data,
current_user.id
)
db.session.add(new_message)
db.session.commit()
flash('New entry was successfully posted. Thank.')
return redirect(url_for('homes.home'))
#return render_template(
#'index.html', posts=posts, error=error, form=form, context=context)
else:
posts = db.session.query(BlogPost).all()
return render_template(
'index.html', posts=posts, error=error, form=form, context=context) # render a template
示例2: home
# 需要导入模块: from forms import MessageForm [as 别名]
# 或者: from forms.MessageForm import validate_on_submit [as 别名]
def home():
error = None
form = MessageForm(request.form)
if form.validate_on_submit():
new_message = BlogPost(form.title.data, form.description.data, current_user.id)
db.session.add(new_message)
db.session.commit()
flash("New entry was successfully posted. Thanks.")
return redirect(url_for('home.home'))
else:
posts = db.session.query(BlogPost).all()
return render_template('index.html', posts=posts, form=form, error=error)
示例3: new_blogpost
# 需要导入模块: from forms import MessageForm [as 别名]
# 或者: from forms.MessageForm import validate_on_submit [as 别名]
def new_blogpost(author_id):
error = None
form = MessageForm(request.form)
if form.validate_on_submit():
newpost = BlogPost(title=form.title.data,
description=form.description.data,
author_id=current_user.id)
db.session.add(newpost)
db.session.commit()
flash('your post was successful', 'success')
return redirect(url_for('blog'))
return render_template('create-post.html', form=form, error=error)
示例4: edit_blogpost
# 需要导入模块: from forms import MessageForm [as 别名]
# 或者: from forms.MessageForm import validate_on_submit [as 别名]
def edit_blogpost(author_id, blog_id):
error = None
editpost = db.session.query(BlogPost).filter_by(id=blog_id).one()
form = MessageForm(obj=editpost)
if form.validate_on_submit():
editpost.title = form.title.data
editpost.description = form.description.data
db.session.add(editpost)
db.session.commit()
flash("Post successfully edited", "success")
return redirect(url_for('blog'))
return render_template('edit-post.html', editpost=editpost,
form=form, error=error)
示例5: forum
# 需要导入模块: from forms import MessageForm [as 别名]
# 或者: from forms.MessageForm import validate_on_submit [as 别名]
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))
示例6: topic
# 需要导入模块: from forms import MessageForm [as 别名]
# 或者: from forms.MessageForm import validate_on_submit [as 别名]
def topic(topic_id, page=1):
# Объект текущего топика
current_topic = ForumTopic.query.get(topic_id)
# Нужная страница
if request.args.get('page'):
page = int(request.args.get('page'))
# Проверка существования объекта
if not current_topic:
abort(404)
# Форма добавления нового сообщения
form_message = MessageForm()
# Разбиение на страницы
pagination = Pagination(page, MESSAGE_PER_PAGE, current_topic.message.count())
# Если отправлена форма постинга
if request.method == 'POST' and form_message.validate_on_submit():
# Данные из формы c применением форматирования
data_message = message_format(form_message.message.data, True)
# Создание сообщения
new_mes = ForumMessage(topic_id=topic_id, author_id=current_user.id, text=data_message)
current_topic.time_last = datetime.utcnow()
current_user.message_count += 1
db.session.add(new_mes)
# Запись последнего автора темы
ForumTopic.query.get(topic_id).editor_id = current_user.id
db.session.commit()
# Вернуть последнюю страницу
if pagination.last_full:
return(redirect(url_for('topic', topic_id=topic_id, page=pagination.pages+1)))
else:
return(redirect(url_for('topic', topic_id=topic_id, page=pagination.pages)))
# Счётчик просмотров +1
current_topic.views += 1
db.session.commit()
# Вернуть страницу
return(render_template('topic.html',
user=current_user,
topic=current_topic,
form_message=form_message,
pagination=pagination))
示例7: home
# 需要导入模块: from forms import MessageForm [as 别名]
# 或者: from forms.MessageForm import validate_on_submit [as 别名]
def home():
error = None
form = MessageForm(request.form)
#db.session.delete(information)
if form.validate_on_submit():
new_message = information(
form.title.data,
form.description.data,
current_user.id
)
db.session.add(new_message)
db.session.commit()
flash('Your information was successfully updated. Thanks.')
return redirect(url_for('home.home'))
else:
posts = db.session.query(information).all()
return render_template(
'index.html', posts=posts, form=form, error=error)
示例8: home
# 需要导入模块: from forms import MessageForm [as 别名]
# 或者: from forms.MessageForm import validate_on_submit [as 别名]
def home():
error = None
form = MessageForm(request.form)
if form.validate_on_submit():
new_message = BlogPost(
form.title.data,
form.description.data,
current_user.id
)
db.session.add(new_message)
db.session.commit()
flash('New entry was successfully posted. Thanks.')
return redirect(url_for('home.home'))
else:
tracks = db.session.query(Track).order_by(Track.upvote.desc()).all()
user = User.query.filter_by(id=current_user.id).first()
#songs = os.listdir(os.path.abspath(app.config['UPLOAD_FOLDER']))
#folder=os.path.abspath(app.config['UPLOAD_FOLDER'])
return render_template(
'index.html', form=form, error=error, tracks=tracks, username=user.name)
示例9: home
# 需要导入模块: from forms import MessageForm [as 别名]
# 或者: from forms.MessageForm import validate_on_submit [as 别名]
def home():
error = None
form = MessageForm(request.form)
if form.validate_on_submit():
new_message = BlogPost(
form.title.data,
form.description.data,
current_user.id
)
db.session.add(new_message)
db.session.commit()
flash('New entry was successfully posted. Thanks.', 'bg-success')
return redirect(url_for('home.home'))
else:
# current_u = current_user()
posts = db.session.query(BlogPost).all()
u_posts = db.session.query(User,BlogPost).filter(current_user.id==BlogPost.author_id)
return render_template(
'index.html', posts=posts, form=form, u_posts=u_posts, error=error)
示例10: edit_message
# 需要导入模块: from forms import MessageForm [as 别名]
# 或者: from forms.MessageForm import validate_on_submit [as 别名]
def edit_message(message_id):
edit_message = ForumMessage.query.get(message_id)
# Проверка существования объекта
if not edit_message:
abort(404)
# Форма постинга сообщения
form_message = MessageForm()
# Если отправлена форма постинга
if request.method == 'POST' and form_message.validate_on_submit():
# Данные из формы c применением форматирования
edit_message.text = message_format(form_message.message.data, True)
# Отметка о дате изменения
edit_message.date_edit = datetime.utcnow()
# Отметка о последнем редакторе
edit_message.editor_id = current_user.id
db.session.commit()
# Вернуть страницу топика
return(redirect(url_for('topic', topic_id=edit_message.topic_id)))
# Является ли пользователь автором сообщения или админом/модером
if edit_message.author == current_user or current_user.role < 2:
# Вывод старого сообщения в форме
form_message.message.data = edit_message.text.replace('<', '[').replace('>', ']')
# Вывод странички
return(render_template('message_edit.html',
user=current_user,
message=edit_message,
form_message=form_message))
# Если нет, то выдать ошибку
else:
return(render_template('info.html',
user=current_user,
text="You can't edit message if you are not it's author"))
示例11: mail_write
# 需要导入模块: from forms import MessageForm [as 别名]
# 或者: from forms.MessageForm import validate_on_submit [as 别名]
def mail_write(reply=None):
# Форма для нового сообщения
form_recepient = RecepientForm()
form_subject = TopicForm()
form_message = MessageForm()
# Если отправлена форма с новым сообщением
if request.method == 'POST' and \
form_recepient.validate_on_submit() and \
form_subject.validate_on_submit() and \
form_message.validate_on_submit():
# Данные из формы c применением форматирования
data_recipient = form_recepient.recepient.data
data_subject = form_subject.topic.data
data_message = message_format(form_message.message.data, True)
# От кого и кому отправлено сообщение
sender_id = current_user.id
recipient_id = User.query.filter_by(login=data_recipient).first()
if recipient_id:
recipient_id = recipient_id.id
else:
return(render_template('info.html',
user=current_user,
text='No such user'))
# Создание сообщений в отправленных у посылающего и
# во входящих у того, кому адресовано письмо
new_message = Mailbox(sender_id=current_user.id,
owner_id=sender_id,
directory=1,
recipient_id=recipient_id,
subject=data_subject,
text=data_message,
read=True)
db.session.add(new_message)
new_message = Mailbox(sender_id=current_user.id,
owner_id=recipient_id,
directory=0,
recipient_id=recipient_id,
subject=data_subject,
text=data_message)
db.session.add(new_message)
db.session.commit()
# Перейти в отправленные
return(redirect(url_for('mailbox', box='sent')))
# Заполнение полей надо делать после проверки отправки, иначе
# form_message.message.data останется =quote и не перезапишется
# теми данными, которые ввёл пользователь
# Если это ответное сообщение
reply = request.args.get('reply')
recepient = ''
subject = ''
if reply:
previous_message = Mailbox.query.get(reply)
recepient = previous_message.sender.login
subject = 'Re: ' + previous_message.subject
text = message_format(previous_message.text, False)
quote = ''
for line in text.split('\n'):
if line[:3] == '>> ':
quote += '>> ' + line + '\n'
else:
for i in range(0, len(line), 50):
quote += '>> ' + line[i:i+50] + '\n'
quote += '\n'
form_message.message.data = quote
# Все пользователей для выбора в качестве адреса
all_users = User.query.all()
return(render_template('mail_write.html',
user=current_user,
recepient=recepient,
subject=subject,
form_recepient=form_recepient,
form_subject=form_subject,
form_message=form_message,
all_users=all_users))