-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
049fa96
commit e80f055
Showing
17 changed files
with
273 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ManagementConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "management" |
136 changes: 136 additions & 0 deletions
136
Django/management/management/commands/import_recipes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import json | ||
import logging | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.db import transaction | ||
from ingredient.models import Ingredient | ||
from ingredient.models import IngreMajor | ||
from recipe.models import CookingAttribute | ||
from recipe.models import CookingMainIngre | ||
from recipe.models import CookingMethod | ||
from recipe.models import CookingNameList | ||
from recipe.models import CookingSituation | ||
from recipe.models import CookingType | ||
from recipe.models import DetailRecipe | ||
from recipe.models import Recipe | ||
from recipe.models import RecipeDifficulty | ||
from recipe.models import RecipeIngredientList | ||
|
||
# 로깅 설정 | ||
logging.basicConfig( | ||
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" | ||
) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Import recipes from JSON file" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument("json_file", type=str, help="Path to the JSON file") | ||
|
||
def handle(self, *args, **options): | ||
json_file = options["json_file"] | ||
|
||
# JSON 데이터 로드 | ||
with open(json_file, "r", encoding="utf-8") as f: | ||
data = json.load(f) | ||
|
||
self.import_recipes(data) | ||
|
||
@transaction.atomic | ||
def import_recipes(self, data): | ||
for recipe_data in data.values(): | ||
try: | ||
self.create_recipe(recipe_data) | ||
except Exception as e: | ||
logging.error( | ||
f"Error inserting recipe {recipe_data.get('recipe_name', 'Unknown')}: {str(e)}" | ||
) | ||
|
||
def map_difficulty(self, difficulty): | ||
difficulty = difficulty.upper() | ||
if difficulty in ["초급", "EASY"]: | ||
return RecipeDifficulty.EASY | ||
elif difficulty in ["중급", "MEDIUM"]: | ||
return RecipeDifficulty.MEDIUM | ||
elif difficulty in ["고급", "HARD"]: | ||
return RecipeDifficulty.HARD | ||
else: | ||
return RecipeDifficulty.EASY # 기본값 | ||
|
||
def get_or_create(self, model, name): | ||
obj, created = model.objects.get_or_create(name=name) | ||
return obj | ||
|
||
def get_or_create_ingredient(self, ingre_name): | ||
ingredient = Ingredient.objects.filter(name=ingre_name).first() | ||
if ingredient: | ||
return ingredient | ||
else: | ||
# 기본 IngreMajor 생성 또는 가져오기 | ||
default_major, _ = IngreMajor.objects.get_or_create(name="기타") | ||
return Ingredient.objects.create( | ||
name=ingre_name, major=default_major, is_custom=True | ||
) | ||
|
||
def create_recipe(self, recipe_data): | ||
# CookingAttribute 관련 객체 가져오기 또는 생성 | ||
name = self.get_or_create(CookingNameList, recipe_data["cooking_name"]) | ||
method = self.get_or_create(CookingMethod, recipe_data["cooking_method"]) | ||
situation = self.get_or_create(CookingSituation, recipe_data["situation_type"]) | ||
main_ingre = self.get_or_create( | ||
CookingMainIngre, recipe_data["main_ingredient_type"] | ||
) | ||
type_ = self.get_or_create(CookingType, recipe_data["cooking_type"]) | ||
|
||
# CookingAttribute 생성 | ||
attribute, _ = CookingAttribute.objects.get_or_create( | ||
name=name, | ||
method=method, | ||
situation=situation, | ||
main_ingre=main_ingre, | ||
type=type_, | ||
) | ||
|
||
# Recipe 생성 | ||
recipe, created = Recipe.objects.get_or_create( | ||
url=recipe_data["URL"], | ||
defaults={ | ||
"recipe_name": recipe_data["recipe_name"], | ||
"nick_name": recipe_data.get("nick_name", "None")[ | ||
:255 | ||
], # max_length=255 제한 | ||
"recommend_num": recipe_data["recommand_num"], | ||
"recipe_intro": recipe_data["recipe_intro"], | ||
"eat_people": int(recipe_data["eat_people"].replace("인분", "")), | ||
"difficulty": self.map_difficulty(recipe_data["difficulty"]), | ||
"cooking_time": int(recipe_data["cooking_time"][0]), | ||
"thumbnail_url": recipe_data.get("thumbnail_url", ""), | ||
"attribute": attribute, | ||
}, | ||
) | ||
|
||
if not created: | ||
logging.info(f"Recipe already exists: {recipe_data['recipe_name']}") | ||
return | ||
|
||
# RecipeIngredientList 생성 | ||
for ingre_name, quantity in recipe_data["ingre_list"].items(): | ||
ingredient = self.get_or_create_ingredient(ingre_name) | ||
RecipeIngredientList.objects.create( | ||
recipe=recipe, | ||
ingredient=ingredient, | ||
quantity=quantity[:50], # max_length=50 제한 | ||
) | ||
|
||
# DetailRecipe 생성 | ||
for step, detail in recipe_data["detail_recipes"].items(): | ||
DetailRecipe.objects.create( | ||
recipe=recipe, | ||
step=int(step), | ||
img_url=detail.get("img_url", ""), | ||
recipe_text=detail["recipe"], | ||
tip=detail.get("tip", ""), | ||
) | ||
|
||
logging.info(f"Successfully inserted recipe: {recipe_data['recipe_name']}") |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.shortcuts import render | ||
|
||
# Create your views here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 5.0.7 on 2024-08-04 07:59 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("recipe", "0005_alter_cookingattribute_unique_together"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="recipe", | ||
name="nick_name", | ||
field=models.CharField(blank=True, max_length=255), | ||
), | ||
] |
38 changes: 38 additions & 0 deletions
38
...o/recipe/migrations/0007_alter_cookingmainingre_name_alter_cookingmethod_name_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Generated by Django 5.0.7 on 2024-08-04 08:08 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("recipe", "0006_alter_recipe_nick_name"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="cookingmainingre", | ||
name="name", | ||
field=models.CharField(max_length=255, unique=True), | ||
), | ||
migrations.AlterField( | ||
model_name="cookingmethod", | ||
name="name", | ||
field=models.CharField(max_length=255, unique=True), | ||
), | ||
migrations.AlterField( | ||
model_name="cookingnamelist", | ||
name="name", | ||
field=models.CharField(max_length=255, unique=True), | ||
), | ||
migrations.AlterField( | ||
model_name="cookingsituation", | ||
name="name", | ||
field=models.CharField(max_length=255, unique=True), | ||
), | ||
migrations.AlterField( | ||
model_name="cookingtype", | ||
name="name", | ||
field=models.CharField(max_length=255, unique=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Generated by Django 5.0.7 on 2024-08-04 08:20 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
( | ||
"recipe", | ||
"0007_alter_cookingmainingre_name_alter_cookingmethod_name_and_more", | ||
), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="recipe", | ||
name="nick_name", | ||
field=models.CharField(blank=True, max_length=255, null=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.