flask_photo_scaling_app/app/scripts/delete_photo.py

32 lines
1.2 KiB
Python
Raw Normal View History

2021-01-08 14:22:28 -08:00
#!/usr/bin/env python3
import psycopg2
import os
def delete_photo(photo, app_config):
conn = psycopg2.connect(
dbname=app_config['DATABASE_NAME'],
user=app_config['DATABASE_USER'],
2021-01-13 23:47:32 -08:00
host=app_config['DATABASE_HOST'],
2021-01-08 14:22:28 -08:00
password=app_config['DATABASE_PASSWORD']
)
cur = conn.cursor()
cur.execute("SELECT count(id) FROM photo WHERE contributor_id=%s AND id>%s", (photo.contributor_id, photo.id))
if cur.fetchone()[0] == 0:
cur.execute("SELECT id FROM photo WHERE contributor_id=%s ORDER BY id", (photo.contributor_id, ))
else:
cur.execute("SELECT id FROM photo WHERE contributor_id=%s AND id>%s ORDER BY id", (photo.contributor_id, photo.id))
next_photo_id = cur.fetchone()[0]
os.chdir(app_config['PHOTO_SAVE_PATH'])
if os.path.exists('raw_' + photo.photo_name):
os.remove('raw_' + photo.photo_name)
if os.path.exists('1280_' + photo.photo_name):
os.remove('1280_' + photo.photo_name)
if os.path.exists('480_' + photo.photo_name):
os.remove('480_' + photo.photo_name)
cur.execute("DELETE FROM photo WHERE id=%s", (photo.id, ))
conn.commit()
conn.close()
return next_photo_id