-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper-browser.py
114 lines (87 loc) · 3.56 KB
/
paper-browser.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
from flask import Flask, render_template, request, redirect, url_for
from db import get_papers, get_cats, add_paper, del_paper
import time
import os.path
app = Flask(__name__)
@app.route("/")
@app.route("/view")
def index():
""" Serves the index page. Here, the user can:
1: View lists of papers of a particular directory
2: Change directory (default "/")
"""
cat = request.args.get("cat")
if cat is None:
papers = get_papers("/")
else:
papers = get_papers(cat)
cat_list = sorted(get_cats())
return render_template("index.html", title="Viewing Papers", papers=papers, cats=cat_list)
@app.route("/add", methods=["GET", "POST"])
def add():
""" Enables adding of a paper
On method GET, Displays form to add paper
On method POST, Attempts to add paper
If no errors, redirects to index
Else shows error page
"""
cat_list = sorted(get_cats())
if request.method == "POST":
title = request.form["title"]
# print title
if is_empty(title):
error = "Error: Title should be specified"
return render_template("add.html", cats=cat_list, title="Add Paper", error=error)
authors = request.form["authors"]
author_list = comma_list(authors)
# print author_list
if is_empty(author_list):
error = "Error: Authors should be specified"
return render_template("add.html", cats=cat_list, title="Add Paper", error=error)
year = request.form["year"]
# print year
if (not is_empty(year)) and (not year.isdigit()):
error = "Error: Year should be numeric"
return render_template("add.html", cats=cat_list, title="Add Paper", error=error)
category = request.form["cat_sel"] if request.form["cat_sel"] != "_other" else request.form["cat_text"]
category = category if category.startswith("/") else ("/" + category)
# print category
keywords = request.form["keywords"]
keyword_list = comma_list(keywords)
# print keyword_list
notes = request.form["notes"]
# print notes
important = 1 if "important" in request.form else 0
upfile = request.files["file"]
if is_empty(upfile.filename):
error = "Error: Specify a file to upload"
return render_template("add.html", cats=cat_list, title="Add Paper", error=error)
filename = "{0}{1}".format(time.time(), os.path.splitext(upfile.filename)[1])
filepath = "{0}/{1}".format(get_papers_path(), filename)
upfile.save(filepath)
# Now actually add paper
paper = [title, author_list, year, category, keyword_list, filename, notes, important]
add_paper(paper)
return redirect(url_for("index"))
else:
return render_template("add.html", cats=cat_list, title="Add Paper")
@app.route("/delete")
def delete():
paperid = request.args.get("paperid")
filename = del_paper(paperid)
filepath = "{0}/{1}".format(get_papers_path(), filename)
os.remove(filepath)
return redirect(url_for("index"))
##############################
# Utility functions
def is_empty(string):
return string == ""
def comma_list(string):
""" Accepts a comma-separated string which may contain several embedded spaces, and converts it to a standard comma-separated string
"""
return ",".join([s.strip() for s in string.split(",")])
def get_papers_path():
currpath = os.path.dirname(os.path.realpath(__file__))
return currpath + "/static/papers"
if __name__ == "__main__":
app.run(debug=True)