Last active
May 20, 2021 18:29
-
-
Save MagnetonBora/b747cb1118823787f15e2e91a946a3f8 to your computer and use it in GitHub Desktop.
Original vs refactored
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
import json | |
import os | |
import pathlib | |
from . import db, config | |
sponsored_id_list = config.get_sponsored() | |
class ProductFinder: | |
def get_product_details(ids=[]): | |
ids.extend(sponsored_id_list) | |
cursor = db.cursor() | |
cursor.execute(''' | |
SELECT product_id, product_manual_data | |
FROM product | |
WHERE product_id IN {} | |
'''.format(tuple(ids))) | |
return cursor.fetchall() | |
tmp = pathlib.Path('/var/lib/app').glob('*') | |
plist = [] | |
for p in tmp: | |
if p.name.endswith('pdf'): | |
plist.insert(0, p) | |
del tmp | |
def render_product_manual(data): | |
d = json.loads(data) | |
if not d['manual_filename']: | |
raise ValueError('Product details have no manual') | |
found_pdf = False | |
for p in plist: | |
if d['manual_filename'] == p: | |
found_pdf = True | |
if not found_pdf: | |
raise ValueError('Product PDF is not found') | |
os.system('convert {} {} /tmp/tmp_image.jpg'.format( | |
d.get('manual_render_params', ''), d['manual_filename'])) | |
try: | |
return open('/tmp/tmp_image.jpg', 'rb') | |
except: | |
pass |
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
import json | |
import os | |
import pathlib | |
from . import db, config | |
sponsored_id_list = config.get_sponsored() | |
class ProductFinder: | |
def __init__(self, connection): | |
self._connection = connection | |
def get_product_details(self, ids, sponsored_ids=None): | |
if sponsored_ids is not None: | |
target_ids = ids + sponsored_ids | |
else: | |
target_ids = ids | |
cursor = self._connection.cursor() | |
sql = self._prepare_sql_statement(target_ids) | |
cursor.execute(sql, target_ids) | |
return cursor.fetchall() | |
@staticmethod | |
def _prepare_sql_statement(ids): | |
place_holders = ",".join("?" * len(ids)) | |
return f""" | |
SELECT product_id, product_manual_data | |
FROM product | |
WHERE product_id IN ({place_holders}) | |
""" | |
tmp = pathlib.Path('/var/lib/app').glob('*') | |
plist = [] | |
for p in tmp: | |
if p.name.endswith('pdf'): | |
plist.insert(0, p) | |
del tmp | |
def render_product_manual(data): | |
d = json.loads(data) | |
if not d['manual_filename']: | |
raise ValueError('Product details have no manual') | |
found_pdf = False | |
for p in plist: | |
if d['manual_filename'] == p: | |
found_pdf = True | |
if not found_pdf: | |
raise ValueError('Product PDF is not found') | |
os.system('convert {} {} /tmp/tmp_image.jpg'.format( | |
d.get('manual_render_params', ''), d['manual_filename'])) | |
try: | |
return open('/tmp/tmp_image.jpg', 'rb') | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment