os_template.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 OSTemplate
  9. from models import Rules
  10. from models import Utils
  11. __author__ = 'James Iter'
  12. __date__ = '2017/3/30'
  13. __contact__ = 'james.iter.cn@gmail.com'
  14. __copyright__ = '(c) 2017 by James Iter.'
  15. blueprint = Blueprint(
  16. 'api_os_template',
  17. __name__,
  18. url_prefix='/api/os_template'
  19. )
  20. blueprints = Blueprint(
  21. 'api_os_templates',
  22. __name__,
  23. url_prefix='/api/os_templates'
  24. )
  25. os_template_base = Base(the_class=OSTemplate, the_blueprint=blueprint, the_blueprints=blueprints)
  26. @Utils.dumps2response
  27. def r_create():
  28. os_template = OSTemplate()
  29. args_rules = [
  30. Rules.LABEL.value,
  31. Rules.PATH.value,
  32. Rules.ACTIVE.value,
  33. Rules.OS_TYPE.value,
  34. Rules.ICON.value,
  35. Rules.BOOT_JOB_ID_EXT.value
  36. ]
  37. os_template.label = request.json.get('label')
  38. os_template.path = request.json.get('path')
  39. os_template.os_type = request.json.get('os_type')
  40. os_template.active = request.json.get('active')
  41. os_template.icon = request.json.get('icon')
  42. os_template.boot_job_id = request.json.get('boot_job_id', 0)
  43. try:
  44. ji.Check.previewing(args_rules, os_template.__dict__)
  45. ret = dict()
  46. ret['state'] = ji.Common.exchange_state(20000)
  47. if os_template.exist_by('path'):
  48. ret['state'] = ji.Common.exchange_state(40901)
  49. ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ': ', os_template.path])
  50. return ret
  51. os_template.create()
  52. os_template.get_by('path')
  53. ret['data'] = os_template.__dict__
  54. return ret
  55. except ji.PreviewingError, e:
  56. return json.loads(e.message)
  57. @Utils.dumps2response
  58. def r_update(_id):
  59. os_template = OSTemplate()
  60. args_rules = [
  61. Rules.ID.value
  62. ]
  63. if 'label' in request.json:
  64. args_rules.append(
  65. Rules.LABEL.value,
  66. )
  67. if 'path' in request.json:
  68. args_rules.append(
  69. Rules.PATH.value,
  70. )
  71. if 'os_type' in request.json:
  72. args_rules.append(
  73. Rules.OS_TYPE.value,
  74. )
  75. if 'active' in request.json:
  76. args_rules.append(
  77. Rules.ACTIVE.value,
  78. )
  79. if 'icon' in request.json:
  80. args_rules.append(
  81. Rules.ICON.value,
  82. )
  83. if 'boot_job_id' in request.json:
  84. args_rules.append(
  85. Rules.BOOT_JOB_ID_EXT.value,
  86. )
  87. if args_rules.__len__() < 2:
  88. ret = dict()
  89. ret['state'] = ji.Common.exchange_state(20000)
  90. return ret
  91. request.json['id'] = _id
  92. try:
  93. ji.Check.previewing(args_rules, request.json)
  94. os_template.id = request.json.get('id')
  95. os_template.get()
  96. os_template.label = request.json.get('label', os_template.label)
  97. os_template.path = request.json.get('path', os_template.path)
  98. os_template.os_type = request.json.get('os_type', os_template.os_type)
  99. os_template.active = request.json.get('active', os_template.active)
  100. os_template.icon = request.json.get('icon', os_template.icon)
  101. os_template.boot_job_id = request.json.get('boot_job_id', os_template.boot_job_id)
  102. os_template.update()
  103. os_template.get()
  104. ret = dict()
  105. ret['state'] = ji.Common.exchange_state(20000)
  106. ret['data'] = os_template.__dict__
  107. return ret
  108. except ji.PreviewingError, e:
  109. return json.loads(e.message)
  110. @Utils.dumps2response
  111. def r_delete(ids):
  112. return os_template_base.delete(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
  113. @Utils.dumps2response
  114. def r_get(ids):
  115. return os_template_base.get(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
  116. @Utils.dumps2response
  117. def r_get_by_filter():
  118. return os_template_base.get_by_filter()
  119. @Utils.dumps2response
  120. def r_content_search():
  121. return os_template_base.content_search()