field_generator.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import tortoise.fields
  2. from .constants import constants
  3. class field_generator:
  4. """
  5. That class is used to store methods, which generate fields by items
  6. which would be stored in that fields, like nick, password or
  7. description.
  8. Methods
  9. -------
  10. id : tortoise.fields.IntField
  11. Field for storing objects ID.
  12. name : tortoise.fields.TextField
  13. Field which store name.
  14. password : tortosie.fields.TextField
  15. Field which store password.
  16. """
  17. @staticmethod
  18. def id() -> tortoise.fields.IntField:
  19. return tortoise.fields.IntField(primary_key = True)
  20. @staticmethod
  21. def name() -> tortoise.fields.TextField:
  22. return tortoise.fields.TextField(max_lenght = 30)
  23. @staticmethod
  24. def password() -> tortoise.fields.TextField:
  25. return tortoise.fields.TextField(max_lenght = 256)
  26. @staticmethod
  27. def surname() -> tortoise.fields.TextField:
  28. return tortoise.fields.TextField(max_lenght = 30)
  29. @staticmethod
  30. def nick() -> tortoise.fields.TextField:
  31. return tortoise.fields.TextField(max_lenght = 20)
  32. @staticmethod
  33. def description() -> tortoise.fields.TextField:
  34. return tortoise.fields.TextField(max_lenght = 4096)
  35. @staticmethod
  36. def phone_number() -> tortoise.fields.TextField:
  37. return tortoise.fields.TextField(max_lenght = 15)
  38. @staticmethod
  39. def barcode() -> tortoise.fields.TextField:
  40. return tortoise.fields.TextField(max_lenght = 13)
  41. @staticmethod
  42. def email() -> tortoise.fields.TextField:
  43. return tortoise.fields.TextField(max_lenght = 30)
  44. @staticmethod
  45. def apikey() -> tortoise.fields.TextField:
  46. return tortoise.fields.TextField(max_lenght = 280)
  47. @staticmethod
  48. def secret() -> tortoise.fields.TextField:
  49. return tortoise.fields.TextField(max_lenght = 256)
  50. @staticmethod
  51. def permissions() -> tortoise.fields.BooleanField:
  52. return tortoise.fields.BooleanField()
  53. @staticmethod
  54. def stock() -> tortoise.fields.IntField:
  55. return tortoise.fields.IntField
  56. @staticmethod
  57. def boolean() -> tortoise.fields.BooleanField:
  58. return tortoise.fields.BooleanField()
  59. @staticmethod
  60. def path() -> tortoise.fields.TextField:
  61. return tortoise.fields.TextField(max_lenght = 4096)
  62. @staticmethod
  63. def __connected_names(
  64. target: type,
  65. own: type | str,
  66. field: str | None) -> [str, str]:
  67. target_model = constants.get_model_name(target)
  68. related_field = constants.get_related_name(own, field)
  69. return target_model, related_field
  70. @classmethod
  71. def connected_many(
  72. cls,
  73. target: type,
  74. own: type | str,
  75. field: str | None = None
  76. ) -> tortoise.fields.ManyToManyField:
  77. model, related = cls.__connected_names(target, own, field)
  78. return tortoise.fields.ManyToManyField(
  79. model,
  80. related_name = related
  81. )
  82. @classmethod
  83. def connected_single(
  84. cls,
  85. target: type,
  86. own: type | str,
  87. field: str | None = None
  88. ) -> tortoise.fields.ForeignKeyField:
  89. model, related = cls.__connected_names(target, own, field)
  90. return tortoise.fields.ForeignKeyField(
  91. model,
  92. related_name = related
  93. )