-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
230 lines (173 loc) · 6.9 KB
/
app.py
File metadata and controls
230 lines (173 loc) · 6.9 KB
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
from flask import Flask, render_template, request, session, redirect, url_for, make_response, send_file, flash
import requests
from flask_mail import Mail, Message
import json
app = Flask(__name__)
app.static_folder = 'static'
app.secret_key='mysecretkey'
mail = Mail(app)
ip_nginx="localhost"
ip_server="34.16.163.134"
@app.route('/')
def index():
return render_template('index.html')
@app.route('/search-by-author')
def select_by_author():
return render_template('select-by-author.html')
@app.route('/search-by-language')
def select_by_language():
return render_template('select-by-language.html')
@app.route('/search-by-author')
def search_by_author():
return render_template('search-by-author.html')
@app.route('/search')
def search():
query = request.args.get('query')
if not query:
flash('Error: The search field is empty. Please enter a query.', 'error')
return redirect(url_for('index'))
query_list = query.split()
if len(query_list) == 1:
api_url = f'http://localhost:8080/datamart/{query}'
else:
query = query.replace(' ', '+')
api_url = f'http://{ip_nginx}:8080/datamart-recommend/{query}'
response = requests.get(api_url)
results = response.json()
transformed_data = transform(results)
print(transformed_data)
return render_template('search_results.html', results=transformed_data,json_dumps=json.dumps)
@app.template_filter('tojson')
def tojson_filter(obj):
return json.dumps(obj)
@app.route('/book_details/<int:book_id>')
def book_details(book_id):
book_json = request.args.get('book')
if book_json:
book = json.loads(book_json)
print(book)
else:
book = None
return render_template('book_details.html', book=book, book_id=book_id)
def transform(results):
transformed_data = []
for item in results:
for key, value in item.items():
key = key.replace('"', '')
value['book_id']=int(key)
transformed_data.append(value)
return transformed_data
@app.route('/search-author')
def search_author():
author = request.args.get('query')
api_url = f'http://{ip_nginx}:8080/metadata/author/{author}'
response = requests.get(api_url)
results = response.json()
new_data = {}
for key, value in results.items():
new_key = int(key.strip('"'))
new_data[new_key] = value
return render_template('metadata_results.html', results=new_data, author=author)
@app.route('/search-language')
def search_language():
language = request.args.get('query')
api_url = f'http://{ip_nginx}:8080/metadata/language/{language}'
response = requests.get(api_url)
results = response.json()
new_data = {}
for key, value in results.items():
new_key = int(key.strip('"'))
new_data[new_key] = value
return render_template('metadata_results.html', results=new_data, language=language)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
url = f'http://{ip_server}/user/login?username={username}&password={password}'
response = requests.get(url)
session_id = response.cookies.get('Session')
session['session_id'] = session_id
if response.status_code == 200:
session['username'] = username
response = make_response(redirect(url_for('index')))
return response
else:
error_message = 'Incorrect credentials. Please try again.'
return render_template('login.html', error=error_message if 'error_message' in locals() else None)
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/profile', methods=['GET', 'POST'])
def profile():
if request.method == 'POST':
name = request.form['name']
language = request.form['language']
date = request.form['date']
status = request.form['status']
txt_file = request.files['txt']
txt_content = ""
if txt_file:
txt_content = txt_file.read().decode('utf-8')
api_url =f'http://{ip_server}/user/post'
params = f"?name={name}&language={language}&date={date}&status={status}"
response = requests.post(api_url + params, data=txt_content, cookies={'Session': session['session_id']})
if response.status_code == 200:
return user_books()
else:
return user_books()
def user_books():
api_url = f'http://{ip_server}/user/books'
response = requests.get(api_url, cookies={'Session': session['session_id']})
if response.status_code == 200:
data = response.json()
return render_template('profile.html', data=data)
else:
return "Error getting data from the API"
@app.route('/contact')
def contact():
return render_template('contact.html')
@app.route('/send_message', methods=['POST'])
def send_message():
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
message = request.form['message']
msg_to_company = Message('New message', sender=email, recipients=['arduinoalumnos2023@gmail.com'])
msg_to_company.body = f'Name: {name}\nEmail address: {email}\Message:\n{message}'
mail.send(msg_to_company)
msg_to_user = Message('Confirmation of message sent', sender='arduinoalumnos2023@gmail.com', recipients=[email])
msg_to_user.body = 'Thank you for contacting us. We have received your message and will get back to you soon.'
mail.send(msg_to_user)
return redirect(url_for('index'))
@app.route('/register', methods=['GET', 'POST'])
def register():
error = None
message = None
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
confirm_password = request.form['confirm_password']
if len(password) < 8:
error = 'Password must be at least 8 characters long.'
elif password != confirm_password:
error = 'Passwords do not match. Please try again.'
else:
url = f'http://{ip_server}/user/sign-up?username={username}&password={password}'
response = requests.get(url)
if response.status_code == 200:
message = 'Correctly registered'
else:
error = 'Registration failed. Please try again.'
return render_template('register.html', error=error, message=message)
@app.route('/logout')
def logout():
response = make_response(redirect(url_for('index')))
session.pop('username', None)
response.delete_cookie('Session')
url = f'http://{ip_server}/user/logout'
requests.get(url)
return redirect(url_for('index'))
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000, debug=True)
app.static_folder = 'static'