/
index.py
269 lines (205 loc) · 7.15 KB
/
index.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
knuth
~~~~~
A document repository with browsing, storage and indexing capabilities.
(C) 10.12.25, 2013, 2014, Lukas Prokop, Andi
"""
# imports
from flask import Flask, render_template, request, url_for
from flask import request, redirect, abort, send_from_directory
from werkzeug.contrib.atom import AtomFeed
from database import Document
from database import db
import os.path
import document
import datetime
import feedparser
import collections
import elasticsearch
# setup
FEED_TITLE = 'Newest Uploads'
FEED_NUM_DOCUMENTS = 5
es = elasticsearch.Elasticsearch()
app = Flask(__name__)
app.jinja_env.add_extension('jinja2.ext.do')
DB_URI = u'postgresql://postgres:1445@localhost/knuth_db'
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI
db.init_app(app)
FEED_BODY = u'''Title: {title:}<br />
Author: {author:}<br />
DOI: {doi:}<br />
Upload: {upload:}'''
# routing
@app.route('/faq')
def faq():
"""Frequently Asked Questions page"""
return render_template('faq.html', page='faq')
@app.route('/impressum')
def impressum():
"""Impressum page"""
return render_template('impressum.html', page='impressum')
@app.route('/syntax')
def info():
"""Information about the search engine syntax"""
return render_template('syntax.html', page='info')
def normalize_data(form, files):
"""Take userform from upload and return normalized data"""
try:
doc_file = files['doc']
except KeyError:
doc_file = None
try:
attach_file = files['attach_doc']
except KeyError:
attach_file = None
return {
'op': form['op'].lower(),
'id': int(form['doc_id']),
'title': form['doc_title'],
'author': form['doc_author'],
'doi': form['doc_doi'],
'tags': [tag for tag in form['doc_tags'].split() if tag],
'doc': doc_file,
'attachment': {
'title': form['attach_title'],
'author': form['attach_author'],
'doi': '',
'doc': attach_file
}
}
def create_empty_doc():
"""Return an empty document for `create`."""
d = collections.defaultdict(lambda: '', {'doc_id': 0})
return normalize_data(d, d)
@app.route('/create', methods=['GET', 'POST'])
def create():
"""Create a new document (POST) or provide new document form."""
if request.method == 'POST':
data = normalize_data(request.form, request.files)
if data['id'] == 0:
doc_id = document.create_document(data['title'],
data['author'], data['doi'], data['tags'])
document.index_document_by_id(es, doc_id)
else:
d = {}
whitelist = ['id', 'title', 'author', 'doi']
for key in whitelist:
if data[key]:
d[key] = data[key]
document.update_document(data['id'], **d)
doc_id = data['id']
if data['doc']:
filename = document.upload_doc(es, data['doc'], doc_id)
else:
filename = document.get_filename(doc_id)
# tags get inherited
if data['attachment']['doc']:
at = document.create_attachment(doc_id,
data['attachment']['title'],
data['attachment']['author'],
data['attachment']['doi'],
data['tags'])
document.index_document_by_id(es, at)
attach = document.upload_doc(es, data['attachment']['doc'], at)
docu = document.retrieve_document(doc_id)
else:
# create empty document
docu = create_empty_doc()
op = request.form.get('op')
if not op or op == u'Attach':
return render_template('create.html', **docu)
else:
return redirect(url_for('doc', doc_id=docu['id']))
@app.route('/browse')
@app.route('/list')
def listing(page=0):
"""Create a list of all available documents"""
# 1. request all documents from DB
documents = Document.query.order_by(Document.timestamp.desc()).all()
all_docs = []
for doc in documents:
all_docs.append(document.retrieve_document(doc.id))
# 2. provide data to template engine
return render_template('list.html',
documents=all_docs, page='listing'
)
@app.route('/update', methods=['POST', 'PUT'])
def update():
"""Update a document"""
return render_template('doc.html', page='update')
@app.route('/delete/<int:doc_id>')
def delete(doc_id, methods=['DELETE']):
"""Delete a document"""
document.delete_document(doc_id)
return redirect(url_for('main'))
def make_feed_body(doc):
"""Create the body text of a feed entry"""
doi = ''
if doc.doi is not None:
doi = doc.doi
return FEED_BODY.format(title=doc.title, author=doc.author,
doi=doi, upload=doc.get_format_date_string()
)
def create_feed():
"""Create an AtomFeed instance with latest documents"""
feed = AtomFeed(FEED_TITLE, feed_url=request.url, url=request.url_root)
newest_documents = Document.query.order_by(Document.timestamp.desc()) \
.limit(FEED_NUM_DOCUMENTS).all()
for doc in newest_documents:
feed.add(doc.title or u'(no title given)',
make_feed_body(doc),
content_type='html',
url=url_for('doc', doc_id=doc.id),
updated=doc.get_date())
return feed
@app.route('/feed')
def feed():
"""Return a AtomFeed for the latest uploaded documents"""
feed = create_feed()
return feed.get_response()
@app.route('/news')
def news():
"""Page listing latest documents"""
feed_str = create_feed().to_string()
parsed_feed = feedparser.parse(feed_str)
for entry in parsed_feed['entries']:
dt = datetime.datetime.strptime(entry['updated'], '%Y-%m-%dT%H:%M:%SZ')
entry['updated'] = dt.strftime('%Y-%m-%d %H:%M')
return render_template('news.html', **{'feed': parsed_feed})
@app.route('/doc/<int:doc_id>')
def doc(doc_id):
"""Presentation of a single document"""
docu = document.retrieve_document(doc_id)
if not docu:
abort(404)
return render_template('doc.html', page='doc', **docu)
@app.route('/data/<filename>')
def download(filename):
"""Download a file from the repository"""
filename = os.path.basename(filename)
return send_from_directory(document.UPLOAD_FOLDER, filename)
@app.route('/')
@app.route('/search')
def main():
"""Main routine"""
query = request.args.get('q')
results = {}
if query:
# if some query is specified, search
search_query = {'query': {'query_string': {'query': query}}}
es_params = {'index': 'knuth', 'doc_type': 'document',
'size': 50, 'body': search_query}
search_result = es.search(**es_params)
hits = search_result['hits']['hits']
for hit in hits:
document_id = hit['_id']
d = Document.query.filter_by(id=document_id).first()
results[str(document_id)] = d
print results
tmpl_params = {'results': results, 'page': 'listresults'}
return render_template('listresults.html', **tmpl_params)
if __name__ == '__main__':
# TODO: change to debug=False for production
app.run(debug=True, port=5000)