-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
151 lines (132 loc) · 5.37 KB
/
models.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
from flask_sqlalchemy import SQLAlchemy
import os
from flask import Flask, request, jsonify
import psycopg2
app = Flask(__name__)
# from app import db
db = SQLAlchemy(app)
class Product(db.Model):
__tablename__ = 'products'
id = db.Column(db.Integer, primary_key=True)
wproductid = db.Column(db.String())
image = db.Column(db.String())
fprice = db.Column(db.String())
category = db.Column(db.String())
subcategory = db.Column(db.String())
title = db.Column(db.String())
seller = db.Column(db.String())
rprice = db.Column(db.String())
discount = db.Column(db.String())
rating = db.Column(db.String())
numreviews = db.Column(db.String())
tikinow = db.Column(db.String())
productlink = db.Column(db.String())
cat_id = db.Column(db.String())
def __init__(self, id, wproductid, image, fprice, category, subcategory, title, seller, rprice, discount, rating, numreviews, tikinow, productlink, cat_id):
self.id = id
self.wproductid = wproductid
self.image = image
self.fprice = fprice
self.category = category
self.subcategory = subcategory
self.title = title
self.seller = seller
self.rprice = rprice
self.discount = discount
self.rating = rating
self.numreviews = numreviews
self.tikinow = tikinow
self.productlink = productlink
self.cat_id = cat_id
def save_into_db(self):
query = 'SELECT wproductid FROM products WHERE wproductid LIKE %s;'
val = (self.wproductid,)
try:
conn = psycopg2.connect("dbname=thuctamdb user=postgres password=thuctam")
conn.autocommit = True
cur = conn.cursor()
cur.execute(query, val)
result = cur.fetchall()
if len(result) > 0:
conn.close()
return ''
except Exception as err:
print(f'SELECT wproductid FROM products WHERE wproductid LIKE {val}: {err}')
query = f"""
INSERT INTO products (wproductid, image, fprice, category, subcategory, title, seller, rprice, discount, rating, numreviews, tikinow, productlink, cat_id)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING id;
"""
val = (self.wproductid, self.image, self.fprice, self.category, self.subcategory, self.title, self.seller, self.rprice, self.discount, self.rating, self.numreviews, self.tikinow, self.productlink, self.cat_id)
try:
cur.execute(query, val)
# Get id of the new row
self.prod_id = cur.fetchone()[0]
conn.close()
except Exception as err:
print(f'ERROR INSERT INTO products..{val}: {err}')
def __repr__(self):
return f'ID: webProductID: {self.wproductid}, ImageURL: {self.image}, FinalPrice: {self.fprice}, MainCategory: {self.category}, SubCategory: {self.subcategory}, Title: {self.title}, Seller: {self.seller}, RegularPrice: {self.rprice}, Discount: {self.discount}, Rating: {self.rating}, NoOfReviews: {self.numreviews}, TikiNOW: {self.tikinow}, ProductLink: {self.productlink}, SubCatID: {self.cat_id}'
def serialize(self):
return {
'id' : self.id,
'wproductid' : self.wproductid,
'image' : self.image,
'fprice' : self.fprice,
'category' : self.category,
'subcategory' : self.subcategory,
'title' : self.title,
'seller' : self.seller,
'rprice' : self.rprice,
'discount' : self.discount,
'rating' : self.rating,
'numreviews' : self.numreviews,
'tikinow' : self.tikinow,
'productlink' : self.productlink,
'cat_id' : self.cat_id,
}
class Category(db.Model):
__tablename__ = 'categories'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String())
url = db.Column(db.String())
parent_id = db.Column(db.String())
def __init__(self, id, name, url, parent_id):
self.id = id
self.name = name
self.url = url
self.parent_id = parent_id
def save_into_db(self):
query = 'SELECT url FROM categories WHERE url LIKE %s;'
val = (self.url,)
try:
conn = psycopg2.connect("dbname=thuctamdb user=postgres password=thuctam")
conn.autocommit = True
cur = conn.cursor()
cur.execute(query, val)
result = cur.fetchall()
if len(result) > 0:
conn.close()
return ''
except Exception as err:
print(f'ERROR: {err}')
query = f"""
INSERT INTO categories (name, url, parent_id)
VALUES (%s, %s, %s) RETURNING id;
"""
val = (self.name, self.url, self.parent_id)
try:
cur.execute(query, val)
# Get id of the new row
self.cat_id = cur.fetchone()[0]
conn.close()
except Exception as err:
print(f'ERROR: {err}')
def __repr__(self):
return f'ID: {self.id}, Name: {self.name}, URL: {self.url}, Parent ID: {self.parent_id}'
def serialize(self):
return {
'id': self.id,
'name': self.name,
'url': self.url,
'parent_id':self.parent_id
}