From 7596e942fa0a82e87a7255a8a7de9efec7e64338 Mon Sep 17 00:00:00 2001 From: the catalyst Date: Tue, 10 Mar 2020 15:55:07 +0530 Subject: [PATCH] Add a function to check admin in the user class and fix codacy errors and improve the admin creation command --- fact-bounty-flask/api/commands.py | 15 +++++++++++++-- fact-bounty-flask/api/user/model.py | 11 +++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/fact-bounty-flask/api/commands.py b/fact-bounty-flask/api/commands.py index 97f0f5fc..36e0de40 100644 --- a/fact-bounty-flask/api/commands.py +++ b/fact-bounty-flask/api/commands.py @@ -6,7 +6,7 @@ import coverage import sys import click -from flask import current_app, make_response, jsonify +from flask import current_app, make_response from flask.cli import with_appcontext from flask_migrate import upgrade from werkzeug.exceptions import MethodNotAllowed, NotFound @@ -201,7 +201,17 @@ def create_admin(): create an admin user """ admin_username = input("Enter the admin username: ") - admin_password = getpass.getpass(prompt="Enter the admin password: ") + passwords_not_same = False + + while not passwords_not_same: + admin_password = getpass.getpass(prompt="Enter the admin password: ") + admin_password2 = getpass.getpass(prompt="Renter the admin password: ") + valid = bool(admin_password == admin_password2) + if valid: + break + else: + print("Passwords dont match renter the passwords") + pass admin_role = "admin" valid = False while not valid: @@ -219,5 +229,6 @@ def create_admin(): role=admin_role, ) user.save() + except Exception as e: return {"Error occured": str(e)} diff --git a/fact-bounty-flask/api/user/model.py b/fact-bounty-flask/api/user/model.py index 19ac0ede..a19b35f5 100644 --- a/fact-bounty-flask/api/user/model.py +++ b/fact-bounty-flask/api/user/model.py @@ -67,6 +67,17 @@ def find_by_verification_token(cls, verification_token): verification_token=verification_token ).first() + @classmethod + def verify_admin(cls, email): + """ + Verify admin role + """ + try: + query = cls.query.filter_by(email=email, role="admin").first() + return print(query) + except Exception as err: + print("Error: ", err) + @staticmethod def generate_token(): """