6
0
Kaynağa Gözat

updated some files queries

Dimitri Korsch 3 yıl önce
ebeveyn
işleme
f17487145b
2 değiştirilmiş dosya ile 18 ekleme ve 31 silme
  1. 9 9
      pycs/database/File.py
  2. 9 22
      pycs/database/Project.py

+ 9 - 9
pycs/database/File.py

@@ -94,11 +94,7 @@ class File(NamedBaseModel):
 
         :return: another file or None
         """
-        # return self.project.files.filter(*query)\
-        return File.query.filter_by(project_id=self.project_id)\
-            .filter(*query)\
-            .order_by(File.id.desc())\
-            .first()
+        return File.query.filter_by(project_id=self.project_id).filter(*query)
 
     def next(self) -> T.Optional[File]:
         """
@@ -106,7 +102,8 @@ class File(NamedBaseModel):
 
         :return: another file or None
         """
-        return self._get_another_file(File.id > self.id)
+        return self._get_another_file(File.id > self.id)\
+            .order_by(File.id).first()
 
 
     def previous(self) -> T.Optional[File]:
@@ -115,7 +112,8 @@ class File(NamedBaseModel):
 
         :return: another file or None
         """
-        return self._get_another_file(File.id < self.id)
+        return self._get_another_file(File.id < self.id)\
+            .order_by(File.id.desc()).first()
 
 
     def next_in_collection(self) -> T.Optional[File]:
@@ -124,7 +122,8 @@ class File(NamedBaseModel):
 
         :return: another file or None
         """
-        return self._get_another_file(File.id > self.id, Collection.id == self.collection_id)
+        return self._get_another_file(File.id > self.id, Collection.id == self.collection_id)\
+            .order_by(File.id).first()
 
 
     def previous_in_collection(self) -> T.Optional[File]:
@@ -133,7 +132,8 @@ class File(NamedBaseModel):
 
         :return: another file or None
         """
-        return self._get_another_file(File.id < self.id, Collection.id == self.collection_id)
+        return self._get_another_file(File.id < self.id, Collection.id == self.collection_id)\
+            .order_by(File.id.desc()).first()
 
 
     def result(self, id: int) -> T.Optional[Result]:

+ 9 - 22
pycs/database/Project.py

@@ -194,22 +194,21 @@ class Project(NamedBaseModel):
         """
         return self.files.order_by(File.id).offset(offset).limit(limit)
 
+    def _files_without_results(self):
+        """
+        get files without any results
+        :return: a query object
+        """
+        return self.files.filter(~File.results.any())
+
     def count_files_without_results(self) -> int:
         """
         count files without associated results
 
         :return: count
         """
-        raise NotImplementedError
 
-        with closing(self.database.con.cursor()) as cursor:
-            cursor.execute('''
-                SELECT COUNT(*)
-                FROM files
-                LEFT JOIN results ON files.id = results.file
-                WHERE files.project = ? AND results.id IS NULL
-            ''', [self.id])
-            return cursor.fetchone()[0]
+        return self._files_without_results().count()
 
     def files_without_results(self) -> T.Iterator[File]:
         """
@@ -217,19 +216,7 @@ class Project(NamedBaseModel):
 
         :return: list of files
         """
-        raise NotImplementedError
-
-        with closing(self.database.con.cursor()) as cursor:
-            cursor.execute('''
-                SELECT files.*
-                FROM files
-                LEFT JOIN results ON files.id = results.file
-                WHERE files.project = ? AND results.id IS NULL
-                ORDER BY id ASC
-            ''', [self.id])
-
-            for row in cursor:
-                yield File(self.database, row)
+        return self._files_without_results().all()
 
     def files_without_collection(self, offset: int = 0, limit: int = -1) -> T.Iterator[File]:
         """