6
0
Quellcode durchsuchen

removed some unnecessary transaction creations

Dimitri Korsch vor 3 Jahren
Ursprung
Commit
ec9e1c7373

+ 0 - 10
pycs/database/Project.py

@@ -215,16 +215,6 @@ class Project(NamedBaseModel):
         return file, is_new
 
 
-    def set_description(self, description: str):
-        """
-        set this projects description
-
-        :param description: new description
-        :return:
-        """
-        self.description = description
-        self.commit()
-
     def count_files(self) -> int:
         """
         count files associated with this project

+ 6 - 9
pycs/frontend/endpoints/labels/RemoveLabel.py

@@ -19,7 +19,7 @@ class RemoveLabel(View):
         # extract request data
         data = request.get_json(force=True)
 
-        if 'remove' not in data or data['remove'] is not True:
+        if data.get('remove', False):
             abort(400)
 
         # find project
@@ -32,19 +32,16 @@ class RemoveLabel(View):
         if label is None:
             abort(404)
 
-        # find children
-        children = label.children
-
         # start transaction
         with db.session.begin_nested():
             # remove children's parent entry
-            for child in children:
-                child.set_parent(None)
+            for child in label.children:
+                child.set_parent(None, commit=False)
                 NotificationManager.edited("label", child.id, Label)
 
-            # remove label
-            label.remove()
-            NotificationManager.removed("label", label.serialize())
+        # remove label
+        label.remove()
+        NotificationManager.removed("label", label.serialize())
 
         # return success response
         return make_response()

+ 10 - 10
pycs/frontend/endpoints/projects/EditProjectDescription.py

@@ -1,7 +1,6 @@
 from flask import make_response, abort
 from flask.views import View, request
 
-from pycs import db
 from pycs.database.Project import Project
 from pycs.frontend.notifications.NotificationManager import NotificationManager
 
@@ -18,17 +17,18 @@ class EditProjectDescription(View):
         # extract request data
         data = request.get_json(force=True)
 
-        if 'description' not in data or not data['description']:
+        if data.get('description') is None:
             return abort(400)
 
-        with db.session.begin_nested():
-            # find project
-            project = Project.query.get(identifier)
-            if project is None:
-                return abort(404)
+        # find project
+        project = Project.query.get(identifier)
+        if project is None:
+            return abort(404)
 
-            # set description
-            project.set_description(data['description'])
-            NotificationManager.edited("project", project.id, Project)
+        # set description
+        project.description = data['description']
+        project.commit()
+
+        NotificationManager.edited("project", project.id, Project)
 
         return make_response()

+ 10 - 11
pycs/frontend/endpoints/projects/EditProjectName.py

@@ -1,7 +1,6 @@
 from flask import make_response, abort
 from flask.views import View, request
 
-from pycs import db
 from pycs.database.Project import Project
 from pycs.frontend.notifications.NotificationManager import NotificationManager
 
@@ -21,15 +20,15 @@ class EditProjectName(View):
         if data.get('name') is None:
             return abort(400)
 
-        # start transaction
-        with db.session.begin_nested():
-            # find project
-            project = Project.query.get(identifier)
-            if project is None:
-                return abort(404)
+        # find project
+        project = Project.query.get(identifier)
+        if project is None:
+            return abort(404)
 
-            # set name
-            project.set_name(data['name'])
-            NotificationManager.edited("project", project.id, Project)
+        # set name
+        project.name = data['name']
+        project.commit()
 
-            return make_response()
+        NotificationManager.edited("project", project.id, Project)
+
+        return make_response()

+ 6 - 8
pycs/frontend/endpoints/results/CreateResult.py

@@ -1,7 +1,6 @@
 from flask import request, abort, jsonify
 from flask.views import View
 
-from pycs import db
 from pycs.database.File import File
 from pycs.database.Result import Result
 from pycs.frontend.notifications.NotificationManager import NotificationManager
@@ -49,13 +48,12 @@ class CreateResult(View):
 
 
         removed = []
-        # start transaction
-        with db.session.begin_nested():
-            # find full-image labels and remove them
-            results = file.results.filter_by(type='labeled-image')
-            for result in results.all():
-                removed.append(result.serialize())
-            results.delete()
+
+        # find full-image labels and remove them
+        results = file.results.filter_by(type='labeled-image')
+        for result in results.all():
+            removed.append(result.serialize())
+        results.delete()
 
         # insert into database
         new_result = file.create_result('user', rtype, label, data)

+ 1 - 0
pycs/frontend/endpoints/results/EditResultData.py

@@ -26,6 +26,7 @@ class EditResultData(View):
 
         result.data = data['data']
         result.origin = 'user'
+        result.commit()
 
         NotificationManager.edited("result", result.id, Result)
         return make_response()

+ 3 - 4
pycs/frontend/endpoints/results/EditResultLabel.py

@@ -29,10 +29,9 @@ class EditResultLabel(View):
         if result.type == 'labeled-image' and not data['label']:
             return abort(400)
 
-        # start transaction and set label
-        with db.session.begin_nested():
-            result.label_id = int(data['label'])
-            result.origin = 'user'
+        result.label_id = int(data['label'])
+        result.origin = 'user'
+        result.commit()
 
         NotificationManager.edited("result", result.id, Result)
         return make_response()

+ 5 - 9
pycs/frontend/endpoints/results/ResetResults.py

@@ -1,7 +1,6 @@
 from flask import make_response, abort
 from flask.views import View, request
 
-from pycs import db
 from pycs.database.File import File
 from pycs.database.Result import Result
 from pycs.frontend.notifications.NotificationManager import NotificationManager
@@ -27,15 +26,12 @@ class ResetResults(View):
         if file is None:
             return abort(404)
 
-        # get results
-        results = file.results.all()
-
-        # start transaction
         removed = []
-        with db.session.begin_nested():
-            for result in results:
-                removed.append(result.serialize())
-                result.remove()
+
+        for result in file.results.all():
+            removed.append(result.serialize())
+
+        file.results.delete()
 
         for result in removed:
             NotificationManager.removed("result", result, Result)

+ 4 - 2
test/test_database.py

@@ -125,10 +125,12 @@ class DatabaseTests(BaseTestCase):
         # set properties
         project = Project.query.first()
 
-        project.set_name('Project 0')
+        project.name = 'Project 0'
+        project.commit()
         self.assertEqual(Project.query.first().name, 'Project 0')
 
-        project.set_description('Description 0')
+        project.description = 'Description 0'
+        project.commit()
         self.assertEqual(Project.query.first().description, 'Description 0')