123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- # -*- coding: utf-8 -*-
- from ...exceptions import *
- from .base import get_response
-
- from lodel.context import LodelContext
- LodelContext.expose_modules(globals(), {
- 'lodel.leapi.exceptions': [],
- 'lodel.logger': 'logger',
- 'lodel.leapi.datahandlers.base_classes': ['MultipleRef'],
- 'lodel.leapi.exceptions': ['LeApiDataCheckErrors'],
- 'lodel.exceptions': ['LodelExceptions']})
- LodelContext.expose_dyncode(globals(), 'dyncode')
-
- from ...client import WebUiClient
- import warnings
-
- LIST_SEPARATOR = ','
-
- ##@brief These functions are called by the rules defined in ../urls.py
- ## To administrate the instance of the editorial model
-
- ##@brief Controller's function to redirect on the home page of the admin
- # @param request : the request (get or post)
- # @note the response is given in a html page called in get_response_function
- def index_admin(request):
- # We have to be identified to admin the instance
- # temporary, the acl will be more restrictive
- #if WebUiClient.is_anonymous():
- # return get_response('users/signin.html')
- return get_response('admin/admin.html')
-
- ##@brief Controller's function to update an object of the editorial model
- # @param request : the request (get or post)
- # @note the response is given in a html page (in templates/admin) called in get_response_function
- def admin_update(request):
- # We have to be identified to admin the instance
- # temporary, the acl will be more restrictive
- #if WebUiClient.is_anonymous():
- # return get_response('users/signin.html')
- msg=''
-
- datas = process_form(request)
- if not(datas is False):
- if 'lodel_id' not in datas:
- raise HttpException(400)
- target_leo = dyncode.Object.name2class(datas['classname'])
- leo = target_leo.get_from_uid(datas['lodel_id'])
-
- #for in_value in datas:
- # if datas[in_value] == '':
- # datas[in_value] = None
-
- if leo is None:
- raise HttpException(404,
- custom = 'No %s with id %s' % (
- target_leo.__name__, datas['lodel_id']))
-
- try:
- leo.update(
- { f:datas[f] for f in datas if f not in ('classname', 'lodel_id')})
- except LeApiDataCheckErrors as e:
- raise HttpErrors(
- title='Form validation errors', errors = e._exceptions)
-
-
-
- # Display of the form with the object's values to be updated
- if 'classname' in request.GET:
- # We need the class of the object to update
- classname = request.GET['classname']
- if len(classname) > 1:
- raise HttpException(400)
- classname = classname[0]
- try:
- target_leo = dyncode.Object.name2class(classname)
- except LeApiError:
- # classname = None
- raise HttpException(400)
- logger.warning('Composed uids broken here')
- uid_field = target_leo.uid_fieldname()[0]
-
- # We need the uid of the object
- test_valid = 'lodel_id' in request.GET \
- and len(request.GET['lodel_id']) == 1
-
- if test_valid:
- try:
- dh = target_leo.field(uid_field)
- # we cast the uid extrated form the request to the adequate type
- # given by the datahandler of the uidfield's datahandler
- lodel_id = dh.cast_type(request.GET['lodel_id'][0])
- except (ValueError, TypeError):
- test_valid = False
-
- if not test_valid:
- raise HttpException(400)
- else:
- # Check if the object actually exists
- # We get it from the database
- query_filters = list()
- query_filters.append((uid_field,'=',lodel_id))
- obj = target_leo.get(query_filters)
- if len(obj) == 0:
- raise HttpException(404)
- return get_response('admin/admin_edit.html', target=target_leo, lodel_id =lodel_id)
-
- ##@brief Controller's function to create an object of the editorial model
- # @param request : the request (get or post)
- # @note the response is given in a html page (in templates/admin) called in get_response_function
- def admin_create(request):
- # We have to be identified to admin the instance
- # temporary, the acl will be more restrictive
- #if WebUiClient.is_anonymous():
- # return get_response('users/signin.html')
-
- datas = process_form(request)
- if not(datas is False):
- target_leo = dyncode.Object.name2class(datas['classname'])
- if 'lodel_id' in datas:
- raise HttpException(400)
- try:
- for in_value in datas:
- if datas[in_value] == '':
- datas[in_value] = None
- new_uid = target_leo.insert(
- { f:datas[f] for f in datas if f != 'classname'})
- except LeApiDataCheckErrors as e:
- raise HttpErrors(
- title='Form validation errors', errors = e._exceptions)
- if new_uid is None:
- raise HttpException(400, "Creation fails")
- else:
- return get_response(
- 'admin/admin_create.html', target=target_leo,
- msg = "Created with uid %s" % new_uid)
-
- # Display of an empty form
- if 'classname' in request.GET:
- # We need the class to create an object in
- classname = request.GET['classname']
- if len(classname) > 1:
- raise HttpException(400)
- classname = classname[0]
- try:
- target_leo = dyncode.Object.name2class(classname)
- except LeApiError:
- classname = None
-
- if classname is None or target_leo.is_abstract():
- raise HttpException(400)
- return get_response('admin/admin_create.html', target=target_leo)
-
- ##@brief Controller's function to delete an object of the editorial model
- # @param request : the request (get)
- # @note the response is given in a html page (in templates/admin) called in get_response_function
- def admin_delete(request):
- # We have to be identified to admin the instance
- # temporary, the acl will be more restrictive
- #if WebUiClient.is_anonymous():
- # return get_response('users/signin.html')
- classname = None
-
- if 'classname' in request.GET:
- # We need the class to delete an object in
- classname = request.GET['classname']
- if len(classname) > 1:
- raise HttpException(400)
- classname = classname[0]
- try:
- target_leo = dyncode.Object.name2class(classname)
- except LeApiError:
- # classname = None
- raise HttpException(400)
- logger.warning('Composed uids broken here')
- uid_field = target_leo.uid_fieldname()[0]
-
- # We also need the uid of the object to delete
- test_valid = 'lodel_id' in request.GET \
- and len(request.GET['lodel_id']) == 1
-
- if test_valid:
- try:
- dh = target_leo.field(uid_field)
- # we cast the uid extrated form the request to the adequate type
- # given by the datahandler of the uidfield's datahandler
- lodel_id = dh.cast_type(request.GET['lodel_id'][0])
- except (ValueError, TypeError):
- test_valid = False
-
- if not test_valid:
- raise HttpException(400)
- else:
- query_filters = list()
- query_filters.append((uid_field,'=',lodel_id))
- nb_deleted = target_leo.delete_bundle(query_filters)
-
- if nb_deleted == 1:
- msg = 'Object successfully deleted';
- else:
- msg = 'Oops something wrong happened...object still here'
-
- return get_response('admin/admin_delete.html', target=target_leo, lodel_id =lodel_id, msg = msg)
-
-
-
- def admin_classes(request):
- # We have to be identified to admin the instance
- # temporary, the acl will be more restrictive
- #if WebUiClient.is_anonymous():
- # return get_response('users/signin.html')
- return get_response('admin/list_classes_admin.html', my_classes = dyncode.dynclasses)
-
- def create_object(request):
- # We have to be identified to admin the instance
- # temporary, the acl will be more restrictive
- #if WebUiClient.is_anonymous():
- # return get_response('users/signin.html')
- return get_response('admin/list_classes_create.html', my_classes = dyncode.dynclasses)
-
- def delete_object(request):
- # We have to be identified to admin the instance
- # temporary, the acl will be more restrictive
- #if WebUiClient.is_anonymous():
- # return get_response('users/signin.html')
- return get_response('admin/list_classes_delete.html', my_classes = dyncode.dynclasses)
-
- def admin_class(request):
- # We have to be identified to admin the instance
- # temporary, the acl will be more restrictive
- #if WebUiClient.is_anonymous():
- # return get_response('users/signin.html')
- # We need the class we'll list to select the object to edit
- if 'classname' in request.GET:
- classname = request.GET['classname']
- if len(classname) > 1:
- raise HttpException(400)
- classname = classname[0]
- try:
- target_leo = dyncode.Object.name2class(classname)
- except LeApiError:
- classname = None
- if classname is None or target_leo.is_abstract():
- raise HttpException(400)
- return get_response('admin/show_class_admin.html', target=target_leo)
-
- def delete_in_class(request):
- # We have to be identified to admin the instance
- # temporary, the acl will be more restrictive
- #if WebUiClient.is_anonymous():
- # return get_response('users/signin.html')
- # We need the class we'll list to select the object to delete
- if 'classname' in request.GET:
- classname = request.GET['classname']
- if len(classname) > 1:
- raise HttpException(400)
- classname = classname[0]
- try:
- target_leo = dyncode.Object.name2class(classname)
- except LeApiError:
- classname = None
- if classname is None or target_leo.is_abstract():
- raise HttpException(400)
- return get_response('admin/show_class_delete.html', target=target_leo)
-
- def admin(request):
- # We have to be identified to admin the instance
- # temporary, the acl will be more restrictive
- #if WebUiClient.is_anonymous():
- # return get_response('users/signin.html')
- return get_response('admin/admin.html')
-
-
- def search_object(request):
- if request.method == 'POST':
- classname = request.POST['classname']
- searchstring = request.POST['searchstring']
- try:
- target_leo = dyncode.Object.name2class(classname)
- except LeApiError:
- raise HttpException(400)
- # TODO The get method must be implemented here
- return get_response('admin/admin_search.html', my_classes = dyncode.dynclasses)
-
- ##@brief Process a form POST and return the posted datas
- #@param request : the request object
- #@return a dict with datas as value and fieldname as key
- def process_form(request):
- if request.method != 'POST':
- return False
- res = dict()
- errors = dict()
- #Fetch concerned LeObject
- if 'classname' not in request.form:
- logger.error("Received a form without classname !")
- raise HttpException(400)
- res['classname'] = classname = request.form['classname']
- try:
- target_leo = dyncode.Object.name2class(classname)
- except LeApiError:
- logger.error(
- "Received a form with an invalid leo name : '%s'" % classname)
- raise HttpException(400, "No leobject named '%s'" % classname)
- if target_leo.is_abstract():
- logger.error(
- "Received a form with an abstract leo : '%s'" % classname)
- raise HttpException(400, '%s is abstract' % classname)
- #Process input fields
- for fieldname, value in request.form.items():
- if fieldname == 'classname':
- continue
- elif fieldname == 'uid':
- fieldname = 'lodel_id' #wow
- elif fieldname.startswith('field_input_'):
- fieldname = fieldname[12:]
- try:
- dh = target_leo.data_handler(fieldname)
- except NameError as e:
- errors[fieldname] = e
- continue
- if dh.is_reference() and not dh.is_singlereference():
- #Converting multiple references fields
- value = value.strip()
- if len(value) == 0:
- #handling default value for empty string
- if hasattr(dh, 'default'):
- value = dh.default
- else:
- #if not explicit default value, enforcing default as
- #an empty list
- value = []
- else:
- value = [ v.strip() for v in value.split(LIST_SEPARATOR) ]
- value = [ v for v in value if len(v) > 0]
- else:
- #Handling default value for empty string
- if len(value.strip()) == 0 and hasattr(dh, 'default'):
- value = dh.default
- res[fieldname] = value
- if len(errors) > 0:
- del(res)
- raise HttpErrors(errors, title="Form validation error")
- return res
|