#16 Added "role" required field for register endpoint

This commit is contained in:
2023-03-18 21:19:33 -04:00
parent 5d1710f023
commit 18e651f8b9
3 changed files with 46 additions and 7 deletions

View File

@@ -7,6 +7,7 @@ from werkzeug.security import generate_password_hash, check_password_hash
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
role = db.Column(db.String(32), index=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
last_seen = db.Column(db.DateTime, default=datetime.utcnow)
@@ -29,7 +30,7 @@ class User(UserMixin, db.Model):
}
def from_dict(self, data, new_user=False):
for field in ["username", "email"]:
for field in ["role", "username", "email"]:
if field in data:
setattr(self, field, data[field])
if new_user and "password" in data:

View File

@@ -18,7 +18,7 @@ def login_route():
data = request.get_json()
if current_user.is_authenticated:
return error_response(400, 'A user is already logged in!')
return error_response(400, "A user is already logged in!")
if not data.get("user_id") or not data.get("password"):
return error_response(400, "Must supply user_id and password")
@@ -34,6 +34,7 @@ def login_route():
resp = jsonify(user.to_dict())
return resp
@bp.route("/logout", methods=["POST"])
def logout_route():
if not current_user.is_authenticated:
@@ -43,19 +44,20 @@ def logout_route():
logout_user()
return resp
@bp.route("/register", methods=["POST"])
def register():
data = request.get_json()
required_fields = ['username', 'email', 'password', 'password2']
required_fields = ["role", "username", "email", "password", "password2"]
for f in required_fields:
if f not in data:
return error_response(400, f"Must supply {f}")
if User.query.filter_by(username=data['username']).first():
if User.query.filter_by(username=data["username"]).first():
return error_response(409, "User with that username already exists")
if User.query.filter_by(email=data['email']).first():
if User.query.filter_by(email=data["email"]).first():
return error_response(409, "User with that email already exists")
u = User()

View File

@@ -0,0 +1,36 @@
"""empty message
Revision ID: 8e48199f1417
Revises: 7736bc740f9b
Create Date: 2023-03-18 21:18:21.923362
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '8e48199f1417'
down_revision = '7736bc740f9b'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('user', schema=None) as batch_op:
batch_op.add_column(sa.Column('role', sa.String(length=32), nullable=True))
batch_op.create_index(batch_op.f('ix_user_role'), ['role'], unique=False)
batch_op.drop_column('about_me')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('user', schema=None) as batch_op:
batch_op.add_column(sa.Column('about_me', sa.VARCHAR(length=140), nullable=True))
batch_op.drop_index(batch_op.f('ix_user_role'))
batch_op.drop_column('role')
# ### end Alembic commands ###