-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
1164 lines (1015 loc) · 51.2 KB
/
app.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
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 g, Flask, request, render_template, redirect, url_for, send_file, abort, jsonify
from flask_login import LoginManager, login_user, current_user, login_required, logout_user
from werkzeug.utils import secure_filename
from elasticapm.contrib.flask import ElasticAPM
from celery import Celery
import os, datetime, logging
import lukso_utils
from captcha import ImageCaptcha
from nft_doc import NFTDoc
from user import User
from feedback import Feedback
from elastic import Elastic
from form import *
from announcement import Announcement
from common import Common
from config import Config
from lukso import Lukso
import db, security, file_util, nft_util, trophy
from flask_bootstrap import Bootstrap5
# env = {'DEV', 'LIVE'}
site_env = 'DEV'
if site_env == 'LIVE':
db_url = 'postgres://127.0.0.1:5432/mainnet'
else:
db_url = 'postgres://127.0.0.1:5432/testnet'
conf = Config(site_env, db_url)
site_version = '1.0.0'
site_title = conf.get('site_name') + ' v' + site_version
acquire_period = 13
app = Flask(__name__)
app.config['SECRET_KEY'] = conf.get('secret_key')
app.config['WTF_CSRF_SECRET_KEY'] = conf.get('csrf_key')
# app.config['ELASTIC_APM'] = {
# 'SERVICE_NAME': conf.get('site_name'),
# 'ELASTIC_APM_SERVICE_VERSION': site_version,
# 'ELASTIC_APM_DISABLE_METRICS': None,
# 'ELASTIC_APM_BREAKDOWN_METRICS': True
# }
app.config['CELERY_broker_url'] = 'redis://localhost:6379/0'
app.config['result_backend'] = 'redis://localhost:6379/0'
app.jinja_env.globals['apply_markup'] = nft_util.apply_markup
app.jinja_env.globals['generate_token'] = nft_util.generate_token
app.jinja_env.globals['pretty_size'] = file_util.pretty_size
app.jinja_env.globals['pretty_time'] = file_util.pretty_time
app.jinja_env.globals['site_title'] = site_title
# apm = ElasticAPM(app, logging=logging.WARNING)
login_manager = LoginManager(app)
es_nft = Elastic(index_name=conf.get('index_name'))
captcha = ImageCaptcha()
bootstrap = Bootstrap5(app)
lukso = Lukso(3000)
celery = Celery(app.name, broker=app.config['CELERY_broker_url'])
celery.conf.update(app.config)
# app.jinja_env.globals['menu_items'] = menuItems
@app.template_filter('format_balance')
def format_balance(value):
if value is None:
return ""
return '{0:.4f}'.format(value).rstrip('0').rstrip('.')
@celery.task(bind=True, autoretry_for=(Exception,), retry_kwargs={'max_retries': 5})
def celery_new_nft(self, name, url, description, symbol, creator):
with app.app_context():
self.update_state(state='UPLOADING_METADATA', meta={'details': {}})
meta = lukso.upload_metadata(name, url, description)
self.update_state(state='METADATA_UPLOADED', meta={'details': meta})
lsp7 = lukso.new_lsp7(name, symbol, creator, meta)
self.update_state(state='LSP7_CREATED', meta={'details': lsp7})
minted = lukso.mint_lsp8(creator, lsp7['address'])
self.update_state(state='LSP8_MINTED', meta={'details': minted})
import db
conn = db.get_connection(db_url)
conn.cursor().execute('UPDATE nft SET lsp7=%s WHERE id=%s;', (lsp7['address'], url[19:]))
conn.close()
self.update_state(state='DB_UPDATED', meta={'details': minted})
return {'details': minted}
@app.route('/status_new_nft/<task_id>')
def status_new_nft(task_id):
task = celery_new_nft.AsyncResult(task_id)
response = {
'state': task.state,
'details': task.info.get('details', 'error'),
}
return jsonify(response)
@celery.task(bind=True, autoretry_for=(Exception,), retry_kwargs={'max_retries': 5})
def celery_purchase(self, up, lsp7):
with app.app_context():
self.update_state(state='MINTING_LSP7', meta={'details': {}})
minted = lukso.mint_lsp7(up, lsp7)
self.update_state(state='LSP7_MINTED', meta={'details': minted})
return {'details': minted}
@app.route('/status_purchase/<task_id>')
def status_purchase(task_id):
task = celery_purchase.AsyncResult(task_id)
response = {
'state': task.state,
'details': task.info.get('details', 'error'),
}
return jsonify(response)
@app.route('/transaction', methods=['GET'])
def transaction():
if request.environ.get('HTTP_X_FORWARDED_FOR') is None:
ip = request.environ['REMOTE_ADDR']
else:
ip = request.environ['HTTP_X_FORWARDED_FOR']
if ip != '127.0.0.1':
return jsonify({'result': 'access forbidden (' + ip + ')'})
else:
print(request.args.get('amount'))
print(request.args.get('sender').lower())
g.cur.execute('UPDATE users SET balance=balance+%s WHERE id=%s;',
(request.args.get('amount'), request.args.get('sender').lower()))
return jsonify({'result': 'ok'})
@app.route('/', methods=['GET'])
def index():
if request.method == 'GET':
if current_user.is_authenticated:
return redirect(url_for('user', id=current_user.get_id()))
else:
return render_template('pages/auth/auth-parallax.html',
reg=conf.get('registration_status'))
@app.route('/user/<id>', strict_slashes=False)
@app.route('/user/<id>/<custom_from>')
@login_required
def user(id, custom_from=1):
custom_from = int(custom_from)
user = User().get(id.lower())
if user is None:
return render_template('pages/message.html',
message='User not found.',
anchor_text='Return Home',
anchor=url_for('index')), 404
else:
if custom_from < 1:
custom_from = 1
if custom_from * conf.get('row_per_page') > conf.get('max_result_window'):
custom_from = int(conf.get('max_result_window') / conf.get('row_per_page'))
nfts = es_nft.get_user_nfts(user=user.get_id(),
custom_size=conf.get('row_per_page'),
custom_from=custom_from - 1)
g.cur.execute('SELECT biography FROM users WHERE id=%s;',
(user.get_id(),))
biography = g.cur.fetchone()[0]
return render_template('pages/profile.html',
user=user,
nfts=nfts,
biography=biography,
trophies=trophy.get_trophies(user.get_id()),
custom_from=custom_from,
row_per_page=conf.get('row_per_page'),
max_result_window=conf.get('max_result_window'))
@app.route('/top-up', strict_slashes=False)
@login_required
def top_up():
return render_template('pages/top_up.html', receive_address=conf.get('receive_address'))
@app.route('/purchase', methods=['POST'])
@login_required
def purchase():
form = PurchaseForm()
if not form.validate_on_submit():
return render_template('pages/message.html',
message='Can\'t purchase.',
anchor_text='Return Home',
anchor=url_for('index'))
id = form.nft_id.data
g.cur.execute('SELECT price FROM purchase WHERE user_id=%s AND nft_id=%s;',
(current_user.get_id(), id))
old_price = g.cur.fetchone()
if old_price is not None:
return render_template('pages/message.html',
message='Already purchased at ' + str(old_price[0]) + ' LYX.',
anchor_text='View NFT',
anchor=url_for('nft', id=id))
else:
g.cur.execute('SELECT price, owner, lsp7 FROM nft WHERE id=%s;', (id,))
price_owner = g.cur.fetchone()
if price_owner is not None:
if price_owner[1] == current_user.get_id():
return render_template('pages/message.html',
message='You\'re the owner of this NFT. Something went wrong.',
anchor_text='View NFT',
anchor=url_for('nft', id=id))
elif price_owner[2] is None:
return render_template('pages/message.html',
message='NFT is still pending blockchain validation. Please try again in a few minutes.',
anchor_text='View NFT',
anchor=url_for('nft', id=id))
else:
g.cur.execute('SELECT balance FROM users WHERE id=%s;', (current_user.get_id(),))
balance = g.cur.fetchone()[0]
if balance >= price_owner[0]:
g.cur.execute('UPDATE users SET balance=balance-%s WHERE id=%s;',
(price_owner[0], current_user.get_id()))
g.cur.execute('INSERT INTO purchase (user_id, nft_id, price) VALUES (%s, %s, %s);',
(current_user.get_id(), id, price_owner[0]))
g.cur.execute('UPDATE users SET balance=balance+%s WHERE id=%s;',
(price_owner[0], price_owner[1]))
# LUKSO mint LSP-7 for user's UP
task = celery_purchase.apply_async((current_user.get_id(), price_owner[2]))
return render_template('pages/message.html',
message='Purchased for ' + str(format_balance(price_owner[0])) + ' LYX.',
anchor_text='View NFT',
anchor=url_for('nft', id=id))
else:
return render_template('pages/message.html',
message='You can\'t afford this NFT.',
anchor_text='View NFT',
anchor=url_for('nft', id=id))
else:
return render_template('pages/message.html',
message='NFT not found.',
anchor_text='Return Home',
anchor=url_for('index')), 404
@app.route('/nft/<id>')
@login_required
def nft(id):
upload_form = UploadForm()
feedback_from = FeedbackForm()
purchase_form = PurchaseForm(nft_id=id)
feedback_obj = Feedback()
preview = None
user_status = 'BUYER'
g.cur.execute(
'SELECT owner, price, name, status, preview_of, description, last_edit_time, lsp7 FROM nft WHERE id=%s;',
(id,))
the_nft = g.cur.fetchone()
if the_nft is None:
return render_template('pages/message.html',
message='NFT not found.',
anchor_text='Return Home',
anchor=url_for('index')), 404
else:
if the_nft[3] == 'PREVIEW':
g.cur.execute('SELECT name FROM nft WHERE id=%s;',
(the_nft[4],))
name = g.cur.fetchone()[0]
user_status = 'PURCHASER'
else:
feedback_obj.calculate_feedback(id)
name = the_nft[2]
preview = nft_util.get_preview_id(id)
if current_user.get_id() == the_nft[0]:
user_status = 'OWNER'
elif the_nft[3] != 'PREVIEW':
g.cur.execute(
'SELECT price, honesty_feedback, quality_feedback FROM purchase WHERE nft_id=%s AND user_id=%s;',
(id, current_user.get_id()))
price_feedback = g.cur.fetchone()
if price_feedback is not None:
user_status = 'PURCHASER'
feedback_from.honesty.data = nft_util.honesty_feedback_reversal(price_feedback[1])
feedback_from.quality.data = nft_util.quality_feedback_reversal(price_feedback[2])
g.cur.execute(
'SELECT file_nft.file_hash, file_nft.file_name, file.size, file.creation_time, file.path, file.type_general FROM file_nft, file WHERE file_nft.file_hash = file.hash AND file_nft.status=%s AND file_nft.nft_id=%s ORDER BY file.creation_time DESC;',
('ACTIVE', id))
files = g.cur.fetchall()
g.cur.execute('SELECT name '
'FROM universal_profiles WHERE id = %s;', (the_nft[0],))
up = g.cur.fetchone()
owner_name = up[0]
return render_template('pages/nft.html',
files=files,
id=id,
upload_form=upload_form,
feedback_from=feedback_from,
user_status=user_status,
nft_status=the_nft[3],
owner=the_nft[0],
owner_name=owner_name,
price=the_nft[1],
name=name,
preview_of=the_nft[4],
preview=preview,
feedback_obj=feedback_obj,
description=the_nft[5],
last_edit_time=the_nft[6],
lsp7=the_nft[7],
purchase_form=purchase_form)
@app.route('/file/<hash>')
@login_required
def file(hash):
g.cur.execute(
'SELECT nft_id FROM purchase WHERE user_id=%s AND nft_id IN (SELECT nft_id FROM file_nft WHERE file_hash=%s) UNION SELECT nft.id FROM nft, file_nft WHERE nft.id=file_nft.nft_id AND nft.status=%s and file_nft.file_hash=%s;',
(current_user.get_id(), hash, 'PREVIEW', hash))
nfts = g.cur.fetchone()
if nfts is not None:
g.cur.execute(
'SELECT file.path, file.size, file_nft.file_name FROM file, file_nft WHERE file.hash=file_nft.file_hash AND file.hash=%s;',
(hash,))
file = g.cur.fetchone()
if file is not None:
g.cur.execute(
'SELECT release_time from acquire WHERE user_id=%s AND file_hash=%s AND release_time > NOW();',
(current_user.get_id(), hash))
release_time = g.cur.fetchone()
if release_time is None:
g.cur.execute(
'INSERT INTO acquire (user_id, file_hash, release_time) VALUES (%s, %s, NOW() + INTERVAL %s HOUR);',
(current_user.get_id(), hash, str(acquire_period)))
else:
g.cur.execute(
'UPDATE acquire SET download_count=download_count+1 WHERE user_id=%s AND file_hash=%s AND release_time=%s;',
(current_user.get_id(), hash, release_time[0]))
return send_file(file[0], as_attachment=True, attachment_filename=file[2])
else:
return render_template('pages/message.html',
message='File not found. Something went wrong.',
anchor_text='Return Home',
anchor=url_for('index')), 404
else:
# NEXT VERSION: you can't afford the transportation fee to acquire this file
return render_template('pages/message.html',
message='First purchase the file\'s NFT',
anchor_text='Return Home',
anchor=url_for('index'))
@app.route('/get_login_message', methods=['POST'])
def get_login_message():
content = request.get_json(silent=True)
address = content.get('address')
return jsonify(lukso_utils.get_sign_message(address)) # read 'message' from json
@app.route('/check_login', methods=['POST'])
def check_login():
login_result = {
"is_successful": None,
"user_id": None,
}
content = request.get_json(silent=True)
signature = content.get('signature')
address = content.get('address').lower()
user = address
is_valid = lukso_utils.check_signature(signature, address)
if is_valid:
g.cur.execute('SELECT id FROM users WHERE id=%s',
(user,))
the_user = g.cur.fetchone()
if the_user is not None and the_user[0]:
login_user(User().get(user))
else:
if conf.get('registration_status') == 'OPEN' and security.can_create_user(user):
up_data = lukso_utils.get_user_profile(user)
# Save Universal Profile Data in DB
try:
profile_image = up_data['value']['LSP3Profile']['profileImage'][3]['url']
except:
profile_image = "ipfs://QmfFFpHVWBa16J7iJTv9DfCEPVPGKYUgkwNDbwEPxneBdQ"
truncated_user = user[0:5] + '...' + user[-5:]
g.cur.execute(
'INSERT INTO universal_profiles (id, name, description, profile_image_url ) '
'VALUES (%s, %s, %s, %s);',
(user, up_data['value']['LSP3Profile'].get("name", "") or truncated_user,
up_data['value']['LSP3Profile'].get("description", "") or None,
profile_image))
# Add User in DB
g.cur.execute('INSERT INTO users (id, balance, prestige) VALUES (%s, %s, %s);',
(user, 0, 3))
else:
login_result["is_successful"] = False
login_user(User().get(user))
g.cur.execute('UPDATE users SET last_login_time=current_login_time WHERE id=%s;', (user,))
g.cur.execute('UPDATE users SET current_login_time=NOW() WHERE id=%s;', (user,))
login_result["user_id"] = current_user.get_id()
login_result["is_successful"] = True
else:
login_result["user_id"] = None
login_result["is_successful"] = False
return 'failed to verify'
return jsonify(login_result)
@app.route('/new_nft', methods=['POST', 'GET'])
@login_required
def new_nft():
if current_user.get_prestige() < -3:
return render_template('pages/message.html',
message='You cannot create a new nft right now. It is a temporary restriction.',
anchor_text='Return Home',
anchor=url_for('index'))
form = NFTForm()
# form.status.data = 'ACTIVE'
if request.method == 'GET':
if current_user.get_prestige() < 2 and conf.get('captcha_status') == 'ENABLED':
puzzle = captcha.get_json()
form.puzzle.data = ImageCaptcha.get_puzzle_id(puzzle)
form.captcha.label = ImageCaptcha.get_question(puzzle)
form.captcha.choices = ImageCaptcha.get_options(puzzle)
return render_template('pages/new_nft.html',
form=form,
captcha=conf.get('captcha_status'))
elif request.method == 'POST':
if form.validate_on_submit():
if current_user.get_prestige() < 2:
answer_arr = []
if form.captcha.data is not None:
for answer in form.captcha.data:
answer_arr.append(answer)
if conf.get('captcha_status') == 'ENABLED':
is_correct_answer = captcha.check(form.puzzle.data, ','.join(answer_arr))
else:
is_correct_answer = True
else:
is_correct_answer = True
if is_correct_answer:
if security.nft_name_is_not_duplicate(current_user.get_id(), nft_util.strip_whitespace(form.name.data),
0):
msg_nft_limit = security.msg_nft_limit(current_user.get_id(), current_user.get_prestige())
if msg_nft_limit == 'allowed':
msg_price_rational = security.msg_price_rational(current_user.get_prestige(), form.price.data)
if msg_price_rational == 'rational':
g.cur.execute(
'INSERT INTO nft (owner, name, price, description) VALUES (%s, %s, %s, %s) RETURNING id;',
(current_user.get_id(),
nft_util.strip_whitespace(form.name.data),
form.price.data,
nft_util.strip_whitespace(form.description.data)))
new_id = g.cur.fetchone()
g.cur.execute('INSERT INTO purchase (user_id, nft_id, price) VALUES (%s, %s, %s);',
(current_user.get_id(), new_id[0], 0))
es_nft.index(body=NFTDoc(id=new_id[0],
owner=current_user.get_id(),
name=nft_util.strip_whitespace(form.name.data),
description=nft_util.strip_whitespace(form.description.data),
price=form.price.data,
status='ACTIVE',
creation_time=datetime.datetime.now(),
preview_of=None).get_nft(), id=new_id[0])
# LUKSO create new LSP-7 with user's UP as beneficiary and mint LSP-8
task = celery_new_nft.apply_async((
nft_util.strip_whitespace(form.name.data),
'luksense.store/nft/' + str(new_id[0]),
nft_util.strip_whitespace(form.description.data),
nft_util.strip_whitespace(form.name.data).upper(),
current_user.get_id()))
return redirect(url_for('nft', id=new_id[0]))
else:
return render_template('pages/message.html',
message=msg_price_rational,
anchor_text='New NFT',
anchor=url_for('new_nft'))
else:
return render_template('pages/message.html',
message=msg_nft_limit,
anchor_text='Return Home',
anchor=url_for('index'))
else:
return render_template('pages/message.html',
message='You already have an NFT with same name.',
anchor_text='New NFT',
anchor=url_for('new_nft'))
else:
return render_template('pages/message.html',
message='Incorrect Captcha answer.',
anchor_text='Try again',
anchor=url_for('new_nft'))
else:
if current_user.get_prestige() < 2 and conf.get('captcha_status') == 'ENABLED':
puzzle = captcha.get_json()
form.puzzle.data = ImageCaptcha.get_puzzle_id(puzzle)
form.captcha.label = ImageCaptcha.get_question(puzzle)
form.captcha.choices = ImageCaptcha.get_options(puzzle)
return render_template('pages/new_nft.html',
form=form,
captcha=conf.get('captcha_status'))
@app.route('/edit_nft/<id>', methods=['POST', 'GET'])
@login_required
def edit_nft(id):
form = EditNFTForm()
g.cur.execute('SELECT owner, name, price, description FROM nft WHERE id=%s AND status!=%s;',
(id, 'PREVIEW'))
nft = g.cur.fetchone()
if nft is None:
return render_template('pages/message.html',
message='NFT not found.',
anchor_text='Return Home',
anchor=url_for('index')), 404
else:
if nft[0] != current_user.get_id():
return render_template('pages/message.html',
message='Only the owner can edit this NFT.',
anchor_text='View NFT',
anchor=url_for('nft', id=id)), 403
if request.method == 'GET':
form.name.data = nft[1]
form.price.data = nft[2]
form.description.data = nft[3]
return render_template('pages/edit_nft.html',
form=form,
id=id)
elif request.method == 'POST':
if form.validate_on_submit():
new_name = nft_util.strip_whitespace(form.name.data)
new_price = form.price.data
new_description = nft_util.strip_whitespace(form.description.data)
if nft[1] == new_name and nft[2] == new_price and nft[3] == new_description:
return render_template('pages/message.html',
message='No change detected.',
anchor_text='View NFT',
anchor=url_for('nft', id=id))
if not security.nft_name_is_not_duplicate(current_user.get_id(), new_name, id):
return render_template('pages/message.html',
message='You already have an NFT with the same name.',
anchor_text='Edit NFT',
anchor=url_for('edit_nft', id=id))
msg_price_rational = security.msg_price_rational(current_user.get_prestige(), new_price)
if msg_price_rational != 'rational':
return render_template('pages/message.html',
message=msg_price_rational,
anchor_text='Edit NFT',
anchor=url_for('edit_nft', id=id))
msg_edit_limit = security.msg_edit_limit(id, current_user.get_prestige())
if msg_edit_limit != 'allowed':
return render_template('pages/message.html',
message=msg_edit_limit,
anchor_text='View NFT',
anchor=url_for('nft', id=id))
g.cur.execute('UPDATE nft SET name=%s, price=%s, description=%s, last_edit_time=NOW() WHERE id=%s;',
(new_name, new_price, new_description, id))
es_nft.update(body=NFTDoc(name=new_name, description=new_description,
price=new_price).edit_nft(), id=id)
g.cur.execute(
'INSERT INTO nft_edit_history (nft_id, reason, previous_name, previous_description, previous_price) VALUES (%s, %s, %s, %s, %s)',
(id, nft_util.strip_whitespace(form.reason.data), nft[1], nft[3], nft[2]))
return render_template('pages/message.html',
message='Changes successfully applied to the NFT.',
anchor_text='View NFT',
anchor=url_for('nft', id=id))
else:
return render_template('pages/edit_nft.html',
form=form,
id=id)
@app.route('/edit_history/<id>', methods=['GET'])
@login_required
def edit_history(id):
g.cur.execute(
'SELECT edit_time, reason, previous_name, previous_description, previous_price FROM nft_edit_history WHERE nft_id=%s ORDER BY edit_time DESC;',
(id,))
edits = g.cur.fetchall()
if len(edits) == 0:
return render_template('pages/message.html',
message='Edit history not available.',
anchor_text='Return Home',
anchor=url_for('index'))
g.cur.execute('SELECT id, name, description, price, creation_time FROM nft WHERE id=%s;',
(id,))
the_nft = g.cur.fetchone()
return render_template('pages/edit_history.html',
edits=edits,
nft=the_nft)
@app.route('/upload/<id>', methods=['POST'])
@login_required
def upload(id):
if current_user.get_prestige() < -3:
return render_template('pages/message.html',
message='You cannot upload a new file right now. It is a temporary restriction.',
anchor_text='Return Home',
anchor=url_for('index'))
form = UploadForm()
error_string = ''
if form.validate_on_submit():
file_list = request.files.getlist(form.files.name)
upload_dir = os.path.join(conf.get('storage_location'),
datetime.date.today().strftime('%y%m'),
datetime.date.today().strftime('%d'))
if not os.path.isdir(upload_dir):
os.makedirs(upload_dir)
new_file_counter = 0
if security.can_upload_file(current_user.get_id(), id, len(file_list)):
for each_file in file_list:
new_file_counter += nft_util.upload_and_process_form(file_data=each_file.stream.read(),
file_name=each_file.filename,
upload_dir=upload_dir,
nft_id=id,
es=es_nft)
if len(file_list) - new_file_counter == 0:
m = 'Upload was successful.'
else:
m = 'Upload was successful. Duplicates have been merged.'
return render_template('pages/message.html',
message=m,
anchor_text='View NFT',
anchor=url_for('nft', id=id))
else:
return render_template('pages/message.html',
message='Can\'t upload more than 666 files.',
anchor_text='View NFT',
anchor=url_for('nft', id=id))
else:
error_string += str(form.files.errors)
return render_template('pages/message.html',
message='Validation error: ' + error_string,
anchor_text='View NFT',
anchor=url_for('nft', id=id))
@app.route('/purchased', strict_slashes=False)
@app.route('/purchased/<custom_from>')
@login_required
def purchased(custom_from=1):
custom_from = int(custom_from)
if custom_from < 1:
custom_from = 1
if custom_from * conf.get('row_per_page') > conf.get('max_result_window'):
custom_from = int(conf.get('max_result_window') / conf.get('row_per_page'))
g.cur.execute(
'SELECT COUNT(nft.id) FROM nft, purchase WHERE owner!=%s AND purchase.user_id=%s AND nft.id = purchase.nft_id AND nft.status!=%s;',
(current_user.get_id(), current_user.get_id(), 'PREVIEW'))
total = g.cur.fetchone()
g.cur.execute(
'SELECT nft.id FROM nft, purchase WHERE owner!=%s AND purchase.user_id=%s AND nft.id = purchase.nft_id AND nft.status!=%s ORDER BY purchase.purchase_time DESC LIMIT %s OFFSET %s;',
(current_user.get_id(), current_user.get_id(), 'PREVIEW', conf.get('row_per_page'),
(custom_from - 1) * conf.get('row_per_page')))
ids = g.cur.fetchall()
ids = list(map(lambda n: n[0], ids))
nfts = es_nft.get_purchased_nfts(ids)
return render_template('pages/purchased.html',
user=user,
total=total[0],
nfts=nfts,
custom_from=custom_from,
row_per_page=conf.get('row_per_page'),
max_result_window=conf.get('max_result_window'))
@app.route('/nft_stats/<id>')
@login_required
def nft_stats(id):
g.cur.execute('SELECT COUNT(*) FROM purchase WHERE nft_id=%s;', (id,))
purchase_count = g.cur.fetchone()
if purchase_count[0] > 0:
g.cur.execute('SELECT name, owner, status FROM nft WHERE id=%s;', (id,))
name_owner_status = g.cur.fetchone()
if name_owner_status[2] == 'PREVIEW':
return render_template('pages/message.html',
message='Statistics page is not available for preview nfts.',
anchor_text='View Preview',
anchor=url_for('nft', id=id)), 403
g.cur.execute(
'SELECT purchase.user_id, purchase.price, purchase.purchase_time FROM nft, purchase WHERE purchase.nft_id=%s AND purchase.user_id!=nft.owner AND nft.id=purchase.nft_id ORDER BY purchase.purchase_time DESC LIMIT 69;',
(id,))
users = g.cur.fetchall()
g.cur.execute(
'SELECT SUM(price), AVG(price), MIN(price), MAX(price) FROM purchase WHERE nft_id=%s AND user_id!=%s;',
(id, name_owner_status[1]))
price_table = g.cur.fetchone()
return render_template('pages/nft_stats.html',
id=id,
users=users,
price_table=price_table,
purchase_count=purchase_count[0] - 1,
name=name_owner_status[0],
owner=name_owner_status[1])
else:
return render_template('pages/message.html',
message='NFT not found.',
anchor_text='Return Home',
anchor=url_for('index')), 404
@app.route('/enable_preview/<id>')
@login_required
def enable_preview(id):
g.cur.execute('SELECT name, owner FROM nft WHERE id=%s AND status!=%s;', (id, 'PREVIEW'))
name_owner = g.cur.fetchone()
if name_owner is not None:
if name_owner[1] == current_user.get_id():
g.cur.execute('SELECT id FROM nft WHERE preview_of=%s;', (id,))
preview_id = g.cur.fetchone()
if preview_id is None:
g.cur.execute('INSERT INTO nft (owner, status, preview_of) VALUES (%s, %s, %s) RETURNING id;',
(name_owner[1], 'PREVIEW', id))
new_preview_id = g.cur.fetchone()
return render_template('pages/message.html',
message='Preview enabled for ' + name_owner[0] + '.',
anchor_text='View Preview',
anchor=url_for('nft', id=new_preview_id[0]))
else:
return render_template('pages/message.html',
message='This nft already has a preview.',
anchor_text='View Preview',
anchor=url_for('nft', id=preview_id[0]))
else:
return render_template('pages/message.html',
message='Only the owner can enable preview.',
anchor_text='View NFT',
anchor=url_for('nft', id=id)), 401
else:
return render_template('pages/message.html',
message='NFT not found.',
anchor_text='Return Home',
anchor=url_for('index')), 404
@app.route('/edit_biography', methods=['POST', 'GET'])
@login_required
def edit_biography():
form = EditBiographyForm()
g.cur.execute('SELECT biography FROM users WHERE id=%s;',
(current_user.get_id(),))
former_biography = g.cur.fetchone()[0]
if request.method == 'GET':
form.biography.data = former_biography
return render_template('pages/edit_biography.html',
form=form)
elif request.method == 'POST':
if form.validate_on_submit():
new_biography = nft_util.strip_whitespace(form.biography.data)
if new_biography != former_biography:
g.cur.execute('UPDATE users SET biography=%s WHERE id=%s;',
(new_biography, current_user.get_id()))
return render_template('pages/message.html',
message='Biography changed successfully.',
anchor_text='Return Home',
anchor=url_for('index'))
else:
return render_template('pages/message.html',
message='No change detected.',
anchor_text='Return Home',
anchor=url_for('index'))
else:
return render_template('pages/edit_biography.html',
form=form)
@app.route('/rate_nft/<id>', methods=['POST'])
@login_required
def rate_nft(id):
form = FeedbackForm()
if form.validate_on_submit():
g.cur.execute('SELECT name, owner FROM nft WHERE id=%s AND status!=%s;', (id, 'PREVIEW'))
name_owner = g.cur.fetchone()
if name_owner is not None:
if name_owner[1] == current_user.get_id():
return render_template('pages/message.html',
message='You can\'t rate your own NFTs.',
anchor_text='View NFT',
anchor=url_for('nft', id=id))
else:
g.cur.execute('SELECT honesty_feedback, quality_feedback FROM purchase WHERE nft_id=%s AND user_id=%s;',
(id, current_user.get_id()))
feedback = g.cur.fetchone()
if feedback is not None:
if security.can_rate_nft(current_user.get_id()):
g.cur.execute(
'UPDATE purchase SET honesty_feedback=%s, quality_feedback=%s WHERE nft_id=%s AND user_id=%s;',
(nft_util.honesty_feedback_check(form.honesty.data),
nft_util.quality_feedback_check(form.quality.data),
id, current_user.get_id()))
fb = Feedback()
fb.calculate_feedback(id)
nft_doc = NFTDoc(id=id)
nft_doc.set_feedback(fb)
es_nft.update(body=nft_doc.rate_nft(), id=id)
return render_template('pages/message.html',
message='Rating submitted successfully.',
anchor_text='View NFT',
anchor=url_for('nft', id=id))
else:
return render_template('pages/message.html',
message='Sorry, your prestige must be greater than zero to be able to rate NFTs.',
anchor_text='View NFT',
anchor=url_for('nft', id=id))
else:
return render_template('pages/message.html',
message='You should purchase the NFT first!',
anchor_text='View NFT',
anchor=url_for('nft', id=id))
else:
return render_template('pages/message.html',
message='NFT not found.',
anchor_text='Return Home',
anchor=url_for('index')), 404
else:
return render_template('pages/message.html',
message='There is a problem with your inputs.',
anchor_text='View NFT',
anchor=url_for('nft', id=id)), 403
@app.route('/add_to_preview', methods=['POST', 'GET'])
def add_to_preview():
is_bad_request = False
if request.method == 'GET':
file_id = request.args.get('file')
nft_id = request.args.get('nft')
file_name = None
if len(file_id) != 69:
is_bad_request = True
try:
nft_id = int(nft_id)
except:
is_bad_request = True
try:
g.cur.execute('SELECT file_name FROM file_nft WHERE file_hash=%s AND nft_id=%s;',
(file_id, nft_id))
file_name = g.cur.fetchone()[0]
except:
is_bad_request = True
if is_bad_request:
return render_template('pages/message.html',
message='There is a problem with your inputs.',
anchor_text='Return Home',
anchor=url_for('index')), 403
msg = 'You\'re about to add "' + str(file_name) + '" from nft #' + str(nft_id) + \
' to its preview. Once it\'s added, you can\'t remove it. Are you sure?'
form = AddToPreviewForm()
form.nft_id.data = nft_id
form.file_id.data = file_id
return render_template('pages/add_to_preview.html',
message=msg,
form=form,
nft_id=nft_id)
elif request.method == 'POST':
form = AddToPreviewForm()
nft_id = form.nft_id.data
file_id = form.file_id.data
file_name = None
#
if len(file_id) != 69:
is_bad_request = True
try:
nft_id = int(nft_id)
except:
is_bad_request = True
g.cur.execute('SELECT owner FROM nft WHERE id=%s;', (nft_id,))
owner = g.cur.fetchone()
if owner is None or len(owner) < 1 or owner[0] != current_user.get_id():
is_bad_request = True
try:
g.cur.execute('SELECT file_name FROM file_nft WHERE file_hash=%s AND nft_id=%s;',
(file_id, nft_id))
file_name = g.cur.fetchone()[0]
except:
is_bad_request = True
if nft_util.is_preview(nft_id):
is_bad_request = True
if is_bad_request:
return render_template('pages/message.html',
message='There is a problem with your inputs.',
anchor_text='Return Home',
anchor=url_for('index')), 403
#
preview_id = nft_util.get_preview_id(nft_id)
if preview_id is None:
return render_template('pages/message.html',
message='Please enable preview first.',
anchor_text='View NFT',
anchor=url_for('nft', id=nft_id))
if security.can_upload_file(current_user.get_id(), preview_id, 1):
g.cur.execute('SELECT nft_id, file_hash FROM file_nft WHERE nft_id=%s AND file_hash=%s;',
(preview_id, file_id))
duplicate_file = g.cur.fetchone()
if duplicate_file is None:
g.cur.execute('INSERT INTO file_nft (nft_id, file_hash, file_name) VALUES (%s, %s, %s);',
(preview_id, file_id, file_name))
nft_doc = NFTDoc(id=nft_id)
nft_doc.set_preview_files(nft_util.get_files(preview_id))
es_nft.update(body=nft_doc.upload_preview_files(), id=nft_id)
return render_template('pages/message.html',
message='Your file has been successfully added to preview.',
anchor_text='View Preview',
anchor=url_for('nft', id=preview_id))
else:
return render_template('pages/message.html',
message='This file is already in the preview.',
anchor_text='View NFT',
anchor=url_for('nft', id=nft_id))
else:
return render_template('pages/message.html',
message='Can\'t upload more than 666 files.',
anchor_text='View NFT',
anchor=url_for('nft', id=nft_id))
@app.route('/announcement/<id>', methods=['GET'])
@login_required
def announcement(id):
g.cur.execute('SELECT id, title, creation_time, html FROM announcement WHERE id=%s;', (id,))
tmp = g.cur.fetchone()
the_announcement = Announcement(tmp[0], tmp[1], tmp[2], tmp[3])
g.cur.execute('UPDATE users SET last_login_time=NOW() WHERE id=%s;', (current_user.get_id(),))
return render_template('pages/announcement.html',
id=id,
announcement=the_announcement)
@app.route('/announcements', methods=['GET'])
@login_required
def announcements():
g.cur.execute('SELECT id, title, creation_time FROM announcement ORDER BY creation_time DESC LIMIT 13;')
tmp_list = g.cur.fetchall()
the_announcements = []
for tmp in tmp_list:
the_announcements.append(Announcement(tmp[0], tmp[1], tmp[2]))
g.cur.execute('UPDATE users SET last_login_time=NOW() WHERE id=%s;', (current_user.get_id(),))
return render_template('pages/announcements.html',
announcements=the_announcements)
@app.route('/help', methods=['GET'])
@login_required
def help():
help_dic = {}