os_template.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. 'os_template',
  17. __name__,
  18. url_prefix='/api/os_template'
  19. )
  20. blueprints = Blueprint(
  21. '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_INIT_ID_EXT.value
  34. ]
  35. os_template.label = request.json.get('label')
  36. os_template.path = request.json.get('path')
  37. os_template.active = request.json.get('active')
  38. os_template.os_init_id = request.json.get('os_init_id', 0)
  39. try:
  40. ji.Check.previewing(args_rules, os_template.__dict__)
  41. ret = dict()
  42. ret['state'] = ji.Common.exchange_state(20000)
  43. if os_template.exist_by('path'):
  44. ret['state'] = ji.Common.exchange_state(40901)
  45. ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ': ', os_template.path])
  46. return ret
  47. os_template.create()
  48. os_template.get_by('path')
  49. ret['data'] = os_template.__dict__
  50. return ret
  51. except ji.PreviewingError, e:
  52. return json.loads(e.message)
  53. @Utils.dumps2response
  54. def r_update(_id):
  55. os_template = OSTemplate()
  56. args_rules = [
  57. Rules.ID.value
  58. ]
  59. if 'label' in request.json:
  60. args_rules.append(
  61. Rules.LABEL.value,
  62. )
  63. if 'path' in request.json:
  64. args_rules.append(
  65. Rules.PATH.value,
  66. )
  67. if 'active' in request.json:
  68. args_rules.append(
  69. Rules.ACTIVE.value,
  70. )
  71. if 'os_init_id' in request.json:
  72. args_rules.append(
  73. Rules.OS_INIT_ID_EXT.value,
  74. )
  75. if args_rules.__len__() < 2:
  76. ret = dict()
  77. ret['state'] = ji.Common.exchange_state(20000)
  78. return ret
  79. request.json['id'] = _id
  80. try:
  81. ji.Check.previewing(args_rules, request.json)
  82. os_template.id = request.json.get('id')
  83. os_template.get()
  84. os_template.label = request.json.get('label', os_template.label)
  85. os_template.path = request.json.get('path', os_template.path)
  86. os_template.active = request.json.get('active', os_template.active)
  87. os_template.os_init_id = request.json.get('os_init_id', os_template.os_init_id)
  88. os_template.update()
  89. os_template.get()
  90. ret = dict()
  91. ret['state'] = ji.Common.exchange_state(20000)
  92. ret['data'] = os_template.__dict__
  93. return ret
  94. except ji.PreviewingError, e:
  95. return json.loads(e.message)
  96. @Utils.dumps2response
  97. def r_delete(_id):
  98. os_template = OSTemplate()
  99. args_rules = [
  100. Rules.ID.value
  101. ]
  102. os_template.id = _id
  103. try:
  104. ji.Check.previewing(args_rules, os_template.__dict__)
  105. if os_template.exist():
  106. os_template.delete()
  107. else:
  108. ret = dict()
  109. ret['state'] = ji.Common.exchange_state(40401)
  110. return ret
  111. except ji.PreviewingError, e:
  112. return json.loads(e.message)
  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()