Skip to content

Commit

Permalink
Adding the organisation field to users object
Browse files Browse the repository at this point in the history
  • Loading branch information
LanternNassi committed Jun 19, 2023
1 parent be438ba commit 80e2513
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
3 changes: 3 additions & 0 deletions api_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ paths:
- email
- name
- password
- organisation
properties:
email:
type: string
name:
type: string
password:
type: string
organisation:
type: string
produces:
- application/json
responses:
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def post(self):
status="fail",
message=f"Email {validated_user_data['email']} already in use."
), 400

user = User(**validated_user_data)

if user_role:
Expand Down
4 changes: 3 additions & 1 deletion app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ class User(ModelMixin):
date_created = db.Column(db.DateTime, default=db.func.current_timestamp())
last_seen = db.Column(db.DateTime, default=db.func.current_timestamp())
projects = db.relationship('Project', backref='owner', lazy=True)
organisation = db.Column(db.String(256), nullable=True, default="")
other_projects = db.relationship('ProjectUser', back_populates='user')
is_beta_user = db.Column(db.Boolean, nullable=False, default=False)
credits = db.relationship('Credit', backref='user', lazy=True)
credit_assignments = db.relationship('CreditAssignment', backref='user', lazy=True)

def __init__(self, email, name, password):
def __init__(self, email, name, password, organisation):
""" initialize with email, username and password """
self.email = email
self.name = name
self.username = name
self.organisation = organisation
self.password = Bcrypt().generate_password_hash(password).decode()

def password_is_valid(self, password):
Expand Down
9 changes: 8 additions & 1 deletion app/schemas/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ class UserSchema(Schema):
age = fields.Method("get_age", dump_only=True)
is_beta_user = fields.Boolean()
credits = fields.Nested(CreditSchema, many=True, dump_only=True)

organisation = fields.String(required=True, error_message={
"required": "Organisation name is required"},
validate=[
validate.Regexp(
regex=r'^(?!\s*$)', error='Organisations should be a valid string'
),
])

def get_age(self, obj):
return get_item_age(obj.date_created)

Expand Down
28 changes: 28 additions & 0 deletions migrations/versions/9020288abc29_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""empty message
Revision ID: 9020288abc29
Revises: 9e5db6c0d658
Create Date: 2023-06-19 16:33:16.193323
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '9020288abc29'
down_revision = '9e5db6c0d658'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('user', sa.Column('organisation', sa.String(length=256), nullable=True))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('user', 'organisation')
# ### end Alembic commands ###

0 comments on commit 80e2513

Please sign in to comment.