김찬진의 개발 블로그

[23/05/23] 도커 빌드 커맨드 커스텀 본문

1일1배움/Django

[23/05/23] 도커 빌드 커맨드 커스텀

kim chan jin 2023. 5. 23. 11:02

BaseCommand를 상속받고 
handle() 메서드를 오버라이딩하고 내가 원하는 로직을 구현하면 된다

근데 해결하지 못한 궁금증이 있는데 왜 os.environ.get()을 쓰지 못하는걸까?

os.environ.get()을 쓰지 못해서 with open 으로 직접 파일을 읽어오고 한 줄 한 줄 읽으면서 전처리를 했다

완전 무식한 방법이긴 하지만 어떻게 할지 몰라서 노가다로 했다.

 

webapp/.env

DJANGO_SECRET_KEY="example"
DATABASE_LOCAL_NAME='example'
DATABASE_LOCAL_USER='example'
DATABASE_LOCAL_PASSWORD='example'
DATABASE_LOCAL_HOST='example'
DATABASE_LOCAL_PORT=example
KAKAO_REST_API_KEY="example"
KAKAO_REDIRECT_URI="example"
KAKAO_GIFT_BIZ_REST_APP_KEY='example'

TEMPLATE_TOKEN_LIST=
[
{'공차 밀크티':'example'},
{'할리스 에스프레소':'example'},
{'투썸플레이스 아메리카노':'example'},
]

 

template/management/commands/template_objs_in_db.py

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")

import django

django.setup()

import requests
from django.core.management.base import BaseCommand
from template.models import Template

script_absol_dir = os.path.dirname(os.path.abspath(__file__))
env_absol_dir = os.path.join(script_absol_dir, '../../../.env')

template_token_list = []
template_num_in_env = 3


class Command(BaseCommand):
    help = 'Create template objects and save in DB'

    def handle(self, *args, **options):
        with open(env_absol_dir, "r", encoding="utf-8") as file:

            app_key_dict = {}

            for line in file:
                if line.startswith("#") or line.strip() == "":
                    continue

                elif line.startswith("KAKAO_GIFT_BIZ_REST_APP_KEY"):
                    key, value = line.strip().split("=")
                    value = value.strip("\'")
                    app_key_dict[key] = value

                elif line.startswith("TEMPLATE_TOKEN_LIST"):

                    while True:
                        line = file.readline().strip()

                        if line.startswith("["):
                            continue

                        elif not line or line.endswith("]"):
                            break

                        else:
                            template_token_dict = {}

                            now_use_dict = line.strip("\r\n").strip(",")
                            key, value = now_use_dict.strip("{").strip("}").split(":")
                            key = key.strip("\'")
                            value = value.strip("\'")

                            template_token_dict[key] = value
                            template_token_list.append(template_token_dict)

        url = "https://gateway-giftbiz.kakao.com/openapi/giftbiz/v1/template"
        api_key = "KakaoAK " + app_key_dict["KAKAO_GIFT_BIZ_REST_APP_KEY"]
        headers = {
            "Authorization": api_key,
            "Accept": "application/json"
        }

        response = requests.get(url, headers=headers)
        json_data = response.json()
        total_count = json_data["totalCount"]
        templates = json_data["contents"]

        if Template.objects.exists():

            if Template.objects.count() == template_num_in_env:
                print("template 객체 개수 = ", Template.objects.count())
                print("env 속 template 객체 개수 = ", template_num_in_env)
                print("빌드가 실행된 적이 있지만 템플릿 개수가 일치함 -> 템플릿 업데이트 불필요")
                pass
            else:
                print("template 객체 개수 = ", Template.objects.count())
                print("env 속 template 객체 개수 = ", template_num_in_env)
                print("빌드가 실행된 적이 있지만 템플릿 개수가 일치하지 않음 -> 템플릿 업데이트 필요")

                Template.objects.all().delete()

                self.create_templates(templates, total_count)

        else:
            print("template 객체 개수 = ", Template.objects.count())
            print("env 속 template 객체 개수 = ", template_num_in_env)
            print("빌드가 처음으로 실행")
            self.create_templates(templates, total_count)

    def create_templates(self, templates, total_count):
        for n in range(0, total_count):
            template = templates[n]
            template_name = template["template_name"]

            for template_token_list_dict in template_token_list:

                if template_name in template_token_list_dict:
                    template_token = template_token_list_dict[template_name]

                    template_trace_id = template["template_trace_id"]
                    order_template_status = template["order_template_status"]
                    budget_type = template["budget_type"]
                    gift_sent_count = template["gift_sent_count"]
                    bm_sender_name = template["bm_sender_name"]
                    mc_image_url = template["mc_image_url"]
                    mc_text = template["mc_text"]

                    product_data = template["product"]

                    item_type = product_data["item_type"]
                    product_name = product_data["product_name"]
                    brand_name = product_data["brand_name"]
                    product_image_url = product_data["product_image_url"]
                    product_thumb_image_url = product_data["product_thumb_image_url"]
                    brand_image_url = product_data["brand_image_url"]
                    product_price = product_data["product_price"]

                    new_template = Template(template_token=template_token, template_name=template_name,
                                            template_trace_id=template_trace_id,
                                            order_template_status=order_template_status,
                                            budget_type=budget_type, gift_sent_count=gift_sent_count,
                                            bm_sender_name=bm_sender_name, mc_image_url=mc_image_url,
                                            mc_text=mc_text, item_type=item_type,
                                            product_name=product_name, brand_name=brand_name,
                                            product_image_url=product_image_url,
                                            product_thumb_image_url=product_thumb_image_url,
                                            brand_image_url=brand_image_url, product_price=product_price)

                    new_template.save()
Comments