mirror of
https://github.com/TrentSPalmer/flask_photo_scaling_app.git
synced 2025-09-05 11:01:33 -07:00
initial commit
This commit is contained in:
26
app/scripts/crop_photo.py
Normal file
26
app/scripts/crop_photo.py
Normal file
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from PIL import Image
|
||||
import os
|
||||
|
||||
|
||||
def crop_photo(photo, photo_save_path):
|
||||
photo_1280 = '1280_' + photo
|
||||
photo_480 = '480_' + photo
|
||||
processing_tuple = ((1280, photo_1280), (480, photo_480))
|
||||
os.chdir(photo_save_path)
|
||||
img = Image.open('raw_' + photo)
|
||||
if img.size[0] > img.size[1]:
|
||||
for x in processing_tuple:
|
||||
basewidth = x[0]
|
||||
wpercent = (basewidth / float(img.size[0]))
|
||||
hsize = int(float(img.size[1] * float(wpercent)))
|
||||
img = img.resize((basewidth, hsize), Image.ANTIALIAS)
|
||||
img.save(x[1])
|
||||
else:
|
||||
for x in processing_tuple:
|
||||
baseheight = x[0]
|
||||
hpercent = (baseheight / float(img.size[1]))
|
||||
wsize = int(float(img.size[0] * float(hpercent)))
|
||||
img = img.resize((wsize, baseheight), Image.ANTIALIAS)
|
||||
img.save(x[1])
|
30
app/scripts/delete_photo.py
Normal file
30
app/scripts/delete_photo.py
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/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'],
|
||||
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
|
94
app/scripts/get_exif_data.py
Normal file
94
app/scripts/get_exif_data.py
Normal file
@@ -0,0 +1,94 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from PIL import Image
|
||||
from PIL.ExifTags import TAGS, GPSTAGS
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
def get_exif_data(photo):
|
||||
exif_data = {}
|
||||
get_file_sizes(photo, exif_data)
|
||||
return(exif_data)
|
||||
|
||||
|
||||
def get_exif(img_raw, exif_data):
|
||||
if hasattr(img_raw, '_getexif'):
|
||||
exifdata = img_raw._getexif()
|
||||
date_format = "%Y:%m:%d %H:%M:%S"
|
||||
for k, v in TAGS.items():
|
||||
if k in exifdata:
|
||||
if v == "Make":
|
||||
exif_data['Make'] = exifdata[k]
|
||||
if v == "Model":
|
||||
exif_data['Model'] = exifdata[k]
|
||||
if v == "Software":
|
||||
exif_data['Software'] = exifdata[k]
|
||||
if v == "DateTime":
|
||||
exif_data['DateTime'] = datetime.strptime(exifdata[k], date_format)
|
||||
if v == "DateTimeOriginal":
|
||||
exif_data['DateTimeOriginal'] = datetime.strptime(exifdata[k], date_format)
|
||||
if v == "DateTimeDigitized":
|
||||
exif_data['DateTimeDigitized'] = datetime.strptime(exifdata[k], date_format)
|
||||
if v == "FNumber":
|
||||
exif_data['fnumber'] = round(exifdata[k][0] / exifdata[k][1], 1)
|
||||
if v == "DigitalZoomRatio":
|
||||
exif_data['DigitalZoomRatio'] = round(exifdata[k][0] / exifdata[k][1], 2)
|
||||
if v == "TimeZoneOffset":
|
||||
exif_data['TimeZoneOffset'] = exifdata[k]
|
||||
if v == "GPSInfo":
|
||||
gpsinfo = {}
|
||||
for h, i in GPSTAGS.items():
|
||||
if h in exifdata[k]:
|
||||
if i == 'GPSAltitudeRef':
|
||||
gpsinfo['GPSAltitudeRef'] = int.from_bytes(exifdata[k][h], "big")
|
||||
if i == 'GPSAltitude':
|
||||
gpsinfo['GPSAltitude'] = round(exifdata[k][h][0] / exifdata[k][h][1], 3)
|
||||
if i == 'GPSLatitudeRef':
|
||||
gpsinfo['GPSLatitudeRef'] = exifdata[k][h]
|
||||
if i == 'GPSLatitude':
|
||||
gpsinfo['GPSLatitude'] = calc_coordinate(exifdata[k][h])
|
||||
if i == 'GPSLongitudeRef':
|
||||
gpsinfo['GPSLongitudeRef'] = exifdata[k][h]
|
||||
if i == 'GPSLongitude':
|
||||
gpsinfo['GPSLongitude'] = calc_coordinate(exifdata[k][h])
|
||||
update_gpsinfo(gpsinfo, exif_data)
|
||||
|
||||
|
||||
def update_gpsinfo(gpsinfo, exif_data):
|
||||
if 'GPSAltitudeRef' in gpsinfo and 'GPSLatitude' in gpsinfo:
|
||||
exif_data['GPSAltitude'] = gpsinfo['GPSAltitude']
|
||||
exif_data['GPSAboveSeaLevel'] = False if gpsinfo['GPSAltitudeRef'] != 0 else True
|
||||
if 'GPSLatitudeRef' in gpsinfo and 'GPSLatitude' in gpsinfo:
|
||||
exif_data['GPSLatitude'] = gpsinfo['GPSLatitude'] if gpsinfo['GPSLatitudeRef'] != 'S' else 0 - gpsinfo['GPSLatitude']
|
||||
if 'GPSLongitudeRef' in gpsinfo and 'GPSLongitude' in gpsinfo:
|
||||
exif_data['GPSLongitude'] = gpsinfo['GPSLongitude'] if gpsinfo['GPSLongitudeRef'] != 'W' else 0 - gpsinfo['GPSLongitude']
|
||||
|
||||
|
||||
def calc_coordinate(x):
|
||||
degrees = x[0][0] / x[0][1]
|
||||
minutes = x[1][0] / x[1][1]
|
||||
seconds = x[2][0] / x[2][1]
|
||||
return round(degrees + minutes / 60 + seconds / 3600, 5)
|
||||
|
||||
|
||||
def get_dimensions_and_format(photo, exif_data):
|
||||
img_raw = Image.open('raw_' + photo)
|
||||
img_1280 = Image.open('1280_' + photo)
|
||||
img_480 = Image.open('480_' + photo)
|
||||
exif_data['AspectRatio'] = round(img_raw.width / img_raw.height, 5)
|
||||
exif_data['photo_format'] = img_raw.format
|
||||
exif_data['photo_width'] = img_raw.width
|
||||
exif_data['photo_height'] = img_raw.height
|
||||
exif_data['photo_1280_width'] = img_1280.width
|
||||
exif_data['photo_1280_height'] = img_1280.height
|
||||
exif_data['photo_480_width'] = img_480.width
|
||||
exif_data['photo_480_height'] = img_480.height
|
||||
get_exif(img_raw, exif_data)
|
||||
|
||||
|
||||
def get_file_sizes(photo, exif_data):
|
||||
exif_data['photo_raw_size'] = os.path.getsize('raw_' + photo)
|
||||
exif_data['photo_1280_size'] = os.path.getsize('1280_' + photo)
|
||||
exif_data['photo_480_size'] = os.path.getsize('480_' + photo)
|
||||
get_dimensions_and_format(photo, exif_data)
|
76
app/scripts/get_photo_list.py
Normal file
76
app/scripts/get_photo_list.py
Normal file
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import psycopg2
|
||||
import psycopg2.extras
|
||||
from shutil import disk_usage
|
||||
|
||||
|
||||
def find_next_previous(photo, app_config):
|
||||
conn = psycopg2.connect(
|
||||
dbname=app_config['DATABASE_NAME'],
|
||||
user=app_config['DATABASE_USER'],
|
||||
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))
|
||||
count = cur.fetchone()[0]
|
||||
if count == 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))
|
||||
photo.next_photo_id = cur.fetchone()[0]
|
||||
cur.execute("SELECT count(id) FROM photo WHERE contributor_id=%s AND id < %s", (photo.contributor_id, photo.id))
|
||||
count = cur.fetchone()[0]
|
||||
if count == 0:
|
||||
cur.execute("SELECT id FROM photo WHERE contributor_id=%s ORDER BY id DESC", (photo.contributor_id, ))
|
||||
else:
|
||||
cur.execute("SELECT id FROM photo WHERE contributor_id=%s AND id < %s ORDER BY id DESC", (photo.contributor_id, photo.id))
|
||||
photo.previous_photo_id = cur.fetchone()[0]
|
||||
conn.close()
|
||||
|
||||
|
||||
def calc_additional_data(photo):
|
||||
photo.UploadDate = photo.timestamp.strftime("%Y-%m-%d %H:%M:%S")
|
||||
if photo.photo_raw_size >= 1048576:
|
||||
photo.SizeOnDisc = str(round(photo.photo_raw_size / 1048576, 1)) + 'M'
|
||||
else:
|
||||
photo.SizeOnDisc = str(round(photo.photo_raw_size / 1024, 1)) + 'K'
|
||||
if photo.photo_1280_size >= 1048576:
|
||||
photo.SizeOnDisc1280 = str(round(photo.photo_1280_size / 1048576, 1)) + 'M'
|
||||
else:
|
||||
photo.SizeOnDisc1280 = str(round(photo.photo_1280_size / 1024, 1)) + 'K'
|
||||
if photo.photo_480_size >= 1048576:
|
||||
photo.SizeOnDisc480 = str(round(photo.photo_480_size / 1048576, 1)) + 'M'
|
||||
else:
|
||||
photo.SizeOnDisc480 = str(round(photo.photo_480_size / 1024, 1)) + 'K'
|
||||
if photo.GPSAltitude is not None:
|
||||
photo.GPSAltitudeFeet = round(photo.GPSAltitude * 3.28084, 1)
|
||||
else:
|
||||
photo.GPSAltitudeFeet = None
|
||||
if photo.GPSLatitude is not None and photo.GPSLongitude is not None:
|
||||
photo.LatLong = "{},{}".format(photo.GPSLatitude, photo.GPSLongitude)
|
||||
photo.MapUrl = "https://www.google.com/maps/search/?api=1&query={}".format(photo.LatLong)
|
||||
else:
|
||||
photo.LatLong, photo.MapUrl = None, None
|
||||
|
||||
|
||||
def get_photo_list(contributor_id, app_config):
|
||||
conn = psycopg2.connect(
|
||||
dbname=app_config['DATABASE_NAME'],
|
||||
user=app_config['DATABASE_USER'],
|
||||
password=app_config['DATABASE_PASSWORD']
|
||||
)
|
||||
cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
|
||||
cur.execute("SELECT photo_name,id FROM photo WHERE contributor_id=%s ORDER BY timestamp,\"DateTimeOriginal\" DESC", (contributor_id, ))
|
||||
photos = cur.fetchall()
|
||||
conn.close()
|
||||
return photos
|
||||
|
||||
|
||||
def get_disk_stats():
|
||||
disk_stats = disk_usage('/')
|
||||
return("Used {}GB of {}GB, {}GB free".format(
|
||||
round(disk_stats.used / 1073741824, 1),
|
||||
round(disk_stats.total / 1073741824, 1),
|
||||
round(disk_stats.free / 1073741824, 1)
|
||||
))
|
162
app/scripts/process_uploaded_photo.py
Normal file
162
app/scripts/process_uploaded_photo.py
Normal file
@@ -0,0 +1,162 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import psycopg2
|
||||
from datetime import datetime
|
||||
from time import time
|
||||
from .crop_photo import crop_photo
|
||||
from .get_exif_data import get_exif_data
|
||||
|
||||
|
||||
def process_uploaded_photo(filename, current_user, app_config):
|
||||
crop_photo(filename, app_config['PHOTO_SAVE_PATH'])
|
||||
exif_data = get_exif_data(filename)
|
||||
conn = psycopg2.connect(
|
||||
dbname=app_config['DATABASE_NAME'],
|
||||
user=app_config['DATABASE_USER'],
|
||||
password=app_config['DATABASE_PASSWORD']
|
||||
)
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT setval('photo_id_seq', (SELECT MAX(id) FROM photo))")
|
||||
conn.commit()
|
||||
cur.execute("SELECT count(id) FROM photo WHERE photo_name=%s", (filename, ))
|
||||
if cur.fetchone()[0] == 0:
|
||||
sql_statement = "INSERT INTO photo("
|
||||
|
||||
sql_statement += "photo_name,"
|
||||
sql_statement += "contributor_id,"
|
||||
sql_statement += "timestamp,"
|
||||
|
||||
sql_statement += "photo_raw_size,"
|
||||
sql_statement += "photo_1280_size,"
|
||||
sql_statement += "photo_480_size,"
|
||||
|
||||
sql_statement += "photo_format,"
|
||||
sql_statement += "photo_width,"
|
||||
sql_statement += "photo_height,"
|
||||
|
||||
sql_statement += "photo_1280_width,"
|
||||
sql_statement += "photo_1280_height,"
|
||||
sql_statement += "photo_480_width,"
|
||||
|
||||
sql_statement += "photo_480_height,"
|
||||
sql_statement += "\"Make\","
|
||||
sql_statement += "\"Model\","
|
||||
|
||||
sql_statement += "\"Software\","
|
||||
sql_statement += "\"DateTime\","
|
||||
sql_statement += "\"DateTimeOriginal\","
|
||||
|
||||
sql_statement += "\"DateTimeDigitized\","
|
||||
sql_statement += "fnumber,"
|
||||
sql_statement += "\"DigitalZoomRatio\","
|
||||
|
||||
sql_statement += "\"AspectRatio\","
|
||||
sql_statement += "\"TimeZoneOffset\","
|
||||
sql_statement += "\"GPSAltitude\","
|
||||
|
||||
sql_statement += "\"GPSAboveSeaLevel\","
|
||||
sql_statement += "\"GPSLatitude\","
|
||||
sql_statement += "\"GPSLongitude\","
|
||||
|
||||
sql_statement += "timestamp_int) "
|
||||
|
||||
sql_statement += "VALUES(%s,%s,%s,%s,%s,%s,"
|
||||
sql_statement += "%s,%s,%s,%s,%s,%s,"
|
||||
sql_statement += "%s,%s,%s,%s,%s,%s,"
|
||||
sql_statement += "%s,%s,%s,%s,%s,%s,"
|
||||
sql_statement += "%s,%s,%s,%s) RETURNING id"
|
||||
cur.execute(sql_statement, (
|
||||
filename,
|
||||
current_user.id,
|
||||
str(datetime.utcnow()),
|
||||
exif_data['photo_raw_size'],
|
||||
exif_data['photo_1280_size'],
|
||||
exif_data['photo_480_size'],
|
||||
exif_data['photo_format'],
|
||||
exif_data['photo_width'],
|
||||
exif_data['photo_height'],
|
||||
exif_data['photo_1280_width'],
|
||||
exif_data['photo_1280_height'],
|
||||
exif_data['photo_480_width'],
|
||||
exif_data['photo_480_height'],
|
||||
exif_data['Make'] if 'Make' in exif_data else None,
|
||||
exif_data['Model'] if 'Model' in exif_data else None,
|
||||
exif_data['Software'] if 'Software' in exif_data else None,
|
||||
exif_data['DateTime'] if 'DateTime' in exif_data else None,
|
||||
exif_data['DateTimeOriginal'] if 'DateTimeOriginal' in exif_data else None,
|
||||
exif_data['DateTimeDigitized'] if 'DateTimeDigitized' in exif_data else None,
|
||||
exif_data['fnumber'] if 'fnumber' in exif_data else None,
|
||||
exif_data['DigitalZoomRatio'] if 'DigitalZoomRatio' in exif_data else None,
|
||||
exif_data['AspectRatio'],
|
||||
exif_data['TimeZoneOffset'] if 'TimeZoneOffset' in exif_data else None,
|
||||
exif_data['GPSAltitude'] if 'GPSAltitude' in exif_data else None,
|
||||
exif_data['GPSAboveSeaLevel'] if 'GPSAboveSeaLevel' in exif_data else None,
|
||||
exif_data['GPSLatitude'] if 'GPSLatitude' in exif_data else None,
|
||||
exif_data['GPSLongitude'] if 'GPSLongitude' in exif_data else None,
|
||||
int(time() * 1000)
|
||||
))
|
||||
photo_id = cur.fetchone()[0]
|
||||
else:
|
||||
cur.execute("SELECT id FROM photo WHERE photo_name=%s", (filename, ))
|
||||
photo_id = cur.fetchone()[0]
|
||||
sql_statement = "UPDATE photo SET "
|
||||
sql_statement += "contributor_id=%s, "
|
||||
sql_statement += "timestamp=%s, "
|
||||
sql_statement += "photo_raw_size=%s, "
|
||||
sql_statement += "photo_1280_size=%s, "
|
||||
sql_statement += "photo_480_size=%s, "
|
||||
sql_statement += "photo_format=%s, "
|
||||
sql_statement += "photo_width=%s, "
|
||||
sql_statement += "photo_height=%s, "
|
||||
sql_statement += "photo_1280_width=%s, "
|
||||
sql_statement += "photo_1280_height=%s, "
|
||||
sql_statement += "photo_480_width=%s, "
|
||||
sql_statement += "photo_480_height=%s, "
|
||||
sql_statement += "\"Make\"=%s, "
|
||||
sql_statement += "\"Model\"=%s, "
|
||||
sql_statement += "\"Software\"=%s, "
|
||||
sql_statement += "\"DateTime\"=%s, "
|
||||
sql_statement += "\"DateTimeOriginal\"=%s, "
|
||||
sql_statement += "\"DateTimeDigitized\"=%s, "
|
||||
sql_statement += "fnumber=%s, "
|
||||
sql_statement += "\"DigitalZoomRatio\"=%s, "
|
||||
sql_statement += "\"AspectRatio\"=%s, "
|
||||
sql_statement += "\"TimeZoneOffset\"=%s, "
|
||||
sql_statement += "\"GPSAltitude\"=%s, "
|
||||
sql_statement += "\"GPSAboveSeaLevel\"=%s, "
|
||||
sql_statement += "\"GPSLatitude\"=%s, "
|
||||
sql_statement += "\"GPSLongitude\"=%s, "
|
||||
sql_statement += "timestamp_int=%s WHERE photo_name=%s"
|
||||
cur.execute(sql_statement, (
|
||||
current_user.id,
|
||||
str(datetime.utcnow()),
|
||||
exif_data['photo_raw_size'],
|
||||
exif_data['photo_1280_size'],
|
||||
exif_data['photo_480_size'],
|
||||
exif_data['photo_format'],
|
||||
exif_data['photo_width'],
|
||||
exif_data['photo_height'],
|
||||
exif_data['photo_1280_width'],
|
||||
exif_data['photo_1280_height'],
|
||||
exif_data['photo_480_width'],
|
||||
exif_data['photo_480_height'],
|
||||
exif_data['Make'] if 'Make' in exif_data else None,
|
||||
exif_data['Model'] if 'Model' in exif_data else None,
|
||||
exif_data['Software'] if 'Software' in exif_data else None,
|
||||
exif_data['DateTime'] if 'DateTime' in exif_data else None,
|
||||
exif_data['DateTimeOriginal'] if 'DateTimeOriginal' in exif_data else None,
|
||||
exif_data['DateTimeDigitized'] if 'DateTimeDigitized' in exif_data else None,
|
||||
exif_data['fnumber'] if 'fnumber' in exif_data else None,
|
||||
exif_data['DigitalZoomRatio'] if 'DigitalZoomRatio' in exif_data else None,
|
||||
exif_data['AspectRatio'],
|
||||
exif_data['TimeZoneOffset'] if 'TimeZoneOffset' in exif_data else None,
|
||||
exif_data['GPSAltitude'] if 'GPSAltitude' in exif_data else None,
|
||||
exif_data['GPSAboveSeaLevel'] if 'GPSAboveSeaLevel' in exif_data else None,
|
||||
exif_data['GPSLatitude'] if 'GPSLatitude' in exif_data else None,
|
||||
exif_data['GPSLongitude'] if 'GPSLongitude' in exif_data else None,
|
||||
int(time() * 1000),
|
||||
filename
|
||||
))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return(photo_id)
|
16
app/scripts/set_contributor_id_seq.py
Normal file
16
app/scripts/set_contributor_id_seq.py
Normal file
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import psycopg2
|
||||
|
||||
|
||||
def set_contributor_id_seq(app_config):
|
||||
conn = psycopg2.connect(
|
||||
dbname=app_config['DATABASE_NAME'],
|
||||
user=app_config['DATABASE_USER'],
|
||||
password=app_config['DATABASE_PASSWORD']
|
||||
)
|
||||
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT setval('contributor_id_seq', (SELECT MAX(id) FROM contributor))")
|
||||
conn.commit()
|
||||
conn.close()
|
56
app/scripts/totp_utils.py
Normal file
56
app/scripts/totp_utils.py
Normal file
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import psycopg2
|
||||
import pyotp
|
||||
import qrcode
|
||||
import qrcode.image.svg
|
||||
from io import BytesIO
|
||||
|
||||
|
||||
def get_totp_qr(contributor, app_config):
|
||||
totp_key = pyotp.random_base32() if contributor.totp_key is None else contributor.totp_key
|
||||
if contributor.totp_key is None:
|
||||
conn = psycopg2.connect(
|
||||
dbname=app_config['DATABASE_NAME'],
|
||||
user=app_config['DATABASE_USER'],
|
||||
password=app_config['DATABASE_PASSWORD']
|
||||
)
|
||||
cur = conn.cursor()
|
||||
cur.execute("UPDATE contributor SET totp_key=%s WHERE id=%s", (totp_key, contributor.id))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
totp_uri = pyotp.totp.TOTP(totp_key).provisioning_uri(name=contributor.email, issuer_name='Photo App')
|
||||
img = qrcode.make(totp_uri, image_factory=qrcode.image.svg.SvgPathImage)
|
||||
f = BytesIO()
|
||||
img.save(f)
|
||||
return(f.getvalue().decode('utf-8'))
|
||||
|
||||
|
||||
def validate_totp(contributor, totp_code, app_config):
|
||||
if pyotp.TOTP(contributor.totp_key).verify(int(totp_code)):
|
||||
conn = psycopg2.connect(
|
||||
dbname=app_config['DATABASE_NAME'],
|
||||
user=app_config['DATABASE_USER'],
|
||||
password=app_config['DATABASE_PASSWORD']
|
||||
)
|
||||
cur = conn.cursor()
|
||||
cur.execute("UPDATE contributor SET use_totp='1' WHERE id=%s", (contributor.id, ))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def disable_2fa(contributor, app_config):
|
||||
conn = psycopg2.connect(
|
||||
dbname=app_config['DATABASE_NAME'],
|
||||
user=app_config['DATABASE_USER'],
|
||||
password=app_config['DATABASE_PASSWORD']
|
||||
)
|
||||
cur = conn.cursor()
|
||||
cur.execute("UPDATE contributor SET use_totp='0', totp_key=Null WHERE id=%s", (contributor.id, ))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return True
|
Reference in New Issue
Block a user