__init__.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from datetime import datetime
  2. from sqlalchemy import Column, DateTime, inspect
  3. from pydantic import ValidationError
  4. class Base:
  5. _schema = None
  6. date_created = Column(DateTime, default=datetime.utcnow)
  7. date_updated = Column(DateTime, onupdate=datetime.utcnow)
  8. @classmethod
  9. def validate(cls, data: dict):
  10. try:
  11. instance = cls._schema(**data)
  12. return True, instance
  13. except ValidationError as e:
  14. return False, e
  15. @classmethod
  16. def to_yaml(cls, representer, node):
  17. return representer.represent_data(node.attrs)
  18. @classmethod
  19. def from_yaml(cls, constructor, node):
  20. return cls()
  21. @classmethod
  22. def column_properties(cls):
  23. ins = inspect(cls)
  24. return [p.key for p in ins.iterate_properties if hasattr(p, 'columns')]
  25. @property
  26. def short_uuid(self):
  27. return self.uuid[:8]
  28. @property
  29. def attrs(self):
  30. return {
  31. k: getattr(self, k) for k in self.column_properties()
  32. }
  33. class BaseEnum:
  34. @classmethod
  35. def to_yaml(cls, representer, node):
  36. return representer.represent_scalar(
  37. node.value
  38. )
  39. @classmethod
  40. def from_yaml(cls, constructor, node):
  41. return cls(node)
  42. from .collections import Collection
  43. from .milestones import Milestone
  44. from .sprints import Sprint
  45. from .items import Item
  46. from .planned import (PlannedItem, PlannedItemType,
  47. Limitation, Constraint, Feature, Unknown,
  48. ItemPriority, Length)
  49. from .unplanned import (UnplannedItem, UnplannedItemType,
  50. Idea, Experimentation)
  51. from .comments import Comment
  52. from .tasks import Task
  53. from .index import UniversalIndex
  54. __all__ = [
  55. 'Collection',
  56. 'Milestone',
  57. 'Sprint',
  58. 'Item',
  59. 'PlannedItem', 'PlannedItemType',
  60. 'Limitation', 'Constraint', 'Feature', 'Unknown',
  61. 'ItemPriority', 'Length',
  62. 'UnplannedItem', 'UnplannedItemType',
  63. 'Experimentation', 'Idea',
  64. 'Comment',
  65. 'Task',
  66. 'UniversalIndex'
  67. ]