Skip to content

Commit 0d8d7d0

Browse files
authored
Merge pull request #61 from dani37x/dev
This merge includes commits from various sections of the project. The 'errors.py' description was created, and a few changes were made to HTML files. It is important to note that a new logic for the buying system was implemented. Additionally, this merge adds new actions to the admin panel. Some functions were rebuilt and regrouped as well.
2 parents 1fe394e + df697e6 commit 0d8d7d0

16 files changed

+177
-91
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Overall, E-lectro is a well-designed and fully-functional web application that d
5252
- [x] set the random price for products from last month
5353
- [x] Redis and Redis queue
5454
- [x] Redis queue and workers
55-
- [x] old functions with async queue usage
55+
- [x] old and new functions with co-work with async queue usage
5656
- [x] time to cancel task from queue
5757
- [x] delete cancelled task from rq history
5858
- [x] Forms
@@ -61,6 +61,7 @@ Overall, E-lectro is a well-designed and fully-functional web application that d
6161
- [x] Key form
6262
- [x] New Password form
6363
- [x] Remind Password form
64+
- [x] Captcha Chars Counter form
6465
- [x] Flask Login
6566
- [x] Login
6667
- [x] Authantacion
@@ -135,12 +136,16 @@ Overall, E-lectro is a well-designed and fully-functional web application that d
135136
- [x] users can see how much money they will save on each product during the promotion, thanks to the discount
136137
- [x] User panel
137138
- [x] a user can change his nickname, e-mail, and the personal data
139+
- [x] history of boughted products by users
138140
- [ ] better Readme section of installation which depict the correct process with Redis and Redis queue
141+
- [ ] option of payment for products
142+
- [ ] descriptions of funcs
143+
- [ ] more descriptions of files
144+
- [ ] tests of funcs
145+
- [ ] shop cart
139146
- [ ] multi-device login (max 3)
140147
- [ ] account lvl and points system with discounts.
141148
- [ ] payments for products
142-
- [ ] boughted products by users
143-
- [ ] list of products
144149
- [ ] own hash password algorithm
145150
- [ ] own authentication system
146151

project_files/admin.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212

1313
from .scripts.functions import check_admin, check_user, captcha
1414
from .scripts.functions import save_event, the_price, save_price
15+
from .scripts.functions import rq_add_row_to_db, rq_delete_db_row
1516

1617
from .scripts.actions import delete_rows, block_users, message, backup
1718
from .scripts.actions import account_activation, account_deactivation
1819
from .scripts.actions import delete_inactive_accounts, restore_database
19-
from .scripts.actions import send_newsletter
20+
from .scripts.actions import send_newsletter, the_price_actions
2021
from .scripts.actions import discount, previous_price, price_hike
21-
from .scripts.actions import rq_add_row_to_db, rq_delete_db_row
2222

2323
from .form import not_null
2424

@@ -278,32 +278,32 @@ def admin_product():
278278
)
279279
return render_template('admin/admin_product.html', products=products, task=task)
280280

281-
# if selected_action == 'the_lowest_price':
282-
# task = queue.enqueue(
283-
# the_price,
284-
# data=data,
285-
# price_type=selected_action,
286-
# retry=Retry(max=3, interval=[10, 30, 60])
287-
# )
288-
# return render_template('admin/admin_product.html', products=products, task=task)
289-
290-
# if selected_action == 'the_highest_price':
291-
# task = queue.enqueue(
292-
# the_price,
293-
# data=data,
294-
# price_type=selected_action,
295-
# retry=Retry(max=3, interval=[10, 30, 60])
296-
# )
297-
# return render_template('admin/admin_product.html', products=products, task=task)
298-
299-
# if selected_action == 'the_random_price':
300-
# task = queue.enqueue(
301-
# the_price,
302-
# data=data,
303-
# price_type=selected_action,
304-
# retry=Retry(max=3, interval=[10, 30, 60])
305-
# )
306-
# return render_template('admin/admin_product.html', products=products, task=task)
281+
if selected_action == 'the_lowest_price':
282+
task = queue.enqueue(
283+
the_price_actions,
284+
data=data,
285+
price_type=selected_action,
286+
retry=Retry(max=3, interval=[10, 30, 60])
287+
)
288+
return render_template('admin/admin_product.html', products=products, task=task)
289+
290+
if selected_action == 'the_highest_price':
291+
task = queue.enqueue(
292+
the_price_actions,
293+
data=data,
294+
price_type=selected_action,
295+
retry=Retry(max=3, interval=[10, 30, 60])
296+
)
297+
return render_template('admin/admin_product.html', products=products, task=task)
298+
299+
if selected_action == 'the_random_price':
300+
task = queue.enqueue(
301+
the_price_actions,
302+
data=data,
303+
price_type=selected_action,
304+
retry=Retry(max=3, interval=[10, 30, 60])
305+
)
306+
return render_template('admin/admin_product.html', products=products, task=task)
307307

308308

309309
if selected_action == 'backup':

project_files/auth.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def register():
8282
if form.validate_on_submit():
8383

8484
try:
85-
8685
session_list = open_json(file_path=SESSIONS)
8786
sess = check_session(session_list=session_list)
8887
key = random_string(size=6)
@@ -105,7 +104,6 @@ def register():
105104
return redirect( url_for('register_session', rendered_session=sess))
106105

107106
except Exception as e:
108-
109107
save_event(event=e, site=register.__name__)
110108
return 'Error with add register sessions'
111109

@@ -160,7 +158,6 @@ def login():
160158
if request.method == 'POST':
161159

162160
if form.validate_on_submit():
163-
164161
user = Users.query.filter_by(
165162
email=form.email.data,
166163
username=form.username.data
@@ -197,7 +194,7 @@ def remind():
197194
if request.method == 'POST':
198195

199196
if form.validate_on_submit():
200-
197+
201198
user = Users.query.filter_by(
202199
username=form.username.data,
203200
email=form.email.data
@@ -225,7 +222,6 @@ def remind():
225222
return 'xd'
226223

227224
save_json(file_path=SESSIONS, data=session_list)
228-
229225
return redirect( url_for( 'hash_session', rendered_session=sess))
230226

231227
return render_template('auth/remind_password.html', form=form)

project_files/errors.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
"""
2+
The file errors.py contains error handlers for the entire application
3+
and addresses the most common errors. These error handlers collect the
4+
errors and store them in a file called EVENTS.json, which works in
5+
conjunction with the event system.
6+
7+
This approach can help to streamline the error handling process and
8+
ensure that issues are properly recorded for future reference.
9+
"""
10+
11+
112
from project_files import app
213

314
from .scripts.functions import save_event
@@ -11,7 +22,6 @@ def handle_405(e):
1122

1223
@app.errorhandler(404)
1324
def handle_404(e):
14-
# save_event(event=e, site=handle_404.__name__)
1525
return '404'
1626

1727

project_files/main.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from project_files import app
22
from project_files import db
3+
from project_files import queue
34
from project_files import login_manager
45

56
from .database import Users, Products, UsersProducts
@@ -8,11 +9,13 @@
89
from .scripts.functions import check_admin, check_user, captcha
910
from .scripts.functions import similar_products_to_queries, recently_searched
1011
from .scripts.functions import classification, save_event, user_searched
11-
from .scripts.functions import the_price
12+
from .scripts.functions import the_price, rq_add_row_to_db
1213

1314
from flask_login import login_required, current_user
1415
from flask import render_template, url_for, redirect, request, session, make_response
1516

17+
from rq import Retry
18+
1619
from datetime import datetime
1720

1821
import random
@@ -125,7 +128,6 @@ def product_info(product_id):
125128
discount = the_price(product=product, price_type='the_highest_price')
126129
discount = round(product.price*100/discount, 0)
127130

128-
129131
resp = make_response(
130132
render_template(
131133
'shop/product_info.html',
@@ -134,12 +136,26 @@ def product_info(product_id):
134136
the_lowest_price=the_price(product=product, price_type='the_lowest_price'),
135137
the_highest_price=the_price(product=product, price_type='the_highest_price')
136138
))
137-
138139
resp.set_cookie(
139140
key=f'{product.category}',
140141
value=f'{product.price}',
141142
max_age=10*60*60*24
142143
)
144+
145+
if request.method == 'POST':
146+
buy_product = UsersProducts(
147+
user_id=current_user.id,
148+
product_id=product.id,
149+
price=product.price
150+
)
151+
queue.enqueue(
152+
rq_add_row_to_db,
153+
obj=buy_product,
154+
retry=Retry(max=3, interval=[10, 30, 60])
155+
)
156+
# flash message
157+
return redirect( url_for('product_info', product_id=product_id))
158+
143159
return resp
144160

145161

project_files/scripts/actions.py

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
from project_files import app
1313
from project_files import mail
1414
from project_files import queue
15-
from project_files import BLOCKED_USERS, USERS, PRODUCTS
15+
from project_files import BLOCKED_USERS, USERS, PRODUCTS, PRICES
1616

1717
from .functions import open_json, save_json , string_to_date
18-
from .functions import save_price
18+
from .functions import save_price, back_to_slash
1919

2020
from ..database import Users, BlockedUsers, Products
2121

@@ -27,31 +27,21 @@
2727

2828
import time
2929

30-
31-
# joint funcs
30+
import random
3231

3332

34-
def rq_add_row_to_db(obj):
35-
app.app_context().push()
36-
db.session.add(obj)
37-
db.session.commit()
38-
33+
# joint funcs
3934

4035
def delete_rows(model, data):
4136
time.sleep(10)
4237
app.app_context().push()
4338

39+
4440
for number in data:
4541
model.query.filter_by(id=number).delete()
4642
db.session.commit()
4743

4844

49-
def rq_delete_db_row(obj):
50-
app.app_context().push()
51-
db.session.delete(obj)
52-
db.session.commit()
53-
54-
5545
def backup(model):
5646

5747
if model == Users:
@@ -260,7 +250,7 @@ def newsletter_deactivation(username):
260250
# The funcs of Products panel
261251

262252

263-
def discount(percent, data, days):
253+
def discount(percent, data, days=0):
264254
time.sleep(10)
265255
app.app_context().push()
266256
percent = 1-(int(percent)/100)
@@ -271,10 +261,10 @@ def discount(percent, data, days):
271261
product.price *= percent
272262
product.price = round(product.price, 2)
273263
db.session.commit()
274-
save_price(data=[product], days=days)
264+
save_price(product=product, days=days)
275265

276266

277-
def price_hike(percent, data, days):
267+
def price_hike(percent, data, days=0):
278268
time.sleep(10)
279269
app.app_context().push()
280270
percent = 1+(int(percent)/100)
@@ -285,7 +275,7 @@ def price_hike(percent, data, days):
285275
product.price *= percent
286276
product.price = round(product.price, 2)
287277
db.session.commit()
288-
save_price(data=[product], days=days)
278+
save_price(product=product, days=days)
289279

290280

291281
def previous_price(data):
@@ -297,8 +287,38 @@ def previous_price(data):
297287
product.price = product.old_price
298288
product.old_price = variable
299289
db.session.commit()
300-
save_price(data=[product])
290+
save_price(product=product)
301291

302292

303-
293+
def the_price_actions(data, price_type):
294+
app.app_context().push()
295+
file_path = back_to_slash(PRICES)
296+
objects_list = open_json(file_path=file_path)
297+
298+
for number in data:
299+
product = Products.query.get(int(number))
300+
product_info = []
301+
for obj in objects_list:
302+
if int(obj['id']) == product.id:
303+
product_info.append(float(obj['price']))
304+
product_info.append(float(obj['old_price']))
305+
306+
variable = product.price
307+
308+
if price_type == 'the_lowest_price':
309+
product.price = min(product_info)
310+
product.old_price = variable
311+
312+
elif price_type == 'the_highest_price':
313+
variable = product.price
314+
product.price = max(product_info)
315+
product.old_price = variable
316+
317+
elif price_type == 'the_random_price':
318+
product.price = random.choice(product_info)
319+
product.old_price = variable
320+
321+
save_price(product=product)
322+
323+
db.session.commit()
304324

0 commit comments

Comments
 (0)