field_generator.py 2.6 KB

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