This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
168 lines (133 loc) · 4.69 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import re
import flask
import utils
from app import app
from models import Note, FTSNote, Category, NoteCategory, MD
@app.route("/", methods=["GET", "POST"])
@app.route("/page/<int:page>", methods=["GET", "POST"])
def index(page=1):
per_page = flask.current_app.config["PER_PAGE"]
number_of_notes = len(list(Note))
pages = number_of_notes // per_page + (number_of_notes % per_page > 0)
# TODO: Select category
form = dict(flask.request.form)
cids = []
if "apply_filter" in list(form):
form.pop("apply_filter")
cids = [
int(re.match("category:([0-9]*)", category).group(1))
for category in list(form)
]
categories = utils.categories(cids=cids)
keywords = flask.request.form.get("search", "", type=str)
notes = Note.search(keywords)
if not notes:
notes = Note.public()
nids = []
for note in notes:
# TODO: Better solution for deleting all empty notes
if not note.title and not note.content:
note.delete_instance()
# TODO: Improve filtering of notes
if all([
NoteCategory.select().where(
NoteCategory.note == note.id,
NoteCategory.category == cid,
).exists() for cid in cids
]):
nids.append(note.id)
# Filter notes
notes = notes.where(Note.id.in_(nids))
return flask.render_template(
"index.html",
notes=notes.paginate(page, per_page),
page=page,
pages=pages,
categories=categories,
keywords=keywords,
)
@app.route("/save/<int:nid>", methods=["POST"])
def save_note(nid):
form = dict(flask.request.form)
title = form.pop("note-title", [""])
content = form.pop("note-content", [""])
form.pop("categories-input")
categories = form
Note.update(
title=title[0],
content=content[0],
).where(Note.id == nid).execute()
FTSNote.store_note(Note.get(Note.id == nid))
NoteCategory.delete().where(NoteCategory.note == nid).execute()
for category in list(categories):
cid = int(re.match("category:([0-9]*)", category).group(1))
NoteCategory.create(note=nid, category=cid)
return flask.redirect(flask.url_for("index"))
@app.route("/delete/<int:nid>", methods=["GET"])
def delete_note(nid):
try:
note = Note.get(Note.id == nid)
except Note.DoesNotExist:
flask.abort(404)
note.delete_instance()
if FTSNote.select().where(FTSNote.docid == note.id).exists():
FTSNote.get(FTSNote.docid == note.id).delete_instance()
return flask.redirect(flask.url_for("index"))
@app.route("/new", methods=["GET"])
def new_note():
note = Note.create(
title="",
content="",
)
categories = utils.categories()
return flask.render_template(
"edit.html",
note=note,
categories=categories,
new=True
)
@app.route("/edit/<int:nid>", methods=["GET"])
def edit_note(nid):
try:
note = Note.get(Note.id == nid)
except Note.DoesNotExist:
flask.abort(404)
categories = utils.categories(nid=nid)
return flask.render_template("edit.html", note=note, categories=categories)
@app.route("/add_categories", methods=["POST"])
def add_categories():
new_categories = {}
categories_input = flask.request.form.get("categories-input", "", type=str)
for name in categories_input.split(";"):
name = name.strip()
if not name:
break
if not Category.select().where(Category.name == name).exists():
category = Category.create(name=name)
new_categories[str(category.id)] = category.name
return flask.jsonify(**new_categories)
@app.route("/get_note", methods=["POST"])
def get_note():
title = MD.convert(flask.request.form.get("note-title", "", type=str))
content = MD.convert(flask.request.form.get("note-content", "", type=str))
return flask.jsonify({
"title": flask.Markup(title),
"content": flask.Markup(content),
})
@app.route("/categories")
def edit_categories():
categories = Category.public()
notes = utils.notesPerCategory(categories)
return flask.render_template(
"categories.html",
categories=categories,
notes=notes,
)
@app.route("/delete_categories", methods=["POST"])
def delete_categories():
for category in list(flask.request.form):
cid = int(re.match("category:([0-9]*)", category).group(1))
Category.get(Category.id == cid).delete_instance()
if NoteCategory.select().where(NoteCategory.category == cid).exists():
NoteCategory.get(NoteCategory.category == cid).delete_instance()
return flask.jsonify({"status": "success"})