admintool cli to make a user into an admin
This commit is contained in:
parent
6e1ebd63b2
commit
fb5c4bac27
0
verse/adminpage.py
Normal file
0
verse/adminpage.py
Normal file
41
verse/admintool.py
Normal file
41
verse/admintool.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import sys
|
||||||
|
from app import create_app, db
|
||||||
|
from sqlalchemy.exc import (
|
||||||
|
InvalidRequestError,
|
||||||
|
InterfaceError,
|
||||||
|
DataError,
|
||||||
|
DatabaseError,
|
||||||
|
)
|
||||||
|
from usermodel import User # noqa: F401
|
||||||
|
from distribusimodel import Distribusis # noqa: F401
|
||||||
|
|
||||||
|
def admintool():
|
||||||
|
"""Admin CLI tool. To elevate a user to admin"""
|
||||||
|
app = create_app()
|
||||||
|
app.app_context().push()
|
||||||
|
elevateusertoadmin()
|
||||||
|
|
||||||
|
|
||||||
|
def elevateusertoadmin():
|
||||||
|
"""To elevates user of first command line argument to admin"""
|
||||||
|
user = User.query.filter_by(email=sys.argv[1]).first()
|
||||||
|
print(f"user {user.username} found with email {user.email}")
|
||||||
|
try:
|
||||||
|
user.admin = True
|
||||||
|
db.session.commit()
|
||||||
|
print(f"Account {user.email} succesfully made into an admin")
|
||||||
|
except InvalidRequestError:
|
||||||
|
db.session.rollback()
|
||||||
|
print("Something went wrong!")
|
||||||
|
except InterfaceError:
|
||||||
|
db.session.rollback()
|
||||||
|
print("Error connecting to the database")
|
||||||
|
except DataError:
|
||||||
|
db.session.rollback()
|
||||||
|
print("Invalid Entry")
|
||||||
|
except DatabaseError:
|
||||||
|
db.session.rollback()
|
||||||
|
print("Error connecting to the database")
|
||||||
|
|
||||||
|
|
||||||
|
admintool()
|
@ -14,7 +14,7 @@ class RegisterForm(FlaskForm):
|
|||||||
"""Register for distribusi-verse form class"""
|
"""Register for distribusi-verse form class"""
|
||||||
username = StringField(
|
username = StringField(
|
||||||
"Username:",
|
"Username:",
|
||||||
validators=[validators.InputRequired(), Length(6, 150)],
|
validators=[validators.InputRequired(), Length(3, 150)],
|
||||||
)
|
)
|
||||||
|
|
||||||
email = StringField(
|
email = StringField(
|
||||||
|
@ -12,8 +12,9 @@ class User(UserMixin, db.Model):
|
|||||||
email = db.Column(db.String(150), unique=True, nullable=False)
|
email = db.Column(db.String(150), unique=True, nullable=False)
|
||||||
password = db.Column(db.String(300), nullable=False, unique=False)
|
password = db.Column(db.String(300), nullable=False, unique=False)
|
||||||
currentdistribusi = db.Column(db.String(300), nullable=True, unique=False)
|
currentdistribusi = db.Column(db.String(300), nullable=True, unique=False)
|
||||||
admin = db.Column(db.Boolean, server_default="false")
|
tutor = db.Column(db.Boolean, default=False)
|
||||||
tutor = db.Column(db.Boolean, server_default="false")
|
admin = db.Column(db.Boolean, default=False)
|
||||||
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<User %r>" % self.email
|
return "<User %r>" % self.email
|
||||||
|
Loading…
Reference in New Issue
Block a user