当前位置: 首页>>代码示例>>Python>>正文


Python Tag.create_or_get方法代码示例

本文整理汇总了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)
开发者ID:keybits,项目名称:permanote,代码行数:51,代码来源:views.py

示例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")
开发者ID:keybits,项目名称:permanote,代码行数:44,代码来源:views.py


注:本文中的models.Tag.create_or_get方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。