Description: Check for None before using string methods, not after

(wtforms now returns None instead of an empty string for no input)

Origin: upstream d269390c 77a3fbf5 7832b71a 4351d921 dbb90e2c
 83ad40b6 2889d1e9 dcb9b6a9
Author: Michal Konečný, Dominik Wombacher
Forwarded: not-needed

--- a/pagure/ui/issues.py
+++ b/pagure/ui/issues.py
@@ -188,23 +188,30 @@ def update_issue(repo, issueid, username
 
         comment = form.comment.data
         depends = []
-        for depend in form.depending.data.split(","):
-            if depend.strip():
-                try:
-                    depends.append(int(depend.strip()))
-                except ValueError:
-                    pass
+        # This field is optional, check if it's filled first
+        if form.depending.data:
+            for depend in form.depending.data.split(","):
+                if depend.strip():
+                    try:
+                        depends.append(int(depend.strip()))
+                    except ValueError:
+                        pass
 
         blocks = []
-        for block in form.blocking.data.split(","):
-            if block.strip():
-                try:
-                    blocks.append(int(block.strip()))
-                except ValueError:
-                    pass
-
-        assignee = form.assignee.data.strip() or None
-        new_status = form.status.data.strip() or None
+        # Check if the optional field is filled
+        if form.blocking.data:
+            for block in form.blocking.data.split(","):
+                if block.strip():
+                    try:
+                        blocks.append(int(block.strip()))
+                    except ValueError:
+                        pass
+
+        assignee = None
+        # Check if the optional field is filled
+        if form.assignee.data:
+            assignee = form.assignee.data.strip()
+        new_status = form.status.data.strip() if form.status.data else None
         close_status = form.close_status.data or None
         if close_status not in repo.close_status:
             close_status = None
@@ -214,12 +221,15 @@ def update_issue(repo, issueid, username
             new_priority = int(form.priority.data)
         except (ValueError, TypeError):
             pass
-        tags = [tag.strip() for tag in form.tag.data.split(",") if tag.strip()]
+        tags = []
+        # Check if the optional field is filled
+        if form.tag.data:
+            tags = [tag.strip() for tag in form.tag.data.split(",")]
 
         new_milestone = None
         try:
             if repo.milestones:
-                new_milestone = form.milestone.data.strip() or None
+                new_milestone = form.milestone.data.strip() if form.milestone.data else None
         except AttributeError:
             pass
 
--- a/pagure/ui/app.py
+++ b/pagure/ui/app.py
@@ -1547,10 +1547,14 @@ def add_api_user_token():
 
     if form.validate_on_submit():
         try:
+            description = None
+            # Check if the optional value is filled
+            if form.description.data:
+                description = form.description.data.strip()
             pagure.lib.query.add_token_to_user(
                 flask.g.session,
                 project=None,
-                description=form.description.data.strip() or None,
+                description=description,
                 acls=form.acls.data,
                 username=user.username,
                 expiration_date=form.expiration_date.data,
--- a/pagure/api/fork.py
+++ b/pagure/api/fork.py
@@ -512,7 +512,10 @@ def api_pull_request_update(repo, reques
         )
     else:
         request.title = form.title.data.strip()
-        request.initial_comment = form.initial_comment.data.strip()
+        request.initial_comment = ""
+        # This value is optional, check first if it's filled
+        if form.initial_comment.data:
+            request.initial_comment = form.initial_comment.data.strip()
         flask.g.session.add(request)
         if not request.private and not request.project.private:
             pagure.lib.notify.log(
@@ -1126,7 +1129,7 @@ def api_pull_request_add_flag(repo, requ
         form = pagure.forms.AddPullRequestFlagFormV1(csrf_enabled=False)
     if form.validate_on_submit():
         username = form.username.data
-        percent = form.percent.data.strip() or None
+        percent = form.percent.data.strip() if form.percent.data else None
         comment = form.comment.data.strip()
         url = form.url.data.strip()
         uid = form.uid.data.strip() if form.uid.data else None
@@ -1603,7 +1606,10 @@ def api_pull_request_create(repo, userna
     if orig_commit:
         orig_commit = str(orig_commit.id)
 
-    initial_comment = form.initial_comment.data.strip() or None
+    initial_comment = None
+    # This value is optional, check first if it's filled
+    if form.initial_comment.data:
+        initial_comment = form.initial_comment.data.strip()
 
     commit_start = commit_stop = None
     if diff_commits:
--- a/pagure/api/project.py
+++ b/pagure/api/project.py
@@ -1750,7 +1750,9 @@ def api_fork_project():
     if form.validate_on_submit():
         repo = form.repo.data
         username = form.username.data or None
-        namespace = form.namespace.data.strip() or None
+        namespace = None
+        if form.namespace.data:
+            namespace = form.namespace.data.strip()
 
         repo = get_authorized_api_project(
             flask.g.session, repo, user=username, namespace=namespace
@@ -2441,7 +2443,7 @@ def api_commit_add_flag(repo, commit_has
     form = pagure.forms.AddPullRequestFlagForm(csrf_enabled=False)
     if form.validate_on_submit():
         username = form.username.data
-        percent = form.percent.data.strip() or None
+        percent = form.percent.data.strip() if form.percent.data else None
         comment = form.comment.data.strip()
         url = form.url.data.strip()
         uid = form.uid.data.strip() if form.uid.data else None
--- a/pagure/ui/fork.py
+++ b/pagure/ui/fork.py
@@ -557,9 +557,9 @@ def request_pull_edit(repo, requestid, u
 
     form = pagure.forms.RequestPullEditForm(branches=flask.g.branches)
     if form.validate_on_submit():
-        request.title = form.title.data.strip()
-        request.initial_comment = form.initial_comment.data.strip()
-        request.branch = form.branch_to.data.strip()
+        request.title = form.title.data.strip() if form.title.data else None
+        request.initial_comment = form.initial_comment.data.strip() if form.initial_comment.data else None
+        request.branch = form.branch_to.data.strip() if form.branch_to.data else None
         if flask.g.fas_user.username == request.user.username:
             request.allow_rebase = form.allow_rebase.data
         flask.g.session.add(request)
@@ -1722,7 +1722,7 @@ def new_request_pull(
             if orig_commit:
                 orig_commit = str(orig_commit.id)
 
-            initial_comment = form.initial_comment.data.strip() or None
+            initial_comment = form.initial_comment.data.strip() if form.initial_comment.data else None
             commit_start = commit_stop = None
             if diff_commits:
                 commit_stop = str(diff_commits[0].id)
@@ -1888,9 +1888,9 @@ def new_remote_request_pull(repo, userna
             except Exception as err:
                 flask.abort(500, description=err)
 
-        branch_from = form.branch_from.data.strip()
-        branch_to = form.branch_to.data.strip()
-        remote_git = form.git_repo.data.strip()
+        branch_from = form.branch_from.data.strip() if form.branch_from.data else None
+        branch_to = form.branch_to.data.strip() if form.branch_to.data else None
+        remote_git = form.git_repo.data.strip() if form.git_repo.data else None
 
         repopath = pagure.utils.get_remote_repo_path(remote_git, branch_from)
         if not repopath:
@@ -1970,7 +1970,7 @@ def new_remote_request_pull(repo, userna
                 user=flask.g.fas_user.username,
             )
 
-            if form.initial_comment.data.strip() != "":
+            if form.initial_comment.data and form.initial_comment.data.strip() != "":
                 pagure.lib.query.add_pull_request_comment(
                     flask.g.session,
                     request=request,
@@ -2028,7 +2028,7 @@ def new_remote_request_pull(repo, userna
         except pygit2.GitError:
             branch_to = "master"
     else:
-        branch_to = form.branch_to.data.strip()
+        branch_to = form.branch_to.data.strip() if form.branch_to.data else None
 
     return flask.render_template(
         "remote_pull_request.html",
--- a/pagure/ui/repo.py
+++ b/pagure/ui/repo.py
@@ -1408,18 +1408,24 @@ def update_project(repo, username=None,
 
         try:
             repo.description = form.description.data
-            repo.avatar_email = form.avatar_email.data.strip()
-            repo.url = form.url.data.strip()
+            # Check if the optional value is filled
+            if form.avatar_email.data is not None:
+                repo.avatar_email = form.avatar_email.data.strip()
+            # Check if the optional value is filled
+            if form.url.data:
+                repo.url = form.url.data.strip()
             if repo.private:
                 repo.private = form.private.data
             if repo.mirrored_from:
                 repo.mirrored_from = form.mirrored_from.data
-            pagure.lib.query.update_tags(
-                flask.g.session,
-                repo,
-                tags=[t.strip() for t in form.tags.data.split(",")],
-                username=flask.g.fas_user.username,
-            )
+            # Check if the optional value is filled
+            if form.tags.data:
+                pagure.lib.query.update_tags(
+                    flask.g.session,
+                    repo,
+                    tags=[t.strip() for t in form.tags.data.split(",")],
+                    username=flask.g.fas_user.username,
+                )
             flask.g.session.add(repo)
             flask.g.session.commit()
             flask.flash("Project updated")
@@ -2371,7 +2377,7 @@ def add_token(repo, username=None, names
             pagure.lib.query.add_token_to_user(
                 flask.g.session,
                 repo,
-                description=form.description.data.strip() or None,
+                description=form.description.data.strip() if form.description.data else None,
                 acls=form.acls.data,
                 username=flask.g.fas_user.username,
                 expiration_date=form.expiration_date.data,
@@ -2593,7 +2599,7 @@ def edit_file(repo, branchname, filename
                 branch=branchname,
                 branchto=form.branch.data,
                 filename=filename,
-                content=form.content.data,
+                content=form.content.data if form.content.data else str(),
                 message="%s\n\n%s"
                 % (
                     form.commit_title.data.strip(),
