Upload fingerprint
This commit is contained in:
Amritanshu Agrawal 2020-11-06 12:29:52 +05:30
parent 0f6484cd2f
commit ad88dbf0d0
2 changed files with 16 additions and 31 deletions

View File

@ -1,3 +1,4 @@
[flake8]
ignore = E203
max-line-length = 120
exclude = .git,__pycache__,__init__.py,.mypy_cache,.pytest_cache

View File

@ -5,7 +5,7 @@ from datetime import date, datetime, time, timedelta
from io import StringIO
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status
from sqlalchemy import and_, bindparam, exists, select
from sqlalchemy import bindparam
from sqlalchemy.dialects.postgresql import insert as pg_insert
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
@ -46,7 +46,18 @@ def upload_prints(
paged_data = [prints[i : i + 100] for i in range(0, len(prints), 100)]
for i, page in enumerate(paged_data):
print(f"Processing page {i} of {len(paged_data)}")
db.execute(get_query(9.4), page)
db.execute(
pg_insert(Fingerprint)
.values(
{
"FingerprintID": bindparam("id"),
"EmployeeID": bindparam("employee_id"),
"Date": bindparam("date"),
}
)
.on_conflict_do_nothing(),
page,
)
db.commit()
return {}
except SQLAlchemyError as e:
@ -60,33 +71,6 @@ def upload_prints(
raise
def get_query(version):
if version == 9.5:
return (
pg_insert(Fingerprint)
.values(
{
"FingerprintID": bindparam("id"),
"EmployeeID": bindparam("employee_id"),
"Date": bindparam("date"),
}
)
.on_conflict_do_nothing()
)
else:
sel = select([bindparam("id"), bindparam("employee_id"), bindparam("date")]).where(
~exists([Fingerprint.id]).where(
and_(
Fingerprint.employee_id == bindparam("employee_id"),
Fingerprint.date == bindparam("date"),
)
)
)
return Fingerprint.__table__.insert().from_select(
[Fingerprint.id, Fingerprint.employee_id, Fingerprint.date], sel
)
def read_file(input_file: UploadFile):
input_file.seek(0)
output = bytearray()
@ -115,13 +99,13 @@ def fp(file_data, employees):
for row in reader:
try:
employee_code = int(row[employee_column]) # EnNo
date = datetime.datetime.strptime(row[date_column], date_format)
date_ = datetime.strptime(row[date_column], date_format)
if employee_code in employees.keys():
fingerprints.append(
{
"id": uuid.uuid4(),
"employee_id": employees[employee_code],
"date": date,
"date": date_,
}
)
except ValueError: