From 8c63c19cd9b14f88e9b9f0ec4573838c83c05c5d Mon Sep 17 00:00:00 2001
From: Abhijeet Kasurde <akasurde@redhat.com>
Date: Mar 31 2017 03:32:40 +0000
Subject: Refactor user existence code in API and UI


Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>

---

diff --git a/pagure/api/user.py b/pagure/api/user.py
index 67386a1..6d16054 100644
--- a/pagure/api/user.py
+++ b/pagure/api/user.py
@@ -90,9 +90,7 @@ def api_view_user(username):
     httpcode = 200
     output = {}
 
-    user = pagure.lib.search_user(SESSION, username=username)
-    if not user:
-        raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOUSER)
+    user = _get_user(username=username)
 
     repopage = flask.request.args.get('repopage', 1)
     try:
@@ -203,9 +201,7 @@ def api_view_user_activity_stats(username):
     """
     date_format = flask.request.args.get('format', 'isoformat')
 
-    user = pagure.lib.search_user(SESSION, username=username)
-    if not user:
-        raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOUSER)
+    user = _get_user(username=username)
 
     stats = pagure.lib.get_yearly_stats_user(
         SESSION,
@@ -310,9 +306,7 @@ def api_view_user_activity_date(username, date):
         raise pagure.exceptions.APIError(
             400, error_code=APIERROR.ENOCODE, error=str(err))
 
-    user = pagure.lib.search_user(SESSION, username=username)
-    if not user:
-        raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOUSER)
+    user = _get_user(username=username)
 
     activities = pagure.lib.get_user_activity_day(SESSION, user, date)
     js_act = []
@@ -353,3 +347,14 @@ def api_view_user_activity_date(username, date):
         )
     )
     return jsonout
+
+
+def _get_user(username=None):
+    """ Check user is valid or not
+    """
+    user = None
+    try:
+        user = pagure.lib.get_user(SESSION, username)
+    except pagure.exceptions.PagureException:
+        raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOUSER)
+    return user
diff --git a/pagure/ui/app.py b/pagure/ui/app.py
index a21f273..b4dbdd4 100644
--- a/pagure/ui/app.py
+++ b/pagure/ui/app.py
@@ -72,10 +72,7 @@ def index():
 def index_auth():
     """ Front page for authenticated user.
     """
-    user = pagure.lib.search_user(SESSION, username=flask.g.fas_user.username)
-    if not user:
-        flask.abort(404, 'No user `%s` found, re-login maybe?' % (
-            flask.g.fas_user.username))
+    user = _get_user(username=flask.g.fas_user.username)
 
     repopage = flask.request.args.get('repopage', 1)
     try:
@@ -275,9 +272,7 @@ def view_projects(pattern=None, namespace=None):
 def view_user(username):
     """ Front page of a specific user.
     """
-    user = pagure.lib.search_user(SESSION, username=username)
-    if not user:
-        flask.abort(404, 'No user `%s` found' % username)
+    user = _get_user(username=username)
 
     repopage = flask.request.args.get('repopage', 1)
     try:
@@ -348,9 +343,7 @@ def view_user(username):
 def view_user_requests(username):
     """ Shows the pull-requests for the specified user.
     """
-    user = pagure.lib.search_user(SESSION, username=username)
-    if not user:
-        flask.abort(404, 'No user `%s` found' % username)
+    user = _get_user(username=username)
 
     requests = pagure.lib.get_pull_request_of_user(
         SESSION,
@@ -378,9 +371,7 @@ def view_user_issues(username):
     if not APP.config.get('ENABLE_TICKETS', True):
         flask.abort(404, 'Tickets have been disabled on this pagure instance')
 
-    user = pagure.lib.search_user(SESSION, username=username)
-    if not user:
-        flask.abort(404, 'No user `%s` found' % username)
+    user = _get_user(username=username)
 
     return flask.render_template(
         'user_issues.html',
@@ -466,10 +457,7 @@ def user_settings():
         return flask.redirect(
             flask.url_for('auth_login', next=flask.request.url))
 
-    user = pagure.lib.search_user(
-        SESSION, username=flask.g.fas_user.username)
-    if not user:
-        flask.abort(404, 'User not found')
+    user = _get_user(username=flask.g.fas_user.username)
 
     form = pagure.forms.UserSettingsForm()
     if form.validate_on_submit():
@@ -513,10 +501,7 @@ def update_user_settings():
         return flask.redirect(
             flask.url_for('auth_login', next=flask.request.url))
 
-    user = pagure.lib.search_user(
-        SESSION, username=flask.g.fas_user.username)
-    if not user:
-        flask.abort(404, 'User not found')
+    user = _get_user(username=flask.g.fas_user.username)
 
     form = pagure.forms.ConfirmationForm()
 
@@ -567,10 +552,7 @@ def remove_user_email():
         return flask.redirect(
             flask.url_for('auth_login', next=flask.request.url))
 
-    user = pagure.lib.search_user(
-        SESSION, username=flask.g.fas_user.username)
-    if not user:
-        flask.abort(404, 'User not found')
+    user = _get_user(username=flask.g.fas_user.username)
 
     if len(user.emails) == 1:
         flask.flash(
@@ -618,10 +600,7 @@ def add_user_email():
         return flask.redirect(
             flask.url_for('auth_login', next=flask.request.url))
 
-    user = pagure.lib.search_user(
-        SESSION, username=flask.g.fas_user.username)
-    if not user:
-        flask.abort(404, 'User not found')
+    user = _get_user(username=flask.g.fas_user.username)
 
     form = pagure.forms.UserEmailForm(
         emails=[mail.email for mail in user.emails])
@@ -656,10 +635,7 @@ def set_default_email():
         return flask.redirect(
             flask.url_for('auth_login', next=flask.request.url))
 
-    user = pagure.lib.search_user(
-        SESSION, username=flask.g.fas_user.username)
-    if not user:
-        flask.abort(404, 'User not found')
+    user = _get_user(username=flask.g.fas_user.username)
 
     form = pagure.forms.UserEmailForm()
     if form.validate_on_submit():
@@ -697,10 +673,7 @@ def reconfirm_email():
         return flask.redirect(
             flask.url_for('auth_login', next=flask.request.url))
 
-    user = pagure.lib.search_user(
-        SESSION, username=flask.g.fas_user.username)
-    if not user:
-        flask.abort(404, 'User not found')
+    user = _get_user(username=flask.g.fas_user.username)
 
     form = pagure.forms.UserEmailForm()
     if form.validate_on_submit():
@@ -772,10 +745,7 @@ def add_api_user_token():
             flask.url_for('auth_login', next=flask.request.url))
 
     # Ensure the user is in the DB at least
-    user = pagure.lib.search_user(
-        SESSION, username=flask.g.fas_user.username)
-    if not user:
-        flask.abort(404, 'User not found')
+    user = _get_user(username=flask.g.fas_user.username)
 
     acls = pagure.lib.get_acls(
         SESSION, restrict=APP.config.get('CROSS_PROJECT_ACLS'))
@@ -845,3 +815,14 @@ def revoke_api_user_token(token_id):
                 'error')
 
     return flask.redirect(flask.url_for('.user_settings'))
+
+
+def _get_user(username=None):
+    """ Check if user exists or not
+    """
+    user = None
+    try:
+        user = pagure.lib.get_user(SESSION, username)
+    except pagure.exceptions.PagureException as e:
+        flask.abort(404, e.message)
+    return user

