-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcrud.py
767 lines (660 loc) · 28.8 KB
/
crud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
from typing import Callable, Dict, Tuple
from collections import defaultdict, Counter
from itertools import groupby
from fastapi_pagination import Params, Page
from fastapi_pagination.api import create_page
from fastapi_pagination.ext.sqlalchemy import paginate_query
from psycopg2.errors import ForeignKeyViolation
from sqlalchemy import func, distinct, column, asc, desc, or_, select, update
from sqlalchemy.orm import Session, aliased
from sqlalchemy.exc import IntegrityError, NoResultFound
from sqlalchemy.sql.expression import delete, intersect
from sqlalchemy.sql.selectable import CompoundSelect
from .config import DEFAULT_PARAMS
from .enum import FilterEnum
from .models import (
AttrType,
Attribute,
AttributeDefinition,
Schema,
Value
)
from .schemas import (
AttrDefSchema,
AttrTypeMapping,
EntityBaseSchema,
SchemaCreateSchema,
SchemaUpdateSchema,
AttributeCreateSchema
)
from .exceptions import *
from .utils import iterate_model_fields
RESERVED_ATTR_NAMES = ['id', 'slug', 'deleted', 'name']
def get_attributes(db: Session) -> List[Attribute]:
return db.execute(select(Attribute)).scalars().all()
def get_attribute(db: Session, attr_id: int) -> Attribute:
attr = db.execute(select(Attribute).where(Attribute.id == attr_id)).scalar()
if attr is None:
raise MissingAttributeException(obj_id=attr_id)
return attr
def create_attribute(db: Session, data: AttributeCreateSchema, commit: bool = True) -> Attribute:
if data.name in RESERVED_ATTR_NAMES:
raise ReservedAttributeException(attr_name=data.name, reserved=RESERVED_ATTR_NAMES)
try:
attr = db.query(Attribute).filter(Attribute.name == data.name,
Attribute.type == AttrType[data.type.value]).one()
except NoResultFound:
pass
else:
return attr
a = Attribute(name=data.name, type=AttrType[data.type.value])
db.add(a)
db.commit() if commit else db.flush([a])
return a
def get_schemas(db: Session, all: bool = False, deleted_only: bool = False) -> List[Schema]:
q = select(Schema)
if not all:
q = q.where(Schema.deleted == deleted_only)
return db.execute(q).scalars().all()
def get_schema(db: Session, id_or_slug: Union[int, str]) -> Schema:
q = select(Schema)
if isinstance(id_or_slug, int):
q = q.where(Schema.id == id_or_slug)
else:
q = q.where(Schema.slug == id_or_slug)
schema = db.execute(q).scalar()
if schema is None:
raise MissingSchemaException(obj_id=id_or_slug)
return schema
def _check_bound_schema_id(db: Session, schema_id: int):
try:
schema = db.query(Schema).filter(Schema.id == schema_id).one()
except NoResultFound:
raise MissingSchemaException(obj_id=schema_id)
if schema.deleted:
raise SchemaIsDeletedException(obj_id=schema_id)
def create_schema(db: Session, data: SchemaCreateSchema, commit: bool = True) -> Schema:
try:
sch = Schema(name=data.name, slug=data.slug, reviewable=data.reviewable)
db.add(sch)
db.flush()
except IntegrityError:
db.rollback()
raise SchemaExistsException(name=data.name, slug=data.slug)
try:
attr_names = set()
for attr in data.attributes:
a = create_attribute(db, attr, commit=False)
db.flush()
if a.name in attr_names:
raise MultipleAttributeOccurencesException(a.name)
attr_names.add(a.name)
if a.type == AttrType.FK:
if attr.bound_schema_id == -1:
attr.bound_schema_id = sch.id
_check_bound_schema_id(db=db, schema_id=attr.bound_schema_id)
ad = AttributeDefinition(
attribute_id=a.id,
schema_id=sch.id,
required=attr.required,
list=attr.list,
unique=attr.unique,
key=attr.key,
description=attr.description,
bound_schema_id=attr.bound_schema_id
)
db.add(ad)
db.flush()
if commit:
db.commit()
else:
db.flush()
except IntegrityError:
db.rollback()
raise SchemaExistsException(name=data.name, slug=data.slug)
except:
db.rollback()
raise
return sch
def delete_schema(db: Session, id_or_slug: Union[int, str], commit: bool = True) -> Schema:
q = select(Schema)
if isinstance(id_or_slug, int):
q = q.where(Schema.id == id_or_slug)
else:
q = q.where(Schema.slug == id_or_slug)
schema = db.execute(q).scalar()
if schema is None:
raise MissingSchemaException(obj_id=id_or_slug)
if schema.deleted:
raise NoOpChangeException(f"Schema with id {schema.id} is already deleted")
db.execute(update(Entity)
.where(Entity.schema_id == schema.id, Entity.deleted == False)
.values(deleted=True))
schema.deleted = True
if commit:
db.commit()
else:
db.flush()
return schema
def _delete_attr_from_schema(db: Session, attr_def: AttributeDefinition, schema: Schema):
ValueModel = attr_def.attribute.type.value.model
db.execute(delete(AttributeDefinition).where(AttributeDefinition.id == attr_def.id))
db.execute(delete(ValueModel)
.where(ValueModel.attribute_id == attr_def.attribute_id)
.where(ValueModel.entity_id == Entity.id)
.where(Entity.schema_id == schema.id)
.execution_options(synchronize_session=False)
)
def _update_attr_in_schema(db: Session, attr_upd: AttrDefSchema, attr_def: AttributeDefinition):
if attr_def.list and not attr_upd.list:
raise ListedToUnlistedException(attr_def_id=attr_def.id)
attr_def.required = attr_upd.required
attr_def.unique = False if attr_upd.list else attr_upd.unique
attr_def.list = attr_upd.list
attr_def.key = attr_upd.key
attr_def.description = attr_upd.description
if attr_upd.name != attr_def.attribute.name:
new_attr = create_attribute(
db=db,
data=AttributeCreateSchema(name=attr_upd.name, type=attr_def.attribute.type.name),
commit=False
)
ValueModel = new_attr.type.value.model
entity_ids = db.query(Entity.id).filter(Entity.schema_id == attr_def.schema_id).subquery()
db.query(ValueModel)\
.filter(ValueModel.entity_id.in_(entity_ids),
ValueModel.attribute_id == attr_def.attribute_id)\
.update({"attribute_id": new_attr.id}, synchronize_session=False)
attr_def.attribute = new_attr
def _add_attr_to_schema(db: Session, attr_schema: AttrDefSchema, schema: Schema):
attribute = create_attribute(db, attr_schema, commit=False)
db.flush()
bound_schema_id = attr_schema.bound_schema_id if attr_schema.bound_schema_id != -1 else schema.id
if bound_schema_id is not None and bound_schema_id != schema.id:
_check_bound_schema_id(db=db, schema_id=bound_schema_id)
try:
attr_def = AttributeDefinition(
attribute_id=attribute.id,
schema_id=schema.id,
required=attr_schema.required,
list=attr_schema.list,
unique=attr_schema.unique if not attr_schema.list else False,
key=attr_schema.key,
description=attr_schema.description,
bound_schema_id=bound_schema_id
)
db.add(attr_def)
db.flush()
except IntegrityError as error:
db.rollback()
if isinstance(error.orig, ForeignKeyViolation):
raise MissingSchemaException(obj_id=attr_schema.bound_schema_id)
raise AttributeAlreadyDefinedException(attr_id=attribute.id, schema_id=schema.id)
def sort_attribute_definitions(schema: Schema, definitions: List[AttrDefSchema])\
-> Tuple[List[AttrDefSchema], List[AttrDefSchema], List[AttributeDefinition]]:
existing_defs = {x.id: x for x in schema.attr_defs}
new, updated = [], []
fields = iterate_model_fields(AttributeDefinition)
skipped_fields = ["schema_id", "attribute_id", "name", "type", "bound_schema_id"]
for attr_def in definitions:
if not getattr(attr_def, "id", None):
new.append(attr_def)
continue
existing = existing_defs.get(attr_def.id, None)
if existing is None:
raise AttributeNotDefinedException(attr_id=attr_def.id, schema_id=schema.id)
if existing.attribute.name != attr_def.name:
updated.append(attr_def)
continue
bound_schema_id = attr_def.bound_schema_id if attr_def.bound_schema_id != -1 else existing.bound_schema_id
if bound_schema_id != existing.bound_schema_id:
updated.append(attr_def)
continue
if any(getattr(attr_def, field) != getattr(existing, field)
for field in fields
if field not in skipped_fields):
updated.append(attr_def)
continue
for field in skipped_fields:
if field in ["name", "type"]:
old_value = getattr(existing.attribute, field)
else:
old_value = getattr(existing, field)
new_value = getattr(attr_def, field, None)
if isinstance(new_value, (AttrType, AttrTypeMapping)):
new_value = new_value.name
if isinstance(old_value, (AttrType, AttrTypeMapping)):
old_value = old_value.name
if new_value is not None and new_value != old_value:
raise InvalidAttributeChange(attr_id=existing.attribute_id, schema_id=schema.id,
field=field)
submitted_ids = {a.id for a in definitions}
deleted = [a for a in existing_defs.values() if a.id not in submitted_ids]
return new, updated, deleted
def update_schema(db: Session, id_or_slug: Union[int, str], data: SchemaUpdateSchema,
commit: bool = True) -> Schema:
schema = get_schema(db=db, id_or_slug=id_or_slug)
if schema.deleted:
raise MissingSchemaException(obj_id=id_or_slug)
duplicate_attr_names = [name
for name, count in Counter(a.name for a in data.attributes).items()
if count > 1]
if duplicate_attr_names:
raise MultipleAttributeOccurencesException(attr_name=duplicate_attr_names[0])
try:
result = db.execute(
update(Schema)
.where(Schema.id == schema.id,
or_(Schema.name != data.name, Schema.slug != data.slug,
Schema.reviewable != (data.reviewable or Schema.reviewable.default.arg)))
.values(
name=data.name or schema.name,
slug=data.slug or schema.slug,
reviewable=data.reviewable if data.reviewable is not None else schema.reviewable)
)
except IntegrityError:
db.rollback()
raise SchemaExistsException(name=data.name, slug=data.slug)
added, updated, deleted = sort_attribute_definitions(schema=schema, definitions=data.attributes)
# Are there meaningful changes?
intersect_update_delete = {(d.name, d.type.name) for d in updated} & {(d.attribute.name, d.attribute.type.name) for d in deleted}
intersect_add_delete = {(d.name, d.type.name) for d in added} & {(d.attribute.name, d.attribute.type.name) for d in deleted}
if result.rowcount == 0 and intersect_update_delete | intersect_add_delete:
raise NoOpChangeException("Trivial change: Attempt to add/update and delete an attribute at"
" the same time")
attr_def_names: Dict[int, AttributeDefinition] = {i.id: i for i in schema.attr_defs}
for attr_def in deleted:
_delete_attr_from_schema(db=db, attr_def=attr_def, schema=schema)
for attr in updated:
attr_def = attr_def_names.get(attr.id)
if attr_def is None:
db.rollback()
raise AttributeNotDefinedException(attr_id=attr.id, schema_id=schema.id)
_update_attr_in_schema(db=db, attr_upd=attr, attr_def=attr_def)
db.flush()
for attr in added:
_add_attr_to_schema(db=db, attr_schema=attr, schema=schema)
try:
if commit:
db.commit()
else:
db.flush()
except IntegrityError:
db.rollback()
raise SchemaExistsException(name=data.name, slug=data.slug)
return schema
def _get_entity_data(db: Session, entity: Entity, attr_names: List[str]) -> Dict[str, Any]:
data = {'id': entity.id, 'slug': entity.slug, 'deleted': entity.deleted, 'name': entity.name}
for attr in attr_names:
val_obj = entity.get(attr, db)
try:
if isinstance(val_obj, list):
data[attr] = [i.value for i in val_obj]
else:
data[attr] = val_obj.value
except AttributeError:
data[attr] = None
return data
def _get_attr_values_batch(db: Session, entities: List[Entity], attrs_to_include: List[AttributeDefinition]) -> List[dict]:
'''Gets attr. values for list of entities by splitting attrs in
groups by type to select multiple attributes for all entities
in one query
'''
results_map = {
entity.id: {
'id': entity.id,
'slug': entity.slug,
'deleted': entity.deleted,
'name': entity.name
}
for entity in entities
}
for i in results_map.values():
for attr_def in attrs_to_include:
if attr_def.list:
i.update({attr_def.attribute.name: []})
else:
i.update({attr_def.attribute.name: None})
attr_groups: Dict[str, List[Attribute]] = defaultdict(list)
attributes = [i.attribute for i in attrs_to_include]
for attr in attributes:
attr_groups[attr.type.name].append(attr)
ent_ids = [i.id for i in entities]
attr_map = {i.attribute.id: i.attribute.name for i in attrs_to_include}
for group, attrs in attr_groups.items():
value_model: Value = AttrType[group].value.model
q = (
select(value_model)
.where(value_model.entity_id.in_(ent_ids))
.where(value_model.attribute_id.in_([i.id for i in attrs]))
)
rows = db.execute(q).scalars().all()
for r in rows:
ent: dict = results_map[r.entity_id]
attr: str = attr_map[r.attribute_id]
if isinstance(ent[attr], list):
ent[attr].append(r.value)
else:
ent[attr] = r.value
for entity in results_map.values():
for attr in entity:
if isinstance(entity[attr], list):
entity[attr].sort()
results = list(results_map.values())
return results
def _parse_filters(filters: dict, attrs: List[str]) \
-> Tuple[Dict[str, Dict[FilterEnum, Any]], Dict[FilterEnum, Any]]:
'''
Returns tuple of two `dict`s like `{attr_name: {op1: value, op2: value}}`.
First `dict` is for attribute filters, second is for `Entity.name` filters
'''
filter_map = {f.value.name: f for f in FilterEnum}
entity_fields = ('name', 'slug')
attr_filters = defaultdict(dict)
entity_filters = defaultdict(dict)
for f, v in filters.items():
split = f.rsplit('.', maxsplit=1)
attr = split[0]
filter = FilterEnum.EQ if len(split) == 1 else filter_map.get(split[-1], None)
if attr not in entity_fields and attr not in attrs:
raise InvalidFilterAttributeException(attr=attr, allowed_attrs=attrs)
if not filter:
raise InvalidFilterOperatorException(attr=attr, filter=split[-1])
if attr in entity_fields:
entity_filters[attr][filter] = v
else:
attr_filters[attr][filter] = v
return attr_filters, entity_filters
def _query_entity_with_filters(filters: dict, schema: Schema, all: bool = False,
deleted_only: bool = False) -> CompoundSelect:
'''
Returns intersection query of several queries with filters
to get entities that satisfy all conditions from `filters`
'''
attrs = {i.attribute.name: i.attribute
for i in schema.attr_defs if i.attribute.type.value.filters}
attr_filters, entity_filters = _parse_filters(filters=filters, attrs=attrs.keys())
selects = []
# Add filters for entity model
if entity_filters:
q = select(Entity).where(Entity.schema_id == schema.id)
if not all:
q = q.where(Entity.deleted == deleted_only)
for field_name, filters in entity_filters.items():
for f, v in filters.items():
field = getattr(Entity, field_name, None)
if field is None:
raise AttributeError(f"Entity has no field {field_name}")
q = q.where(getattr(Entity.name, f.value.op)(v))
selects.append(q)
# Add filters for attribute values
for attr_name, filters in attr_filters.items():
attr = attrs[attr_name]
value_model = attr.type.value.model
q = select(Entity).where(Entity.schema_id == schema.id).join(value_model)
if not all:
q = q.where(Entity.deleted == deleted_only)
for filter, value in filters.items():
q = q.where(getattr(value_model.value, filter.value.op)(value))
q = q.where(value_model.attribute_id == attr.id)
selects.append(q)
return intersect(*selects)
def get_entities(
db: Session,
schema: Schema,
params: Params = DEFAULT_PARAMS,
all: bool = False,
deleted_only: bool = False,
all_fields: bool = False,
filters: dict = None,
order_by: str = 'name',
ascending: bool = True,
) -> Page[EntityBaseSchema]:
if order_by != 'name':
attrs = [i for i in schema.attr_defs if i.attribute.name == order_by]
if not attrs:
raise AttributeNotDefinedException(order_by, schema.id)
if filters:
q = _query_entity_with_filters(filters=filters, schema=schema, all=all, deleted_only=deleted_only)
else:
q = select(Entity).where(Entity.schema_id == schema.id)
if not all:
q = q.where(Entity.deleted == deleted_only)
total = db.execute(select(func.count(distinct(column('id')))).select_from(q.subquery())).scalar()
try:
queries = q.selects
q1 = queries[0]
sub = q1.subquery(name='anon_1')
from_ = Entity.__table__.join(sub, Entity.id == sub.c.id)
for idx, i in enumerate(queries[1:]):
sub = i.subquery(name=f'anon_{idx+2}')
from_ = from_.join(sub, Entity.id == sub.c.id)
q = select(Entity).select_from(from_)
except AttributeError:
pass
if order_by != 'name':
attrs = {i.attribute.name: i.attribute for i in schema.attr_defs}
attr = attrs[order_by]
value_model = attr.type.value.model
direction = asc if ascending else desc
outer_query = subquery = (select(value_model.value)
.where(value_model.attribute_id == attr.id)
.where(value_model.entity_id == Entity.id)
.scalar_subquery()
.correlate(Entity))
if attr.type == AttrType.FK:
attr_defs = {i.attribute.name: i for i in schema.attr_defs}
attr_def = attr_defs[order_by]
e_alias = aliased(Entity)
outer_query = (select(e_alias.name)
.where(e_alias.schema_id == attr_def.bound_schema_id,
e_alias.id == subquery)
.scalar_subquery())
q = q.order_by(direction(outer_query), Entity.name.asc())
else:
direction = 'asc' if ascending else 'desc'
q = q.order_by(getattr(Entity.name, direction)())
q = paginate_query(q, params)
entities = list(db.execute(select(Entity).from_statement(q)).scalars().all())
attr_defs = schema.attr_defs if all_fields else [i for i in schema.attr_defs
if i.key or i.attribute.name == order_by]
entities = _get_attr_values_batch(db, entities, attr_defs)
return create_page(entities, total, params)
def get_entity_by_id(db: Session, entity_id: int) -> Entity:
entity = db.execute(select(Entity).where(Entity.id == entity_id)).scalar()
if entity is None:
raise MissingEntityException(obj_id=entity_id)
return entity
def get_entity_model(db: Session, id_or_slug: Union[int, str], schema: Schema) -> Entity:
q = select(Entity).where(Entity.schema_id == schema.id)
if isinstance(id_or_slug, int):
filter = Entity.id == id_or_slug
else:
filter = Entity.slug == id_or_slug
e = db.execute(q.where(filter)).scalar()
if e is None:
raise MissingEntityException(obj_id=id_or_slug)
return e
def get_entity(db: Session, id_or_slug: Union[int, str], schema: Schema) -> dict:
e = get_entity_model(db=db, id_or_slug=id_or_slug, schema=schema)
attrs = [i.attribute.name for i in schema.attr_defs]
return _get_entity_data(db=db, entity=e, attr_names=attrs)
def _convert_values(attr_def: AttributeDefinition, value: Any, caster: Callable) -> List[Any]:
if isinstance(value, list):
if not attr_def.list:
raise NotListedAttributeException(attr_name=attr_def.attribute.name, schema_id=attr_def.schema_id)
return [caster(i) for i in value if i is not None]
else:
return [caster(value)] if value is not None else []
def _check_fk_value(db: Session, attr_def: AttributeDefinition, entity_ids: List[int]):
entities = {e.id: e
for e in db.query(Entity).filter(Entity.id.in_(entity_ids),
Entity.deleted == False)}
for id_ in entity_ids:
entity = entities.get(id_, None)
if entity is None:
raise MissingEntityException(obj_id=id_)
if entity.schema_id != attr_def.bound_schema_id:
raise WrongSchemaToBindException(
attr_name=attr_def.attribute.name,
schema_id=attr_def.schema_id,
bound_schema_id=attr_def.bound_schema_id,
passed_entity=entity
)
def _check_unique_value(db: Session, attr_def: AttributeDefinition, model: Value, value: Any):
existing = db.execute(
select(model)
.where(model.attribute_id == attr_def.attribute_id)
.where(Entity.schema_id == attr_def.schema_id)
.where(model.value == value)
.join(Entity, Entity.id == model.entity_id)
).scalars().all()
if existing:
for val in existing:
if not val.entity.deleted:
raise UniqueValueException(attr_name=attr_def.attribute.name, schema_id=attr_def.schema_id, value=val.value)
def create_entity(db: Session, schema_id: int, data: dict, commit: bool = True) -> Entity:
sch: Schema = db.execute(
select(Schema).where(Schema.id == schema_id).where(Schema.deleted == False)
).scalar()
if sch is None:
raise MissingSchemaException(obj_id=schema_id)
try:
slug = data.pop('slug')
except KeyError:
raise RequiredFieldException(field='slug')
try:
name = data.pop('name')
except KeyError:
raise RequiredFieldException(field='name')
attr_defs: Dict[str, AttributeDefinition] = {i.attribute.name: i for i in sch.attr_defs}
required = [i for i in attr_defs if attr_defs[i].required]
for i in required:
if i not in data:
raise RequiredFieldException(field=i)
e = Entity(schema_id=schema_id, slug=slug, name=name)
db.add(e)
try:
db.flush()
except IntegrityError:
raise EntityExistsException(slug=slug)
for field, value in data.items():
attr_def = attr_defs.get(field)
if attr_def is None:
raise AttributeNotDefinedException(attr_id=None, schema_id=schema_id)
attr: Attribute = attr_def.attribute
model, caster, _ = attr.type.value
values = _convert_values(attr_def=attr_def, value=value, caster=caster)
if attr.type == AttrType.FK:
_check_fk_value(db=db, attr_def=attr_def, entity_ids=values)
if attr_def.unique and not attr_def.list and values:
_check_unique_value(db=db, attr_def=attr_def, model=model, value=values[0])
for val in values:
v = model(value=val, entity_id=e.id, attribute_id=attr.id)
db.add(v)
if commit:
db.commit()
else:
db.flush()
return e
def update_entity(db: Session, id_or_slug: Union[str, int], schema_id: int, data: dict, commit: bool = True) -> Entity:
q = select(Entity).where(Entity.schema_id == schema_id)
q = q.where(Entity.id == id_or_slug) if isinstance(id_or_slug, int) else q.where(Entity.slug == id_or_slug)
e = db.execute(q).scalar()
if e is None:
raise MissingEntityException(obj_id=id_or_slug)
if e.schema.deleted:
raise MissingSchemaException(obj_id=e.schema.id)
if e.deleted:
raise EntityIsDeletedException(obj_id=e.id)
slug = data.pop('slug', e.slug)
name = data.pop('name', e.name)
try:
db.execute(update(Entity).where(Entity.id == e.id).values(slug=slug, name=name))
except IntegrityError:
db.rollback()
raise EntityExistsException(slug=slug)
attr_defs: Dict[str, AttributeDefinition] = {i.attribute.name: i for i in e.schema.attr_defs}
for field, value in data.items():
attr_def = attr_defs.get(field)
if attr_def is None:
raise AttributeNotDefinedException(attr_id=None, schema_id=schema_id)
attr: Attribute = attr_def.attribute
model, caster, _ = attr.type.value
if value is None:
if attr_def.required:
raise RequiredFieldException(field=field)
db.execute(
delete(model)
.where(model.entity_id == e.id)
.where(model.attribute_id == attr_def.attribute_id)
)
continue
values = _convert_values(attr_def=attr_def, value=value, caster=caster)
if attr.type == AttrType.FK:
_check_fk_value(db=db, attr_def=attr_def, entity_ids=values)
if attr_def.unique and not attr_def.list and values:
_check_unique_value(db=db, attr_def=attr_def, model=model, value=values[0])
db.execute(
delete(model)
.where(model.entity_id == e.id)
.where(model.attribute_id == attr_def.attribute_id)
)
for val in values:
v = model(value=val, entity_id=e.id, attribute_id=attr.id)
db.add(v)
# Ensure that no required attribute remains unset
non_updated_required_fields = [attr_def for name, attr_def in attr_defs.items()
if attr_def.required and name not in data]
for model, req_attr_defs in groupby(non_updated_required_fields,
lambda x: x.attribute.type.value[0]):
expected_attr_ids = {attr_def.attribute_id for attr_def in req_attr_defs}
present_attr_ids = set(aid[0] for aid in db.query(model.attribute_id)
.filter(model.attribute_id.in_(expected_attr_ids),
model.entity_id == e.id)
.distinct())
missing_attr_ids = expected_attr_ids - present_attr_ids
if missing_attr_ids:
attr_id = next(iter(missing_attr_ids))
for name, attr_def in attr_defs.items():
if attr_id == attr_def.attribute_id:
raise RequiredFieldException(name)
if commit:
db.commit()
else:
db.flush()
return e
def delete_entity(db: Session, id_or_slug: Union[int, str], schema_id: int, commit: bool = True) -> Entity:
q = select(Entity).where(Entity.schema_id == schema_id)
if isinstance(id_or_slug, int):
q = q.where(Entity.id == id_or_slug)
else:
q = q.where(Entity.slug == id_or_slug)
e = db.execute(q).scalar()
if e is None:
raise MissingEntityException(obj_id=id_or_slug)
if e.deleted:
raise NoOpChangeException(f"Entity with id {e.id} is already deleted")
e.deleted = True
if commit:
db.commit()
else:
db.flush()
return e
def restore_entity(db: Session, id_or_slug: Union[int, str], schema_id: int, commit: bool = True) -> Entity:
q = select(Entity).where(Entity.schema_id == schema_id)
if isinstance(id_or_slug, int):
q = q.where(Entity.id == id_or_slug)
else:
q = q.where(Entity.slug == id_or_slug)
e = db.execute(q).scalar()
if e is None:
raise MissingEntityException(obj_id=id_or_slug)
if not e.deleted:
raise NoOpChangeException(f"Entity with id {e.id} is not deleted")
e.deleted = False
if commit:
db.commit()
else:
db.flush()
return e