Skip to content

Commit

Permalink
Move export/load recipes commands to export/load data commands
Browse files Browse the repository at this point in the history
  • Loading branch information
earlinn committed Dec 29, 2023
1 parent fabc6af commit dece4d6
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 144 deletions.
99 changes: 74 additions & 25 deletions backend/core/management/commands/export_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def export_categories():

def export_subcategories():
data = apps.get_model("products", "Subcategory")
field_names = ["id", "name", "slug", "parent_category_id"]
field_names = ["id", "name", "slug", "parent_category_id", "image"]
with open(
os.path.join(DATA_DIR, "subcategory.csv"), "w", newline="", encoding="utf-8"
) as csvfile:
Expand Down Expand Up @@ -245,7 +245,15 @@ def export_tokens():

def export_reviews():
data = apps.get_model("reviews", "Review")
field_names = ["id", "text", "score", "pub_date", "author_id", "product_id"]
field_names = [
"id",
"text",
"score",
"pub_date",
"author_id",
"product_id",
"was_edited",
]
with open(
os.path.join(DATA_DIR, "reviews.csv"),
"w",
Expand Down Expand Up @@ -339,89 +347,130 @@ def export_sessions():
writer.writerow(row)


def export_recipes():
data = apps.get_model("recipes", "Recipe")
field_names = [
"id",
"pub_date",
"author_id",
"name",
"image",
"text",
"cooking_time",
]
with open(
os.path.join(DATA_DIR, "recipes.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_in_recipe():
data = apps.get_model("recipes", "ProductsInRecipe")
field_names = ["id", "recipe_id", "ingredient_id", "amount"]
with open(
os.path.join(DATA_DIR, "products_in_recipe.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)


class Command(BaseCommand):
def handle(self, *args, **options):
export_products()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели Product прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели Product прошёл успешно.")
)
export_products_components()
self.stdout.write(
self.style.SUCCESS(
"Экспорт данных поля components модели Product прошёл успешно!"
"Экспорт данных поля components модели Product прошёл успешно."
)
)
export_products_tags()
self.stdout.write(
self.style.SUCCESS(
"Экспорт данных поля tags модели Product прошёл успешно!"
"Экспорт данных поля tags модели Product прошёл успешно."
)
)
export_categories()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели Category прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели Category прошёл успешно.")
)
export_subcategories()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели Subcategory прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели Subcategory прошёл успешно.")
)
export_components()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели Component прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели Component прошёл успешно.")
)
export_tags()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели Tag прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели Tag прошёл успешно.")
)
export_producers()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели Producer прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели Producer прошёл успешно.")
)
export_promotions()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели Promotion прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели Promotion прошёл успешно.")
)
export_products_promotions()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели ProductPromoton прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели ProductPromoton прошёл успешно.")
)
export_users()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели User прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели User прошёл успешно.")
)
export_delivery_points()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели Delivery прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели Delivery прошёл успешно.")
)
export_favorites()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели FavoriteProduct прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели FavoriteProduct прошёл успешно.")
)
export_user_address()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели Address прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели Address прошёл успешно.")
)
export_tokens()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели Token прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели Token прошёл успешно.")
)
export_reviews()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели Review прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели Review прошёл успешно.")
)
export_orders()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели Order прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели Order прошёл успешно.")
)
# export_shopping_cart()
# self.stdout.write(
# self.style.SUCCESS("Экспорт данных модели ShoppingCart прошёл успешно!")
# )
export_order_products()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели OrderProduct прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели OrderProduct прошёл успешно.")
)
export_sessions()
self.stdout.write(
self.style.SUCCESS("Экспорт данных модели Session прошёл успешно!")
self.style.SUCCESS("Экспорт данных модели Session прошёл успешно.")
)
export_recipes()
self.stdout.write(
self.style.SUCCESS("Экспорт даных модели Recipe прошёл успешно.")
)
export_products_in_recipe()
self.stdout.write(
self.style.SUCCESS("Экспорт даных модели ProductsInRecipe прошёл успешно.")
)
43 changes: 43 additions & 0 deletions backend/core/management/commands/load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Subcategory,
Tag,
)
from recipes.models import ProductsInRecipe, Recipe
from reviews.models import Review
from users.models import Address, User

Expand Down Expand Up @@ -288,6 +289,7 @@ def read_reviews():
pub_date=row["pub_date"],
author_id=row["author_id"],
product_id=row["product_id"],
was_edited=row["was_edited"],
)
review.save()

Expand All @@ -304,6 +306,41 @@ def read_sessions():
session.save()


def read_recipes():
with open(os.path.join(DATA_DIR, "recipes.csv"), "r", encoding="utf-8") as f:
reader = DictReader(f)
for row in reader:
if row.get("pub_date"):
pub_date = row["pub_date"]
else:
pub_date = timezone.now()
recipe = Recipe(
id=row["id"],
pub_date=pub_date,
author_id=row["author_id"],
name=row["name"],
image=row["image"],
text=row["text"],
cooking_time=row["cooking_time"],
)
recipe.save()


def read_products_in_recipes():
with open(
os.path.join(DATA_DIR, "products_in_recipe.csv"), "r", encoding="utf-8"
) as f:
reader = DictReader(f)
for row in reader:
recipe = ProductsInRecipe(
id=row["id"],
recipe_id=row["recipe_id"],
ingredient_id=row["ingredient_id"],
amount=row["amount"],
)
recipe.save()


class Command(BaseCommand):
def handle(self, *args, **options):
read_users()
Expand Down Expand Up @@ -344,6 +381,10 @@ def handle(self, *args, **options):
self.stdout.write("Данные из файла reviews.csv загружены")
read_sessions()
self.stdout.write("Данные из файла sessions.csv загружены")
read_recipes()
self.stdout.write("Данные из файла recipes.csv загружены")
read_products_in_recipes()
self.stdout.write("Данные из файла products_in_recipe.csv загружены")

model_list = [
Delivery,
Expand All @@ -362,6 +403,8 @@ def handle(self, *args, **options):
User,
Token,
Session,
Recipe,
ProductsInRecipe,
]
sequence_sql = connection.ops.sequence_reset_sql(no_style(), model_list)
with connection.cursor() as cursor:
Expand Down
58 changes: 0 additions & 58 deletions backend/recipes/management/commands/export_recipes.py

This file was deleted.

61 changes: 0 additions & 61 deletions backend/recipes/management/commands/load_recipes.py

This file was deleted.

0 comments on commit dece4d6

Please sign in to comment.