From 4dc5c666107834bf2a0f9dbb52c8d534a15618a6 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 03 2017 13:42:01 +0000 Subject: Fix editing tags of a project We had an issue where we were trying to find Tag object using a query that returned TagColored objects. Of course that didn't work. So when we were re-adding an existing tag to a new project, the logic was trying to create a Tag object that already existed. That resulted in an IntegrityError due to failing the unique constraint. This commit fixes this by creating a new get_colored_tag that will searched if a TagColored exists and adjust get_tag to search if a Tag exists, making the workflow work. Fixes https://pagure.io/pagure/issue/1818 --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 5805800..377bcfe 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -331,13 +331,8 @@ def add_tag_obj(session, obj, tags, user, ticketfolder): if known: continue - if isinstance(obj, model.Project): - proj_id = obj.id - else: - proj_id = obj.project.id - tagobj = get_tag(session, objtag, proj_id) - if obj.isa == 'project': + tagobj = get_tag(session, objtag) if not tagobj: tagobj = model.Tag(tag=objtag) @@ -350,10 +345,11 @@ def add_tag_obj(session, obj, tags, user, ticketfolder): ) else: + tagobj = get_colored_tag(session, objtag, obj.project.id) if not tagobj: tagobj = model.TagColored( tag=objtag, - project_id=proj_id + project_id=obj.project.id ) session.add(tagobj) session.flush() @@ -658,7 +654,7 @@ def remove_tags(session, project, tags, ticketfolder, user): removed_tags = [] tag_found = False for tag in tags: - tagobj = get_tag(session, tag, project.id) + tagobj = get_colored_tag(session, tag, project.id) if tagobj: tag_found = True removed_tags.append(tag) @@ -744,7 +740,7 @@ def edit_issue_tags( old_tag_name = old_tag if not isinstance(old_tag, model.TagColored): - old_tag = get_tag(session, old_tag_name, project.id) + old_tag = get_colored_tag(session, old_tag_name, project.id) if not old_tag: raise pagure.exceptions.PagureException( @@ -766,7 +762,7 @@ def edit_issue_tags( new_tag_description, new_tag_color)) elif old_tag.tag != new_tag: # Check if new tag already exists - existing_tag = get_tag(session, new_tag, project.id) + existing_tag = get_colored_tag(session, new_tag, project.id) if existing_tag: raise pagure.exceptions.PagureException( 'Can not rename a tag to an existing tag name: %s' % new_tag) @@ -2108,10 +2104,22 @@ def get_tags_of_project(session, project, pattern=None): return query.all() -def get_tag(session, tag, project_id): +def get_tag(session, tag): ''' Returns a Tag object for the given tag text. ''' query = session.query( + model.Tag + ).filter( + model.Tag.tag == tag + ) + + return query.first() + + +def get_colored_tag(session, tag, project_id): + ''' Returns a TagColored object for the given tag text. + ''' + query = session.query( model.TagColored ).filter( model.TagColored.tag == tag diff --git a/pagure/ui/issues.py b/pagure/ui/issues.py index 8bca123..09747b0 100644 --- a/pagure/ui/issues.py +++ b/pagure/ui/issues.py @@ -384,7 +384,7 @@ def edit_tag(repo, tag, username=None, namespace=None): flask.abort(404, 'Project has no tags to edit') # Check the tag exists, and get its old/original color - tagobj = pagure.lib.get_tag(SESSION, tag, repo.id) + tagobj = pagure.lib.get_colored_tag(SESSION, tag, repo.id) if not tagobj: flask.abort(404, 'Tag %s not found in this project' % tag)