本文整理汇总了Python中models.Tag.create_or_get方法的典型用法代码示例。如果您正苦于以下问题:Python Tag.create_or_get方法的具体用法?Python Tag.create_or_get怎么用?Python Tag.create_or_get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Tag
的用法示例。
在下文中一共展示了Tag.create_or_get方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: edit
# 需要导入模块: from models import Tag [as 别名]
# 或者: from models.Tag import create_or_get [as 别名]
def edit(slug):
entry = get_object_or_404(Entry, Entry.slug == slug)
tags = ""
for tag in entry.tags:
tags = tags + " " + tag.tag
if request.method == "POST":
if request.form.get("title") and request.form.get("content"):
try:
entry.title = request.form["title"]
entry.content = request.form["content"]
entry.archived = request.form.get("archived") or False
entry.lastedited = datetime.datetime.now()
# convert the string of tags to a list
tags = request.form["tags"].split()
# present is a check to see if the tag exists
present = 0
# add or create tags
for tag in tags:
for entrytag in entry.tags:
if tag == entrytag.tag:
present = 1
if present == 0:
try:
thistag = Tag.get(Tag.tag == tag)
entry.tags.add(thistag)
except:
tag_obj, was_created = Tag.create_or_get(tag=tag)
EntryTags.create(tag=tag_obj, entry=entry)
present = 0
# remove tags
for entrytag in entry.tags:
for tag in tags:
if entrytag.tag == tag:
present = 1
if present == 0:
thistag = Tag.get(Tag.tag == entrytag.tag)
entry.tags.remove(thistag)
present = 0
entry.save()
flash("Note updated successfully.", "success")
return redirect(url_for("detail", slug=entry.slug))
except:
flash("Note title already exists", "danger")
return render_template("create.html")
else:
flash("Title and Content are required.", "danger")
return render_template("edit.html", entry=entry, tags=tags)
示例2: create
# 需要导入模块: from models import Tag [as 别名]
# 或者: from models.Tag import create_or_get [as 别名]
def create():
if request.method == "POST":
if request.form.get("title") and request.form.get("content"):
try:
entry = Entry.create(
title=request.form["title"],
content=request.form["content"],
archived=request.form.get("archived") or False,
)
tags = request.form["tags"].split()
# present is a check to see if the tag exists
present = 0
# add or create tags
for tag in tags:
for entrytag in entry.tags:
if tag == entrytag.tag:
present = 1
if present == 0:
try:
thistag = Tag.get(Tag.tag == tag)
entry.tags.add(thistag)
except:
tag_obj, was_created = Tag.create_or_get(tag=tag)
EntryTags.create(tag=tag_obj, entry=entry)
present = 0
flash("Entry created successfully.", "success")
return redirect(url_for("detail", slug=entry.slug))
except:
flash("Note title already exists", "danger")
return render_template("create.html")
# TODO Refactor the below and above to make it more DRY or not
# to need to display seconds (e.g. add some kind of suffix if entry
# already exists)
elif request.form.get("content"):
entry = Entry.create(
title="{:%a %d %b %Y at %H:%M:%S}".format(datetime.datetime.now()), content=request.form["content"]
)
flash("Note created successfully.", "success")
return redirect(url_for("detail", slug=entry.slug))
else:
flash("Content is required.", "danger")
return render_template("create.html")