constants.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. class constants:
  2. """
  3. That class stores constant values, which is used in a lot of places
  4. in the app code. Storing constants in one place makes it easier to
  5. change in the future.
  6. Methods
  7. -------
  8. @staticmethid empty_text : str
  9. Return simple empty string (""), but it is more descriptive than
  10. str().
  11. @staticmethod app_name : dtr
  12. Return name of the app, which is required for example in
  13. relationship builder
  14. @classmethod get_model_name : str
  15. Return name of the model for given class, or name in string..
  16. @staticmethod get_related_name : str
  17. It create name of the reverse relation field for other models.
  18. @staticmethod base_on_stock : int
  19. Init count on stock.
  20. """
  21. @staticmethod
  22. def base_on_stock() -> int:
  23. """
  24. Return base count of items on stock.
  25. Returns
  26. -------
  27. int
  28. Init count on stock.
  29. """
  30. return 1
  31. @staticmethod
  32. def empty_text() -> str:
  33. """
  34. That return simple empty text, to make app more readable.
  35. Returns
  36. -------
  37. str
  38. Empty string.
  39. """
  40. return str()
  41. @staticmethod
  42. def app_name() -> str:
  43. """
  44. That retutn name of the app, useable for example when building
  45. relations in database.
  46. Returns
  47. -------
  48. str
  49. Name of the app.
  50. """
  51. return str("app")
  52. @classmethod
  53. def get_model_name(cls, target: str | type) -> str:
  54. """
  55. That generate model name from given model class, or model name in
  56. string. It does not return constant, but generate name from given
  57. parameters constant data in class.
  58. Parameters
  59. ----------
  60. target : str | type
  61. Target class or class name.
  62. Returns
  63. -------
  64. str
  65. Model name of given model class or given model class name.
  66. """
  67. if type(target) is not str:
  68. target = target.__name__
  69. return str(cls.app_name() + "." + target)
  70. @staticmethod
  71. def get_related_name(
  72. target: str | type,
  73. field: str | None = None
  74. ) -> str:
  75. """
  76. It return name of the field, which is related to other object by
  77. reverse relation. Target is name of model which own relation with,
  78. in other words, model which has foreign key. Field is neccssary only
  79. when object who has relation uses same model in more than one
  80. foreign key. Then field must be name of the foreign key field.
  81. For example, when item A has field to_b, which is foreign key of
  82. model B, when call to that would be "get_related_name(A)", or if
  83. b is used in other fields, then it would looks like
  84. "get_related_name(A, "to_b")".
  85. Parameters
  86. ----------
  87. target : str | type
  88. Name or class of the model, which use relation.
  89. field : str | None (Default: None)
  90. Name of the field, which use relation.
  91. """
  92. if type(target) is not str:
  93. target = target.__name__
  94. if field is not None:
  95. return str("related_" + target + "_to_field_" + field)
  96. return str("related_" + target)