os_init_write.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 OSInit
  9. from models import OSInitWrite
  10. from models import Rules
  11. from models import Utils
  12. __author__ = 'James Iter'
  13. __date__ = '2017/3/30'
  14. __contact__ = 'james.iter.cn@gmail.com'
  15. __copyright__ = '(c) 2017 by James Iter.'
  16. blueprint = Blueprint(
  17. 'os_init_write',
  18. __name__,
  19. url_prefix='/api/os_init_write'
  20. )
  21. blueprints = Blueprint(
  22. 'os_init_writes',
  23. __name__,
  24. url_prefix='/api/os_init_writes'
  25. )
  26. os_init_write_base = Base(the_class=OSInitWrite, the_blueprint=blueprint, the_blueprints=blueprints)
  27. @Utils.dumps2response
  28. def r_create():
  29. os_init = OSInit()
  30. os_init_write = OSInitWrite()
  31. args_rules = [
  32. Rules.OS_INIT_ID_EXT.value,
  33. Rules.OS_INIT_WRITE_PATH.value,
  34. Rules.OS_INIT_WRITE_CONTENT.value
  35. ]
  36. os_init_write.os_init_id = request.json.get('os_init_id')
  37. os_init_write.path = request.json.get('path')
  38. os_init_write.content = request.json.get('content')
  39. try:
  40. ji.Check.previewing(args_rules, os_init_write.__dict__)
  41. ret = dict()
  42. ret['state'] = ji.Common.exchange_state(20000)
  43. os_init.id = os_init_write.os_init_id
  44. if not os_init.exist():
  45. ret['state'] = ji.Common.exchange_state(40401)
  46. ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ': ', os_init.id.__str__()])
  47. return ret
  48. data, total = os_init_write.get_by_filter(
  49. filter_str=':'.join(['os_init_id','eq',os_init_write.os_init_id.__str__()]) + ';' +
  50. ':'.join(['path', 'eq', os_init_write.path]))
  51. if data.__len__() > 0:
  52. ret['state'] = ji.Common.exchange_state(40901)
  53. ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ', path: ', os_init_write.path,
  54. ', os_init_id: ', os_init_write.os_init_id.__str__()])
  55. return ret
  56. os_init_write.create()
  57. data, total = os_init_write.get_by_filter(
  58. filter_str=':'.join(['os_init_id','eq',os_init_write.os_init_id.__str__()]) + ';' +
  59. ':'.join(['path', 'eq', os_init_write.path]))
  60. ret['data'] = data[0]
  61. return ret
  62. except ji.PreviewingError, e:
  63. return json.loads(e.message)
  64. @Utils.dumps2response
  65. def r_update(_id):
  66. os_init_write = OSInitWrite()
  67. args_rules = [
  68. Rules.ID.value
  69. ]
  70. if 'path' in request.json:
  71. args_rules.append(
  72. Rules.OS_INIT_WRITE_PATH.value,
  73. )
  74. if 'content' in request.json:
  75. args_rules.append(
  76. Rules.OS_INIT_WRITE_CONTENT.value,
  77. )
  78. if args_rules.__len__() < 2:
  79. ret = dict()
  80. ret['state'] = ji.Common.exchange_state(20000)
  81. return ret
  82. request.json['id'] = _id
  83. try:
  84. ji.Check.previewing(args_rules, request.json)
  85. os_init_write.id = request.json.get('id')
  86. os_init_write.get()
  87. os_init_write.path = request.json.get('path', os_init_write.path)
  88. os_init_write.content = request.json.get('content', os_init_write.content)
  89. os_init_write.update()
  90. os_init_write.get()
  91. ret = dict()
  92. ret['state'] = ji.Common.exchange_state(20000)
  93. ret['data'] = os_init_write.__dict__
  94. return ret
  95. except ji.PreviewingError, e:
  96. return json.loads(e.message)
  97. @Utils.dumps2response
  98. def r_delete(ids):
  99. return os_init_write_base.delete(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
  100. @Utils.dumps2response
  101. def r_get(ids):
  102. return os_init_write_base.get(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
  103. @Utils.dumps2response
  104. def r_get_by_filter():
  105. return os_init_write_base.get_by_filter()
  106. @Utils.dumps2response
  107. def r_content_search():
  108. return os_init_write_base.content_search()