ソースを参照

Adds LeQuery.is_exist function to test the existence of an LeObject instance

prieto 8年前
コミット
54b1521d09
2個のファイルの変更22行の追加1行の削除
  1. 11
    0
      lodel/leapi/query.py
  2. 11
    1
      lodel/plugins/mongodb_datasource/datasource.py

+ 11
- 0
lodel/leapi/query.py ファイルの表示

@@ -13,6 +13,17 @@ LodelContext.expose_modules(globals(), {
13 13
     'lodel.plugin.hooks': ['LodelHook'],
14 14
     'lodel.logger': ['logger']})
15 15
 
16
+##@brief Tool to check if a target_class's object exists and is stored
17
+#@param target_class : class where the object is supposed to be
18
+#@param uid : a unique id in target_class
19
+#@returns true if an object with unique id uid exists in target_class, false if not
20
+def is_exist(target_class, uid):
21
+	from .leobject import LeObject
22
+	if not inspect.isclass(target_class) or not issubclass(target_class, LeObject):
23
+		raise TypeError("target class has to be a child class of LeObject but %s was given"% target_class)
24
+	datasource = target_class._ro_datasource
25
+	return datasource.is_exist(target_class,uid)
26
+
16 27
 
17 28
 ##@todo check datas when running query
18 29
 class LeQuery(object):

+ 11
- 1
lodel/plugins/mongodb_datasource/datasource.py ファイルの表示

@@ -1,4 +1,4 @@
1
-# -*- coding: utf-8 -*-
1
+object# -*- coding: utf-8 -*-
2 2
 
3 3
 import re
4 4
 import warnings
@@ -874,3 +874,13 @@ field/operator couple in a query. We will keep only the first one")
874 874
                 #with sets
875 875
                 datas[dname] = list(datas[dname])
876 876
         return datas
877
+
878
+    ##@brief Tool to check if a record with unique id uid is set in the target_class representation
879
+    #@param target_class : class to check in 
880
+    #@param uid : a unique id in target_class
881
+    #@returns true if a record with unique id uid exists in the target_class representation, false if not
882
+    def is_exist(self, target_class, uid):
883
+    # retrouver la table qui correspond à target_class
884
+    # vérifier qu'il existe, ou pas, un enregistrement contenant uid
885
+        result = self.select(self, target_class, [target_class.uid_fieldname], filters = [(target_class.uid_fieldname, '=', uid)])
886
+        return len(result) == 1

読み込み中…
キャンセル
保存