From ad88dbf0d07266d9db12ecca43306890da7811a9 Mon Sep 17 00:00:00 2001 From: tanshu Date: Fri, 6 Nov 2020 12:29:52 +0530 Subject: [PATCH] Fix: Upload fingerprint --- brewman/.flake8 | 1 + brewman/brewman/routers/fingerprint.py | 46 +++++++++----------------- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/brewman/.flake8 b/brewman/.flake8 index 1e468919..01ee7607 100644 --- a/brewman/.flake8 +++ b/brewman/.flake8 @@ -1,3 +1,4 @@ [flake8] +ignore = E203 max-line-length = 120 exclude = .git,__pycache__,__init__.py,.mypy_cache,.pytest_cache diff --git a/brewman/brewman/routers/fingerprint.py b/brewman/brewman/routers/fingerprint.py index 9a9dcb1c..e2a14c88 100644 --- a/brewman/brewman/routers/fingerprint.py +++ b/brewman/brewman/routers/fingerprint.py @@ -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: