Skip to content

Commit

Permalink
Add coupons to the load/export data commands and update these command…
Browse files Browse the repository at this point in the history
…s for orders (add coupon_applied and coupon_discount fields)
  • Loading branch information
earlinn committed Feb 1, 2024
1 parent a11407c commit b23b07a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
19 changes: 19 additions & 0 deletions backend/core/management/commands/export_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ def export_promotions():
writer.writerow(row)


def export_coupons():
data = apps.get_model("products", "Coupon")
field_names = [f.name for f in data._meta.fields]
with open(
os.path.join(DATA_DIR, "coupons.csv"), "w", newline="", encoding="utf-8"
) as csvfile:
writer = csv.writer(csvfile)
writer.writerow(field_names)
for obj in data.objects.all():
row = [getattr(obj, field) for field in field_names]
writer.writerow(row)


def export_products_promotions():
data = apps.get_model("products", "ProductPromotion")
field_names = ["id", "product_id", "promotion_id"]
Expand Down Expand Up @@ -285,6 +298,8 @@ def export_orders():
"add_address",
"total_price",
"user_data",
"coupon_applied_id",
"coupon_discount",
]
with open(
os.path.join(DATA_DIR, "orders.csv"),
Expand Down Expand Up @@ -428,6 +443,10 @@ def handle(self, *args, **options):
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели Promotion прошёл успешно.")
)
export_coupons()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели Coupon прошёл успешно.")
)
export_products_promotions()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели ProductPromoton прошёл успешно.")
Expand Down
31 changes: 31 additions & 0 deletions backend/core/management/commands/load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from products.models import (
Category,
Component,
Coupon,
FavoriteProduct,
Producer,
Product,
Expand Down Expand Up @@ -134,6 +135,29 @@ def read_promotions():
promotion.save()


def read_coupons():
with open(os.path.join(DATA_DIR, "coupons.csv"), "r", encoding="utf-8") as f:
reader = DictReader(f)
for row in reader:
coupon = Coupon(
id=row["id"],
promotion_type=row["promotion_type"],
name=row["name"],
slug=row["slug"],
code=row["code"],
discount=row["discount"],
conditions=row["conditions"],
is_active=row["is_active"],
is_constant=row["is_constant"],
image=row["image"],
)
if row.get("start_time"):
coupon.start_time = row["start_time"]
if row.get("end_time"):
coupon.start_time = row["end_time"]
coupon.save()


def read_products():
with open(os.path.join(DATA_DIR, "products.csv"), "r", encoding="utf-8") as f:
reader = DictReader(f)
Expand Down Expand Up @@ -238,6 +262,10 @@ def read_orders():
add_address=row["add_address"],
total_price=row["total_price"],
user_data=row["user_data"],
coupon_applied_id=row["coupon_applied_id"],
coupon_discount=None
if row["coupon_discount"] == ""
else row["coupon_discount"],
)
order.save()

Expand Down Expand Up @@ -361,6 +389,8 @@ def handle(self, *args, **options):
self.stdout.write("Данные из файла components.csv загружены")
read_promotions()
self.stdout.write("Данные из файла promotions.csv загружены")
read_coupons()
self.stdout.write("Данные из файла coupons.csv загружены")
read_products()
self.stdout.write("Данные из файла products.csv загружены")
read_products_components()
Expand Down Expand Up @@ -398,6 +428,7 @@ def handle(self, *args, **options):
Producer,
Product,
Promotion,
Coupon,
Subcategory,
Tag,
Review,
Expand Down

0 comments on commit b23b07a

Please sign in to comment.