field_generator.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import tortoise.fields
  2. from .model import model
  3. from .constants import constants
  4. class field_generator:
  5. @staticmethod
  6. def id() -> tortoise.fields.IntField:
  7. return tortoise.fields.IntField(primary_key = True)
  8. @staticmethod
  9. def name() -> tortoise.fields.TextField:
  10. return tortoise.fields.TextField(max_lenght = 30)
  11. @staticmethod
  12. def password() -> tortoise.fields.TextField:
  13. return tortoise.fields.TextField(max_lenght = 256)
  14. @staticmethod
  15. def surname() -> tortoise.fields.TextField:
  16. return tortoise.fields.TextField(max_lenght = 30)
  17. @staticmethod
  18. def nick() -> tortoise.fields.TextField:
  19. return tortoise.fields.TextField(max_lenght = 20)
  20. @staticmethod
  21. def description() -> tortoise.fields.TextField:
  22. return tortoise.fields.TextField(max_lenght = 4096)
  23. @staticmethod
  24. def phone_number() -> tortoise.fields.TextField:
  25. return tortoise.fields.TextField(max_lenght = 15)
  26. @staticmethod
  27. def barcode() -> tortoise.fields.TextField:
  28. return tortoise.fields.TextField(max_lenght = 13)
  29. @staticmethod
  30. def email() -> tortoise.fields.TextField:
  31. return tortoise.fields.TextField(max_lenght = 30)
  32. @staticmethod
  33. def apikey() -> tortoise.fields.TextField:
  34. return tortoise.fields.TextField(max_lenght = 280)
  35. @staticmethod
  36. def secret() -> tortoise.fields.TextField:
  37. return tortoise.fields.TextField(max_lenght = 256)
  38. @staticmethod
  39. def permissions() -> tortoise.fields.BooleanField:
  40. return tortoise.fields.BooleanField()
  41. @staticmethod
  42. def stock() -> tortoise.fields.IntField:
  43. return tortoise.fields.IntField
  44. @staticmethod
  45. def __connected_names(target: type, field: str | None) -> [str, str]:
  46. if not issubclass(target, model):
  47. raise TypeError("Foreign key target is not child of model.")
  48. name = target.__name__
  49. model_name = model.Meta.app + "." + name
  50. related_name = constants.related_name(name, field)
  51. return model_name, related_name
  52. @classmethod
  53. def connected_many(
  54. cls,
  55. target: type,
  56. field: str | None = None
  57. ) -> tortoise.fields.ManyToManyField:
  58. model, related = cls.__connected_names(target, field)
  59. return tortoise.fields.ManyToManyField(
  60. model,
  61. related_name = related
  62. )
  63. @classmethod
  64. def connected_single(
  65. cls,
  66. target: type,
  67. field: str | None = None
  68. ) -> tortoise.fields.ForeignKeyField:
  69. model, related = cls.__connected_names(target, field)
  70. return tortoise.fields.ForeignKeyField(
  71. model,
  72. related_name = related
  73. )