6
0

Label.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from __future__ import annotations
  2. from contextlib import closing
  3. from datetime import datetime
  4. from pycs import db
  5. from pycs.database.base import NamedBaseModel
  6. def compare_children(start_label: Label, id: int):
  7. """ check for cyclic relationships """
  8. labels_to_check = [start_label]
  9. while id is not None and labels_to_check:
  10. label = labels_to_check.pop(0)
  11. if label.id == id:
  12. return False
  13. labels_to_check.extend(label.children)
  14. return True
  15. class Label(NamedBaseModel):
  16. project_id = db.Column(
  17. db.Integer,
  18. db.ForeignKey("project.id", ondelete="CASCADE"),
  19. nullable=False)
  20. parent_id = db.Column(
  21. db.Integer,
  22. db.ForeignKey("label.id", ondelete="SET NULL"))
  23. created = db.Column(db.DateTime, default=datetime.utcnow,
  24. index=True, nullable=False)
  25. reference = db.Column(db.String)
  26. # contraints
  27. __table_args__ = (
  28. db.UniqueConstraint('project_id', 'reference'),
  29. )
  30. # relationships to other models
  31. parent = db.relationship("Label",
  32. backref="children",
  33. remote_side=[id],
  34. )
  35. results = db.relationship("Result",
  36. backref="label",
  37. passive_deletes=True,
  38. lazy="dynamic",
  39. )
  40. serialize_only = (
  41. "id",
  42. "name",
  43. "project_id",
  44. "parent_id",
  45. "reference",
  46. )
  47. def set_parent(self, parent_id: int, commit: bool = True):
  48. """
  49. set this labels parent
  50. :param parent_id: parent's id
  51. :return:
  52. """
  53. if not compare_children(self, parent_id):
  54. raise ValueError('Cyclic relationship detected!')
  55. self.parent_id = parent_id
  56. if commit:
  57. self.commit()