-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1082 lines (948 loc) · 41.8 KB
/
app.py
File metadata and controls
1082 lines (948 loc) · 41.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.dialects.postgresql import BIGINT, JSONB, VARCHAR, INTEGER
from flask_marshmallow import Marshmallow
import uuid
import bcrypt
import jwt
import datetime
import time
from functools import wraps
import os
import requests
import json
from dotenv import load_dotenv
from flask_cors import CORS, cross_origin
import sys # Used to make log statements,
# add a line after log statement:
# sys.stdout.flush()
# remember to include uswgi, psycopg2, marshmallow-sqlalchemy in reqs.txt, also bcrypt==3.1.7 which pipreqs gets wrong:
# psycopg2_binary==2.8.3
# marshmallow-sqlalchemy==0.19.0
# bcrypt==3.1.7
# psycopg2==2.8.4
# uwsgi==2.0.18
app = Flask(__name__)
ma = Marshmallow(app)
CORS(app)
app.config['DEBUG'] = True
dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
if os.path.exists(dotenv_path):
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv(
'SQLALCHEMY_DATABASE_URI')
else:
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
app.config['SECRET_KEY'] = 'totally%@#$%^T@#Secure!'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# Pinata API endpoints
pinata_pin_list = 'https://api.pinata.cloud/data/pinList'
pinata_json_url = 'https://api.pinata.cloud/pinning/pinJSONToIPFS'
def log(string, item=None):
print(string)
if item is not None:
print(json.dumps(item))
sys.stdout.flush()
### Models ###
class Users(db.Model):
__table_args__ = {'schema': 'admin'}
user_id = db.Column(VARCHAR, primary_key=True)
email = db.Column(VARCHAR)
password_hash = db.Column(VARCHAR)
pinata_api = db.Column(VARCHAR)
pinata_key = db.Column(VARCHAR)
def __init__(self, user_id, email, password_hash, pinata_api, pinata_key):
self.user_id = user_id
self.email = email
self.password_hash = password_hash
self.pinata_api = pinata_api
self.pinata_key = pinata_key
class UserCollections(db.Model):
user_id = db.Column(VARCHAR, primary_key=True)
schedule = db.Column(JSONB)
deck_ids = db.Column(JSONB)
deleted_deck_ids = db.Column(JSONB)
all_deck_cids = db.Column(JSONB)
webapp_settings = db.Column(JSONB)
extension_settings = db.Column(JSONB)
highlight_urls = db.Column(JSONB)
all_card_tags = db.Column(JSONB)
all_deck_tags = db.Column(JSONB)
def __init__(self, user_id, schedule, deck_ids, deleted_deck_ids, all_deck_cids, webapp_settings, extension_settings, highlight_urls, all_card_tags, all_deck_tags):
self.user_id = user_id
self.schedule = schedule
self.deck_ids = deck_ids
self.deleted_deck_ids = deleted_deck_ids
self.all_deck_cids = all_deck_cids
self.webapp_settings = webapp_settings
self.extension_settings = extension_settings
self.highlight_urls = highlight_urls
self.all_card_tags = all_card_tags
self.all_deck_tags = all_deck_tags
class Decks(db.Model):
deck_id = db.Column(VARCHAR, primary_key=True)
edited = db.Column(BIGINT)
deck_cid = db.Column(VARCHAR)
deck = db.Column(JSONB)
title = db.Column(VARCHAR)
card_count = db.Column(INTEGER)
# created by?
def __init__(self, deck_id, edited, deck_cid, deck, title, card_count):
self.deck = deck
# These values are repeated, should be the same as inside the deck, used for
# Quick access of metadata, without an expensive query of the deck
self.deck_id = deck_id
self.edited = edited
self.deck_cid = deck_cid
self.title = title
self.card_count = card_count
class Websites(db.Model):
url = db.Column(VARCHAR, primary_key=True)
site_owner = db.Column(VARCHAR)
cards = db.Column(JSONB)
lessons = db.Column(JSONB)
highlights = db.Column(JSONB)
deleted = db.Column(JSONB)
def __init__(self, url, site_owner, cards, lessons, highlights, deleted):
self.url = url
self.site_owner = site_owner
self.cards = cards
self.lessons = lessons
self.highlights = highlights
self.deleted = deleted
### Schemas ###
class UserCollectionsSchema(ma.Schema):
class Meta:
fields = ("user_id", "schedule", "deck_ids", "all_deck_cids",
"deleted_deck_ids", "webapp_settings", "extension_settings", "highlight_urls", "all_card_tags", "all_deck_tags")
class DecksSchema(ma.Schema):
class Meta:
fields = ("deck_id", "edited", "deck_cid",
"deck", "title", "card_count")
class WebsitesSchema(ma.Schema):
class Meta:
fields = ("url", "site_owner", "cards",
"lessons", "highlights", "deleted")
user_collection_schema = UserCollectionsSchema()
deck_schema = DecksSchema()
decks_schema = DecksSchema(many=True)
website_schema = WebsitesSchema()
websites_schema = WebsitesSchema(many=True)
### JWT token checker ###
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
token = None
if 'x-access-token' in request.headers:
token = request.headers['x-access-token']
if not token:
return jsonify({'message': 'Token is missing!'}), 401
try:
data = jwt.decode(token, app.config['SECRET_KEY'])
current_user = Users.query.filter_by(
user_id=data['user_id']).first()
except:
return jsonify({'message': 'Token is invalid!'}), 401
return f(current_user, *args, **kwargs)
return decorated
@app.route('/')
@cross_origin(origin='*')
def welcome():
return 'Welcome to the IPFC backend, for more information, visit https://github.com/ipfc'
### API call routes ###
@app.route('/sign_up', methods=['POST'])
@cross_origin(origin='*')
def sign_up():
data = request.get_json()
log(' >>>>>>signing up, data', data)
exists = Users.query.filter_by(email=data['email']).first()
if exists is not None:
return jsonify({"error": "Email already exists"})
else:
hashed_password = bcrypt.hashpw(
data['password'].encode('utf8'), bcrypt.gensalt())
user_id = str(uuid.uuid4())
new_user = Users(user_id=user_id,
email=data['email'],
password_hash=hashed_password.decode('utf8'),
pinata_api=data['pinata_api'],
pinata_key=data['pinata_key'])
db.session.add(new_user)
new_collection = UserCollections(user_id=user_id,
schedule={},
deck_ids=[],
deleted_deck_ids=[],
all_deck_cids=[],
webapp_settings={},
extension_settings={},
highlight_urls={
'list': [], 'edited': 0},
all_card_tags={
'list': [], 'edited': 0},
all_deck_tags={
'list': [], 'edited': 0}
)
if 'user_collection' in data:
if 'schedule' in data['user_collection']:
new_collection.schedule = data['user_collection']['schedule']
if 'deleted_deck_ids' in data['user_collection']:
new_collection.deleted_deck_ids = data['user_collection']['deleted_deck_ids']
if 'all_deck_cids' in data['user_collection']:
new_collection.all_deck_cids = data['user_collection']['all_deck_cids']
if 'webapp_settings' in data['user_collection']:
new_collection.webapp_settings = data['user_collection']['webapp_settings']
if 'extension_settings' in data['user_collection']:
new_collection.extension_settings = data['user_collection']['extension_settings']
if 'highlight_urls' in data['user_collection']:
new_collection.highlight_urls = data['user_collection']['highlight_urls']
if 'all_card_tags' in data['user_collection']:
new_collection.all_card_tags = data['user_collection']['all_card_tags']
if 'all_deck_tags' in data['user_collection']:
new_collection.all_deck_tags = data['user_collection']['all_deck_tags']
db.session.add(new_collection)
db.session.commit()
return jsonify({'message': 'New user created!'})
@app.route('/login', methods=['GET'])
# @cross_origin(origin='*')
def login():
# log(" starting login ", str(datetime.datetime.utcnow()))
sys.stdout.flush()
auth = request.authorization
if not auth or not auth.username or not auth.password:
return jsonify({"error": "Invalid credentials"})
user = Users.query.filter_by(email=auth.username).first()
if not user:
return jsonify({"error": "Invalid credentials"})
# verified path
if bcrypt.checkpw(auth.password.encode('utf8'), user.password_hash.encode('utf8')):
token = jwt.encode({'user_id': user.user_id, 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=3)},
app.config['SECRET_KEY'])
# Get pinata keys
log(" Getting pinata keys " + str(datetime.datetime.utcnow()))
sys.stdout.flush()
user = Users.query.filter_by(user_id=user.user_id).first()
pinata_keys = {'pinata_api': user.pinata_api,
'pinata_key': user.pinata_key, }
# # Get decks metadata
# log(" Getting decks meta " + str(datetime.datetime.utcnow()))
# sys.stdout.flush()
# deck_ids = user_collection.deck_ids
# decks_meta = []
# for deck_id in deck_ids:
# deck = Decks.query.filter_by(deck_id=deck_id).first()
# dump = deck_schema.dump(deck)
# if len(dump) > 3:
# deck_meta = {
# 'title': dump['title'],
# 'edited': dump['edited'],
# 'deck_cid': dump['deck_cid'],
# 'deck_id': dump['deck_id']
# }
# decks_meta.append(deck_meta)
# # delete blank or incomplete decks
# else:
# log(" incomplete decks detected ", dump)
# sys.stdout.flush()
# db.session.query(Decks).filter(Decks.deck_id == deck_id).delete()
# if deck_id in user_collection.deck_ids:
# deck_ids_list = user_collection.deck_ids.copy()
# deck_ids_list.remove(deck_id)
# user_collection.deck_ids = deck_ids_list
# if deck_id not in user_collection.deleted_deck_ids:
# deleted_deck_ids_list = user_collection.deleted_deck_ids.copy()
# deleted_deck_ids_list.append(deck_id)
# user_collection.deleted_deck_ids = deleted_deck_ids_list
# db.session.commit()
login_return_data = {
'token': token.decode('UTF-8'),
'pinata_keys': pinata_keys,
'user_id': user.user_id,
}
log(" returning" + str(datetime.datetime.utcnow()))
sys.stdout.flush()
return jsonify(login_return_data)
return jsonify({"error": "Invalid credentials"})
# deprecated -already added this step to sign up. leaving this just in case
@app.route('/post_user_collection', methods=['POST'])
@cross_origin(origin='*')
@token_required
def post_user_collection(current_user):
data = request.get_json()
new_collection = UserCollections(user_id=current_user.user_id,
schedule=data['schedule'],
deck_ids=data['deck_ids'],
deleted_deck_ids=data['deleted_deck_ids'],
all_deck_cids=data['all_deck_cids'],
webapp_settings=data['webapp_settings'],
extension_settings=data['extension_settings'],
highlight_urls=data['highlight_urls'],
)
db.session.add(new_collection)
db.session.commit()
return user_collection_schema.dump(new_collection)
@app.route('/get_decks_meta_and_collection', methods=['GET'])
@cross_origin(origin='*')
@token_required
def get_decks_meta_and_collection(current_user):
# check pinata here
user_collection = UserCollections.query.filter_by(
user_id=current_user.user_id).first()
user_collection_dump = user_collection_schema.dump(user_collection)
# dealing with mysterious null values in the deleted_deck_ids
deleted_deck_ids_list = []
for item in user_collection_dump['deleted_deck_ids']:
if isinstance(item, str):
deleted_deck_ids_list.append(item)
user_collection.deleted_deck_ids = deleted_deck_ids_list
user_collection_dump['deleted_deck_ids'] = deleted_deck_ids_list
db.session.commit()
###
return_data = {
'user_collection': user_collection_dump,
'decks_meta': []
}
deck_ids = user_collection_dump['deck_ids']
for deck_id in deck_ids:
# getting the whole schema includes the deck. Should update this to only get the meta feilds
dump = deck_schema.dump(Decks.query.filter_by(deck_id=deck_id).first())
if len(dump) > 3: # this shouldnt be the case, maybe do a check on login that deck colleciton and decks are aligned or empty
deck_meta = {
'title': dump['title'],
'edited': dump['edited'],
'deck_cid': dump['deck_cid'],
'deck_id': dump['deck_id'],
'card_count': dump['card_count']
}
return_data['decks_meta'].append(deck_meta)
return jsonify(return_data)
@app.route('/get_user_collection', methods=['GET'])
@cross_origin(origin='*')
@token_required
def get_user_collection(current_user):
# check pinata here
user_collection = UserCollections.query.filter_by(
user_id=current_user.user_id).first()
return user_collection_schema.dump(user_collection)
@app.route('/put_user_collection', methods=['PUT'])
@cross_origin(origin='*')
@token_required
def put_user_collection(current_user):
data = request.get_json()
user_collection = UserCollections.query.filter_by(
user_id=current_user.user_id).first()
if 'schedule' in data:
user_collection.schedule = data['schedule']
if 'deck_ids' in data:
user_collection.deck_ids = data['deck_ids']
if 'deleted_deck_ids' in data:
user_collection.deleted_deck_ids = data['deleted_deck_ids']
if 'all_deck_cids' in data:
user_collection.all_deck_cids = data['all_deck_cids']
if 'webapp_settings' in data:
user_collection.webapp_settings = data['webapp_settings']
if 'extension_settings' in data:
user_collection.extension_settings = data['extension_settings']
if 'highlight_urls' in data:
user_collection.highlight_urls = data['highlight_urls']
if 'all_card_tags' in data:
user_collection.all_card_tags = data['all_card_tags']
if 'all_deck_tags' in data:
user_collection.all_deck_tags = data['all_deck_tags']
db.session.commit()
user_collection = UserCollections.query.filter_by(
user_id=current_user.user_id).first()
return user_collection_schema.dump(user_collection)
@app.route('/get_deck', methods=['POST'])
@cross_origin(origin='*')
@token_required
def get_deck(current_user):
data = request.get_json()
deck_id = data['deck_id']
dump = deck_schema.dump(Decks.query.filter_by(deck_id=deck_id).first())
return dump['deck']
@app.route('/get_decks', methods=['POST'])
@cross_origin(origin='*')
@token_required
def get_decks(current_user):
data = request.get_json()
deck_ids = data
decks = []
not_found = []
for deck_id in deck_ids:
dump = deck_schema.dump(Decks.query.filter_by(deck_id=deck_id).first())
if 'deck' in dump:
decks.append(dump['deck'])
else:
log('deck not found: ', deck_id)
not_found.append(deck_id)
user_collection = UserCollections.query.filter_by(
user_id=current_user.user_id).first()
collection_dump = user_collection_schema.dump(user_collection)
server_deck_ids = collection_dump['deck_ids']
user_collection.deck_ids = server_deck_ids
del server_deck_ids[server_deck_ids.index(deck_id)]
# log('server_deck_ids', server_deck_ids)
db.session.commit()
return jsonify({'decks': decks, 'not_found': not_found})
@app.route('/post_deck', methods=['POST'])
@cross_origin(origin='*')
@token_required
def post_deck(current_user):
client_deck = request.get_json()
exists_in_decks = Decks.query.filter_by(
deck_id=client_deck['deck_id']).first()
user_collection = UserCollections.query.filter_by(
user_id=current_user.user_id).first()
if client_deck['deck_id'] not in user_collection.deck_ids:
deck_ids_list = user_collection.deck_ids.copy()
deck_ids_list.append(client_deck['deck_id'])
user_collection.deck_ids = deck_ids_list
db.session.commit()
pinata_api = current_user.pinata_api
pinata_key = current_user.pinata_key
pinata_api_headers = {"Content-Type": "application/json", "pinata_api_key": pinata_api,
"pinata_secret_api_key": pinata_key}
if exists_in_decks is not None:
return jsonify({"error": "Deck already exists"})
else:
new_deck = Decks(
deck=client_deck,
# these echo 'deck' internal info to allow for less expensive database metadata queries
deck_id=client_deck['deck_id'],
title=client_deck['title'],
edited=client_deck['edited'],
deck_cid="",
card_count=len(client_deck['cards'])
)
db.session.add(new_deck)
db.session.commit()
json_data_for_API = {}
json_data_for_API["pinataMetadata"] = {
"name": new_deck.title,
"keyvalues": {"deck_id": new_deck.deck_id, "edited": new_deck.edited}
}
json_data_for_API["pinataContent"] = deck_schema.dump(new_deck)
req = requests.post(
pinata_json_url, json=json_data_for_API, headers=pinata_api_headers)
pinata_api_response = json.loads(req.text)
log(" uploaded deck to IPFS. Hash: " +
pinata_api_response["IpfsHash"])
sys.stdout.flush()
deck_cid = pinata_api_response["IpfsHash"]
new_deck.deck_cid = deck_cid
db.session.commit()
return deck_schema.dump(new_deck)
@app.route('/post_decks', methods=['POST'])
@cross_origin(origin='*')
@token_required
def post_decks(current_user):
client_decks = request.get_json()
decks_added = []
decks_not_added = []
user_collection = UserCollections.query.filter_by(
user_id=current_user.user_id).first()
for client_deck in client_decks:
if client_deck['deck_id'] not in user_collection.deck_ids:
# aparently you can't directly append a list in SQLalchemy
deck_ids_list = user_collection.deck_ids.copy()
deck_ids_list.append(client_deck['deck_id'])
user_collection.deck_ids = deck_ids_list
db.session.commit()
exists = Decks.query.filter_by(deck_id=client_deck['deck_id']).first()
pinata_api = current_user.pinata_api
pinata_key = current_user.pinata_key
pinata_api_headers = {"Content-Type": "application/json", "pinata_api_key": pinata_api,
"pinata_secret_api_key": pinata_key}
if exists is not None:
decks_not_added.append(client_deck['title'])
else:
new_deck = Decks(
deck=client_deck,
# these echo 'deck' internal info to allow for less expensive database metadata queries
deck_id=client_deck['deck_id'],
title=client_deck['title'],
edited=client_deck['edited'],
deck_cid="",
card_count=len(client_deck['cards'])
)
db.session.add(new_deck)
db.session.commit()
json_data_for_API = {}
json_data_for_API["pinataMetadata"] = {
"name": new_deck.title,
"keyvalues": {"deck_id": new_deck.deck_id, "edited": new_deck.edited}
}
json_data_for_API["pinataContent"] = deck_schema.dump(new_deck)
req = requests.post(
pinata_json_url, json=json_data_for_API, headers=pinata_api_headers)
pinata_api_response = json.loads(req.text)
log(" uploaded deck to IPFS. Hash: " +
pinata_api_response["IpfsHash"])
sys.stdout.flush()
deck_cid = pinata_api_response["IpfsHash"]
new_deck.deck_cid = deck_cid
db.session.commit()
decks_added.append(client_deck['title'])
return jsonify({'decks_added': decks_added, 'decks_not_added': decks_not_added})
@app.route('/put_deck', methods=['PUT'])
@cross_origin(origin='*')
@token_required
def put_deck(current_user):
client_deck = request.get_json()
server_deck = Decks.query.filter_by(
deck_id=client_deck['deck_id']).first()
pinata_api = current_user.pinata_api
pinata_key = current_user.pinata_key
pinata_api_headers = {"Content-Type": "application/json", "pinata_api_key": pinata_api,
"pinata_secret_api_key": pinata_key}
# Check IPFS metadata here-------
# query_string = '?metadata[keyvalues]={"deck_id":{"value":"' + data['deck_id'] + '","op":"eq"}}'
# req = requests.get(pinata_pin_list + query_string, headers=pinata_api_headers)
# pinata_data = json.loads(req.text)
# if pinata_data['edited'] > server_deck.edited and pinata_data['edited'] > data['edited']:
# server_deck... = pinata_data['...']
# db.session.commit()
# this check should've already been performed in app, but its not too expensive
# check edited date isn't older than one in database, if it is, return newest
# and data['edited'] > pinata_data['edited']:
if client_deck['edited'] > server_deck.edited:
server_deck.deck = client_deck
server_deck.title = client_deck['title']
server_deck.edited = client_deck['edited']
server_deck.card_count = len(client_deck['cards'])
db.session.commit()
# then if the pinata version wasn't the newest, upload to pinata
# change this when add pinata
# if pinata_data['edited'] < server_deck.edited or pinata_data['edited'] < data['edited']:
json_data_for_API = {}
json_data_for_API["pinataMetadata"] = {
"name": server_deck.title,
"keyvalues": {"deck_id": server_deck.deck_id, "edited": server_deck.edited}
}
json_data_for_API["pinataContent"] = deck_schema.dump(server_deck)
req = requests.post(
pinata_json_url, json=json_data_for_API, headers=pinata_api_headers)
pinata_api_response = json.loads(req.text)
log(" uploaded deck to IPFS. Hash: " +
pinata_api_response["IpfsHash"])
sys.stdout.flush()
server_deck.deck_cid = pinata_api_response["IpfsHash"]
db.session.commit()
return jsonify({'message': 'Deck updated', 'deck': deck_schema.dump(server_deck)})
# else return the database version saved as server_deck
else: # do we need the dump? compare with put_decks
return jsonify({'message': 'Server decks is newer', 'deck': deck_schema.dump(server_deck)})
@app.route('/put_decks', methods=['PUT'])
@cross_origin(origin='*')
@token_required
def put_decks(current_user):
pinata_api = current_user.pinata_api
pinata_key = current_user.pinata_key
pinata_api_headers = {"Content-Type": "application/json", "pinata_api_key": pinata_api,
"pinata_secret_api_key": pinata_key}
updated_decks = []
not_updated_decks = []
data = request.get_json()
for client_deck in data:
server_deck = Decks.query.filter_by(
deck_id=client_deck['deck_id']).first()
# Check IPFS metadata here-------
# query_string = '?metadata[keyvalues]={"deck_id":{"value":"' + data['deck_id'] + '","op":"eq"}}'
# req = requests.get(pinata_pin_list + query_string, headers=pinata_api_headers)
# pinata_data = json.loads(req.text)
# if pinata_data['edited'] > server_deck.edited and pinata_data['edited'] > data['edited']:
# server_deck... = pinata_data['...']
# db.session.commit()
# this check should've already been performed in app, but its not too expensive
# check edited date isn't older than one in database, if it is, return newest
# and data['edited'] > pinata_data['edited']:
if client_deck['edited'] > server_deck.edited:
# https: //stackoverflow.com/questions/47735329/updating-a-row-using-sqlalchemy-orm
# some weirdness where there is no .update function unless you use .query
db.session.query(Decks).filter(Decks.deck_id == client_deck['deck_id']).update({
'deck': client_deck,
'title': client_deck['title'],
'edited': client_deck['edited'],
'card_count': len(client_deck['cards'])
}, synchronize_session=False)
db.session.commit()
# then if the pinata version wasn't the newest, upload to pinata
# change this when add pinata
# if pinata_data['edited'] < server_deck.edited or pinata_data['edited'] < data['edited']:
json_data_for_API = {}
json_data_for_API["pinataMetadata"] = {
"name": server_deck.title,
"keyvalues": {"deck_id": server_deck.deck_id, "edited": server_deck.edited}
}
json_data_for_API["pinataContent"] = deck_schema.dump(server_deck)
req = requests.post(
pinata_json_url, json=json_data_for_API, headers=pinata_api_headers)
pinata_api_response = json.loads(req.text)
log(" uploaded deck to IPFS. Hash: " +
pinata_api_response["IpfsHash"])
sys.stdout.flush()
server_deck.deck_cid = pinata_api_response["IpfsHash"]
db.session.commit()
updated_decks.append(server_deck.deck_id)
# else return the database version saved as server_deck
else:
not_updated_decks.append(server_deck.deck_id)
return jsonify({"updated decks": updated_decks, "not updated decks": not_updated_decks})
@app.route('/delete_deck', methods=['DELETE'])
@cross_origin(origin='*')
@token_required
def delete_deck(current_user):
data = request.get_json()
deck_id = data['deck_id']
deck = Decks.query.filter_by(deck_id=deck_id).first()
if not deck:
return jsonify({'message': 'No deck found!'})
db.session.delete(deck)
user_collection = UserCollections.query.filter_by(
user_id=current_user.user_id).first()
if deck_id in user_collection.deck_ids:
deck_ids_list = user_collection.deck_ids.copy()
deck_ids_list.remove(deck_id)
user_collection.deck_ids = deck_ids_list
if deck_id not in user_collection.deleted_deck_ids:
deleted_deck_ids_list = user_collection.deleted_deck_ids.copy()
deleted_deck_ids_list.append(deck_id)
user_collection.deleted_deck_ids = deleted_deck_ids_list
db.session.commit()
return jsonify({'message': 'Deck deleted!'})
@app.route('/delete_decks', methods=['DELETE'])
@cross_origin(origin='*')
@token_required
def delete_decks(current_user):
reply_message = {'message': ''}
user_collection = UserCollections.query.filter_by(
user_id=current_user.user_id).first()
data = request.get_json()
for deck_id in data['deck_ids']:
if deck_id is not None:
deck = Decks.query.filter_by(deck_id=deck_id).first()
if not deck:
reply_message['message'] += ' No deck found!: ' + deck_id
else:
db.session.delete(deck)
reply_message['message'] += ' Deck Deleted!: ' + deck_id
if deck_id in user_collection.deck_ids:
deck_ids_list = user_collection.deck_ids.copy()
deck_ids_list.remove(deck_id)
user_collection.deck_ids = deck_ids_list
if deck_id not in user_collection.deleted_deck_ids:
deleted_deck_ids_list = user_collection.deleted_deck_ids.copy()
deleted_deck_ids_list.append(deck_id)
user_collection.deleted_deck_ids = deleted_deck_ids_list
db.session.commit()
return jsonify(reply_message)
@app.route('/post_card', methods=['POST'])
@cross_origin(origin='*')
@token_required
def post_card(current_user):
data = request.get_json()
card = data['card']
deck_id = data['deck_id']
server_deck = Decks.query.filter_by(deck_id=deck_id).first()
deck_dump = deck_schema.dump(server_deck)
deck = deck_dump['deck']
# log('deck before', deck)
for old_card in deck['cards']:
if old_card['card_id'] == card['card_id']:
return jsonify({'card already exists': card})
deck['cards'].append(card)
now = round(time.time() * 1000)
deck['edited'] = now
deck['card_count'] = len(deck['cards'])
db.session.query(Decks).filter(Decks.deck_id == deck_id).update({
'deck': deck,
'edited': now,
'card_count': len(deck['cards'])
}, synchronize_session=False)
db.session.commit()
#
server_deck = deck_schema.dump(
Decks.query.filter_by(deck_id=deck_id).first())
# log('server deck after', server_deck)
return jsonify({'added card': card})
@app.route('/delete_card', methods=['DELETE'])
@cross_origin(origin='*')
@token_required
def delete_card(current_user):
log(' >>>>>>> delete_card')
data = request.get_json()
deck_id = data['deck_id']
card = data['card']
deck = deck_schema.dump(Decks.query.filter_by(deck_id=deck_id).first())
log('deck before', deck)
if not deck:
return jsonify({'message': 'No deck found!'})
updated_cards = []
for old_card in deck['cards']:
if old_card['card_id'] != card['card_id']:
updated_cards.append(card)
deck['cards'] = updated_cards
log('deck after', deck)
now = round(time.time() * 1000)
db.session.query(Decks).filter(Decks.deck_id == deck_id).update({
'deck': deck,
'edited': now,
'card_count': len(deck['cards'])
}, synchronize_session=False)
db.session.commit()
return jsonify({'message': 'Deck deleted!'})
@app.route('/put_card', methods=['PUT'])
@cross_origin(origin='*')
@token_required
def put_card(current_user):
data = request.get_json()
card = data['card']
deck_id = data['deck_id']
server_deck = Decks.query.filter_by(deck_id=deck_id).first()
deck_dump = deck_schema.dump(server_deck)
deck = deck_dump['deck']
#
# log('deck before', deck)
for index in range(0, len(deck['cards'])):
if deck['cards'][index]['card_id'] == card['card_id']:
deck['cards'][index] = card
now = round(time.time() * 1000)
deck['edited'] = now
deck['card_count'] = len(deck['cards'])
db.session.query(Decks).filter(Decks.deck_id == deck_id).update({
'deck': deck,
'edited': now,
'card_count': len(deck['cards'])
}, synchronize_session=False)
db.session.commit()
#
server_deck = deck_schema.dump(
Decks.query.filter_by(deck_id=deck_id).first())
# log('server deck after', server_deck)
return jsonify({'updated card': card})
return jsonify({'message': 'card not found'})
@app.route('/get_deck_meta', methods=['POST'])
@cross_origin(origin='*')
@token_required
def get_deck_meta(current_user):
data = request.get_json()
deck_id = data['deck_id']
dump = deck_schema.dump(Decks.query.filter_by(deck_id=deck_id).first())
if dump is not None:
deck_meta = {
'title': dump['title'],
'edited': dump['edited'],
'deck_cid': dump['deck_cid'],
'deck_id': dump['deck_id'],
'card_count': dump['card_count']
}
return jsonify(deck_meta)
@app.route('/get_decks_meta', methods=['GET'])
@cross_origin(origin='*')
@token_required
def get_decks_meta(current_user):
user_collection = UserCollections.query.filter_by(
user_id=current_user.user_id).first()
deck_ids = user_collection.deck_ids
decks_meta = []
for deck_id in deck_ids:
dump = deck_schema.dump(Decks.query.filter_by(deck_id=deck_id).first())
if len(dump) > 3: # this shouldnt be the case, maybe do a check on login that deck colleciton and decks are aligned or empty
deck_meta = {
'title': dump['title'],
'edited': dump['edited'],
'deck_cid': dump['deck_cid'],
'deck_id': dump['deck_id'],
'card_count': dump['card_count']
}
decks_meta.append(deck_meta)
return jsonify(decks_meta)
@app.route('/get_websites_meta', methods=['GET'])
@cross_origin(origin='*')
@token_required
def get_websites_meta(current_user):
# issue is that its returning deleted cards
log(' >>>>>>> get_websites_meta')
user_collection = user_collection_schema.dump(
UserCollections.query.filter_by(user_id=current_user.user_id).first())
# we want to structure this like the 'highlights' object in the extension
websites_meta = {}
# log('highlight_urls', user_collection['highlight_urls'])
for url in user_collection['highlight_urls']['list']:
website = website_schema.dump(
Websites.query.filter_by(url=url).first())
if website is None or website == {}:
log('website not found', url)
websites_meta[url] = 'website not found'
else:
# log('website', website)
websites_meta[url] = {}
if 'cards' in website:
websites_meta[url]['cards'] = []
for card in website['cards']:
websites_meta[url]['cards'].append(
{'card_id': card['card_id'], 'edited': card['edited'], 'user_id': card['user_id']})
if 'deleted' in website:
websites_meta[url]['deleted'] = website['deleted']
if 'highlights' in website:
websites_meta[url]['highlights'] = {}
for highlight in website['highlights']:
websites_meta[url]['highlights'][highlight] = {
'edited': website['highlights'][highlight]['edited'],
'user_id': website['highlights'][highlight]['user_id']
}
return jsonify({'websites_meta': websites_meta})
@app.route('/get_websites_selected_content', methods=['POST'])
@cross_origin(origin='*')
@token_required
def get_websites_selected_content(current_user):
# also returning deleted cards
log('>>>>>> get_websites_selected_content')
data = request.get_json()
results = {}
for url in data:
server_website = website_schema.dump(
Websites.query.filter_by(url=url).first())
# log('server_website', server_website)
if server_website is not None and server_website != {}:
results[url] = {}
client_website = data[url]
if 'deleted' in server_website and 'deleted' in client_website:
results[url]['deleted'] = []
for item in server_website['deleted']:
if item in client_website['deleted']:
results[url]['deleted'].append(item)
if 'highlights' in server_website and 'highlights' in client_website:
results[url]['highlights'] = {}
for highlight_id in server_website['highlights']:
for highlight_id_c in client_website['highlights']:
if highlight_id == highlight_id_c:
results[url]['highlights'][highlight_id] = server_website['highlights'][highlight_id]
break
if 'cards' in server_website and 'cards' in client_website:
results[url]['cards'] = []
for card in server_website['cards']:
for card_c in client_website['cards']:
if card['card_id'] == card_c['card_id']:
results[url]['cards'].append(card)
break
return jsonify({'websites': results})
# can use this for POST and PUT
@app.route('/post_websites', methods=['POST'])
@cross_origin(origin='*')
@token_required
def post_websites(current_user):
"""Can use this for POST and PUT of highlights.
Make sure to sync user_collection so that our highlights_url is up to date, and compare_highlights first"""
log(' >>>>>> post_websites')
return_message = {}
def create_new_website(client_website):
highlights = {}
# log('client_website', client_website)
if 'highlights' in client_website:
for highlight in client_website['highlights']:
if client_website['highlights'][highlight]['user_id'] == user_id:
highlights[highlight] = client_website['highlights'][highlight]
cards = []
if 'cards' in client_website:
for card in client_website['cards']:
if card['user_id'] == user_id:
cards.append(card)
deleted = []
if 'deleted' in client_website:
deleted = client_website['deleted']
new_url = Websites(url=url, highlights=highlights,
cards=cards, site_owner='', lessons={}, deleted=deleted)
# log('added url', {'highlights': highlights, 'cards': cards})
db.session.add(new_url)
db.session.commit()
if 'websites added' not in return_message:
return_message['websites added'] = {}
return_message['websites added'][url] = {
'highlights': highlights, 'cards': cards, 'deleted': deleted}
data = request.get_json()
# log(' data', data)
user_collection = UserCollections.query.filter_by(
user_id=current_user.user_id).first()
user_id = user_collection.user_id
client_websites = data['websites']
for url in client_websites:
client_website = client_websites[url]
# log('client_website', client_website)
server_website = website_schema.dump(
Websites.query.filter_by(url=url).first())
# log('server_website', server_website)
# log('client_website', client_website)
if server_website is None:
create_new_website(client_website)
elif server_website == {}:
create_new_website(client_website)
else:
highlights = {}
cards = []