Ingen beskrivning
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

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