@@ -213,6 +213,8 @@ def delete(self):
213213 logging .fatal ("{} Not in table {}" .format (self .id , self .table_name ))
214214 raise Exception ("{} Not in table {}" .format (self .id , self .table_name ))
215215
216+ # TODO: Consolidate get, get_multi and get_multis in one function !
217+
216218 @classmethod
217219 def get (cls , ** kwargs ):
218220 """
@@ -297,6 +299,55 @@ def get_multi(cls, **kwargs):
297299 else :
298300 raise ValueError ("Not found" )
299301
302+ @classmethod
303+ def get_multis (cls , ** kwargs ):
304+ """
305+ Get models from the database, using multiple keyword argument as a filter.
306+
307+ Class method allows you to use without instanciation eg.
308+
309+ model = Model.get(username="test", email="test@example.org")
310+
311+ Returns list of instances on success and raises an error if the row count was 0
312+
313+ """
314+
315+ keys = []
316+ values = []
317+ for key , value in kwargs .items ():
318+ keys .append (key )
319+ values .append (value )
320+
321+ where = ""
322+ length = len (keys )
323+ for index , (key , value ) in enumerate (zip (keys , values )):
324+ if index == length - 1 :
325+ where = where + f"{ key } ={ value } "
326+ else :
327+ where = where + f"{ key } ={ value } and "
328+
329+ temp = cls ()
330+ with temp .db .cursor () as c :
331+ c .execute (
332+ """
333+ SELECT
334+ {fields}
335+ FROM
336+ {table}
337+ WHERE {where}""" .format (
338+ fields = ", " .join (temp .fields .keys ()), table = cls .table_name , where = where
339+ )
340+ )
341+
342+ data = c .fetchall ()
343+ if data :
344+ ret_list = []
345+ for i in data :
346+ ret_list .append (cls (i ))
347+ return ret_list
348+ else :
349+ raise ValueError ("Not found" )
350+
300351 @classmethod
301352 def select_all (cls ):
302353 logging .debug ("Getting all items from table {}" .format (cls .table_name ))
0 commit comments