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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. # -*- coding: utf-8 -*-
  2. from ...exceptions import *
  3. from .base import get_response
  4. from lodel.context import LodelContext
  5. LodelContext.expose_modules(globals(), {
  6. 'lodel.leapi.exceptions': [],
  7. 'lodel.logger': 'logger',
  8. 'lodel.leapi.datahandlers.base_classes': ['MultipleRef'],
  9. 'lodel.leapi.exceptions': ['LeApiDataCheckErrors'],
  10. 'lodel.exceptions': ['LodelExceptions']})
  11. from ...client import WebUiClient
  12. import leapi_dyncode as dyncode
  13. import warnings
  14. LIST_SEPARATOR = ','
  15. ##@brief These functions are called by the rules defined in ../urls.py
  16. ## To administrate the instance of the editorial model
  17. ##@brief Controller's function to redirect on the home page of the admin
  18. # @param request : the request (get or post)
  19. # @note the response is given in a html page called in get_response_function
  20. def index_admin(request):
  21. # We have to be identified to admin the instance
  22. # temporary, the acl will be more restrictive
  23. #if WebUiClient.is_anonymous():
  24. # return get_response('users/signin.html')
  25. return get_response('admin/admin.html')
  26. ##@brief Controller's function to update an object of the editorial model
  27. # @param request : the request (get or post)
  28. # @note the response is given in a html page (in templates/admin) called in get_response_function
  29. def admin_update(request):
  30. # We have to be identified to admin the instance
  31. # temporary, the acl will be more restrictive
  32. #if WebUiClient.is_anonymous():
  33. # return get_response('users/signin.html')
  34. msg=''
  35. datas = process_form(request)
  36. if not(datas is False):
  37. if 'lodel_id' not in datas:
  38. raise HttpException(400)
  39. target_leo = dyncode.Object.name2class(datas['classname'])
  40. leo = target_leo.get_from_uid(datas['lodel_id'])
  41. try:
  42. leo.update(
  43. { f:datas[f] for f in datas if f not in ('classname', 'lodel_id')})
  44. except LeApiDataCheckErrors as e:
  45. raise HttpErrors(
  46. title='Form validation errors', errors = e._exceptions)
  47. # Display of the form with the object's values to be updated
  48. if 'classname' in request.GET:
  49. # We need the class of the object to update
  50. classname = request.GET['classname']
  51. if len(classname) > 1:
  52. raise HttpException(400)
  53. classname = classname[0]
  54. try:
  55. target_leo = dyncode.Object.name2class(classname)
  56. except LeApiError:
  57. # classname = None
  58. raise HttpException(400)
  59. logger.warning('Composed uids broken here')
  60. uid_field = target_leo.uid_fieldname()[0]
  61. # We need the uid of the object
  62. test_valid = 'lodel_id' in request.GET \
  63. and len(request.GET['lodel_id']) == 1
  64. if test_valid:
  65. try:
  66. dh = target_leo.field(uid_field)
  67. # we cast the uid extrated form the request to the adequate type
  68. # given by the datahandler of the uidfield's datahandler
  69. lodel_id = dh.cast_type(request.GET['lodel_id'][0])
  70. except (ValueError, TypeError):
  71. test_valid = False
  72. if not test_valid:
  73. raise HttpException(400)
  74. else:
  75. # Check if the object actually exists
  76. # We get it from the database
  77. query_filters = list()
  78. query_filters.append((uid_field,'=',lodel_id))
  79. obj = target_leo.get(query_filters)
  80. if len(obj) == 0:
  81. raise HttpException(404)
  82. return get_response('admin/admin_edit.html', target=target_leo, lodel_id =lodel_id)
  83. ##@brief Controller's function to create an object of the editorial model
  84. # @param request : the request (get or post)
  85. # @note the response is given in a html page (in templates/admin) called in get_response_function
  86. def admin_create(request):
  87. # We have to be identified to admin the instance
  88. # temporary, the acl will be more restrictive
  89. #if WebUiClient.is_anonymous():
  90. # return get_response('users/signin.html')
  91. datas = process_form(request)
  92. if not(datas is False):
  93. target_leo = dyncode.Object.name2class(datas['classname'])
  94. if 'lodel_id' in datas:
  95. raise HttpException(400)
  96. try:
  97. new_uid = target_leo.insert(
  98. { f:datas[f] for f in datas if f != 'classname'})
  99. except LeApiDataCheckErrors as e:
  100. raise HttpErrors(
  101. title='Form validation errors', errors = e._exceptions)
  102. if new_uid is None:
  103. raise HttpException(400, "Creation fails")
  104. else:
  105. return get_response(
  106. 'admin/admin_create.html', target=target_leo,
  107. msg = "Created with uid %s" % new_uid)
  108. # Display of an empty form
  109. if 'classname' in request.GET:
  110. # We need the class to create an object in
  111. classname = request.GET['classname']
  112. if len(classname) > 1:
  113. raise HttpException(400)
  114. classname = classname[0]
  115. try:
  116. target_leo = dyncode.Object.name2class(classname)
  117. except LeApiError:
  118. classname = None
  119. if classname is None or target_leo.is_abstract():
  120. raise HttpException(400)
  121. return get_response('admin/admin_create.html', target=target_leo)
  122. ##@brief Controller's function to delete an object of the editorial model
  123. # @param request : the request (get)
  124. # @note the response is given in a html page (in templates/admin) called in get_response_function
  125. def admin_delete(request):
  126. # We have to be identified to admin the instance
  127. # temporary, the acl will be more restrictive
  128. #if WebUiClient.is_anonymous():
  129. # return get_response('users/signin.html')
  130. classname = None
  131. if 'classname' in request.GET:
  132. # We need the class to delete an object in
  133. classname = request.GET['classname']
  134. if len(classname) > 1:
  135. raise HttpException(400)
  136. classname = classname[0]
  137. try:
  138. target_leo = dyncode.Object.name2class(classname)
  139. except LeApiError:
  140. # classname = None
  141. raise HttpException(400)
  142. logger.warning('Composed uids broken here')
  143. uid_field = target_leo.uid_fieldname()[0]
  144. # We also need the uid of the object to delete
  145. test_valid = 'lodel_id' in request.GET \
  146. and len(request.GET['lodel_id']) == 1
  147. if test_valid:
  148. try:
  149. dh = target_leo.field(uid_field)
  150. # we cast the uid extrated form the request to the adequate type
  151. # given by the datahandler of the uidfield's datahandler
  152. lodel_id = dh.cast_type(request.GET['lodel_id'][0])
  153. except (ValueError, TypeError):
  154. test_valid = False
  155. if not test_valid:
  156. raise HttpException(400)
  157. else:
  158. query_filters = list()
  159. query_filters.append((uid_field,'=',lodel_id))
  160. nb_deleted = target_leo.delete_bundle(query_filters)
  161. if nb_deleted == 1:
  162. msg = 'Object successfully deleted';
  163. else:
  164. msg = 'Oops something wrong happened...object still here'
  165. return get_response('admin/admin_delete.html', target=target_leo, lodel_id =lodel_id, msg = msg)
  166. def admin_classes(request):
  167. # We have to be identified to admin the instance
  168. # temporary, the acl will be more restrictive
  169. #if WebUiClient.is_anonymous():
  170. # return get_response('users/signin.html')
  171. return get_response('admin/list_classes_admin.html', my_classes = dyncode.dynclasses)
  172. def create_object(request):
  173. # We have to be identified to admin the instance
  174. # temporary, the acl will be more restrictive
  175. #if WebUiClient.is_anonymous():
  176. # return get_response('users/signin.html')
  177. return get_response('admin/list_classes_create.html', my_classes = dyncode.dynclasses)
  178. def delete_object(request):
  179. # We have to be identified to admin the instance
  180. # temporary, the acl will be more restrictive
  181. #if WebUiClient.is_anonymous():
  182. # return get_response('users/signin.html')
  183. return get_response('admin/list_classes_delete.html', my_classes = dyncode.dynclasses)
  184. def admin_class(request):
  185. # We have to be identified to admin the instance
  186. # temporary, the acl will be more restrictive
  187. #if WebUiClient.is_anonymous():
  188. # return get_response('users/signin.html')
  189. # We need the class we'll list to select the object to edit
  190. if 'classname' in request.GET:
  191. classname = request.GET['classname']
  192. if len(classname) > 1:
  193. raise HttpException(400)
  194. classname = classname[0]
  195. try:
  196. target_leo = dyncode.Object.name2class(classname)
  197. except LeApiError:
  198. classname = None
  199. if classname is None or target_leo.is_abstract():
  200. raise HttpException(400)
  201. return get_response('admin/show_class_admin.html', target=target_leo)
  202. def delete_in_class(request):
  203. # We have to be identified to admin the instance
  204. # temporary, the acl will be more restrictive
  205. #if WebUiClient.is_anonymous():
  206. # return get_response('users/signin.html')
  207. # We need the class we'll list to select the object to delete
  208. if 'classname' in request.GET:
  209. classname = request.GET['classname']
  210. if len(classname) > 1:
  211. raise HttpException(400)
  212. classname = classname[0]
  213. try:
  214. target_leo = dyncode.Object.name2class(classname)
  215. except LeApiError:
  216. classname = None
  217. if classname is None or target_leo.is_abstract():
  218. raise HttpException(400)
  219. return get_response('admin/show_class_delete.html', target=target_leo)
  220. def admin(request):
  221. # We have to be identified to admin the instance
  222. # temporary, the acl will be more restrictive
  223. #if WebUiClient.is_anonymous():
  224. # return get_response('users/signin.html')
  225. return get_response('admin/admin.html')
  226. def search_object(request):
  227. if request.method == 'POST':
  228. classname = request.POST['classname']
  229. searchstring = request.POST['searchstring']
  230. try:
  231. target_leo = dyncode.Object.name2class(classname)
  232. except LeApiError:
  233. raise HttpException(400)
  234. # TODO The get method must be implemented here
  235. return get_response('admin/admin_search.html', my_classes = dyncode.dynclasses)
  236. ##@brief Process a form POST and return the posted datas
  237. #@param request : the request object
  238. #@return a dict with datas as value and fieldname as key
  239. def process_form(request):
  240. if request.method != 'POST':
  241. return False
  242. res = dict()
  243. errors = dict()
  244. #Fetch concerned LeObject
  245. if 'classname' not in request.form:
  246. logger.error("Received a form without classname !")
  247. raise HttpException(400)
  248. res['classname'] = classname = request.form['classname']
  249. try:
  250. target_leo = dyncode.Object.name2class(classname)
  251. except LeApiError:
  252. logger.error(
  253. "Received a form with an invalid leo name : '%s'" % classname)
  254. raise HttpException(400, "No leobject named '%s'" % classname)
  255. if target_leo.is_abstract():
  256. logger.error(
  257. "Received a form with an abstract leo : '%s'" % classname)
  258. raise HttpException(400, '%s is abstract' % classname)
  259. #Process input fields
  260. for fieldname, value in request.form.items():
  261. if fieldname == 'classname':
  262. continue
  263. elif fieldname == 'uid':
  264. fieldname = 'lodel_id' #wow
  265. elif fieldname.startswith('field_input_'):
  266. fieldname = fieldname[12:]
  267. try:
  268. dh = target_leo.data_handler(fieldname)
  269. except NameError as e:
  270. errors[fieldname] = e
  271. continue
  272. if dh.is_reference() and not dh.is_singlereference():
  273. #Converting multiple references fields
  274. value = value.strip()
  275. if len(value) == 0:
  276. #handling default value for empty string
  277. if hasattr(dh, 'default'):
  278. value = dh.default
  279. else:
  280. #if not explicit default value, enforcing default as
  281. #an empty list
  282. value = []
  283. else:
  284. value = [ v.strip() for v in value.split(LIST_SEPARATOR) ]
  285. else:
  286. #Handling default value for empty string
  287. if len(value.strip()) == 0 and hasattr(dh, 'default'):
  288. value = dh.default
  289. res[fieldname] = value
  290. if len(errors) > 0:
  291. del(res)
  292. raise HttpErrors(errors, title="Form validation error")
  293. return res