db안에 company_name을 가져오고싶을때?
model
# Dispenser QR API model file
# made : hanwh@hunature.net
# date : 2021-04-29
from flask import session
import app.utils.constants as const
from app.models import connect_database
from app.utils.query import get_qr_code_no
def query_qr_by_uuid(qr_uuid):
""" qr_uuid가 일치하는 QR 정보를 조회하여 반환합니다.
Args:
qr_uuid ([string]): 조회할 QR의 uuid 값
Returns:
[dict]: API 결과 형식의 dict 객체 = { 'resultCode': 200, 'resultMsg': 'success', 'resultData': {} }
"""
if qr_uuid == None or qr_uuid == '':
return { 'resultCode':400, 'resultMsg': 'bad request by qr_uuid!' }
database = connect_database()
connection = None
cursor = None
if database == None:
# 데이터베이스 연결 실패 오류 처리 필요
return { 'resultCode':500, 'resultMsg': 'database engine fail!' }
try:
connection = database.raw_connection()
except:
return { 'resultCode':500, 'resultMsg': 'database connection fail!' }
try:
cursor = connection.cursor() # get DISPENSER_V2 mariadb cursor
except:
return { 'resultCode':500, 'resultMsg': 'database cursor fail!' }
query = f"""
SELECT
C.qr_code_no
, M.qr_name
, M.qr_desc
, DATE_FORMAT(IFNULL(M.reg_date, C.reg_date), '%Y-%m-%d %H:%i') AS reg_date
, C.qr_uuid
, M.member_qr_no
, M.member_no
, C.serial_code
, R.company_name
FROM QR_CODE AS C
INNER JOIN QR_CODE_MEMBER AS M ON C.qr_code_no = M.qr_code_no
AND M.is_deleted = 0
-- AND M.is_deleted = 0
LEFT OUTER JOIN MEMBER AS R ON R.member_no = M.member_no
WHERE 1 = 1
AND C.qr_uuid = %s
ORDER BY M.member_qr_no DESC
LIMIT 1;
; """
try:
cursor.execute(query, (qr_uuid, ))
except:
return { 'resultCode':500, 'resultMsg': 'query execution fail : qr info' }
result = [dict((cursor.description[i][0], value) for i, value in enumerate(row)) \
for row in cursor.fetchall()]
if len(result) > 0:
return { 'resultCode': 200, 'resultMsg': 'success', 'resultData': result[0] }
else:
return { 'resultCode': 404, 'resultMsg': 'not found' }
controller
@buyer.route('/show_qr_poster/<string:qr_uuid>', methods=['Get'])
def show_qr_poster(qr_uuid):
company_name_test = query_qr_by_uuid(qr_uuid)
session['company_name'] = company_name_test.get('resultData').get(const.COMPANY_NAME)
return render_template('poster.html', qr_uuid = qr_uuid)
@buyer.route('/download_qr_poster/<string:qr_uuid>', methods=['Get'])
def download_qr_poster(qr_uuid):
# member_no = session.get(const.MEMBER_NO)
company_name_test = query_qr_by_uuid(qr_uuid)
session['company_name'] = company_name_test.get('resultData').get(const.COMPANY_NAME)
path_wkthmltopdf = current_app.config['PATH_WKTHMLTOPDF'] # config.PATH_WKTHMLTOPDF
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
options = {
'quiet':'',
'margin-bottom':1,
'margin-left':1,
'margin-right':1,
'margin-top':1,
'page-height':226,
'page-width':160,
'dpi': 72,
'disable-smart-shrinking':'',
}
#html = render_template("poster.html", qr_uuid = qr_uuid)
#pdf = pdfkit.from_string(html, False, configuration=config)
pdf = pdfkit.from_url(request.host_url + '/show_qr_poster/' + qr_uuid, False, configuration=config, options = options)
response = make_response(pdf)
response.headers["Content-Type"] = "application/pdf"
response.headers["Content-Disposition"] = "inline; filename=QR_check_poster.pdf"
return response
templates
{% if session.get('company_name') == none %}
<img id="logo_3" src="/static/img/logo.png">
{% else %}
<center id="company_name" style="font-size: 30px; text-align: center; color: #2f363d; font-weight: 600;">{{
session.get('company_name') }}</center>
{% endif %}
반응형
'Backend' 카테고리의 다른 글
파이썬 빠른테스트 (0) | 2021.07.07 |
---|---|
[flask] mvc패턴에따른 딕셔너리 활용 (0) | 2021.06.28 |
[해결] 경로문제 python could not be resolved (0) | 2021.06.28 |
[요류][해결중]'pip'은(는) 내부 또는 외부 명령, 실행할 수 있는 프로그램, 또는 배치파일이 아닙니다. (0) | 2021.06.21 |
[오류][해결] ModuleNotFoundError: No module named 'PIL' (0) | 2021.06.21 |