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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. # -*- coding: utf-8 -*-
  2. from ...exceptions import *
  3. from .base import get_response
  4. from lodel.leapi.exceptions import *
  5. from lodel import logger
  6. from ...client import WebUiClient
  7. import leapi_dyncode as dyncode
  8. import warnings
  9. ##@brief These functions are called by the rules defined in ../urls.py
  10. ## To administrate the instance of the editorial model
  11. ##@brief Controller's function to redirect on the home page of the admin
  12. # @param request : the request (get or post)
  13. # @note the response is given in a html page called in get_response_function
  14. def index_admin(request):
  15. # We have to be identified to admin the instance
  16. # temporary, the acl will be more restrictive
  17. if WebUiClient.is_anonymous():
  18. return get_response('users/signin.html')
  19. return get_response('admin/admin.html')
  20. ##@brief Controller's function to update an object of the editorial model
  21. # @param request : the request (get or post)
  22. # @note the response is given in a html page (in templates/admin) called in get_response_function
  23. def admin_update(request):
  24. # We have to be identified to admin the instance
  25. # temporary, the acl will be more restrictive
  26. if WebUiClient.is_anonymous():
  27. return get_response('users/signin.html')
  28. msg=''
  29. # If the form has been submitted
  30. if request.method == 'POST':
  31. error = None
  32. datas = list()
  33. classname = request.form['classname']
  34. logger.warning('Composed uids broken here')
  35. uid = request.form['uid']
  36. try:
  37. target_leo = dyncode.Object.name2class(classname)
  38. except LeApiError:
  39. classname = None
  40. if classname is None or target_leo.is_abstract():
  41. raise HttpException(400)
  42. uid_field = target_leo.uid_fieldname()[0]
  43. fields = dict()
  44. for in_put, in_value in request.form.items():
  45. # The classname is handled by the datasource, we are not allowed to modify it
  46. # uid is not a fieldname
  47. # both are hidden in the form, to identify the object here
  48. if in_put != 'classname' and in_put != 'uid':
  49. dhl = target_leo.data_handler(in_put[12:])
  50. # Here, in case of a Reference we transform the str
  51. # given by the form in a iterable (a list)
  52. if dhl.is_reference() and in_value != '':
  53. in_value.replace(" ","")
  54. in_value=in_value.split(',')
  55. in_value=list(in_value)
  56. if in_value != '':
  57. fields[in_put[12:]] = in_value
  58. # We retrieve the object to update
  59. filter_q = '%s = %s' % (uid_field, uid)
  60. obj = (target_leo.get((filter_q)))[0]
  61. # and update it
  62. inserted = obj.update(fields)
  63. if inserted==1:
  64. msg = 'Successfully updated';
  65. else:
  66. msg = 'Oops something wrong happened...object not saved'
  67. return get_response('admin/admin_edit.html', target=target_leo, uidfield = uid_field, lodel_id = uid, msg = msg)
  68. # Display of the form with the object's values to be updated
  69. if 'classname' in request.GET:
  70. # We need the class of the object to update
  71. classname = request.GET['classname']
  72. if len(classname) > 1:
  73. raise HttpException(400)
  74. classname = classname[0]
  75. try:
  76. target_leo = dyncode.Object.name2class(classname)
  77. except LeApiError:
  78. # classname = None
  79. raise HttpException(400)
  80. logger.warning('Composed uids broken here')
  81. uid_field = target_leo.uid_fieldname()[0]
  82. # We need the uid of the object
  83. test_valid = 'lodel_id' in request.GET \
  84. and len(request.GET['lodel_id']) == 1
  85. if test_valid:
  86. try:
  87. dh = target_leo.field(uid_field)
  88. # we cast the uid extrated form the request to the adequate type
  89. # given by the datahandler of the uidfield's datahandler
  90. lodel_id = dh.cast_type(request.GET['lodel_id'][0])
  91. except (ValueError, TypeError):
  92. test_valid = False
  93. if not test_valid:
  94. raise HttpException(400)
  95. else:
  96. # Check if the object actually exists
  97. # We get it from the database
  98. query_filters = list()
  99. query_filters.append((uid_field,'=',lodel_id))
  100. obj = target_leo.get(query_filters)
  101. if len(obj) == 0:
  102. raise HttpException(404)
  103. return get_response('admin/admin_edit.html', target=target_leo, lodel_id =lodel_id)
  104. ##@brief Controller's function to create an object of the editorial model
  105. # @param request : the request (get or post)
  106. # @note the response is given in a html page (in templates/admin) called in get_response_function
  107. def admin_create(request):
  108. # We have to be identified to admin the instance
  109. # temporary, the acl will be more restrictive
  110. if WebUiClient.is_anonymous():
  111. return get_response('users/signin.html')
  112. classname = None
  113. # If the form has been submitted
  114. if request.method == 'POST':
  115. error = None
  116. datas = list()
  117. classname = request.form['classname']
  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. fieldnames = target_leo.fieldnames()
  125. fields = dict()
  126. for in_put, in_value in request.form.items():
  127. # The classname is handled by the datasource, we are not allowed to modify it
  128. # uid is not a fieldname
  129. # both are hidden in the form, to identify the object here
  130. if in_put != 'classname' and in_value != '':
  131. fields[in_put[12:]] = in_value
  132. # Insertion in the database of the values corresponding to a new object
  133. new_uid = target_leo.insert(fields)
  134. # reurn to the form with a confirmation or error message
  135. if not new_uid is None:
  136. msg = 'Successfull creation';
  137. else:
  138. msg = 'Oops something wrong happened...object not saved'
  139. return get_response('admin/admin_create.html', target=target_leo, msg = msg)
  140. # Display of an empty form
  141. if 'classname' in request.GET:
  142. # We need the class to create an object in
  143. classname = request.GET['classname']
  144. if len(classname) > 1:
  145. raise HttpException(400)
  146. classname = classname[0]
  147. try:
  148. target_leo = dyncode.Object.name2class(classname)
  149. except LeApiError:
  150. classname = None
  151. if classname is None or target_leo.is_abstract():
  152. raise HttpException(400)
  153. return get_response('admin/admin_create.html', target=target_leo)
  154. ##@brief Controller's function to delete an object of the editorial model
  155. # @param request : the request (get)
  156. # @note the response is given in a html page (in templates/admin) called in get_response_function
  157. def admin_delete(request):
  158. # We have to be identified to admin the instance
  159. # temporary, the acl will be more restrictive
  160. if WebUiClient.is_anonymous():
  161. return get_response('users/signin.html')
  162. classname = None
  163. if 'classname' in request.GET:
  164. # We need the class to delete an object in
  165. classname = request.GET['classname']
  166. if len(classname) > 1:
  167. raise HttpException(400)
  168. classname = classname[0]
  169. try:
  170. target_leo = dyncode.Object.name2class(classname)
  171. except LeApiError:
  172. # classname = None
  173. raise HttpException(400)
  174. logger.warning('Composed uids broken here')
  175. uid_field = target_leo.uid_fieldname()[0]
  176. # We also need the uid of the object to delete
  177. test_valid = 'lodel_id' in request.GET \
  178. and len(request.GET['lodel_id']) == 1
  179. if test_valid:
  180. try:
  181. dh = target_leo.field(uid_field)
  182. # we cast the uid extrated form the request to the adequate type
  183. # given by the datahandler of the uidfield's datahandler
  184. lodel_id = dh.cast_type(request.GET['lodel_id'][0])
  185. except (ValueError, TypeError):
  186. test_valid = False
  187. if not test_valid:
  188. raise HttpException(400)
  189. else:
  190. query_filters = list()
  191. query_filters.append((uid_field,'=',lodel_id))
  192. nb_deleted = target_leo.delete_bundle(query_filters)
  193. if nb_deleted == 1:
  194. msg = 'Object successfully deleted';
  195. else:
  196. msg = 'Oops something wrong happened...object still here'
  197. return get_response('admin/admin_delete.html', target=target_leo, lodel_id =lodel_id, msg = msg)
  198. def admin_classes(request):
  199. # We have to be identified to admin the instance
  200. # temporary, the acl will be more restrictive
  201. if WebUiClient.is_anonymous():
  202. return get_response('users/signin.html')
  203. return get_response('admin/list_classes_admin.html', my_classes = dyncode.dynclasses)
  204. def create_object(request):
  205. # We have to be identified to admin the instance
  206. # temporary, the acl will be more restrictive
  207. if WebUiClient.is_anonymous():
  208. return get_response('users/signin.html')
  209. return get_response('admin/list_classes_create.html', my_classes = dyncode.dynclasses)
  210. def delete_object(request):
  211. # We have to be identified to admin the instance
  212. # temporary, the acl will be more restrictive
  213. if WebUiClient.is_anonymous():
  214. return get_response('users/signin.html')
  215. return get_response('admin/list_classes_delete.html', my_classes = dyncode.dynclasses)
  216. def admin_class(request):
  217. # We have to be identified to admin the instance
  218. # temporary, the acl will be more restrictive
  219. if WebUiClient.is_anonymous():
  220. return get_response('users/signin.html')
  221. # We need the class we'll list to select the object to edit
  222. if 'classname' in request.GET:
  223. classname = request.GET['classname']
  224. if len(classname) > 1:
  225. raise HttpException(400)
  226. classname = classname[0]
  227. try:
  228. target_leo = dyncode.Object.name2class(classname)
  229. except LeApiError:
  230. classname = None
  231. if classname is None or target_leo.is_abstract():
  232. raise HttpException(400)
  233. return get_response('admin/show_class_admin.html', target=target_leo)
  234. def delete_in_class(request):
  235. # We have to be identified to admin the instance
  236. # temporary, the acl will be more restrictive
  237. if WebUiClient.is_anonymous():
  238. return get_response('users/signin.html')
  239. # We need the class we'll list to select the object to delete
  240. if 'classname' in request.GET:
  241. classname = request.GET['classname']
  242. if len(classname) > 1:
  243. raise HttpException(400)
  244. classname = classname[0]
  245. try:
  246. target_leo = dyncode.Object.name2class(classname)
  247. except LeApiError:
  248. classname = None
  249. if classname is None or target_leo.is_abstract():
  250. raise HttpException(400)
  251. return get_response('admin/show_class_delete.html', target=target_leo)
  252. def admin(request):
  253. # We have to be identified to admin the instance
  254. # temporary, the acl will be more restrictive
  255. if WebUiClient.is_anonymous():
  256. return get_response('users/signin.html')
  257. return get_response('admin/admin.html')