This repository has been archived by the owner on Mar 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.py
180 lines (154 loc) · 5.19 KB
/
product.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
from datetime import datetime
from database import DB
class Product:
def __init__(self, id, title, content, price):
self.id = id
self.title = title
self.content = content
self.price = price
self.published = self.get_current_datetime()
self.is_active = 1
self.owner_id = Noneconnect_to_DB()
self.publisher_id = None
self.values = (self.id, self.title, self.content, self.price,
self.published, self.is_active)
@staticmethod
def get_current_datetime():
now = datetime.now()
return now.strftime("%d-%m-%Y %H:%M:%S")
def get_product_published_date(self):
with DB() as db:
return db.execute(
'''
SELECT published
FROM products
WHERE id = ?
''', (self.id,)
).fetchone()[0]
def get_publisher_id(self):
with DB() as db:
return db.execute(
'''
SELECT publisher_id
FROM products
WHERE id = ?
''', (self.id,)
).fetchone()[0]
@staticmethod
def find_product(id):
with DB() as db:
product = db.execute(
'''
SELECT id, title, content, price
FROM products
WHERE id = ?
''', (id,)
).fetchone()
return Product(*product)
def add_product(self, publisher_id):
with DB() as db:
self.values += (publisher_id, publisher_id)
db.execute(
'''
INSERT INTO products (id, title, content, price,
published, is_active, owner_id, publisher_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
''', self.values
)
return self
@staticmethod
def get_all_products(publisher_id):
with DB() as db:
products = db.execute(
'''
SELECT products.title, products.owner_id,
products.publisher_id, users.name, products.price
FROM users
INNER JOIN products
ON users.id = products.owner_id
AND products.publisher_id = ?
''', (publisher_id,)
).fetchall()
return products
@staticmethod
def get_all_active_products():
with DB() as db:
products = db.execute(
'''
SELECT id, title, content, price
FROM products
WHERE is_active = 1
'''
).fetchall()
return [Product(*product) for product in products]
@staticmethod
def get_all_unactive_products(publisher_id):
with DB() as db:
products = db.execute(
'''
SELECT products.title, users.name
FROM users
INNER JOIN products
ON users.id = products.owner_id
AND products.publisher_id != products.owner_id
AND products.publisher_id = ?
''', (publisher_id,)
).fetchall()
return products
def edit_product(self, new):
with DB() as db:
all_values = (new.title, new.content, new.price, self.id)
db.execute(
'''
UPDATE products
SET title = ?,
content = ?,
price = ?
WHERE id = ?
''', all_values
)
return self
def delete_product(self):
with DB() as db:
db.execute(
'''
DELETE
FROM products
WHERE id = ?
''', (self.id,)
)
@staticmethod
def buy_product(product_id, owner_id):
with DB() as db:
db.execute(
'''
UPDATE products
SET is_active = 0,
owner_id = ?
WHERE id = ?
''', (owner_id, product_id)
)
@staticmethod
def get_username_by_publisher_id(product_title):
with DB() as db:
return db.execute(
'''
SELECT users.id, products.publisher_id,
users.name, products.title
FROM users
INNER JOIN products
ON users.id = products.publisher_id
AND products.title = ?
''', (product_title,)
).fetchone()[2]
# @staticmethod
# def get_products_by_name(name):
# with DB() as db:
# products = db.execute(
# '''
# SELECT *
# FROM products
# WHERE title = ?
# ''', (name,)
# ).fetchall()
# return [Product(*product) for product in products]