constants.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. """
  19. @staticmethod
  20. def empty_text() -> str:
  21. """
  22. That return simple empty text, to make app more readable.
  23. Returns
  24. -------
  25. str
  26. Empty string.
  27. """
  28. return str()
  29. @staticmethod
  30. def app_name() -> str:
  31. """
  32. That retutn name of the app, useable for example when building
  33. relations in database.
  34. Returns
  35. -------
  36. str
  37. Name of the app.
  38. """
  39. return str("app")
  40. @classmethod
  41. def get_model_name(cls, target: str | type) -> str:
  42. """
  43. That generate model name from given model class, or model name in
  44. string. It does not return constant, but generate name from given
  45. parameters constant data in class.
  46. Parameters
  47. ----------
  48. target : str | type
  49. Target class or class name.
  50. Returns
  51. -------
  52. str
  53. Model name of given model class or given model class name.
  54. """
  55. if type(target) is not str:
  56. target = target.__name__
  57. return str(cls.app_name() + "." + target)
  58. @staticmethod
  59. def get_related_name(
  60. target: str | type,
  61. field: str | None = None
  62. ) -> str:
  63. """
  64. It return name of the field, which is related to other object by
  65. reverse relation. Target is name of model which own relation with,
  66. in other words, model which has foreign key. Field is neccssary only
  67. when object who has relation uses same model in more than one
  68. foreign key. Then field must be name of the foreign key field.
  69. For example, when item A has field to_b, which is foreign key of
  70. model B, when call to that would be "get_related_name(A)", or if
  71. b is used in other fields, then it would looks like
  72. "get_related_name(A, "to_b")".
  73. Parameters
  74. ----------
  75. target : str | type
  76. Name or class of the model, which use relation.
  77. field : str | None (Default: None)
  78. Name of the field, which use relation.
  79. """
  80. if type(target) is not str:
  81. target = target.__name__
  82. if field is not None:
  83. return str("related_" + target + "_to_field_" + field)
  84. return str("related_" + target)