Fix:
Upload fingerprint
This commit is contained in:
@ -1,3 +1,4 @@
|
|||||||
[flake8]
|
[flake8]
|
||||||
|
ignore = E203
|
||||||
max-line-length = 120
|
max-line-length = 120
|
||||||
exclude = .git,__pycache__,__init__.py,.mypy_cache,.pytest_cache
|
exclude = .git,__pycache__,__init__.py,.mypy_cache,.pytest_cache
|
||||||
|
|||||||
@ -5,7 +5,7 @@ from datetime import date, datetime, time, timedelta
|
|||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status
|
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.dialects.postgresql import insert as pg_insert
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from sqlalchemy.orm import Session
|
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)]
|
paged_data = [prints[i : i + 100] for i in range(0, len(prints), 100)]
|
||||||
for i, page in enumerate(paged_data):
|
for i, page in enumerate(paged_data):
|
||||||
print(f"Processing page {i} of {len(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()
|
db.commit()
|
||||||
return {}
|
return {}
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
@ -60,33 +71,6 @@ def upload_prints(
|
|||||||
raise
|
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):
|
def read_file(input_file: UploadFile):
|
||||||
input_file.seek(0)
|
input_file.seek(0)
|
||||||
output = bytearray()
|
output = bytearray()
|
||||||
@ -115,13 +99,13 @@ def fp(file_data, employees):
|
|||||||
for row in reader:
|
for row in reader:
|
||||||
try:
|
try:
|
||||||
employee_code = int(row[employee_column]) # EnNo
|
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():
|
if employee_code in employees.keys():
|
||||||
fingerprints.append(
|
fingerprints.append(
|
||||||
{
|
{
|
||||||
"id": uuid.uuid4(),
|
"id": uuid.uuid4(),
|
||||||
"employee_id": employees[employee_code],
|
"employee_id": employees[employee_code],
|
||||||
"date": date,
|
"date": date_,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|||||||
Reference in New Issue
Block a user