os_template_profile.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from flask import Blueprint
  4. from flask import request
  5. import jimit as ji
  6. import json
  7. from api.base import Base
  8. from models import OSTemplateProfile
  9. from models import Rules
  10. from models import Utils
  11. __author__ = 'James Iter'
  12. __date__ = '2018/2/4'
  13. __contact__ = 'james.iter.cn@gmail.com'
  14. __copyright__ = '(c) 2018 by James Iter.'
  15. blueprint = Blueprint(
  16. 'api_os_template_profile',
  17. __name__,
  18. url_prefix='/api/os_template_profile'
  19. )
  20. blueprints = Blueprint(
  21. 'api_os_templates_profile',
  22. __name__,
  23. url_prefix='/api/os_templates_profile'
  24. )
  25. os_template_profile_base = Base(the_class=OSTemplateProfile, the_blueprint=blueprint, the_blueprints=blueprints)
  26. @Utils.dumps2response
  27. def r_create():
  28. os_template_profile = OSTemplateProfile()
  29. args_rules = [
  30. Rules.LABEL.value,
  31. Rules.DESCRIPTION.value,
  32. Rules.OS_TYPE.value,
  33. Rules.OS_DISTRO.value,
  34. Rules.OS_MAJOR.value,
  35. Rules.OS_MINOR.value,
  36. Rules.OS_ARCH.value,
  37. Rules.OS_PRODUCT_NAME.value,
  38. Rules.ACTIVE.value,
  39. Rules.ICON.value,
  40. Rules.OS_TEMPLATE_INITIALIZE_OPERATE_SET_ID_EXT.value
  41. ]
  42. os_template_profile.label = request.json.get('label')
  43. os_template_profile.description = request.json.get('description')
  44. os_template_profile.os_type = request.json.get('os_type')
  45. os_template_profile.os_distro = request.json.get('os_distro')
  46. os_template_profile.os_major = request.json.get('os_major')
  47. os_template_profile.os_minor = request.json.get('os_minor')
  48. os_template_profile.os_arch = request.json.get('os_arch')
  49. os_template_profile.os_product_name = request.json.get('os_product_name')
  50. os_template_profile.active = request.json.get('active')
  51. os_template_profile.icon = request.json.get('icon')
  52. os_template_profile.os_template_initialize_operate_set_id = \
  53. request.json.get('os_template_initialize_operate_set_id')
  54. try:
  55. ji.Check.previewing(args_rules, os_template_profile.__dict__)
  56. ret = dict()
  57. ret['state'] = ji.Common.exchange_state(20000)
  58. if os_template_profile.exist_by('label'):
  59. ret['state'] = ji.Common.exchange_state(40901)
  60. ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ': ', os_template_profile.label])
  61. return ret
  62. os_template_profile.create()
  63. os_template_profile.get_by('label')
  64. ret['data'] = os_template_profile.__dict__
  65. return ret
  66. except ji.PreviewingError, e:
  67. return json.loads(e.message)
  68. @Utils.dumps2response
  69. def r_update(_id):
  70. os_template_profile = OSTemplateProfile()
  71. args_rules = [
  72. Rules.ID.value
  73. ]
  74. if 'label' in request.json:
  75. args_rules.append(
  76. Rules.LABEL.value,
  77. )
  78. if 'description' in request.json:
  79. args_rules.append(
  80. Rules.DESCRIPTION.value,
  81. )
  82. if 'os_type' in request.json:
  83. args_rules.append(
  84. Rules.OS_TYPE.value,
  85. )
  86. if 'os_distro' in request.json:
  87. args_rules.append(
  88. Rules.OS_DISTRO.value,
  89. )
  90. if 'os_major' in request.json:
  91. args_rules.append(
  92. Rules.OS_MAJOR.value,
  93. )
  94. if 'os_minor' in request.json:
  95. args_rules.append(
  96. Rules.OS_MINOR.value,
  97. )
  98. if 'os_arch' in request.json:
  99. args_rules.append(
  100. Rules.OS_ARCH.value,
  101. )
  102. if 'os_product_name' in request.json:
  103. args_rules.append(
  104. Rules.OS_PRODUCT_NAME.value,
  105. )
  106. if 'active' in request.json:
  107. args_rules.append(
  108. Rules.ACTIVE.value,
  109. )
  110. if 'icon' in request.json:
  111. args_rules.append(
  112. Rules.ICON.value,
  113. )
  114. if 'os_template_initialize_operate_set_id' in request.json:
  115. args_rules.append(
  116. Rules.OS_TEMPLATE_INITIALIZE_OPERATE_SET_ID_EXT.value,
  117. )
  118. if args_rules.__len__() < 2:
  119. ret = dict()
  120. ret['state'] = ji.Common.exchange_state(20000)
  121. return ret
  122. request.json['id'] = _id
  123. try:
  124. ji.Check.previewing(args_rules, request.json)
  125. os_template_profile.id = request.json.get('id')
  126. os_template_profile.get()
  127. os_template_profile.label = request.json.get('label', os_template_profile.label)
  128. os_template_profile.description = request.json.get('description', os_template_profile.description)
  129. os_template_profile.os_type = request.json.get('os_type', os_template_profile.os_type)
  130. os_template_profile.os_distro = request.json.get('os_distro', os_template_profile.os_distro)
  131. os_template_profile.os_major = request.json.get('os_major', os_template_profile.os_major)
  132. os_template_profile.os_minor = request.json.get('os_minor', os_template_profile.os_minor)
  133. os_template_profile.os_arch = request.json.get('os_arch', os_template_profile.os_arch)
  134. os_template_profile.os_product_name = request.json.get('os_product_name', os_template_profile.os_product_name)
  135. os_template_profile.active = request.json.get('active', os_template_profile.active)
  136. os_template_profile.icon = request.json.get('icon', os_template_profile.icon)
  137. os_template_profile.os_template_initialize_operate_set_id = request.json.get(
  138. 'os_template_initialize_operate_set_id', os_template_profile.os_template_initialize_operate_set_id)
  139. os_template_profile.update()
  140. os_template_profile.get()
  141. ret = dict()
  142. ret['state'] = ji.Common.exchange_state(20000)
  143. ret['data'] = os_template_profile.__dict__
  144. return ret
  145. except ji.PreviewingError, e:
  146. return json.loads(e.message)
  147. @Utils.dumps2response
  148. def r_delete(ids):
  149. return os_template_profile_base.delete(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
  150. @Utils.dumps2response
  151. def r_get(ids):
  152. return os_template_profile_base.get(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
  153. @Utils.dumps2response
  154. def r_get_by_filter():
  155. return os_template_profile_base.get_by_filter()
  156. @Utils.dumps2response
  157. def r_content_search():
  158. return os_template_profile_base.content_search()