os_template.py 3.7 KB

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