-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
569 lines (462 loc) · 16.8 KB
/
models.py
File metadata and controls
569 lines (462 loc) · 16.8 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
from app import app, db, ALLOWED_EXTENSIONS
from flask.ext.bcrypt import Bcrypt
from werkzeug import secure_filename
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from sqlalchemy.ext.hybrid import hybrid_property
from flask.ext.login import UserMixin
# import argparse
import datetime
import json
import os
import requests
import time
import stripe
bcrypt = Bcrypt(app)
BCRYPT_LOG_ROUNDS = 12
stripe_keys = {
'secret_key': os.environ['STRIPE_SECRET_KEY'],
'publishable_key': os.environ['STRIPE_PUBLISHABLE_KEY']
}
stripe.api_key = stripe_keys['secret_key']
AUTHENTISE_KEY = os.environ['AUTHENTISE_API_KEY']
MAILGUN_API_KEY = os.environ['MAILGUN_API_KEY']
MAILGUN_SANDBOX_DOMAIN_URL = os.environ['MAILGUN_SANDBOX_DOMAIN_URL']
today = datetime.datetime.today()
todayiso = today.isoformat()
# DB Models
class Collection(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
description = db.Column(db.String(255))
active = db.Column(db.Boolean)
def __init__(self, name, description,active=None):
self.name = name
self.description = description
self.active = True
class Model(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
path = db.Column(db.String(200))
description = db.Column(db.String(255))
dimensions = db.Column(db.String(100))
price = db.Column(db.Float)
date_added = db.Column(db.Date)
popularity = db.Column(db.Integer)
collection_id = db.Column(db.Integer, db.ForeignKey('collection.id'))
collection = db.relationship("Collection", backref="models")
active = db.Column(db.Boolean)
def __init__(self, name, path, description, dimensions, price, date_added, collection_id,popularity=None,active=None):
self.name = name
self.path = path
self.description = description
self.dimensions = dimensions
self.price = price
self.date_added = today
self.collection_id = collection_id
self.popularity = 0
self.active = True
class Image(db.Model):
id = db.Column(db.Integer, primary_key=True)
path = db.Column(db.String(200))
model_id = db.Column(db.Integer, db.ForeignKey('model.id'))
model = db.relationship("Model", backref="images")
def __init__(self, path, model_id):
self.path = path
self.model_id = model_id
class Token(db.Model):
id = db.Column(db.Integer, primary_key=True)
authentise_token = db.Column(db.String(100))
date_added = db.Column(db.Date)
price_paid = db.Column(db.Float)
stripe_charge_id = db.Column(db.String(100))
model_id = db.Column(db.Integer, db.ForeignKey('model.id'))
model = db.relationship("Model", backref="token")
user_email = db.Column(db.String(100), db.ForeignKey('user.email'))
user = db.relationship("User", backref="token")
def __init__(self,date_added, price_paid, model_id, user_email, authentise_token=None, stripe_charge_id=None):
self.date_added = today
self.price_paid = price_paid
self.model_id = model_id
self.user_email = user_email
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(100),unique=True)
date_added = db.Column(db.Date)
_password = db.Column(db.String(100))
admin = db.Column(db.Boolean)
email_confirmed = db.Column(db.Boolean)
def __init__(self, email, date_added, password, admin, email_confirmed=None):
self.email = email
self.date_added = today
self.password = password
self.admin = admin
self.email_confirmed = False
@hybrid_property
def password(self):
return self._password
@password.setter
def _set_password(self, plaintext):
self._password = bcrypt.generate_password_hash(plaintext)
def is_correct_password(self, plaintext):
return bcrypt.check_password_hash(self._password, plaintext)
# User related functions
def get_users():
# Get all users
users = User.query.all()
return users
def get_10_users():
# Get last 10 users created
users = User.query.limit(10)
return users
def get_user_by_email(email):
# Get user by email
return User.query.filter_by(email=email).first()
def get_user_by_id(id):
# Get user by user id
return User.query.filter_by(id=id).first()
def create_user(email, password):
# Create user
# If it's the first user in the DB, it becomes admin.
if User.query.count() == 0:
admin = True
date_added = today
user = User(email, date_added, password, admin)
db.session.add(user)
db.session.commit()
return user
else:
# If it's not, then it's not admin
date_added = today
admin = False
user = User(email, date_added, password, admin)
db.session.add(user)
db.session.commit()
return user
def send_email_to_user(email, subject, html, shop_name):
mailgun_url = "https://api.mailgun.net/v3/{}/messages".format(MAILGUN_SANDBOX_DOMAIN_URL)
from_shop_name = "{} <mailgun@{}>".format(shop_name, MAILGUN_SANDBOX_DOMAIN_URL)
return requests.post(
mailgun_url,
auth=("api", MAILGUN_API_KEY),
data={"from": from_shop_name,
"to": [email],
"subject": subject,
"text": html})
def confirm_user(email):
# Marks the user as confirmed on the db
user = get_user_by_email(email)
if user:
user.email_confirmed = True
try:
db.session.add(user)
db.session.commit()
return user
except:
# If something went wrong, explicitly roll back the database
db.session.rollback()
def update_user(user, user_email, user_admin):
# Update user info
if user_email is None or user_email == '':
raise Exception("Invalid user email")
user.email = user_email
user.admin = user_admin
try:
db.session.commit()
return user
except:
# If something went wrong, explicitly roll back the database
db.session.rollback()
def change_user_password(user, user_password):
# Changes user password
user.password = user_password
try:
db.session.commit()
return user
except:
# If something went wrong, explicitly roll back the database
db.session.rollback()
return False
def delete_user(id):
# Delete user
# Checks if the user exists
user = User.query.get(id)
if user:
email = user.email
db.session.delete(user)
# Deletes user
try:
db.session.commit()
return "User {} deleted".format(email)
except:
# If something went wrong, explicitly roll back the database
db.session.rollback()
return "Something went wrong"
else:
return "User not found"
# Model related functions
def get_models():
# Get all models
models = Model.query.filter_by(active='True').all()
return models
def get_10_models():
# Get lastest 10 models created
models = Model.query.filter_by(active='True').limit(10)
return models
def get_popular_models():
# Get models ordered by field
models = Model.query.order_by('popularity').all()
return models
def get_models_by_collection(id):
# Get model from a collection
models = Model.query.filter_by(collection_id=id,active='True').all()
return models
def get_model_by_id(id):
# Get model by id
return Model.query.filter_by(id=id).first()
def search_models(search):
# Search models
term = '%{}%'.format(search)
name_models = Model.query.filter(Model.name.match(term))
description_models = Model.query.filter(Model.description.match(term))
models = []
for model in name_models:
models.append(model)
for model in description_models:
if model not in models:
models.append(model)
return models
def create_model(model_name, model_path, model_description, model_dimensions, model_collection, model_price):
# Create a new model
date_added = today
model = Model(model_name, model_path, model_description, model_dimensions, model_price, date_added, model_collection)
try:
db.session.add(model)
LOGGER.info("db.add")
db.session.commit()
LOGGER.info("db.commit")
return model
except Exception as e:
db.session.rollback()
LOGGER.info(e)
return e
def update_model(model, model_name, model_description, model_dimensions, model_collection, model_price):
# Update a model
model.name = model_name
model.description = model_description
model.dimensions = model_dimensions
model.collection_id = model_collection
model.price = model_price
try:
db.session.commit()
return model
except:
# If something went wrong, explicitly roll back the database
db.session.rollback()
def update_model_popularity(model):
# Update the popularity of a model
model.popularity += 1
try:
db.session.commit()
return model
except:
db.session.rollback()
def deactivate_model(id):
# Deactivate a model
model = Model.query.get(id)
if model:
LOGGER.info("Model deactivation")
model.active = False
try:
db.session.commit()
return model
except:
# If something went wrong, explicitly roll back the database
db.session.rollback()
# Saving model STL file related functions
def allowed_file(filename):
# Check if the file has a valid name
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def save_model(file):
# Save the model on the server
filename = "{}{}".format(todayiso,secure_filename(file.filename))
file.save(os.path.join(app.config['MODELS_FOLDER'], filename))
# Return the path to the model in the server
model_path = "/{}/{}".format(app.config['MODELS_FOLDER'], filename)
return model_path
# Saving images files related functions
def get_images_by_model_id(id):
# Get all images of a model by model id
return Image.query.filter_by(model_id=id).all()
def get_first_image_by_model_id(id):
# Get one image of a model by model id
return Image.query.filter_by(model_id=id).first()
def save_images(model_to_create, model_image1, model_image2, model_image3, model_image4, model_image5):
# Save an images on the server and creates an entries for them on the DB
model_id = model_to_create.id
images = [model_image1, model_image2, model_image3, model_image4, model_image5]
for image in images:
if image != None or image != "":
# If image is not null, save it in the server
filename = "{}{}".format(todayiso,secure_filename(image.filename))
image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Form the image path
image_path = "/{}/{}".format(app.config['UPLOAD_FOLDER'], filename)
try:
# Save image_path on the DB
save_image = add_images_to_model(image_path, model_id)
except Exception as e:
db.session.rollback()
LOGGER.info(e)
return e
def add_images_to_model(path, model_id):
# Save image_path on the DB, binding it to a model by model id
image = Image(path, model_id)
try:
db.session.add(image)
db.session.commit()
return image
except Exception as e:
db.session.rollback()
return e
# Collection related functions
def get_collections():
# Get all collections
collections = Collection.query.filter_by(active='True').all()
return collections
def get_collection_name_by_id(id):
# Get a collection name by id
collection = Collection.query.filter_by(id=id).first()
return collection.name
def get_collection_by_id(id):
# Get collection by id
collection = Collection.query.filter_by(id=id).first()
return collection
def create_collection(collection_name, collection_description):
# Create a collection
collection = Collection(collection_name, collection_description)
try:
db.session.add(collection)
db.session.commit()
return collection
except Exception as e:
db.session.rollback()
return e
def update_collection(collection, collection_name, collection_description):
# Update a collection
collection.description = collection_description
try:
db.session.commit()
return collection
except Exception as e:
db.session.rollback()
return e
def deactivate_collection(id):
# Deactivate a collection by id
collection = Collection.query.get(id)
if collection:
collection.active = False
try:
db.session.commit()
return collection
except Exception as e:
db.session.rollback()
return e
# Token related functions
def get_tokens():
# Get all tokens
tokens = Token.query.order_by(Token.date_added).all()
return tokens
def get_10_tokens():
# Get latest 10 tokens created
tokens = Token.query.order_by(Token.date_added).limit(10)
return tokens
def get_tokens_by_email(user_email):
# Get tokens by user email
tokens = Token.query.filter_by(user_email=user_email).order_by(Token.date_added).all()
return tokens
def get_token_by_id(id):
# Get a token by id
token = Token.query.filter_by(id=id).order_by(Token.date_added).first()
return token
def create_token(price_paid, model_id, user_email):
# Create a token
date_added = today
token = Token(date_added, price_paid, model_id, user_email)
try:
db.session.add(token)
db.session.commit()
return token
except Exception as e:
db.session.rollback()
return e
def update_token(token, authentise_token, stripe_charge_id):
# Update a token
token.authentise_token = authentise_token
token.stripe_charge_id = stripe_charge_id
try:
db.session.commit()
return token
except Exception as e:
db.session.rollback()
return e
# Authentise related functions
def authentise_create_token():
# Authenticate with authentise and create token
url = 'https://print.authentise.com/api3/api_create_partner_token'
response = requests.get(url, data={'api_key': AUTHENTISE_KEY})
if not response.ok:
raise Exception("Failed to create token: {} {}".format(response.status_code, response.text))
return response.json()
def authentise_upload_stl(file_name, token_authentise, print_value, email):
# Upload model STL file to Authentise
payload = {
'api_key' : AUTHENTISE_KEY,
'token' : token_authentise,
'receiver_email' : email,
'print_value' : print_value,
'print_value_currency' : 'USD',
}
url = 'https://print.authentise.com/api3/api_upload_partner_stl'
with open(file_name, 'rb') as f:
response = requests.post(url, data=payload, files={'stl_file': f})
if not response.ok:
raise Exception("Failed to upload {} to token {}: {} {}".format(file_name, token_authentise, response.status_code, response.text))
return response.json()
def create_authentise_token(model,token):
# Main Authentise function. Calls authentise_create_token() and authentise_upload_stl
ROOT = 'authentise.com'
result = authentise_create_token()
authentise_token = result['data']['token']
print_value = token.price_paid # price of the purchased 3D file
email = token.user_email # customer email
file_name = '.{}'.format(model.path) # customer purchased 3D file
result = authentise_upload_stl(file_name, authentise_token, print_value, email)
authentise_token_link = result['data']['ssl_token_link']
return authentise_token, authentise_token_link
def get_token_print_status(authentise_token):
# Check the status of a token
url = "https://print.authentise.com/api3/api_get_partner_print_status?api_key={}&token={}".format(AUTHENTISE_KEY, authentise_token)
# Parse json output
authentise_request = requests.get(url)
resp = json.loads(authentise_request.text)
if 'data' not in resp:
status = False
else:
if resp[u'data'][u'printing_job_status_name'] == 'SUCCESS':
status = True
else:
status = False
return status
def get_token_list_status(tokens):
# Check the status of list of tokens
token_status = []
for token in tokens:
status = get_token_print_status(token.authentise_token)
token_status.append(status)
return token_status
if __name__ == "__main__":
# Run this file directly to create the database tables.
print "Creating database tables..."
db.create_all()
print "Done!"