0
\$\begingroup\$

I know this may be a silly question..., but I'm trying to create a function in python that allows me to register an account for the admin user.

If the administrator does not specify any data, be it name, email address, or password, the default data will be specified so that the administrator is able to log in.

And in fact, the approach that I took works as I expected, but even so, it seems to me a bit..., I'm trying to familiarize myself a bit with python and stuff. I feel like I'm not using everything that could be done in python, to be more precise I feel like I'm stuck in a narrow scope. And while I know there is such a thing as setting default parameters, I'm a bit 'skeptical' about its uses, as I'm not very familiar with it, I really don't know if that would be an advantage or disadvantage, especially when it would be a large application.

That's why I was wondering if there was a... clever way? to achieve what I seek to do.

Here's the code:

"""
    application entry point.
"""

import getpass
import datetime

from flask.cli import FlaskGroup

from example import app, db, User

cli = FlaskGroup(app)


@cli.command('create_admin')
def create_admin():
    """
        creates admin user
    """
    firstname = str(input('enter your name: '))
    email = str(input('enter your email: '))
    password = getpass.getpass('Enter password: ')
    confirm_password = getpass.getpass("Enter password again: ")

    if firstname == "":
        firstname = 'admin'
    if email == "":
        email = 'admin@admin.com'
    if password == "":
        password = 'admin123'
    if confirm_password == "":
        confirm_password = 'admin123'

    if password != confirm_password:
        print('passwords don\'t match')
    else:
        try:
            db.session.add(User(
                    first_name=firstname,
                    email=email,
                    password=password,
                    is_admin=True,
                    is_confirmed=True,
                    confirmed_on=datetime.datetime.now()
                )
            )
            db.session.commit()
            print(f'admin with email {email} was created successfully!')
        except Exception:
            print("couldn't create admin user.")


if __name__ == "__main__":
    cli()

Note: This is the link to the complete code, it is supposed to work, so if you need to do some test or something feel free to access it. (And sorry to have to ask such a silly question.)

https://github.com/JcSilverX/my-secrets

The project is very simple, I'm just tried to make a replica, but smaller than the original, since the original has many more things. (Similar things that it has to the original I'm working on is the models.py & wsgi.py. I'm very new to python, so If you have any suggestions or improvements, I'd really appreciate it.)

\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.