def put(self, coupon_id, seller_id): """ 쿠폰 수정 Args: coupon_id: 수정할 쿠폰 아이디 Returns: 200: 시리얼 넘버를 담은 CSV 파일 리턴 400: 존재하지 않는 쿠폰 아이디로 쿠폰 조회, 딕셔너리 키에러 500: 데이터베이스 조작 에러, 내부 에러, 무결성 에러 Author: 이충희([email protected]) History: 2020-10-11(이충희): 초기 생성 """ try: conn = get_connection() data = request.get_json() coupon_name = data['coupon_name'] description = data['description'] coupon_id = validate_coupon_int_required(coupon_id, 'coupon_id') coupon_name = validate_coupon_str_required(coupon_name, 'coupon_name') description = validate_coupon_str_required(description, 'description') params = {} params['coupon_id'] = coupon_id params['coupon_name'] = coupon_name params['description'] = description self.service.update_coupon_info(conn, params) except (err.OperationalError, err.InternalError, err.IntegrityError) as e: conn.rollback() return jsonify({"errno": e.args[0], "errval": e.args[1]}), 500 except TypeError as e: conn.rollback() return jsonify({"message": e.args[0]}), 400 except KeyError as e: return jsonify({ "message": "KEY_ERROR", "key_name": e.args[0] }), 400 else: conn.commit() return jsonify({"message": "SUCCESS"}), 200 finally: conn.close()
def get(self, coupon_id, seller_id): """ 쿠폰 조회 Args: Returns: 200: 상품 JSON 리턴 400: 쿼리 스트링 유효성 검사 에러 500: 데이터베이스 조작 에러, 내부 에러 Author: 이충희([email protected]) History: 2020-10-10(이충희): 초기 생성 """ try: conn = get_connection() coupon_id = validate_coupon_int_required(coupon_id, 'coupon_id') result = self.service.get_coupon_info(conn, coupon_id) except (err.OperationalError, err.InternalError) as e: return jsonify({"errno": e.args[0], "errval": e.args[1]}), 500 except TypeError as e: return jsonify({"message": e.args[0]}), 400 else: return jsonify(result), 200 finally: conn.close()
def get(self, coupon_id): """ 쿠폰 시리얼 넘버 CSV 다운로드 Args: coupon_id: 쿠폰 아이디 Returns: 200: 시리얼 넘버를 담은 CSV 파일 리턴 400: 존재하지 않는 쿠폰 아이디로 쿠폰 조회 500: 데이터베이스 조작 에러, 내부 에러 Author: 이충희([email protected]) History: 2020-10-11(이충희): 초기 생성 """ try: conn = get_connection() coupon_id = validate_coupon_int_required(coupon_id, 'coupon_id') tmp_filename, download_filename = self.service.download_serials( conn, coupon_id) except (err.OperationalError, err.InternalError) as e: return jsonify({"errno": e.args[0], "errval": e.args[1]}), 500 except TypeError as e: return jsonify({"message": e.args[0]}), 400 else: file_to_send = send_file(tmp_filename, mimetype="text/csv", as_attachment=True, attachment_filename=download_filename, conditional=False) os.remove(tmp_filename) return file_to_send finally: conn.close()
def delete(self, coupon_id, seller_id): """ 쿠폰 삭제 Args: coupon_id: 삭제할 쿠폰 아이디 Returns: 200: 삭제 성공 메시지 400: 존재하지 않는 쿠폰 아이디로 쿠폰 조회 500: 데이터베이스 조작 에러, 내부 에러, 무결성 에러 Author: 이충희([email protected]) History: 2020-10-11(이충희): 초기 생성 """ try: conn = get_connection() coupon_id = validate_coupon_int_required(coupon_id, 'coupon_id') self.service.remove_coupon(conn, coupon_id) except (err.OperationalError, err.InternalError, err.IntegrityError) as e: conn.rollback() return jsonify({"errno": e.args[0], "errval": e.args[1]}), 500 except TypeError as e: return jsonify({"message": e.args[0]}), 400 else: conn.commit() return jsonify({"message": "SUCCESS"}), 200 finally: conn.close()
def get(self, seller_id): """ 쿠폰 조회 Args: Returns: 200: 상품 JSON 리턴 400: 쿼리 스트링 유효성 검사 에러 500: 데이터베이스 조작 에러, 내부 에러 Author: 이충희([email protected]) History: 2020-10-10(이충희): 초기 생성 """ KEY_LIMIT = 'limit' KEY_PAGE = 'page' KEY_ID = 'id' KEY_NAME = 'name' KEY_VALID_STARTED_FROM = 'valid_started_from' KEY_VALID_STARTED_TO = 'valid_started_to' KEY_VALID_ENDED_FROM = 'valid_ended_from' KEY_VALID_ENDED_TO = 'valid_ended_to' KEY_DOWNLOAD_STARTED_FROM = 'download_started_from' KEY_DOWNLOAD_STARTED_TO = 'download_started_to' KEY_DOWNLOAD_ENDED_FROM = 'download_ended_from' KEY_DOWNLOAD_ENDED_TO = 'download_ended_to' KEY_ISSUE_TYPE_ID = 'issue_type_id' KEY_IS_LIMITED = 'is_limited' KEY_OFFSET = 'offset' KEY_HAS_CONDITION = 'has_condition' TIME_DAY_BEGIN = ' 00:00:00' TIME_DAY_END = ' 23:59:59' try: conn = get_connection() limit = request.args.get(KEY_LIMIT, 10) page = request.args.get(KEY_PAGE, 1) coupon_id = request.args.get(KEY_ID, None) coupon_name = request.args.get(KEY_NAME, None) valid_started_from = request.args.get(KEY_VALID_STARTED_FROM, None) valid_started_to = request.args.get(KEY_VALID_STARTED_TO, None) valid_ended_from = request.args.get(KEY_VALID_ENDED_FROM, None) valid_ended_to = request.args.get(KEY_VALID_ENDED_TO, None) download_started_from = request.args.get(KEY_DOWNLOAD_STARTED_FROM, None) download_started_to = request.args.get(KEY_DOWNLOAD_STARTED_TO, None) download_ended_from = request.args.get(KEY_DOWNLOAD_ENDED_FROM, None) download_ended_to = request.args.get(KEY_DOWNLOAD_ENDED_TO, None) issue_type_id = request.args.get(KEY_ISSUE_TYPE_ID, None) is_limited = request.args.get(KEY_IS_LIMITED, None) # Y, N # validation limit = validate_coupon_int_required(limit, KEY_LIMIT) if limit not in [10, 20, 50]: limit = 10 page = validate_coupon_int_required(page, KEY_PAGE) if page <= 0: page = 1 coupon_id = validate_coupon_int_optional(coupon_id, KEY_ID) coupon_name = validate_coupon_str_optional(coupon_name, KEY_NAME) valid_started_from = validate_coupon_date_optional( valid_started_from, KEY_VALID_STARTED_FROM) valid_started_to = validate_coupon_date_optional( valid_started_to, KEY_VALID_STARTED_TO) valid_ended_from = validate_coupon_date_optional( valid_ended_from, KEY_VALID_ENDED_FROM) valid_ended_to = validate_coupon_date_optional( valid_ended_to, KEY_VALID_ENDED_TO) download_started_from = validate_coupon_date_optional( download_started_from, KEY_DOWNLOAD_STARTED_FROM) download_started_to = validate_coupon_date_optional( download_started_to, KEY_DOWNLOAD_STARTED_TO) download_ended_from = validate_coupon_date_optional( download_ended_from, KEY_DOWNLOAD_ENDED_FROM) download_ended_to = validate_coupon_date_optional( download_ended_to, KEY_DOWNLOAD_ENDED_TO) issue_type_id = validate_coupon_int_optional( issue_type_id, KEY_ISSUE_TYPE_ID) is_limited = validate_coupon_bool_optional(is_limited, KEY_IS_LIMITED) # to params params = {} params[KEY_LIMIT] = limit params[KEY_OFFSET] = (page - 1) * limit params[KEY_ID] = coupon_id params[ KEY_NAME] = '%%' + coupon_name + '%%' if coupon_name else coupon_name params[ KEY_VALID_STARTED_FROM] = valid_started_from + TIME_DAY_BEGIN if valid_started_from else valid_started_from params[ KEY_VALID_STARTED_TO] = valid_started_to + TIME_DAY_END if valid_started_to else valid_started_to params[ KEY_VALID_ENDED_FROM] = valid_ended_from + TIME_DAY_BEGIN if valid_ended_from else valid_ended_from params[ KEY_VALID_ENDED_TO] = valid_ended_to + TIME_DAY_END if valid_ended_to else valid_ended_to params[ KEY_DOWNLOAD_STARTED_FROM] = download_started_from + TIME_DAY_BEGIN if download_started_from else download_started_from params[ KEY_DOWNLOAD_STARTED_TO] = download_started_to + TIME_DAY_END if download_started_to else download_started_to params[ KEY_DOWNLOAD_ENDED_FROM] = download_ended_from + TIME_DAY_BEGIN if download_ended_from else download_ended_from params[ KEY_DOWNLOAD_ENDED_TO] = download_ended_to + TIME_DAY_END if download_ended_to else download_ended_to params[KEY_ISSUE_TYPE_ID] = issue_type_id params[KEY_IS_LIMITED] = is_limited params[KEY_HAS_CONDITION] = self.check_has_condition(params) results = self.service.get_coupons(conn, params) except CouponValidationError as e: return jsonify(e.to_dict()), 400 except (err.OperationalError, err.InternalError) as e: return jsonify({"errno": e.args[0], "errval": e.args[1]}), 500 else: return jsonify(results), 200 finally: conn.close()