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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. # both are hidden in the form, to identify the object here
  129. if in_put != 'classname' and in_value != '':
  130. dhl = target_leo.data_handler(in_put[12:])
  131. if dhl.is_singlereference():
  132. in_value=int(in_value)
  133. elif dhl.is_reference() and in_value != '':
  134. logger.info(in_value)
  135. in_value.replace(" ","")
  136. in_value=in_value.split(',')
  137. in_value=list(in_value)
  138. l=list()
  139. for i in in_value:
  140. l.append(int(i))
  141. in_value=l
  142. fields[in_put[12:]] = in_value
  143. if in_value == '':
  144. fields[in_put[12:]] = None
  145. # Insertion in the database of the values corresponding to a new object
  146. new_uid = target_leo.insert(fields)
  147. # reurn to the form with a confirmation or error message
  148. if not new_uid is None:
  149. msg = 'Successfull creation';
  150. else:
  151. msg = 'Oops something wrong happened...object not saved'
  152. return get_response('admin/admin_create.html', target=target_leo, msg = msg)
  153. # Display of an empty form
  154. if 'classname' in request.GET:
  155. # We need the class to create an object in
  156. classname = request.GET['classname']
  157. if len(classname) > 1:
  158. raise HttpException(400)
  159. classname = classname[0]
  160. try:
  161. target_leo = dyncode.Object.name2class(classname)
  162. except LeApiError:
  163. classname = None
  164. if classname is None or target_leo.is_abstract():
  165. raise HttpException(400)
  166. return get_response('admin/admin_create.html', target=target_leo)
  167. ##@brief Controller's function to delete an object of the editorial model
  168. # @param request : the request (get)
  169. # @note the response is given in a html page (in templates/admin) called in get_response_function
  170. def admin_delete(request):
  171. # We have to be identified to admin the instance
  172. # temporary, the acl will be more restrictive
  173. #if WebUiClient.is_anonymous():
  174. # return get_response('users/signin.html')
  175. classname = None
  176. if 'classname' in request.GET:
  177. # We need the class to delete an object in
  178. classname = request.GET['classname']
  179. if len(classname) > 1:
  180. raise HttpException(400)
  181. classname = classname[0]
  182. try:
  183. target_leo = dyncode.Object.name2class(classname)
  184. except LeApiError:
  185. # classname = None
  186. raise HttpException(400)
  187. logger.warning('Composed uids broken here')
  188. uid_field = target_leo.uid_fieldname()[0]
  189. # We also need the uid of the object to delete
  190. test_valid = 'lodel_id' in request.GET \
  191. and len(request.GET['lodel_id']) == 1
  192. if test_valid:
  193. try:
  194. dh = target_leo.field(uid_field)
  195. # we cast the uid extrated form the request to the adequate type
  196. # given by the datahandler of the uidfield's datahandler
  197. lodel_id = dh.cast_type(request.GET['lodel_id'][0])
  198. except (ValueError, TypeError):
  199. test_valid = False
  200. if not test_valid:
  201. raise HttpException(400)
  202. else:
  203. query_filters = list()
  204. query_filters.append((uid_field,'=',lodel_id))
  205. nb_deleted = target_leo.delete_bundle(query_filters)
  206. if nb_deleted == 1:
  207. msg = 'Object successfully deleted';
  208. else:
  209. msg = 'Oops something wrong happened...object still here'
  210. return get_response('admin/admin_delete.html', target=target_leo, lodel_id =lodel_id, msg = msg)
  211. def admin_classes(request):
  212. # We have to be identified to admin the instance
  213. # temporary, the acl will be more restrictive
  214. #if WebUiClient.is_anonymous():
  215. # return get_response('users/signin.html')
  216. return get_response('admin/list_classes_admin.html', my_classes = dyncode.dynclasses)
  217. def create_object(request):
  218. # We have to be identified to admin the instance
  219. # temporary, the acl will be more restrictive
  220. #if WebUiClient.is_anonymous():
  221. # return get_response('users/signin.html')
  222. return get_response('admin/list_classes_create.html', my_classes = dyncode.dynclasses)
  223. def delete_object(request):
  224. # We have to be identified to admin the instance
  225. # temporary, the acl will be more restrictive
  226. #if WebUiClient.is_anonymous():
  227. # return get_response('users/signin.html')
  228. return get_response('admin/list_classes_delete.html', my_classes = dyncode.dynclasses)
  229. def admin_class(request):
  230. # We have to be identified to admin the instance
  231. # temporary, the acl will be more restrictive
  232. #if WebUiClient.is_anonymous():
  233. # return get_response('users/signin.html')
  234. # We need the class we'll list to select the object to edit
  235. if 'classname' in request.GET:
  236. classname = request.GET['classname']
  237. if len(classname) > 1:
  238. raise HttpException(400)
  239. classname = classname[0]
  240. try:
  241. target_leo = dyncode.Object.name2class(classname)
  242. except LeApiError:
  243. classname = None
  244. if classname is None or target_leo.is_abstract():
  245. raise HttpException(400)
  246. return get_response('admin/show_class_admin.html', target=target_leo)
  247. def delete_in_class(request):
  248. # We have to be identified to admin the instance
  249. # temporary, the acl will be more restrictive
  250. #if WebUiClient.is_anonymous():
  251. # return get_response('users/signin.html')
  252. # We need the class we'll list to select the object to delete
  253. if 'classname' in request.GET:
  254. classname = request.GET['classname']
  255. if len(classname) > 1:
  256. raise HttpException(400)
  257. classname = classname[0]
  258. try:
  259. target_leo = dyncode.Object.name2class(classname)
  260. except LeApiError:
  261. classname = None
  262. if classname is None or target_leo.is_abstract():
  263. raise HttpException(400)
  264. return get_response('admin/show_class_delete.html', target=target_leo)
  265. def admin(request):
  266. # We have to be identified to admin the instance
  267. # temporary, the acl will be more restrictive
  268. #if WebUiClient.is_anonymous():
  269. # return get_response('users/signin.html')
  270. return get_response('admin/admin.html')