Revize 9e22e20c
Přidáno uživatelem David Friesecký před asi 4 roky(ů)
src/dao/certificate_repository.py | ||
---|---|---|
1 |
from typing import Dict |
|
1 |
from typing import Dict, List
|
|
2 | 2 |
from sqlite3 import Connection, Cursor, Error |
3 | 3 |
|
4 | 4 |
from src.db_objects.certificate import Certificate |
... | ... | |
112 | 112 |
else: |
113 | 113 |
return None |
114 | 114 |
|
115 |
def read_all(self, filter_type: int = None): |
|
116 |
""" |
|
117 |
Reads (selects) all certificates (with type). |
|
118 |
|
|
119 |
:param filter_type: ID of certificate type from CertificateTypes table |
|
120 |
|
|
121 |
:return: list of certificates |
|
122 |
""" |
|
123 |
|
|
124 |
try: |
|
125 |
sql_extension = "" |
|
126 |
values = [] |
|
127 |
if filter_type is not None: |
|
128 |
sql_extension = (f" WHERE {COL_TYPE_ID} = (" |
|
129 |
f"SELECT {COL_ID} FROM {TAB_CERTIFICATE_TYPES} WHERE {COL_ID} = ?") |
|
130 |
values = [filter_type] |
|
131 |
|
|
132 |
sql = (f"SELECT * FROM {TAB_CERTIFICATES}{sql_extension}") |
|
133 |
self.cursor.execute(sql, values) |
|
134 |
certificate_rows = self.cursor.fetchall() |
|
135 |
|
|
136 |
certificates: List[Certificate] = [] |
|
137 |
for certificate_row in certificate_rows: |
|
138 |
sql = (f"SELECT * FROM {TAB_CERTIFICATE_USAGES} " |
|
139 |
f"WHERE {COL_CERTIFICATE_ID} = ?") |
|
140 |
values = [certificate_row[0]] |
|
141 |
self.cursor.execute(sql, values) |
|
142 |
usage_rows = self.cursor.fetchall() |
|
143 |
|
|
144 |
usage_dict: Dict[int, bool] = {} |
|
145 |
for usage_row in usage_rows: |
|
146 |
usage_dict[usage_row[2]] = True |
|
147 |
|
|
148 |
certificates.append(Certificate(certificate_row[0], |
|
149 |
certificate_row[1], |
|
150 |
certificate_row[2], |
|
151 |
certificate_row[3], |
|
152 |
certificate_row[4], |
|
153 |
certificate_row[5], |
|
154 |
certificate_row[6], |
|
155 |
certificate_row[7], |
|
156 |
usage_dict)) |
|
157 |
except Error as e: |
|
158 |
print(e) |
|
159 |
return None |
|
160 |
|
|
161 |
if len(certificates) > 0: |
|
162 |
return certificates |
|
163 |
else: |
|
164 |
return None |
|
165 |
|
|
115 | 166 |
def update(self, certificate_id: int, certificate: Certificate) -> bool: |
116 | 167 |
""" |
117 | 168 |
Updates a certificate. |
Také k dispozici: Unified diff
Re #8471 - Implemented filter of certificates
- Implemented filter by certificate type using index (id) from CertificateTypes table
- No multiple types supported