forked from ShakedZrihen/Emanuel-search-engine---FLASK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Emanuel.py
105 lines (83 loc) · 3.15 KB
/
Emanuel.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
from flask import Flask, request, render_template, redirect
from DB.handler import DBHandler
import mammoth
# from flask_restful import Resource, Api
app = Flask(__name__)
my_db = DBHandler()
my_db.connect()
words_to_bold = []
IS_ADMIN = False
@app.route('/')
def index():
my_db.build_dictionary()
return render_template('index.html', data=None)
@app.route('/search')
def search():
global words_to_bold
query = request.query_string
query = query.replace("query=", "")
data, words_to_bold = my_db.search(query)
query = query.replace("+", " ")
query = query.replace("%29", ")")
query = query.replace("%7C", "|")
query = query.replace("%26", "&")
query = query.replace("%28", "(")
query = query.replace("%21", "!")
query = query.replace("%22", '"')
filtered_data = filter(lambda doc: doc[0] not in my_db.dictionary.hidden_docs, data)
if IS_ADMIN:
hidden = filter(lambda doc: doc[0] in my_db.dictionary.hidden_docs, data)
return render_template('admingui.html', data=filtered_data, hidden=hidden, len=len(data), query=query)
else:
return render_template('search.html', data=filtered_data, len=len(filtered_data), query=query)
@app.route('/documents/<int:doc_id>')
def display_document(doc_id):
with open("E:\Emanuel\storedFiles\{}.docx".format(doc_id), "rb") as docx_file:
result = mammoth.convert_to_html(docx_file)
str_res = result.value
for word in words_to_bold:
str_res = str_res.replace(word, '<b>{}</b>'.format(word))
return render_template('resalt.html', text=str_res)
@app.route('/admin')
def show_admin():
global IS_ADMIN
IS_ADMIN = True
return render_template('admingui.html', len=0, message_display="none", message="")
@app.route('/upload', methods=['POST'])
def upload():
author = request.form["author"]
subject = request.form["subject"]
for file in request.files.getlist("file"):
filename = file.filename
my_db.load_new_doc(
"E:\Emanuel\inputFiles\\{}".format(filename),
title=filename,
author=author,
subject=subject
)
return render_template('admingui.html', len=0, message_display="block", message="File was uploaded successfully")
@app.route('/build-dict')
def re_index():
my_db.build_dictionary()
return render_template('admingui.html', len=0, message_display="block", message="Dictionary was updated!")
@app.route('/delete/<int:doc_id>')
def delete(doc_id):
my_db.delete(doc_id)
return render_template('admingui.html',
len=0,
message_display="block",
message="document number: {} was delete!".format(doc_id))
@app.route('/restore/<int:doc_id>')
def restore(doc_id):
my_db.restore(doc_id)
return render_template('admingui.html',
len=0,
message_display="block",
message="Document number: {} was restored!!".format(doc_id))
@app.route('/logoff')
def log_off():
global IS_ADMIN
IS_ADMIN = False
return redirect('/')
if __name__ == '__main__':
app.run()