Skip to content

Commit

Permalink
Merge pull request #1426 from jadmsaadaot/TRACK-task#1282
Browse files Browse the repository at this point in the history
Add filters queries for my workplan page
  • Loading branch information
jadmsaadaot authored Dec 8, 2023
2 parents c1bf31e + 7ff1b2f commit f3bbfa1
Show file tree
Hide file tree
Showing 30 changed files with 1,039 additions and 505 deletions.
75 changes: 40 additions & 35 deletions epictrack-api/src/api/models/region.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
# Copyright © 2019 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Model to handle all operations related to Region."""

from sqlalchemy import Column, Integer, String

from .code_table import CodeTableVersioned
from .db import db


class Region(db.Model, CodeTableVersioned):
"""Model class for Region."""

__tablename__ = 'regions'

id = Column(Integer, primary_key=True, autoincrement=True) # TODO check how it can be inherited from parent
entity = Column(String())
sort_order = Column(Integer, nullable=False)

def as_dict(self):
"""Return Json representation."""
result = CodeTableVersioned.as_dict(self)
result['entity'] = self.entity
return result
# Copyright © 2019 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Model to handle all operations related to Region."""

from sqlalchemy import Column, Integer, String

from .code_table import CodeTableVersioned
from .db import db


class Region(db.Model, CodeTableVersioned):
"""Model class for Region."""

__tablename__ = 'regions'

id = Column(Integer, primary_key=True, autoincrement=True) # TODO check how it can be inherited from parent
entity = Column(String())
sort_order = Column(Integer, nullable=False)

@classmethod
def find_all_by_region_type(cls, region_type: str):
"""Find all regions by region type."""
return cls.query.filter_by(entity=region_type).all()

def as_dict(self):
"""Return Json representation."""
result = CodeTableVersioned.as_dict(self)
result['entity'] = self.entity
return result
194 changes: 97 additions & 97 deletions epictrack-api/src/api/models/staff.py
Original file line number Diff line number Diff line change
@@ -1,97 +1,97 @@
# Copyright © 2019 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Model to handle all operations related to Staff."""

from typing import List

from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, func
from sqlalchemy.orm import column_property, relationship

from api.models.base_model import BaseModelVersioned


class Staff(BaseModelVersioned):
"""Model class for Staff."""

__tablename__ = "staffs"

id = Column(Integer, primary_key=True, autoincrement=True)
first_name = Column(String(100), nullable=False)
last_name = Column(String(100), nullable=False)
phone = Column(String(), nullable=False)
email = Column(String(), nullable=False)
is_active = Column(Boolean(), default=True, nullable=False)
position_id = Column(ForeignKey("positions.id"), nullable=False)
is_deleted = Column(Boolean(), default=False, nullable=False)

position = relationship("Position", foreign_keys=[position_id], lazy="select")

full_name = column_property(last_name + ", " + first_name)

def as_dict(self): # pylint: disable=arguments-differ
"""Return Json representation."""
return {
"id": self.id,
"first_name": self.first_name,
"last_name": self.last_name,
"full_name": self.full_name,
"phone": self.phone,
"email": self.email,
"is_active": self.is_active,
"position_id": self.position_id,
"position": self.position.as_dict(),
}

@classmethod
def find_active_staff_by_position(cls, position_id: int):
"""Return active staff by position id."""
return cls.query.filter_by(position_id=position_id, is_active=True)

@classmethod
def find_active_staff_by_positions(cls, position_ids: List[int]):
"""Return active staffs by position ids."""
return cls.query.filter(
Staff.position_id.in_(position_ids), Staff.is_active.is_(True)
)

@classmethod
def find_all_active_staff(cls):
"""Return all active staff."""
return cls.query.filter_by(is_active=True, is_deleted=False)

@classmethod
def find_all_non_deleted_staff(cls, is_active=False):
"""Return all non-deleted staff"""
query = {"is_deleted": False}
if is_active:
query["is_active"] = is_active
return cls.query.filter_by(**query)

@classmethod
def check_existence(cls, email, staff_id):
"""Checks if a staff exists with given email address"""
query = cls.query.filter(
func.lower(Staff.email) == func.lower(email),
Staff.is_deleted.is_(False),
)
if staff_id:
query = query.filter(Staff.id != staff_id)
if query.count() > 0:
return True
return False

@classmethod
def find_by_email(cls, email):
"""Returns a staff by email"""
return cls.query.filter(Staff.email == email).first()
# Copyright © 2019 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Model to handle all operations related to Staff."""

from typing import List

from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, func
from sqlalchemy.orm import column_property, relationship

from api.models.base_model import BaseModelVersioned


class Staff(BaseModelVersioned):
"""Model class for Staff."""

__tablename__ = "staffs"

id = Column(Integer, primary_key=True, autoincrement=True)
first_name = Column(String(100), nullable=False)
last_name = Column(String(100), nullable=False)
phone = Column(String(), nullable=False)
email = Column(String(), nullable=False)
is_active = Column(Boolean(), default=True, nullable=False)
position_id = Column(ForeignKey("positions.id"), nullable=False)
is_deleted = Column(Boolean(), default=False, nullable=False)

position = relationship("Position", foreign_keys=[position_id], lazy="select")

full_name = column_property(last_name + ", " + first_name)

def as_dict(self): # pylint: disable=arguments-differ
"""Return Json representation."""
return {
"id": self.id,
"first_name": self.first_name,
"last_name": self.last_name,
"full_name": self.full_name,
"phone": self.phone,
"email": self.email,
"is_active": self.is_active,
"position_id": self.position_id,
"position": self.position.as_dict(),
}

@classmethod
def find_active_staff_by_position(cls, position_id: int):
"""Return active staff by position id."""
return cls.query.filter_by(position_id=position_id, is_active=True)

@classmethod
def find_active_staff_by_positions(cls, position_ids: List[int]):
"""Return active staffs by position ids."""
return cls.query.filter(
Staff.position_id.in_(position_ids), Staff.is_active.is_(True)
)

@classmethod
def find_all_active_staff(cls):
"""Return all active staff."""
return cls.query.filter_by(is_active=True, is_deleted=False)

@classmethod
def find_all_non_deleted_staff(cls, is_active=False):
"""Return all non-deleted staff"""
query = {"is_deleted": False}
if is_active:
query["is_active"] = is_active
return cls.query.filter_by(**query)

@classmethod
def check_existence(cls, email, staff_id):
"""Checks if a staff exists with given email address"""
query = cls.query.filter(
func.lower(Staff.email) == func.lower(email),
Staff.is_deleted.is_(False),
)
if staff_id:
query = query.filter(Staff.id != staff_id)
if query.count() > 0:
return True
return False

@classmethod
def find_by_email(cls, email):
"""Returns a staff by email"""
return cls.query.filter(Staff.email == email).first()
98 changes: 49 additions & 49 deletions epictrack-api/src/api/models/staff_work_role.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
# Copyright © 2019 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Model to handle all operations related to StaffWorkRole."""

from sqlalchemy import Boolean, Column, ForeignKey, Integer
from sqlalchemy.orm import relationship

from .base_model import BaseModelVersioned


class StaffWorkRole(BaseModelVersioned):
"""Model class for StaffWorkRole."""

__tablename__ = 'staff_work_roles'

id = Column(Integer, primary_key=True, autoincrement=True)
is_deleted = Column(Boolean(), default=False, nullable=False)
work_id = Column(ForeignKey('works.id'), nullable=False)
role_id = Column(ForeignKey('roles.id'), nullable=False)
staff_id = Column(ForeignKey('staffs.id'), nullable=False)

work = relationship('Work', foreign_keys=[work_id], lazy='select')
role = relationship('Role', foreign_keys=[role_id], lazy='select')
staff = relationship('Staff', foreign_keys=[staff_id], lazy='select')

def as_dict(self): # pylint:disable=arguments-differ
"""Return Json representation."""
return {
'id': self.id,
'work_id': self.work_id,
'role': self.role.as_dict(),
'staff': self.staff.as_dict()
}

@classmethod
def find_by_work_id(cls, work_id: int):
"""Return by work id."""
return cls.query.filter_by(work_id=work_id)
# Copyright © 2019 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Model to handle all operations related to StaffWorkRole."""

from sqlalchemy import Boolean, Column, ForeignKey, Integer
from sqlalchemy.orm import relationship

from .base_model import BaseModelVersioned


class StaffWorkRole(BaseModelVersioned):
"""Model class for StaffWorkRole."""

__tablename__ = 'staff_work_roles'

id = Column(Integer, primary_key=True, autoincrement=True)
is_deleted = Column(Boolean(), default=False, nullable=False)
work_id = Column(ForeignKey('works.id'), nullable=False)
role_id = Column(ForeignKey('roles.id'), nullable=False)
staff_id = Column(ForeignKey('staffs.id'), nullable=False)

work = relationship('Work', foreign_keys=[work_id], lazy='select')
role = relationship('Role', foreign_keys=[role_id], lazy='select')
staff = relationship('Staff', foreign_keys=[staff_id], lazy='select')

def as_dict(self): # pylint:disable=arguments-differ
"""Return Json representation."""
return {
'id': self.id,
'work_id': self.work_id,
'role': self.role.as_dict(),
'staff': self.staff.as_dict()
}

@classmethod
def find_by_work_id(cls, work_id: int):
"""Return by work id."""
return cls.query.filter_by(work_id=work_id)
Loading

0 comments on commit f3bbfa1

Please sign in to comment.