Skip to content

Instantly share code, notes, and snippets.

@JnyJny
Created November 12, 2019 15:57
Show Gist options
  • Save JnyJny/e9a3c481ece5385f91156248c0d5555c to your computer and use it in GitHub Desktop.
Save JnyJny/e9a3c481ece5385f91156248c0d5555c to your computer and use it in GitHub Desktop.
Horrific Code to Review
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment