-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
348 lines (275 loc) · 10.3 KB
/
main.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from flask import Flask, render_template, redirect, url_for, flash, abort, request, send_from_directory, send_file, Response, g
from flask_bootstrap import Bootstrap
from flask_ckeditor import CKEditor
from datetime import date, datetime
from functools import wraps
from werkzeug.security import generate_password_hash, check_password_hash
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import relationship
from sqlalchemy import and_, or_, not_
from flask_login import UserMixin, login_user, LoginManager, login_required, current_user, logout_user
from werkzeug.utils import secure_filename
from forms import LoginForm, RegisterForm, CreateDocumentForm, SearchForm
import os
import boto3
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
# AWS setup
AWS_BUCKET_NAME = 'docdoczilla'
UPLOAD_FOLDER = 'uploads'
# AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
# AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 4 * 1024 * 1024
is_prod = os.environ.get('IS_HEROKU', None)
if is_prod:
APP_SECRET_KEY = os.getenv('APP_SECRET_KEY')
else:
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv(), override=True)
APP_SECRET_KEY = os.getenv('APP_SECRET_KEY')
app.config['SECRET_KEY'] = APP_SECRET_KEY
ckeditor = CKEditor(app)
Bootstrap(app)
# CONNECT TO DB
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv(
'DATABASE_URL', "sqlite:///document.db")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
s3 = boto3.client(
"s3",
aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY')
)
class User(UserMixin, db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100))
name = db.Column(db.String(100))
documents = db.relationship("Document", backref="created_by")
def __repr__(self):
return '<User {}>'.format(self.name)
class Document(db.Model):
__tablename__ = "documents"
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
title = db.Column(db.String(250), unique=True, nullable=False)
description = db.Column(db.String(250), nullable=False)
upload_date = db.Column(db.String(250), nullable=False)
file_url = db.Column(db.String(250), nullable=False)
tags = db.Column(db.String(250), unique=False, nullable=True)
def __repr__(self):
return '<Document {}>'.format(self.title)
# DB seed stuff
db.drop_all()
db.create_all()
# new_user = User(
# id=1,
# email='james@james.com',
# name='james',
# password='james',
# )
# new_doc = Document(
# user_id=1,
# title='Test',
# description='Test',
# upload_date=datetime.today(),
# file_url='coffee.jpg',
# tags='new'
# )
# db.session.add(new_user)
# db.session.add(new_doc)
# db.session.commit()
@app.before_request
def before_request():
g.search_form = SearchForm()
def admin_only(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if current_user.id != 1:
return abort(403)
return f(*args, **kwargs)
return decorated_function
@app.route('/elements')
def elements():
return render_template("elements.html")
# TODO get search working
@app.route('/', methods=["GET"])
def landing():
return render_template("landing.html")
@app.route('/register', methods=["GET", "POST"])
def register():
form = RegisterForm()
if form.validate_on_submit():
if User.query.filter_by(email=form.email.data).first():
print(User.query.filter_by(email=form.email.data).first())
# User already exists
flash("You've already signed up with that email, log in instead!")
return redirect(url_for('login'))
hash_and_salted_password = generate_password_hash(
form.password.data,
method='pbkdf2:sha256',
salt_length=8
)
new_user = User(
email=form.email.data,
name=form.name.data,
password=hash_and_salted_password,
)
db.session.add(new_user)
db.session.commit()
login_user(new_user)
return redirect(url_for("get_all_documents"))
return render_template("register.html", form=form, current_user=current_user)
@app.route('/login', methods=["GET", "POST"])
def login():
form = LoginForm()
if form.validate_on_submit():
email = form.email.data
password = form.password.data
user = User.query.filter_by(email=email).first()
# Email doesn't exist or password incorrect.
if not user:
flash("That email does not exist, please try again.")
return redirect(url_for('login'))
elif not check_password_hash(user.password, password):
flash('Password incorrect, please try again.')
return redirect(url_for('login'))
else:
login_user(user)
return redirect(url_for('get_all_documents'))
return render_template("login.html", form=form, current_user=current_user)
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('get_all_documents'))
@app.route("/user/<int:user_id>", methods=["GET", "POST"])
@login_required
def edit_user(user_id):
user = User.query.get(user_id)
edit_form = RegisterForm(
name=user.name,
email=user.email,
password=user.password,
)
if edit_form.validate_on_submit():
user.name = edit_form.name.data
user.email = edit_form.email.data
user.password = edit_form.password.data
db.session.commit()
return redirect(url_for("show_user", document_id=user.id))
return render_template("register.html", form=edit_form, is_edit=True, current_user=current_user)
@app.route('/documents', methods=["GET"])
def get_all_documents():
documents = Document.query.all()
print(documents)
return render_template("index.html", form=g.search_form, documents=documents, current_user=current_user)
@app.route('/search', methods=["GET", "POST"])
def search():
if g.search_form.validate():
search_string = g.search_form.query.data
print(search_string)
results = Document.query.filter(
Document.tags.like('%' + search_string + '%'))
return render_template("index.html", form=g.search_form, documents=results, current_user=current_user)
print(g.search_form.errors)
return redirect(url_for("get_all_documents"))
@ app.route("/document/<int:document_id>", methods=["GET", "POST"])
def show_document(document_id):
requested_document = Document.query.get(document_id)
return render_template("document.html", document=requested_document, current_user=current_user)
def validate_and_upload(file):
file.filename = secure_filename(file.filename)
output = upload_file_to_s3(file, AWS_BUCKET_NAME)
return str(output)
def upload_file_to_s3(file, bucket_name):
"""
Docs: http://boto3.readthedocs.io/en/latest/guide/s3.html
"""
try:
s3.upload_fileobj(
file,
bucket_name,
file.filename,
ExtraArgs={
"ContentType": file.content_type
}
)
except Exception as e:
print("Something Happened: ", e)
return e
return str(file.filename)
# return f"https://{AWS_BUCKET_NAME}.s3-us-west-2.amazonaws.com/{file.filename}"
@ app.route("/download", methods=['POST'])
def download():
id = request.form['id']
document = Document.query.get(id)
key = document.file_url
s3_resource = boto3.resource('s3')
my_bucket = s3_resource.Bucket(AWS_BUCKET_NAME)
file_obj = my_bucket.Object(key).get()
return Response(
file_obj['Body'].read(),
mimetype='text/plain',
headers={"Content-Disposition": "attachment;filename={}".format(key)}
)
@ app.route("/new-document", methods=["GET", "POST"])
@login_required
def add_document():
form = CreateDocumentForm()
if request.method == 'POST':
# check if the post request has the file part
new_document = Document(
title=form.title.data,
description=form.description.data,
user_id=current_user.id,
upload_date=date.today().strftime("%B %d, %Y"),
file_url=validate_and_upload(form.file_url.data),
)
db.session.add(new_document)
db.session.commit()
return redirect(url_for("get_all_documents"))
return render_template("make-document.html", form=form, current_user=current_user)
@ app.route("/edit-document/<int:document_id>", methods=["GET", "POST"])
def edit_document(document_id):
document = Document.query.get(document_id)
edit_form = CreateDocumentForm(
title=document.title,
description=document.description,
file_url=document.file_url,
)
if edit_form.validate_on_submit():
document.title = edit_form.title.data
document.description = edit_form.description.data
document.file_url = edit_form.file_url.data
db.session.commit()
return redirect(url_for("show_document", document_id=document.id))
return render_template("make-document.html", form=edit_form, is_edit=True, current_user=current_user)
@ app.route("/delete", methods=["POST"])
def delete_document():
id = request.form['id']
doc = Document.query.get(id)
s3_key = doc.file_url
s3_resource = boto3.resource('s3')
my_bucket = s3_resource.Bucket(AWS_BUCKET_NAME)
my_bucket.Object(s3_key).delete()
db.session.delete(doc)
db.session.commit()
flash('File deleted successfully')
return redirect(url_for('get_all_documents'))
@ app.route("/about")
def about():
return render_template("about.html", current_user=current_user)
@ app.route("/contact")
def contact():
return render_template("contact.html", current_user=current_user)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)