No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

admin.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. from ...exceptions import *
  2. from .base import get_response
  3. from lodel.leapi.exceptions import *
  4. from lodel import logger
  5. import leapi_dyncode as dyncode
  6. import warnings
  7. from lodel import logger
  8. def index_admin(request):
  9. return get_response('admin/admin.html')
  10. def admin_update(request):
  11. msg=''
  12. if request.method == 'POST':
  13. error = None
  14. datas = list()
  15. classname = request.form['classname']
  16. logger.warning('Composed uids broken here')
  17. uid = request.form['uid']
  18. try:
  19. target_leo = dyncode.Object.name2class(classname)
  20. except LeApiError:
  21. classname = None
  22. if classname is None or target_leo.is_abstract():
  23. raise HttpException(400)
  24. fieldnames = target_leo.fieldnames()
  25. uid_field = target_leo.uid_fieldname()[0]
  26. fields = dict()
  27. for in_put, in_value in request.form.items():
  28. if in_put != 'classname' and in_put != 'uid':
  29. fields[in_put[12:]] = in_value
  30. #elif in_put == 'classname':
  31. # fields['classname'] = in_value
  32. filter_q = '%s = %s' % (uid_field, uid)
  33. obj = (target_leo.get((filter_q)))[0]
  34. inserted = obj.update(fields)
  35. if inserted==1:
  36. msg = 'Successfully updated';
  37. else:
  38. msg = 'Oops something wrong happened...object not saved'
  39. return get_response('admin/admin_edit.html', target=target_leo, uidfield = uid_field, lodel_id = uid, msg = msg)
  40. if 'classname' in request.GET:
  41. classname = request.GET['classname']
  42. if len(classname) > 1:
  43. raise HttpException(400)
  44. classname = classname[0]
  45. try:
  46. target_leo = dyncode.Object.name2class(classname)
  47. except LeApiError:
  48. # classname = None
  49. raise HttpException(400)
  50. logger.warning('Composed uids broken here')
  51. uid_field = target_leo.uid_fieldname()[0]
  52. test_valid = uid_field in request.GET \
  53. and len(request.GET[uid_field]) == 1
  54. if test_valid:
  55. try:
  56. lodel_id = request.GET[uid_field][0]
  57. except (ValueError, TypeError):
  58. test_valid = False
  59. if not test_valid:
  60. raise HttpException(400)
  61. else:
  62. query_filters = list()
  63. query_filters.append((uid_field,'=',lodel_id))
  64. obj = dyncode.Object.get(query_filters)
  65. if len(obj) == 0:
  66. raise HttpException(404)
  67. return get_response('admin/admin_edit.html', target=target_leo, uidfield = uid_field, lodel_id =lodel_id)
  68. def admin_create(request):
  69. classname = None
  70. if request.method == 'POST':
  71. error = None
  72. datas = list()
  73. classname = request.form['classname']
  74. try:
  75. target_leo = dyncode.Object.name2class(classname)
  76. except LeApiError:
  77. classname = None
  78. if classname is None or target_leo.is_abstract():
  79. raise HttpException(400)
  80. fieldnames = target_leo.fieldnames()
  81. fields = dict()
  82. for in_put, in_value in request.form.items():
  83. if in_put != 'classname':
  84. fields[in_put[12:]] = in_value
  85. #else:
  86. # fields[in_put] = in_value
  87. new_uid = target_leo.insert(fields)
  88. if not new_uid is None:
  89. msg = 'Successfull creation';
  90. else:
  91. msg = 'Oops something wrong happened...object not saved'
  92. return get_response('admin/admin_create.html', target=target_leo, msg = msg)
  93. if 'classname' in request.GET:
  94. classname = request.GET['classname']
  95. if len(classname) > 1:
  96. raise HttpException(400)
  97. classname = classname[0]
  98. try:
  99. target_leo = dyncode.Object.name2class(classname)
  100. except LeApiError:
  101. classname = None
  102. msg = None
  103. if 'msg' in request.GET:
  104. msg = request.GET['msg']
  105. if classname is None or target_leo.is_abstract():
  106. raise HttpException(400)
  107. return get_response('admin/admin_create.html', target=target_leo)
  108. def admin_classes(request):
  109. return get_response('admin/list_classes_admin.html', my_classes = dyncode.dynclasses)
  110. def create_object(request):
  111. return get_response('admin/list_classes_create.html', my_classes = dyncode.dynclasses)
  112. def admin_class(request):
  113. if 'classname' in request.GET:
  114. classname = request.GET['classname']
  115. if len(classname) > 1:
  116. raise HttpException(400)
  117. classname = classname[0]
  118. try:
  119. target_leo = dyncode.Object.name2class(classname)
  120. except LeApiError:
  121. classname = None
  122. if classname is None or target_leo.is_abstract():
  123. raise HttpException(400)
  124. return get_response('admin/show_class_admin.html', target=target_leo)
  125. def admin(request):
  126. return get_response('admin/admin.html')