-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataengine.py
More file actions
997 lines (860 loc) · 34.3 KB
/
dataengine.py
File metadata and controls
997 lines (860 loc) · 34.3 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
"""
LineCMS - Content Management System (Product & Blogging) for Rapid website development
Website: www.linecms.com
Author: S. Jangra & Mark A.R. Pequeras
"""
import sqlite3
import datetime
import time
import urllib
import re
import random
import json
import os
import random
import settings
from flask import g
from ast import literal_eval as lite
UPLOAD_FOLDER_PRODUCTS = 'static/dashboard/uploads/products'
UPLOAD_FOLDER_BLOG = 'static/dashboard/uploads/blog'
class SandEngine:
"""Group of functions for database actions
"""
connection = sqlite3.connect(settings.dbase_path, check_same_thread=False)
def install_cred(self,uname: str,pwd: str) -> None:
"""Install login credential
Args:
uname (str): username
pwd (str): password (raw)
"""
params = "INSERT INTO users (username,passw) VALUES (?,?)"
vals = (uname,pwd)
c = self.connection.cursor()
c.execute(params, vals)
self.connection.commit()
self.log(f"Welcome to LineCMS, Credentials Installed")
def themeset(self, theme: str) -> None:
"""Sets selected theme
Args:
theme (str): theme name
"""
try:
c = self.connection.cursor()
q = f"UPDATE control SET theme = '{theme}'"
c.execute(q)
self.connection.commit()
self.log(f"Theme changed to {theme}")
except Exception as err:
self.log(f"Theme assignment error: {err}")
def themeget(self) -> tuple:
"""Current selected theme
Returns:
tuple: assigned theme
"""
c = self.connection.cursor()
q = "SELECT theme FROM control"
c.execute(q)
return c.fetchone()
def orderfulfill(self,data: dict) -> bool:
"""Mark order as Fulfilled
fulfilled 1 = Manual (No Email)
fulfilled 2 = Auto (Sends email)
Returns:
bool: if success
"""
try:
c = self.connection.cursor()
if "manual" in data:
q = """UPDATE productorders SET tracking="{t}", fulfilled="2", additional="{a}" where ordernumber='{o}';""".format(t=data['tracking'],o=str(data['ordernumber']),a=str(data['additional']))
else:
q = """UPDATE productorders SET tracking="{t}", fulfilled="1", additional="{a}" where ordernumber='{o}';""".format(t=data['tracking'],o=str(data['ordernumber']),a=str(data['additional']))
c.execute(q)
self.connection.commit()
return True
except Exception as err:
self.log(f"Order fulfilment error: {err}")
return False
def orderhistory_get(self,onum: str) -> tuple:
"""loads order history
Args:
onum (str): order number
Returns:
tuple: order history
"""
c = self.connection.cursor()
q = f"SELECT history FROM productorders WHERE ordernumber='{onum}';"
c.execute(q)
return c.fetchone()
def orderhistory_add(self,data: dict) -> bool:
"""Attach history to order
Args:
data (dict): contains history information
Returns:
bool: if success
"""
c = self.connection.cursor()
q = """UPDATE productorders SET history="{o}" where ordernumber="{orn}";""".format(o=str(data['obj']),orn=str(data['ordernumber']))
c.execute(q)
self.connection.commit()
return True
def url_gen(self, content: str) -> str:
"""_summary_
Args:
content (str): url to modify
Returns:
str: modified url str
"""
remove_sym = re.sub(r'[^\w]', ' ', content)
v = urllib.parse.quote_plus(str(random.randint(10, 50))+remove_sym)
n = str(v).replace("+", "-")
g.new_blog_url = n
return n
def productorders_get(self,q=False,total=False) -> tuple:
"""_summary_
Args:
q (bool, optional): query. Defaults to False.
total (bool, optional): Length. Defaults to False.
Returns:
tuple: order
"""
c = self.connection.cursor()
if total:
c.execute("SELECT Count(*) FROM productorders")
return c.fetchone()
fetch = c.execute(q)
return fetch.fetchall()
def get_products_cat_lists(self) -> list:
"""Builds category list
Returns:
list: generated (seperated categories)
"""
c = self.connection.cursor()
m = c.execute("SELECT category FROM products")
mf = m.fetchall()
cats = {}
for cat in mf:
if not cat[0]: # empty
pass
else:
for itm in cat[0].split(","):
if itm in cats:
cats[itm] = cats[itm] + 1
else:
cats[itm] = 1
return cats
def productorders_single_get(self,ids: str,loadfulfill=False) -> tuple:
"""Load product Order
Args:
ids (str): load using order ID
loadfulfill (bool, optional): load using order number. Defaults to False.
Returns:
tuple: order
"""
c = self.connection.cursor()
q = f"SELECT * FROM productorders WHERE id = {ids}"
if loadfulfill:
q = f"SELECT * FROM productorders WHERE ordernumber = '{loadfulfill}'"
c.execute(q)
return c.fetchone()
def productorders_set(self,order: dict) -> str:
"""Set new order
Args:
order (dict): data generated by stripe
Returns:
str: Id and Timestamp
"""
try:
c = self.connection.cursor()
params = "INSERT INTO productorders (fulfilled,customer_name,customer_email,amount_total,created,payment_status,customer_country,customer_postal,currency,items,session_id,metadata,address,phone,shipping_cost) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
vals = ("0",order['customer_name'],order['customer_email'],order['amount_total'],order['created'],order['payment_status'],order['customer_country'],order['customer_postal'],order['currency'],order['items'],order['session_id'],str(order['metadata']),order['address'],order['phone'],order['shipping_cost'])
c.execute(params, vals)
curr_id = c.lastrowid
secq = "UPDATE productorders SET ordernumber='{o}' where id='{c}';".format(o=f"{curr_id}{order['created']}",c=curr_id)
c.execute(secq)
self.connection.commit()
return f"{curr_id}{order['created']}"
except Exception as err:
self.log(f"Adding new order error: {err}")
return False
def productsettings_smtp(self,data: dict) -> bool:
"""Update SMTP Settings
Args:
data (dict): contains dict of data (smtp credentials)
Returns:
bool: if no error
"""
try:
c = self.connection.cursor()
q = f"""UPDATE productsetting SET smtp="{str(data)}";"""
c.execute(q)
self.connection.commit()
return True
except Exception as err:
self.log(f"SMTP assignment error: {err}")
return False
def productstock_deduct(self,ids: str,isvariant=False) -> None:
"""Deduct Stock on success payment
2 types of stock, Main product and Variant
Args:
ids (str): product_id
isvariant (bool): to deduct on variant (dict) in db
Returns:
None: Not needed
"""
c = self.connection.cursor()
if isvariant:
pull = c.execute(f"SELECT variant_details FROM products WHERE product_id='{ids}';")
dict_cast = None
try:
dict_cast = lite(pull.fetchone()[0])
except:
pass
if dict_cast:
try:
d = dict_cast[isvariant+"-ivar"]['instock']
dict_cast[isvariant+"-ivar"]['instock'] = int(d) - 1 # will update soon with dynamic from quantity
c.execute(f"""UPDATE products SET variant_details = "{str(dict_cast)}" WHERE product_id='{ids}';""")
self.connection.commit()
except Exception as e:
pass
else:
pull = c.execute(f"SELECT stock FROM products WHERE product_id='{ids}';")
pull_ = pull.fetchone()
if pull_:
if int(pull_[0]) > 1:
count_ = int(pull_[0]) - 1 # will update soon with dynamic from quantity
c.execute(f"UPDATE products SET stock = {count_} WHERE product_id = '{ids}';")
self.connection.commit()
else:
pass
def productsettings_temp(self,data: dict) -> bool:
"""Update template (Jinja style)
Args:
data (dict): Type of template with Html/Jinja syntax
Returns:
bool: if no error
"""
try:
_p = {"f":data['templates']['fulfilled'],"p":data['templates']['placed'],"a":data['templates']['abandoned']}
c = self.connection.cursor()
q = f"""UPDATE productsetting SET templates="{str(data['status'])}", template_fulfilled="{str(_p['f'])}", template_placed="{str(_p['p'])}", template_abandoned="{str(_p['a'])}";"""
c.execute(q)
self.connection.commit()
return True
except Exception as e:
print(e)
return False
def productsettings_ship(self,data: dict) -> bool:
"""Shipping rates
Args:
data (dict): shipping data
Returns:
bool: if no error
"""
try:
c = self.connection.cursor()
q = f"""UPDATE productsetting SET shipping_enable='{data['status']}', shipping_rates='{str(data['shipping'])}', shipping_countries="{data['countries']}";"""
c.execute(q)
self.connection.commit()
return True
except Exception as err:
self.log(f"Shipping options error: {err}")
return False
def productsettings_str(self,data: dict) -> bool:
"""Save Shop data (API information)
Args:
data (dict): shop information (Stripe Api, etc..)
Returns:
bool: if no error
"""
try:
c = self.connection.cursor()
q = f"""UPDATE productsetting SET secretkey='{data['sk']}', publishablekey='{data['pk']}', currency='{data['ck']}', webhookkey='{data['wsk']}', signkey='{data['wsk']}';"""
c.execute(q)
self.connection.commit()
return True
except Exception as err:
self.log(f"Product settings (STR) assignment error: {err}")
return False
def productsettings_set(self, sk: str, pk: str, ck: str, wk: str, wsk: str,s_enable: str,s_rates: str,s_countries: list) -> tuple:
"Unused func, will delete soon"
try:
c = self.connection.cursor()
q = """UPDATE productsetting SET secretkey = "{sk}", publishablekey = "{pk}", currency = "{ck}", webhookkey = "{wk}",signkey = "{wsk}", shipping_enable = "{se}", shipping_rates = '{sr}', shipping_countries = "{sc}" WHERE id = 1; """.format(
sk=sk, pk=pk, ck=ck,wk=wk,wsk=wsk,se=s_enable,sr=s_rates,sc=s_countries)
c.execute(q)
self.connection.commit()
return True
except Exception as err:
self.log(f"Product settings assignment: {err}")
return False
def productsettings_get(self) -> tuple:
"""Product settings (Mostly Stripe data)
Returns:
tuple: shop settings
"""
c = self.connection.cursor()
q = "SELECT * FROM productsetting where id=1"
c.execute(q)
_c = c.fetchone()
if _c:
return _c
else:
return [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] # Empty
def productimagesupdater(self, imgcat: str, data: dict, newimage: str, filename: str) -> tuple:
c = self.connection.cursor()
m_fetch = c.execute(
"SELECT {sel} FROM products WHERE product_id='{m}'".format(
sel=imgcat, m=data['p_id'])
)
try:
_old = m_fetch.fetchone()
_dblist_ = eval(_old[0])
if filename not in _dblist_: # new file add to db
_dblist_.append(filename)
_inserts = """UPDATE products SET images = "{i}" WHERE product_id = '{product_id}'""".format(
i=_dblist_, product_id=data['p_id'])
c.execute(_inserts)
self.connection.commit()
return True
except Exception as e:
print(e)
return False
def productvariantsupdater(self, imgcat: str, data: dict, newimage: str) -> bool:
"""Variant image update
Args:
imgcat (str): image cat
data (dict): product info
newimage (str): file
Returns:
bool: if no error
"""
variant_name = data['p_variant']+"-ivar"
_c = self.connection.cursor()
m_fetch = _c.execute(
"SELECT {sel} FROM products WHERE product_id='{m}'".format(
sel=imgcat, m=data['p_id'])
)
try:
_old = m_fetch.fetchone()
_new_ = eval(_old[0])
_new_[variant_name] = newimage
variants = json.dumps(_new_)
_inserts = """UPDATE products SET variants = '{variants}' WHERE product_id = '{product_id}'""".format(
variants=variants, product_id=data['p_id'])
_c.execute(_inserts)
self.connection.commit()
return True
except Exception as e:
return False
def productsimilar(self, count=5, category=0) -> list:
"""Groups similar categories (pull random if not enough)
Args:
count (int, optional): result. Defaults to 5.
category (int, optional): target category. Defaults to 0.
Returns:
list: sorted products
"""
categ_split = category.split(",")
category = random.choice(categ_split)
count = count
pr = []
_c = self.connection.cursor()
self.m_fetch = _c.execute(
"SELECT * FROM products WHERE category='{m}' LIMIT {l};".format(m=category, l=count))
self.m_data = self.m_fetch.fetchall()
for d in self.m_data:
pr.append(d)
if len(pr) < count:
self.mr_fetch = _c.execute(
"SELECT * FROM products ORDER BY random() LIMIT {l};".format(l=count-len(pr)))
self.mr_data = self.mr_fetch.fetchall()
for i in self.mr_data:
pr.append(i)
return pr
def productimagesmod(self, variants: dict, product_id: str) -> bool:
"""
Update file-not-found as empty and updates db
trigger: product.variantimagemodifier
"""
try:
variants = json.dumps(variants)
c = self.connection.cursor()
inserts = """UPDATE products SET variants = '{variants}' WHERE product_id = '{product_id}'""".format(
variants=variants, product_id=product_id)
c.execute(inserts)
self.connection.commit()
return True
except Exception as err:
self.log(f"Product images modifier error: {err}")
return False
def delete_pr(self, value: str) -> bool:
"""deletes product
Args:
value (str): product id
Returns:
bool: true if success
"""
q = """DELETE FROM products WHERE product_id = '{value}';""".format(
value=value)
c = self.connection.cursor()
try:
c.execute(q)
self.connection.commit()
return True
except Exception as err:
self.log(f"Product deletion error: {err}")
return False
def delete_apip(self, table, column, value) -> bool:
"@mark to be deleted"
q = """DELETE FROM {tb} WHERE product_urlsystem = '{value}';""".format(
tb=table, column=column, value=value)
c = self.connection.cursor()
try:
c.execute(q)
self.connection.commit()
return True
except Exception as e:
return False
def delete_api(self, table, column, value) -> bool:
"@mark to be deleted"
q = """DELETE FROM {tb} WHERE route = '{value}';""".format(
tb=table, column=column, value=value)
c = self.connection.cursor()
try:
c.execute(q)
self.connection.commit()
self.log("Blog post deleted")
return True
except Exception as e:
print(e)
return False
def product_update(self, d: dict) -> bool:
"""updates product data
Args:
d (dict): product data
Returns:
bool: true if no error
"""
c = self.connection.cursor()
q = f"""UPDATE products SET body = "{d['body']}",category = "{d['category']}",images = "{d['images']}", mainimage = "{d['mainimage']}", price = "{d['price']}", product_url = "{d['product_url']}", product_urlsystem = "{d['product_urlsystem']}", seo_description = "{d['seo_description']}", seo_keywords = "{d['seo_keywords']}", title = "{d['title']}", variant_details = "{d['variant_details']}", variants = "{d['variants']}", hidden = "{d['hidden']}" WHERE product_id = "{d['id']}";"""
try:
c.execute(q)
self.connection.commit()
return d['product_urlsystem']
except Exception as err:
self.log(f"Product update error: {err}")
return False
def product_publish(self, d: dict) -> bool or str:
"""new product insert
Args:
d (dict): new product data
Returns:
str: if success (URL)
bool: if not
"""
c = self.connection.cursor()
try:
ts = self.timestamp(routeStyle=1)
ugen = self.url_gen(ts+" "+d['title'])
params = "INSERT INTO products (title,category,variants,product_url,product_urlsystem,seo_description,seo_keywords,images,mainimage,variant_details,timestamp,hidden,product_id,body,price,stock) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
vals = (d['title'], d['category'], str(d['variants']), d['product_url'], ugen,
d['seo_description'], d['seo_keywords'], str(d['images']), d['mainimage'], str(d['variant_details']), ts, "0", d['id'], d['body'], d['price'],d['stock'])
c.execute(params, vals)
self.connection.commit()
return ugen
except Exception as err:
self.log(f"Product publish error: {err}")
def delete_image_partial(self, data: dict) -> None:
"""deletes uploaded yet changed image file (unused) (Blog publish)
Args:
data (dict): image data
"""
c = self.connection.cursor()
try:
os.remove(os.path.join(UPLOAD_FOLDER_BLOG, data['image']))
except:
pass
q = "UPDATE blog SET image = '' WHERE route = '{r}';".format(
r=data['route'])
c.execute(q)
self.connection.commit()
def get_product_single(self, route: str, checkout=False) -> tuple:
"""Load Product data (Single)
Args:
route (_type_): product route
checkout (bool, optional): uses product_urlsystem instead of product_id. Defaults to False.
Returns:
tuple: product data
"""
c = self.connection.cursor()
if checkout:
c.execute(
"SELECT * FROM products WHERE product_id='{m}'".format(m=checkout))
else:
c.execute(
"SELECT * FROM products WHERE product_urlsystem='{m}'".format(m=route))
return c.fetchone()
def get_product_listings(self, getcount=False,getcats=False,quer=[],custom={}) -> tuple:
"""Custom load products from db (product listings)
Args:
getcount (bool, optional): length. Defaults to False.
getcats (bool, optional): categories. Defaults to False.
quer (list, optional): custom db query. Defaults to [].
custom (dict, optional): custom filter. Defaults to {}.
Returns:
tuple: _description_
"""
c = self.connection.cursor()
if custom:
if custom['s']:
if custom['c']:
c.execute("select * from products where hidden=0 AND category='{}' AND (title like '%{}%' OR body like '%{}%') order by id desc limit {},{}".format(custom['c'],custom['s'],custom['s'],custom['off'],custom['perp']))
return c.fetchall()
c.execute("select * from products where hidden=0 AND (title like '%{}%' OR body like '%{}%') order by id desc limit {},{}".format(custom['s'],custom['s'],custom['off'],custom['perp']))
return c.fetchall()
if custom['c']:
if custom['s']:
c.execute("select * from products where hidden=0 AND category='{}' AND (title like '%{}%' OR body like '%{}%') order by id desc limit {},{}".format(custom['c'],custom['s'],custom['s'],custom['off'],custom['perp']))
c.execute("select * from products where hidden=0 AND category='{}' order by id desc limit {},{}".format(custom['c'],custom['off'],custom['perp']))
return c.fetchall()
if getcats:
c.execute("select category from products order by id desc")
return c.fetchall()
if getcount:
c.execute("SELECT Count(*) FROM products")
return c.fetchone()
if quer:
c.execute("select * from products order by id desc limit {},{}".format(quer[0],quer[1]))
return c.fetchall()
c.execute("SELECT * FROM products ORDER BY id DESC")
return c.fetchall()
def blog_publish(self, dicts: dict) -> bool:
"""insert new blog data
Args:
dicts (dict): new blog data
Returns:
bool: true if no error
"""
c = self.connection.cursor()
try:
ts = self.timestamp(routeStyle=1)
tso = self.timestamp(routeStyle=0)
params = "INSERT INTO blog (title,message,image,timestamp,hidden,route,category) VALUES (?,?,?,?,?,?,?)"
vals = (dicts['title'], dicts['body'], dicts['image'],
tso, "0", self.url_gen(ts+" "+dicts['title']), dicts['category'])
c.execute(params, vals)
self.connection.commit()
return True
except Exception as err:
self.log(f"Blog publish error: {err}")
return False
def blog_update(self, dicts: dict) -> bool:
"""update existing blog data
Args:
dicts (dict): new blog data
Returns:
bool: true if no error
"""
c = self.connection.cursor()
try:
q = "UPDATE blog SET title = '{title}',message = '{message}', image = '{image}',hidden = '{hidden}', category = '{category}' WHERE route = '{route}';".format(
title=dicts['title'], message=dicts['body'], image=dicts['image'], hidden=dicts['hidden'], category=dicts['category'], route=dicts['route'])
c.execute(q)
self.connection.commit()
print("Blog updated")
return True
except Exception as err:
self.log(f"Blog update error: {err}")
return False
def get_blog_single(self, route: str) -> tuple:
"""load blog post
Args:
route (str): blog route
Returns:
tuple: blog data
"""
c = self.connection.cursor()
m = c.execute(
"SELECT * FROM blog WHERE route='{m}'".format(m=route))
return m.fetchone()
def get_blog_listings(self,getcount=False,quer=[]) -> list:
"""blog listings
Args:
page (int, optional): unused. Defaults to 0.
result (int, optional): unused. Defaults to 10.
getcount (bool, optional): length. Defaults to False.
quer (list, optional): custom query. Defaults to [].
Returns:
list: tupled blog posts
"""
c = self.connection.cursor()
if getcount:
fetch = c.execute("SELECT Count(*) FROM blog")
return fetch.fetchone()
if quer:
c.execute("select * from blog order by id desc limit {},{}".format(quer[0],quer[1]))
return c.fetchall()
m = c.execute("SELECT * FROM blog ORDER BY id DESC")
return m.fetchall()
def get_blog_cat_lists(self) -> list:
"""creates a list of categories
Returns:
list: generated category list from blog
"""
c = self.connection.cursor()
m = c.execute("SELECT category FROM blog")
mf = m.fetchall()
cats = {}
for cat in mf:
if not cat[0]: # empty
pass
else:
for itm in cat[0].split(","):
if itm in cats:
cats[itm] = cats[itm] + 1
else:
cats[itm] = 1
return cats
def knightclientapi(self, action) -> bool:
"needs a recode"
try:
c = self.connection.cursor()
a = {
"dlogs": """DELETE FROM logging""",
}
c.execute(a[action])
return True
except:
return False
def knightclientapiv2(self, action) -> bool:
"needs a recode"
c = self.connection.cursor()
where = action['where']
action = action['action']
try:
a = {
"blog_0": """UPDATE blog SET hidden = '0' WHERE route = '{r}';""".format(r=where),
"blog_1": """UPDATE blog SET hidden = '1' WHERE route = '{r}';""".format(r=where),
}
c.execute(a[str(action)])
self.connection.commit()
return True
except Exception as e:
return False
def timestamp(self, routeStyle=False) -> str:
"""generates timestamp (returns only date if routeStyle)
Args:
routeStyle (bool, optional): only date if true. Defaults to False.
Returns:
str: _description_
"""
_n = datetime.datetime.now()
d_ = str(_n).split()
dt = d_[0]
t = time.strftime("%I:%M %p")
ts = dt + " " + t
if routeStyle:
return dt
return ts
def message(self, dicts: dict) -> bool:
"""adds new visitor message
Args:
dicts (dict): message dict
Returns:
bool: true if no error
"""
try:
c = self.connection.cursor()
params = "INSERT INTO messages (name,email,message,phone,timestamp) VALUES (?,?,?,?,?)"
vals = (dicts['name'], dicts['email'],
dicts['message'], dicts['phone'], self.timestamp())
c.execute(params, vals)
self.connection.commit()
return True
except Exception as err:
self.log(f"Visitor message error: {err}")
return False
def delete_blog(self, route: str) -> bool:
"""deletes blog post
Args:
route (str): blog route
Returns:
bool: true if no error
"""
c = self.connection.cursor()
try:
q = "DELETE FROM blog WHERE route = {ids};".format(ids=route)
c.execute(q)
self.connection.commit()
return True
except Exception as e:
self.log("Unable to delete blog post, "+str(e))
return False
def mvdelete(self, ids: str) -> bool:
"""delete visitor message
Args:
ids (str): message id
Returns:
bool: true if no error
"""
c = self.connection.cursor()
try:
q = "DELETE FROM messages WHERE id = {ids};".format(ids=ids)
c.execute(q)
self.connection.commit()
return True
except Exception as e:
self.log("Unable to delete message, "+str(e))
return False
def log(self, message: dict) -> bool:
"""logger
Args:
message (dict): message
Returns:
true: true if no error
"""
if not settings.logging_enabled:
return False
c = self.connection.cursor()
try:
params = "INSERT INTO logging (log,timestamp) VALUES (?,?)"
vals = (message, self.timestamp())
c.execute(params, vals)
self.connection.commit()
return True
except Exception as e:
return False
def update_data_uploads(self, table, column, newvalue, where, whereequal) -> dict:
"""
@mark to be deleted
"""
c = self.connection.cursor()
q = "UPDATE {table} SET {column} = '{value}' WHERE {where} = '{whereequal}';".format(
table=table, column=column, value=newvalue, where=where, whereequal=whereequal
)
c.execute(q)
self.connection.commit()
return {"status": "success"}
def update_websitesettings(self, dicts: dict, owner: str) -> bool:
"""_summary_
Args:
dicts (dict): new website data
owner (str): needs to be removed (Useless arg)
Returns:
bool: true if no error
"""
c = self.connection.cursor()
try:
q = "UPDATE control SET sitename = '{sitename}',sitedescription = '{sitedescription}',footercopyright = '{footercopyright}',meta_description = '{meta_description}',meta_keywords = '{meta_keywords}', sitenumber = '{sitenumber}', siteemail='{siteemail}', siteaddress='{siteaddress}';".format(
sitename=dicts['sitename'], sitedescription=dicts['description'], footercopyright=dicts['footercopyright'], meta_description=dicts['meta_description'], meta_keywords=dicts['meta_keywords'],sitenumber=dicts['sitenumber'],siteemail=dicts['siteemail'],siteaddress=dicts['siteaddress'])
c.execute(q)
self.connection.commit()
return True
except Exception as e:
print(e)
return False
def update_module(self, data) -> bool:
"""updates module
Args:
data (dict): module info
Returns:
bool: true if no error
"""
c = self.connection.cursor()
try:
data = eval(data)
copy_data = data.copy()
del data['module']
se = json.dumps(data)
q = """UPDATE modules SET '{module}' = '{new}'""".format(
module=copy_data['module'], new=se)
c.execute(q)
self.connection.commit()
return True
except Exception as err:
self.log(f"Module update error: {err}")
return False
def update_credential(self, uname: str, newpwd: str) -> bool:
"""website login update
Args:
uname (str): username
newpwd (str): new password
Returns:
bool: true if success
"""
c = self.connection.cursor()
try:
q = "UPDATE users SET passw = '{value}' WHERE username = '{uname}';".format(
value=newpwd, uname=uname)
c.execute(q)
self.connection.commit()
return True
except Exception as err:
self.log(f"Update credential error: {err}")
return False
def get_messages(self) -> tuple:
"""messages from visitors (if included)
Returns:
tuple: messages
"""
c = self.connection.cursor()
c.execute("SELECT * FROM messages ORDER BY id DESC")
return c.fetchall()
def get_logs(self) -> tuple:
"""Logging (Deployment / Debugging safe)
Returns:
tuple: Logging messages
"""
c = self.connection.cursor()
c.execute("SELECT * FROM logging ORDER BY id DESC")
return c.fetchall()
def get_cred(self, *args,**kwargs) -> tuple:
"""Account information (raw) / Credential will be updated soon for more sec.
Returns:
tuple: Account credentials (raw)
"""
c = self.connection.cursor()
c.execute("SELECT * FROM users")
return c.fetchall()
def load_modules_settings(self) -> tuple:
"""Loads module settings
Returns:
tuple: contains module name and status
"""
c = self.connection.cursor()
c.execute("SELECT * FROM modules")
return c.fetchall()
def load_data_index(self, load: bool) -> tuple:
"""Loads information (for Enginepublic templates usage)
Args:
load (bool): returns site desc.
Returns:
tuple: website information
"""
c = self.connection.cursor()
self.fetch_control = c.execute("SELECT * FROM control")
self.site_data = self.fetch_control.fetchall()
self.fetch_msg = c.execute("SELECT * FROM messages")
self.site_msgs = self.fetch_msg.fetchall()
if load:
return self.site_data[0]
all_data = {
"sitedescription": self.site_data[0][0],
"sitename": self.site_data[0][1],
"footercopyright": self.site_data[0][2],
"logo": self.site_data[0][3],
"uparrow": self.site_data[0][4],
"domain": self.site_data[0][5],
"socialshare": self.site_data[0][6],
"popup": self.site_data[0][7],
"meta_description": self.site_data[0][8],
"meta_keywords": self.site_data[0][9],
"favicon": self.site_data[0][10],
"site_type": self.site_data[0][11],
"messages": len(self.site_msgs),
'sitenumber':self.site_data[0][16],
'siteemail':self.site_data[0][17],
'siteaddress':self.site_data[0][18],
}
return all_data