def confirm_order(): connection = None order_dao = OrderDao() try: connection = connect_db() with connection.cursor(pymysql.cursors.DictCursor) as cursor: query = """ SELECT order_items.id, order_items.detailed_order_number FROM order_items INNER JOIN order_logs ON (order_logs.order_item_id = order_items.id AND order_logs.order_status_id = order_items.order_status_id) WHERE order_items.order_status_id = 4 AND order_logs.created_at < (CURRENT_TIMESTAMP() - INTERVAL 2 MINUTE) """ cursor.execute(query) orders = cursor.fetchall() order_item_id = [order['id'] for order in orders] confirm_order = { "order_item_id": order_item_id, "order_status_id": 4, "new_order_status_id": 5 } confirmed_orders = order_dao.update_order_status( connection, confirm_order) order_log = [[order['id'], None, 5] for order in orders] created_logs = order_dao.create_order_log(connection, order_log) if len(orders) == confirmed_orders == created_logs: connection.commit() for order in orders: print( f"Order confirmed: no.{order['detailed_order_number']}" ) else: connection.rollback() print( "Something went wrong with order confirmation. Will try again in 1 minute" ) except ProgrammingError: connection.rollback() print("No order to update. Will try again in 1 minute") finally: try: if connection: connection.close() except Exception as e: print(f"connection error: {e}")
def create_app(test_config=None): app = Flask(__name__) app.json_encoder = CustomEncoder CORS(app, resources={r'*': {'origins': '*'}}) if test_config is None: app.config.from_pyfile('config.py') else: app.config.update(test_config) #persistence layer account_dao = AccountDao() seller_dao = SellerDao() order_dao = OrderDao() product_dao = ProductDao() #business layer account_service = AccountService(account_dao, config) seller_service = SellerService(seller_dao, config) order_service = OrderService(order_dao) product_service = ProductService(product_dao) #presentation layer(엔드포인트 생성) app.register_blueprint(create_account_endpoints(account_service)) app.register_blueprint(create_seller_endpoints(seller_service)) app.register_blueprint(create_order_endpoints(order_service)) app.register_blueprint(create_product_endpoints(product_service)) return app
def create_app(test_config=None): app = Flask(__name__) app.config['JSON_AS_ASCII'] = False CORS(app) if test_config is None: app.config.from_pyfile("config.py") else: app.config.update(test_config) product_dao = ProductDao() seller_dao = SellerDao() order_dao = OrderDao() user_dao = UserDao() coupon_dao = CouponDao() event_dao = EventDao() services = Services services.product_service = ProductService(product_dao, app.config) services.seller_service = SellerService(seller_dao, app.config) services.order_service = OrderService(order_dao, app.config) services.user_service = UserService(user_dao, app.config) services.coupon_service = CouponService(coupon_dao, app.config) services.event_service = EventService(event_dao, app.config) create_endpoints(app, services) return app
def create_app(test_config=None): def get_session(): session = Session() return session app = Flask(__name__) CORS(app, resources={r'*': {'origins': '*'}}) @app.errorhandler(Exception) def handle_invalid_usage(error): if isinstance(error, InvalidRequest): return {'message': str(error)}, 400 else: response = jsonify(error) response.status_code = error.status_code return response from contextlib import suppress from flask.json import JSONEncoder class MyJSONEncoder(JSONEncoder): def default(self, obj): # Optional: convert datetime objects to ISO format with suppress(AttributeError): return obj.isoformat() return dict(obj) app.json_encoder = MyJSONEncoder if test_config is None: app.config.from_pyfile("config.py") else: app.config.update(test_config) database = create_engine(app.config['DB_URL'], encoding='utf-8', max_overflow=0) Session = sessionmaker(bind=database, autocommit=False) # Persistence Layer seller_dao = SellerDao() product_dao = ProductDao() order_dao = OrderDao() # Business Layer services = Services services.seller_service = SellerService(seller_dao, app.config) services.product_service = ProductService(product_dao) services.order_service = OrderService(order_dao, seller_dao) seller_endpoints(app, services, get_session) product_endpoints(app, services, get_session) order_endpoints(app, services, get_session) return app
def update_status(self): """ 배송완료된 주문들을 10분 후 구매확정으로 상태를 변경합니다. Args: N/A Returns: N/A Authors: [email protected](최지선) History: 2020.11.03(최지선) : 초기 생성 2020.11.06(최지선) : select 쿼리 추가 """ try: db_connection = get_connection() with db_connection.cursor() as cursor: #10분 전에 배송완료가 된 주문들 조회하는 쿼리 select_query = """ SELECT orders.id, detailed_order_number FROM orders LEFT JOIN order_status_history ON orders.id = order_status_history.order_id WHERE order_status_history.order_status_id = 4 AND order_status_history.updated_at < SUBDATE(NOW(), INTERVAL 10 MINUTE) """ cursor.execute(select_query) for order in cursor.fetchall(): query = """ UPDATE orders LEFT JOIN order_status_history ON orders.id = order_status_history.order_id SET orders.order_status_id = 5 WHERE order_status_history.order_status_id = 4 AND orders.id = %(id)s AND order_status_history.updated_at < SUBDATE(NOW(), INTERVAL 10 MINUTE) """ cursor.execute(query, order) order_history_info = { 'order_status_id': 5, 'order_id': order['id'], 'account_id': 1 } order_history = OrderDao().insert_order_history( db_connection, order_history_info) if order_history: db_connection.commit() print(order['detailed_order_number'], "번 주문이 구매확정 되었습니다.") else: db_connection.rollback() finally: db_connection.close()
def create_app(test_config=None): app = Flask(__name__) if test_config is None: app.config.from_pyfile('config.py') else: app.config.update(test_config) # pool size : 1000, max_overflow=100 인 QueuePool로 DB 연결 설정 database = create_engine(app.config['DB_URL'], encoding='utf-8', pool_size=1000, max_overflow=100, poolclass=QueuePool) # database engin와 연동된 session maker 생성, connection 필요시마다 session instance 생성 Session = sessionmaker(bind=database) # CORS 설정 CORS(app, resources={r'*': {'origins': '*'}}) # Persistence layer order_dao = OrderDao() user_dao = UserDao() seller_dao = SellerDao() product_dao = ProductDao() qna_dao = QnADao() review_dao = ReviewDao() coupon_dao = CouponDao() # Business layer order_service = OrderService(order_dao) user_service = UserService(user_dao) seller_service = SellerService(seller_dao) product_service = ProductService(product_dao) qna_service = QnAService(qna_dao) review_service = ReviewService(review_dao) coupon_service = CouponService(coupon_dao) # Presentation layer app.register_blueprint(create_order_endpoints(order_service, Session)) app.register_blueprint(create_user_endpoints(user_service, Session)) app.register_blueprint(create_seller_endpoints(seller_service, Session)) app.register_blueprint(create_product_endpoints(product_service, Session)) app.register_blueprint(create_qna_endpoints(qna_service, Session)) app.register_blueprint(create_review_endpoints(review_service, Session)) app.register_blueprint(create_coupon_endpoints(coupon_service, Session)) return app
def create_app(test_config=None): app = Flask(__name__) if test_config is None: app.config.from_pyfile("config.py") else: app.config.update(test_config) # DB 연결 database = create_engine( app.config["DB_URL"], encoding="utf-8", pool_size=1000, max_overflow=100, poolclass=QueuePool, ) # database와 연동된 session maker 생성, connection 필요시마다 session instace 생성 Session = sessionmaker(bind=database) # CORS 설정 CORS(app, resources={r"*": {"origins": "*"}}) # Persistance layer user_dao = UserDao() product_dao = ProductDao() qna_dao = QnaDao() order_dao = OrderDao() review_dao = ReviewDao() event_dao = EventDao() # Business layer user_service = UserService(user_dao) product_service = ProductService(product_dao) qna_service = QnaService(qna_dao) order_service = OrderService(order_dao) review_service = ReviewService(review_dao) event_service = EventService(event_dao) # Presentation layer app.register_blueprint(create_user_endpoints(user_service, Session)) app.register_blueprint(create_product_endpoints(product_service, Session)) app.register_blueprint(create_qna_endpoints(qna_service, Session)) app.register_blueprint(create_order_endpoints(order_service, Session)) app.register_blueprint(create_review_endpoints(review_service, Session)) app.register_blueprint(create_event_endpoints(event_service, Session)) return app
def create_app(): """ Returns : 생성된 플라스크 앱 객체 Authors : [email protected] (손수정) [email protected] (이곤호) History : 2020-08-19 ([email protected]) : 초기 생성 2020-08-25 ([email protected]) : AdminProduct 관련 추가 """ app = Flask(__name__) app.json_encoder = CustomJSONEncoder #CORS 설정 CORS(app) #config 설정 app.config.from_pyfile("config.py") # DAO 생성 user_dao = UserDao() order_dao = OrderDao() product_dao = ProductDao() # Service 생성 user_service = UserService(user_dao) order_service = OrderService(order_dao) product_service = ProductService(product_dao) # view blueprint 등록 app.register_blueprint(create_user_endpoints(user_service)) app.register_blueprint(create_admin_user_endpoints(user_service)) app.register_blueprint(create_admin_order_endpoints(order_service)) app.register_blueprint(service_product_endpoint(product_service)) app.register_blueprint(create_admin_product_endpoints(product_service)) app.register_blueprint(create_service_order_endpoints(order_service)) return app
def create_app(test_config=None): app = Flask(__name__) cors = CORS(app, resources={r"/*": {"origins": "*"}}) if test_config is None: app.config.from_pyfile("config.py") else: app.config.update(test_config) database = create_engine(app.config['DB_URL'], encoding='utf-8', max_overflow=0) ## Persistenace Layer user_dao = UserDao(database) store_dao = StoreDao(database) category_dao = CategoryDao(database) menu_dao = MenuDao(database) order_dao = OrderDao(database) ## Business Layer s3_client = boto3.client("s3", aws_access_key_id=app.config['S3_ACCESS_KEY'], aws_secret_access_key=app.config['S3_SECRET_KEY'], region_name='ap-northeast-2') services = Services services.user_service = UserService(user_dao, app.config, s3_client) services.store_service = StoreService(store_dao, app.config, s3_client) services.menu_service = MenuService(menu_dao, app.config, s3_client) services.order_service = OrderService(order_dao) services.category_service = CategoryService(category_dao) socket_io = SocketIO(app, cors_allowed_origins="*") ## 엔드포인트들을 생성 create_endpoints(app, services, socket_io) return app, socket_io
def create_app(test_config=None): app = Flask(__name__) app.debug = True app.json_encoder = CustomJSONEncoder app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # By default, submission of cookies across domains is disabled due to the security implications. CORS(app, resources={r'*': {'origins': '*'}}) if test_config is None: app.config.from_pyfile("config.py") else: app.config.update(test_config) database = app.config['DB'] # persistence Layer destination_dao = DestinationDao() cart_item_dao = CartItemDao() sender_dao = SenderDao() event_dao = EventDao() store_order_dao = StoreOrderDao() order_dao = OrderDao() enquiry_dao = EnquiryDao() seller_shop_dao = SellerShopDao() # admin2 order_detail_dao = OrderDetailDao() seller_info_dao = SellerInfoDao() # service services = Services services.user_service = UserService(app.config) services.destination_service = DestinationService(destination_dao) services.cart_item_service = CartItemService(cart_item_dao) services.store_order_service = StoreOrderService(store_order_dao) services.product_list_service = ProductListService() services.category_list_service = CategoryListService() services.event_list_service = EventListService() services.sender_service = SenderService(sender_dao) services.event_service = EventService(event_dao) services.seller_service = SellerService(app.config) services.bookmark_service = BookmarkService() services.product_enquiry_list_service = ProductEnquiryService() services.seller_shop_service = SellerShopService(seller_shop_dao) services.seller_info_service = SellerInfoService(seller_info_dao) #admin1 services.event_service = EventService(event_dao) services.order_service = OrderService(order_dao) services.order_detail_service = OrderService(order_detail_dao) services.enquiry_service = EnquiryService(enquiry_dao) #admin2 services.seller_service = SellerService(app.config) services.seller_info_service = SellerInfoService(seller_info_dao) services.product_manage_service = ProductManageService() # presentation Layer create_endpoints(app, services, database) return app