コード例 #1
0
ファイル: views.py プロジェクト: keybits/permanote
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
0
ファイル: views.py プロジェクト: keybits/permanote
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")