本文整理汇总了Python中forms.ArticleForm.validate方法的典型用法代码示例。如果您正苦于以下问题:Python ArticleForm.validate方法的具体用法?Python ArticleForm.validate怎么用?Python ArticleForm.validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类forms.ArticleForm
的用法示例。
在下文中一共展示了ArticleForm.validate方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: article_new
# 需要导入模块: from forms import ArticleForm [as 别名]
# 或者: from forms.ArticleForm import validate [as 别名]
def article_new():
form = ArticleForm(request.form)
if form.validate():
try:
db.save(Article(current_user, form.title.data, form.content.data, form.plain_text.data))
except AssertionError, err:
current_app.logger.error(err)
return redirect(url_for('Common.index'))
示例2: add_article
# 需要导入模块: from forms import ArticleForm [as 别名]
# 或者: from forms.ArticleForm import validate [as 别名]
def add_article():
form = ArticleForm(request.form)
if request.method == 'POST' and form.validate():
sql = sqlpuzzle.insert_into('article').values({
'title': form.title.data,
'text': form.text.data,
})
cur = get_db().cursor()
cur.execute(str(sql))
return redirect(url_for('article_page.articles'))
return render_template('article/add.html', form=form)
示例3: home
# 需要导入模块: from forms import ArticleForm [as 别名]
# 或者: from forms.ArticleForm import validate [as 别名]
def home():
try:
storage.load()
except IOError:
pass
form = ArticleForm(request.form)
if request.method == 'POST' and form.validate():
article = Articles(form.article_title.data, form.article_body.data)
storage.articles.append(article)
storage.dump()
return render_template('base.html', form=form, storage=storage)
示例4: article_edit
# 需要导入模块: from forms import ArticleForm [as 别名]
# 或者: from forms.ArticleForm import validate [as 别名]
def article_edit(slug):
""" Render edit article form by <slug> or update data from `POST` """
from models import Article, Tag, db
from forms import ArticleForm
article = Article.by_slug(slug=slug).first_or_404()
form = ArticleForm()
if request.method != 'POST':
# init form data from object
form.title.data = article.title
form.slug.data = article.slug
form.description.data = article.description
form.content.data = article.content
tags = [t.title for t in article.tags]
for t in tags:
if form.tags.data:
form.tags.data += ", " + t
else:
form.tags.data = t
form.pub_date.data = article.pub_date.strftime("%Y-%m-%d %H:%M:%S")
form.upd_date.data = article.upd_date.strftime("%Y-%m-%d %H:%M:%S")
else:
if form.validate():
article.title = form.title.data
article.slug = form.slug.data
article.description = form.description.data
article.content = form.content.data
article.pub_date = datetime.strptime(form.pub_date.data, '%Y-%m-%d %H:%M:%S')
# autonow if empty or error
try:
upd_datetime = datetime.strptime(form.upd_date.data, '%Y-%m-%d %H:%M:%S')
except Exception:
article.upd_date = datetime.now()
article.tags = []
tags_raw = form.tags.data.split(',')
for t in tags_raw:
t = t.strip()
# Get or create
try:
article.tags += [Tag.query.filter(Tag.title == t)[0]]
except Exception:
article.tags += [Tag(t)]
db.session.commit()
flash("Article updated success.", 'success')
return redirect(url_for('articles_view', slug=form.slug.data))
return render_template('articles/edit.html', **locals())
示例5: create
# 需要导入模块: from forms import ArticleForm [as 别名]
# 或者: from forms.ArticleForm import validate [as 别名]
def create():
data = request.get_json()
form = ArticleForm(**data)
if form.validate():
form_data = form.data
form_data['ip'] = request.remote_addr
article = g.user.create_article(**form_data)
alert = dict(
type='success',
messages=_("Your article has been submitted successfully. The staff will approve it soon. Thank you for your contribution")
)
redirect = url_for("site.page", name="home")
return jsonify({"data": article.json_data(), "alert": alert, "redirect": redirect})
else:
return json_error(type="VALIDATION_FAILED", messages=form.errors)
示例6: add
# 需要导入模块: from forms import ArticleForm [as 别名]
# 或者: from forms.ArticleForm import validate [as 别名]
def add():
if not session.get('logged_in'):
abort(404)
else:
form = ArticleForm()
if request.method == 'POST':
if form.validate() == False:
return render_template('add.html', form=form)
else:
d = datetime.now().strftime('%d-%m-%Y')
g.db = connect_db()
cur = g.db.execute('INSERT INTO fblog (title, content, date, status) VALUES (?, ?, ?, 1)', [form.title.data, form.content.data, d])
g.db.commit()
g.db.close()
flash('Article added successfully')
return redirect( url_for('admin') )
elif request.method == 'GET':
return render_template('add.html', form=form)
示例7: articles_add
# 需要导入模块: from forms import ArticleForm [as 别名]
# 或者: from forms.ArticleForm import validate [as 别名]
def articles_add():
""" Add new article or render form """
from models import Article, Tag, db
from forms import ArticleForm
form = ArticleForm()
form.pub_date.data = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
form.upd_date.data = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if request.method == 'POST' and form.validate():
tags = []
if form.tags.data:
tags_raw = form.tags.data.split(',')
for t in tags_raw:
t = t.strip()
try:
tags += [Tag.query.filter(Tag.title==t)[0]]
except Exception:
tags += [Tag(t)]
try:
pub_date = datetime.strptime(form.pub_date.data, '%Y-%m-%d %H:%M:%S')
except ValueError:
flash("Can't parse date.", 'error')
else:
article = Article(
form.title.data,
form.description.data,
form.content.data,
tags=tags,
slug=form.slug.data,
pub_date=pub_date,
)
db.session.add(article)
db.session.commit()
flash("Article added.", 'success')
return redirect(url_for('articles_view', slug=article.slug))
return render_template('articles/add.html', **locals())
示例8: edit_article
# 需要导入模块: from forms import ArticleForm [as 别名]
# 或者: from forms.ArticleForm import validate [as 别名]
def edit_article(post_id=None):
form = ArticleForm()
db = get_db()
if request.method == 'POST' and form.validate():
slug = slugify(form.title.data, max_length=50)
data = [
slug,
form.title.data,
form.intro.data,
form.text.data,
form.date.data,
form.published.data,
form.description.data,
form.keywords.data
]
if form.pk.data is not None:
data.append(form.pk.data)
db.execute("update articles set slug=?, title=?, intro=?, text=?, date=?, published=?, "
"description=?, keywords=? where id=?", data)
flash('Статья обновлена', 'success')
return redirect(url_for('edit_article', post_id=form.pk.data))
else:
cur = db.execute("insert into articles (slug, title, intro, text, date, published, "
"description, keywords) values(?,?,?,?,?,?,?,?)", data)
flash('Статья добавлена', 'success')
return redirect(url_for('edit_article', post_id=cur.lastrowid))
if request.method == 'GET':
if post_id is not None:
cur = db.execute("select * from articles where id=?", [post_id])
article = cur.fetchone()
if article is None:
abort(404)
app.logger.debug("edit article %i", article['id'])
form.pk.data = article['id']
form.title.data = article['title']
form.intro.data = article['intro']
form.text.data = article['text']
form.date.data = article['date']
form.published.data = article['published']
form.description.data = article['description']
form.keywords.data = article['keywords']
return render_template('edit.html', form=form)
示例9: update
# 需要导入模块: from forms import ArticleForm [as 别名]
# 或者: from forms.ArticleForm import validate [as 别名]
def update():
article = g.article
user = g.user
data = request.get_json()
form = ArticleForm(**data)
if form.validate():
form_data = form.data
data = form_data
data['ip'] = request.remote_addr
data['user'] = user
# Make sure that users cant change article status
try:
if not user.has_permission('approve_article'):
del data['is_active']
except Exception:
pass
article.update(**data)
cache.update_article(article.id, article)
return jsonify({"data": article.json_data()})
else:
return json_error(type="VALIDATION_FAILED", messages=form.errors)
示例10: update
# 需要导入模块: from forms import ArticleForm [as 别名]
# 或者: from forms.ArticleForm import validate [as 别名]
def update(blog_id):
if not session.get('logged_in'):
abort(404)
else:
form = ArticleForm()
if request.method == 'POST':
if form.validate() == False:
return render_template('add.html', form=form)
else:
g.db = connect_db()
cur = g.db.execute('UPDATE fblog SET title =' + form.title.data + ', content =' + form.content.data + ' WHERE blog_id=' + str(blog_id))
g.db.commit()
g.db.close()
flash('Article updated successfully')
return redirect( url_for('admin') )
elif request.method == 'GET':
g.db = connect_db()
article = query_db('SELECT blog_id, title, content FROM fblog WHERE blog_id = ?', [blog_id], one=True)
form.title.data = article['title']
form.content.data = article['content']
return render_template('edit.html', form=form, article=article)