From 2297d0b5bbc21f6365cb684b0a10ac1968661649 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 18 Feb 2025 12:41:04 +0100 Subject: [PATCH] select your algorithm --- admin/views.py | 3 +- device/models.py | 31 ++++++++++--------- user/migrations/0002_institution_algorithm.py | 18 +++++++++++ user/models.py | 5 ++- 4 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 user/migrations/0002_institution_algorithm.py diff --git a/admin/views.py b/admin/views.py index 345227d..f7755d6 100644 --- a/admin/views.py +++ b/admin/views.py @@ -218,7 +218,8 @@ class InstitutionView(AdminView, UpdateView): "logo", "location", "responsable_person", - "supervisor_person" + "supervisor_person", + "algorithm" ) def get_form_kwargs(self): diff --git a/device/models.py b/device/models.py index 0bd043c..a2c6ad0 100644 --- a/device/models.py +++ b/device/models.py @@ -148,13 +148,13 @@ class Device: ORDER BY CASE WHEN t1.key = 'CUSTOM_ID' THEN 1 - WHEN t1.key = 'ereuse24' THEN 2 - ELSE 3 + WHEN t1.key = '{algorithm}' THEN 2 END, t1.created DESC ) AS row_num FROM evidence_systemproperty AS t1 WHERE t1.owner_id = {institution} + AND t1.key IN ('CUSTOM_ID', '{algorithm}') ) SELECT DISTINCT value @@ -164,6 +164,7 @@ class Device: row_num = 1 """.format( institution=institution.id, + algorithm=institution.algorithm, ) if limit: sql += " limit {} offset {}".format(int(limit), int(offset)) @@ -192,14 +193,14 @@ class Device: ORDER BY CASE WHEN t1.key = 'CUSTOM_ID' THEN 1 - WHEN t1.key = 'ereuse24' THEN 2 - ELSE 3 + WHEN t1.key = '{algorithm}' THEN 2 END, t1.created DESC ) AS row_num FROM evidence_systemproperty AS t1 WHERE t1.owner_id = {institution} + AND t1.key IN ('CUSTOM_ID', '{algorithm}') ) SELECT COUNT(DISTINCT value) @@ -208,7 +209,8 @@ class Device: WHERE row_num = 1 """.format( - institution=institution.id + institution=institution.id, + algorithm=institution.algorithm ) with connection.cursor() as cursor: cursor.execute(sql) @@ -228,8 +230,7 @@ class Device: ORDER BY CASE WHEN t1.key = 'CUSTOM_ID' THEN 1 - WHEN t1.key = 'ereuse24' THEN 2 - ELSE 3 + WHEN t1.key = '{algorithm}' THEN 2 END, t1.created DESC ) AS row_num @@ -237,6 +238,7 @@ class Device: LEFT JOIN lot_devicelot AS t2 ON t1.value = t2.device_id WHERE t2.device_id IS NULL AND t1.owner_id = {institution} + AND t1.key IN ('CUSTOM_ID', '{algorithm}') ) SELECT DISTINCT value @@ -246,6 +248,7 @@ class Device: row_num = 1 """.format( institution=institution.id, + algorithm=institution.algorithm ) if limit: sql += " limit {} offset {}".format(int(limit), int(offset)) @@ -274,8 +277,7 @@ class Device: ORDER BY CASE WHEN t1.key = 'CUSTOM_ID' THEN 1 - WHEN t1.key = 'ereuse24' THEN 2 - ELSE 3 + WHEN t1.key = '{algorithm}' THEN 2 END, t1.created DESC ) AS row_num @@ -283,6 +285,7 @@ class Device: LEFT JOIN lot_devicelot AS t2 ON t1.value = t2.device_id WHERE t2.device_id IS NULL AND t1.owner_id = {institution} + AND t1.key IN ('CUSTOM_ID', '{algorithm}') ) SELECT COUNT(DISTINCT value) @@ -292,6 +295,7 @@ class Device: row_num = 1 """.format( institution=institution.id, + algorithm=institution.algorithm ) with connection.cursor() as cursor: cursor.execute(sql) @@ -309,16 +313,14 @@ class Device: ORDER BY CASE WHEN t1.key = 'CUSTOM_ID' THEN 1 - WHEN t1.key = 'ereuse24' THEN 2 - ELSE 3 + WHEN t1.key = '{algorithm}' THEN 2 END, t1.created DESC ) AS row_num FROM evidence_systemproperty AS t1 - LEFT JOIN lot_devicelot AS t2 ON t1.value = t2.device_id - WHERE t2.device_id IS NULL - AND t1.owner_id = {institution} + WHERE t1.owner_id = {institution} AND t1.uuid = '{uuid}' + AND t1.key IN ('CUSTOM_ID', '{algorithm}') ) SELECT DISTINCT value @@ -329,6 +331,7 @@ class Device: """.format( uuid=uuid.replace("-", ""), institution=institution.id, + algorithm=institution.algorithm, ) properties = [] diff --git a/user/migrations/0002_institution_algorithm.py b/user/migrations/0002_institution_algorithm.py new file mode 100644 index 0000000..3f4660d --- /dev/null +++ b/user/migrations/0002_institution_algorithm.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.6 on 2025-02-18 08:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('user', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='institution', + name='algorithm', + field=models.CharField(choices=[('ereuse24', 'ereuse24'), ('ereuse22', 'ereuse22')], default='ereuse24', max_length=30), + ), + ] diff --git a/user/models.py b/user/models.py index 5fd8424..a289270 100644 --- a/user/models.py +++ b/user/models.py @@ -1,8 +1,10 @@ from django.db import models from django.utils.translation import gettext_lazy as _ from django.contrib.auth.models import BaseUserManager, AbstractBaseUser +from utils.constants import ALGOS -# Create your models here. + +ALGORITHMS = [(x, x) for x in ALGOS.keys()] class Institution(models.Model): @@ -27,6 +29,7 @@ class Institution(models.Model): blank=True, null=True ) + algorithm = models.CharField(max_length=30, choices=ALGORITHMS, default='ereuse24') class UserManager(BaseUserManager):