[PATCH 4/5] Update the logic

Pierre-Yves Chibon pingou at pingoured.fr
Sun Jan 27 13:11:28 UTC 2013


- The check_for_duplicates should be a parameter since the logic can be called from
the API where the form validation is different

- When raising an error, give an error message, this way when the API calls the function
and catch the error, it can make something of it.
---
 coprs_frontend/coprs/logic/coprs_logic.py | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/coprs_frontend/coprs/logic/coprs_logic.py b/coprs_frontend/coprs/logic/coprs_logic.py
index a862055..a27668e 100644
--- a/coprs_frontend/coprs/logic/coprs_logic.py
+++ b/coprs_frontend/coprs/logic/coprs_logic.py
@@ -47,7 +47,8 @@ class CoprsLogic(object):
         return query
 
     @classmethod
-    def add(cls, user, name, repos, selected_chroots, description, instructions):
+    def add(cls, user, name, repos, selected_chroots, description,
+        instructions, check_for_duplicates=False):
         copr = models.Copr(name=name,
                            repos=repos,
                            owner=user,
@@ -55,21 +56,23 @@ class CoprsLogic(object):
                            instructions=instructions,
                            created_on=int(time.time()))
         CoprsLogic.new(user, copr,
-            check_for_duplicates=False) # form validation checks for duplicates
+            check_for_duplicates=check_for_duplicates) # form validation checks for duplicates
         CoprChrootsLogic.new_from_names(user, copr,
             selected_chroots)
         return copr
 
     @classmethod
     def new(cls, user, copr, check_for_duplicates = True):
-        if check_for_duplicates and cls.exists_for_current_user(user, copr.name):
-            raise exceptions.DuplicateCoprNameException
+        if check_for_duplicates and cls.exists_for_current_user(user, copr.name).all():
+            raise exceptions.DuplicateCoprNameException(
+                'Copr: "{0}" already exists'.format(copr.name))
         db.session.add(copr)
 
     @classmethod
     def update(cls, user, copr, check_for_duplicates = True):
-        if check_for_duplicates and cls.exists_for_current_user(user, copr.name):
-            raise exceptions.DuplicateCoprNameException
+        if check_for_duplicates and cls.exists_for_current_user(user, copr.name).all():
+            raise exceptions.DuplicateCoprNameException(
+                'Copr: "{0}" already exists'.format(copr.name))
         db.session.add(copr)
 
     @classmethod
-- 
1.8.1



More information about the copr-devel mailing list