From b547f75c9fabfd1bda6560492004c6f12f3dfd58 Mon Sep 17 00:00:00 2001 From: prieto Date: Thu, 16 Mar 2017 16:13:00 +0100 Subject: [PATCH] Without is_exist --- lodel/leapi/datahandlers/base_classes.py | 9 ++-- lodel/leapi/leobject.py | 9 ---- lodel/leapi/query.py | 52 +++++++------------ .../webui/interface/controllers/admin.py | 36 +++++++------ .../webui/interface/controllers/listing.py | 16 ++++-- 5 files changed, 52 insertions(+), 70 deletions(-) diff --git a/lodel/leapi/datahandlers/base_classes.py b/lodel/leapi/datahandlers/base_classes.py index 8a0b2ff..4ebba69 100644 --- a/lodel/leapi/datahandlers/base_classes.py +++ b/lodel/leapi/datahandlers/base_classes.py @@ -391,14 +391,11 @@ class Reference(DataHandler): logger.warning('Class of the back_reference given is not an allowed class') return False value = datas[fname] - if not target_class.is_exist(value): + target_uidfield = target_class.uid_fieldname()[0] # multi uid broken here + obj = target_class.get([(target_uidfield, '=', value)]) + if len(obj) == 0: logger.warning('Object referenced does not exist') return False - # target_uidfield = target_class.uid_fieldname()[0] #multi uid broken here - # obj = target_class.get([(target_uidfield, '=', value)]) - # if len(obj) == 0: - # logger.warning('Object referenced does not exist') - # return False return True # @brief Utility method designed to fetch referenced objects diff --git a/lodel/leapi/leobject.py b/lodel/leapi/leobject.py index 147896c..52d7af7 100644 --- a/lodel/leapi/leobject.py +++ b/lodel/leapi/leobject.py @@ -654,12 +654,3 @@ object ! For class %s with uid value = %s" % (cls, uid)) elif len(res) == 0: return None return res[0] - - # @brief Checks if an object exists - @classmethod - def is_exist(cls, uid): - if cls.uid_fieldname() is None: - raise LodelFatalError( - "No uid defined for class %s" % cls.__name__) - from .query import is_exist - return is_exist(cls, uid) diff --git a/lodel/leapi/query.py b/lodel/leapi/query.py index d9203ad..47e06b8 100644 --- a/lodel/leapi/query.py +++ b/lodel/leapi/query.py @@ -7,27 +7,15 @@ import warnings from lodel.context import LodelContext LodelContext.expose_modules(globals(), { - 'lodel.leapi.exceptions': ['LeApiError', 'LeApiErrors', + 'lodel.leapi.exceptions': ['LeApiError', 'LeApiErrors', 'LeApiDataCheckError', 'LeApiDataCheckErrors', 'LeApiQueryError', 'LeApiQueryErrors'], 'lodel.plugin.hooks': ['LodelHook'], 'lodel.logger': ['logger']}) -##@brief Tool to check if a target_class's object exists and is stored -#@param target_class : class where the object is supposed to be -#@param uid : a unique id in target_class -#@returns true if an object with unique id uid exists in target_class, false if not -def is_exist(target_class, uid): - from .leobject import LeObject - if not inspect.isclass(target_class) or not issubclass(target_class, LeObject): - raise TypeError("target class has to be a child class of LeObject but %s was given"% target_class) - datasource = target_class._ro_datasource - return datasource.is_exist(target_class,uid) - - ##@todo check datas when running query class LeQuery(object): - + ##@brief Hookname prefix _hook_prefix = None ##@brief arguments for the LeObject.check_data_value() @@ -73,7 +61,7 @@ class LeQuery(object): #@return query result def _query(self, **datas): raise NotImplementedError("Asbtract method") - + ##@return a dict with query infos def dump_infos(self): return {'target_class': self._target_class} @@ -118,7 +106,7 @@ class LeFilteredQuery(LeQuery): self.subqueries = None query_filters = [] if query_filters is None else query_filters self.set_query_filter(query_filters) - + ##@brief Abstract FilteredQuery execution method # # This method takes care to execute subqueries before calling super execute @@ -439,7 +427,7 @@ field to use for the relational filter" #
contributeur IN (1,2,3,5)
will be transformed into : #
(
     #       (
-    #           contributeur, 
+    #           contributeur,
     #           {
     #               auteur: 'lodel_id',
     #               traducteur: 'lodel_id'
@@ -494,7 +482,7 @@ class LeInsertQuery(LeQuery):
             raise LeApiQueryError("Trying to create an insert query on an \
 abstract LeObject : %s" % target_class)
         super().__init__(target_class)
-    
+
     ## @brief Implements an insert query operation, with only one insertion
     # @param datas : datas to be inserted
     def _query(self, datas):
@@ -536,7 +524,7 @@ class LeUpdateQuery(LeFilteredQuery):
     #@todo change strategy with instance update. We have to accept datas for
     #the execute method
     def __init__(self, target, query_filters=None):
-        ##@brief This attr is set only if the target argument is an 
+        ##@brief This attr is set only if the target argument is an
         #instance of a LeObject subclass
         self.__leobject_instance_datas = None
         target_class = target
@@ -565,8 +553,8 @@ target to LeUpdateQuery constructor"
             #Instance update
             #Building query_filter
             filters = [(
-                uid_name, 
-                '=', 
+                uid_name,
+                '=',
                 str(self.__leobject_instance_datas[uid_name]))]
             res = self._rw_datasource.update(
                 self._target_class, filters, [],
@@ -712,22 +700,22 @@ class LeGetQuery(LeFilteredQuery):
         l_datas=self._ro_datasource.select(
             target = self._target_class,
             field_list = fl,
-            filters = self._query_filter[0], 
-            relational_filters = self._query_filter[1], 
-            order = self._order, 
-            group = self._group, 
-            limit = self._limit, 
+            filters = self._query_filter[0],
+            relational_filters = self._query_filter[1],
+            order = self._order,
+            group = self._group,
+            limit = self._limit,
             offset = self._offset)
         return l_datas
 
     ##@return a dict with query infos
     def dump_infos(self):
         ret = super().dump_infos()
-        ret.update({  'field_list' : self._field_list, 
-                        'order' : self._order, 
-                        'group' : self._group, 
-                        'limit' : self._limit, 
-                        'offset': self._offset, 
+        ret.update({  'field_list' : self._field_list,
+                        'order' : self._order,
+                        'group' : self._group,
+                        'limit' : self._limit,
+                        'offset': self._offset,
        })
         return ret
 
@@ -742,5 +730,3 @@ offset={offset}"
                 res %= (n, subq)
         res += ">"
         return res
-
-
diff --git a/lodel/plugins/webui/interface/controllers/admin.py b/lodel/plugins/webui/interface/controllers/admin.py
index ef9420e..c151d30 100644
--- a/lodel/plugins/webui/interface/controllers/admin.py
+++ b/lodel/plugins/webui/interface/controllers/admin.py
@@ -19,17 +19,17 @@ 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 
+##@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 
+    # 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 
+##@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):
@@ -38,7 +38,7 @@ def admin_update(request):
     #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:
@@ -55,8 +55,8 @@ def admin_update(request):
         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:
@@ -72,7 +72,7 @@ def admin_update(request):
             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
@@ -91,11 +91,14 @@ def admin_update(request):
     else:
         # Check if the object actually exists
         # We get it from the database
-        if not target_leo.is_exist(lodel_id):
+        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 
+##@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):
@@ -138,7 +141,7 @@ def admin_create(request):
         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 
+##@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):
@@ -161,7 +164,7 @@ def admin_delete(request):
             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
@@ -186,11 +189,11 @@ def admin_delete(request):
             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
@@ -211,7 +214,7 @@ def delete_object(request):
     #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
@@ -313,7 +316,7 @@ def process_form(request):
                 if hasattr(dh, 'default'):
                     value = dh.default
                 else:
-                    #if not explicit default value, enforcing default as 
+                    #if not explicit default value, enforcing default as
                     #an empty list
                     value = []
             else:
@@ -328,4 +331,3 @@ def process_form(request):
         del(res)
         raise HttpErrors(errors, title="Form validation error")
     return res
-
diff --git a/lodel/plugins/webui/interface/controllers/listing.py b/lodel/plugins/webui/interface/controllers/listing.py
index 76ea45a..110260d 100644
--- a/lodel/plugins/webui/interface/controllers/listing.py
+++ b/lodel/plugins/webui/interface/controllers/listing.py
@@ -32,7 +32,7 @@ def collections(request):
 def issue(request):
     lodel_id = request.GET['lodel_id']
     return get_response('listing/issue.html', lodel_id=lodel_id[0], my_classes=dyncode )
-    
+
 ##@brief Controller's function to display a type (class) of the editorial model
 # @param request : the request (get or post)
 # @note the response is given in a html page called in get_response_function
@@ -65,7 +65,7 @@ def show_object(request):
             classname = None
     else:
         raise HttpException(400)
-    
+
     logger.warning('Composed uids broken here')
     uid_field = target_leo.uid_fieldname()[0]
 
@@ -82,7 +82,10 @@ def show_object(request):
     if not test_valid:
         raise HttpException(400)
     else:
-        if not target_leo.is_exist(lodel_id):
+        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('listing/show_object.html', lodel_id=lodel_id, classname=classname)
 
@@ -101,7 +104,7 @@ def show_object_detailled(request):
             classname = None
     else:
         raise HttpException(400)
-    
+
     logger.warning('Composed uids broken here')
     uid_field = target_leo.uid_fieldname()[0]
 
@@ -118,7 +121,10 @@ def show_object_detailled(request):
     if not test_valid:
         raise HttpException(400)
     else:
-        if not target_leo.is_exist(lodel_id):
+        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('listing/show_object_detailled.html', lodel_id=lodel_id, classname=classname)