schemas.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from marshmallow import post_load, Schema
  2. from ..const import (
  3. API_ADD_COLUMNS_RIS_KEY,
  4. API_ADD_TITLE_RIS_KEY,
  5. API_DESCRIPTION_COLUMNS_RIS_KEY,
  6. API_EDIT_COLUMNS_RIS_KEY,
  7. API_EDIT_TITLE_RIS_KEY,
  8. API_FILTERS_RIS_KEY,
  9. API_LABEL_COLUMNS_RIS_KEY,
  10. API_LIST_COLUMNS_RIS_KEY,
  11. API_LIST_TITLE_RIS_KEY,
  12. API_ORDER_COLUMN_RIS_KEY,
  13. API_ORDER_COLUMNS_RIS_KEY,
  14. API_ORDER_DIRECTION_RIS_KEY,
  15. API_PAGE_INDEX_RIS_KEY,
  16. API_PAGE_SIZE_RIS_KEY,
  17. API_PERMISSIONS_RIS_KEY,
  18. API_SELECT_COLUMNS_RIS_KEY,
  19. API_SELECT_KEYS_RIS_KEY,
  20. API_SELECT_SEL_COLUMNS_RIS_KEY,
  21. API_SHOW_COLUMNS_RIS_KEY,
  22. API_SHOW_TITLE_RIS_KEY,
  23. )
  24. class BaseModelSchema(Schema):
  25. """
  26. Extends marshmallow Schema to add functionality similar to marshmallow-sqlalchemy
  27. for creating and updating SQLAlchemy models on load
  28. """
  29. model_cls = None
  30. """Declare the SQLAlchemy model when creating a new model on load"""
  31. def __init__(self, *arg, **kwargs):
  32. super().__init__()
  33. self.instance = None
  34. @post_load
  35. def process(self, data, **kwargs):
  36. if self.instance is not None:
  37. for key, value in data.items():
  38. setattr(self.instance, key, value)
  39. return self.instance
  40. return self.model_cls(**data)
  41. def load(self, data, *, instance=None, **kwargs):
  42. self.instance = instance
  43. try:
  44. return super().load(data, **kwargs)
  45. finally:
  46. self.instance = None
  47. get_list_schema = {
  48. "type": "object",
  49. "properties": {
  50. API_SELECT_KEYS_RIS_KEY: {
  51. "type": "array",
  52. "items": {
  53. "type": "string",
  54. "enum": [
  55. API_LIST_COLUMNS_RIS_KEY,
  56. API_ORDER_COLUMNS_RIS_KEY,
  57. API_LABEL_COLUMNS_RIS_KEY,
  58. API_DESCRIPTION_COLUMNS_RIS_KEY,
  59. API_LIST_TITLE_RIS_KEY,
  60. "none",
  61. ],
  62. },
  63. },
  64. API_SELECT_COLUMNS_RIS_KEY: {"type": "array", "items": {"type": "string"}},
  65. API_SELECT_SEL_COLUMNS_RIS_KEY: {"type": "array", "items": {"type": "string"}},
  66. API_ORDER_COLUMN_RIS_KEY: {"type": "string"},
  67. API_ORDER_DIRECTION_RIS_KEY: {"type": "string", "enum": ["asc", "desc"]},
  68. API_PAGE_INDEX_RIS_KEY: {"type": "integer"},
  69. API_PAGE_SIZE_RIS_KEY: {"type": "integer"},
  70. API_FILTERS_RIS_KEY: {
  71. "type": "array",
  72. "items": {
  73. "type": "object",
  74. "properties": {
  75. "col": {"type": "string"},
  76. "opr": {"type": "string"},
  77. "value": {
  78. "anyOf": [
  79. {"type": "number"},
  80. {"type": "string"},
  81. {"type": "boolean"},
  82. {"type": "array"},
  83. ]
  84. },
  85. },
  86. "required": ["col", "opr", "value"],
  87. },
  88. },
  89. },
  90. }
  91. get_item_schema = {
  92. "type": "object",
  93. "properties": {
  94. API_SELECT_KEYS_RIS_KEY: {
  95. "type": "array",
  96. "items": {
  97. "type": "string",
  98. "enum": [
  99. API_SHOW_COLUMNS_RIS_KEY,
  100. API_DESCRIPTION_COLUMNS_RIS_KEY,
  101. API_LABEL_COLUMNS_RIS_KEY,
  102. API_SHOW_TITLE_RIS_KEY,
  103. "none",
  104. ],
  105. },
  106. },
  107. API_SELECT_COLUMNS_RIS_KEY: {"type": "array", "items": {"type": "string"}},
  108. },
  109. }
  110. get_info_schema = {
  111. "type": "object",
  112. "properties": {
  113. API_SELECT_KEYS_RIS_KEY: {
  114. "type": "array",
  115. "items": {
  116. "type": "string",
  117. "enum": [
  118. API_ADD_COLUMNS_RIS_KEY,
  119. API_EDIT_COLUMNS_RIS_KEY,
  120. API_FILTERS_RIS_KEY,
  121. API_PERMISSIONS_RIS_KEY,
  122. API_ADD_TITLE_RIS_KEY,
  123. API_EDIT_TITLE_RIS_KEY,
  124. "none",
  125. ],
  126. },
  127. },
  128. API_ADD_COLUMNS_RIS_KEY: {
  129. "type": "object",
  130. "additionalProperties": {
  131. "type": "object",
  132. "properties": {
  133. API_PAGE_SIZE_RIS_KEY: {"type": "integer"},
  134. API_PAGE_INDEX_RIS_KEY: {"type": "integer"},
  135. },
  136. },
  137. },
  138. API_EDIT_COLUMNS_RIS_KEY: {
  139. "type": "object",
  140. "additionalProperties": {
  141. "type": "object",
  142. "properties": {
  143. API_PAGE_SIZE_RIS_KEY: {"type": "integer"},
  144. API_PAGE_INDEX_RIS_KEY: {"type": "integer"},
  145. },
  146. },
  147. },
  148. },
  149. }