Skip to content

Commit

Permalink
default resident filter and adding last modified column
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor Bechthold committed Jan 13, 2024
1 parent 9ec1be4 commit 3fe92d3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
6 changes: 6 additions & 0 deletions backend/app/models/residents.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class Residents(db.Model):
date_joined = db.Column(db.Date, nullable=False)
date_left = db.Column(db.Date, nullable=True)
building_id = db.Column(db.Integer, db.ForeignKey("buildings.id"), nullable=False)
last_modified = db.Column(
db.DateTime,
server_default=db.func.now(),
onupdate=db.func.now(),
nullable=False,
)
building = db.relationship("Buildings", back_populates="resident")
log_records = db.relationship(
"LogRecords", secondary="log_record_residents", back_populates="residents"
Expand Down
8 changes: 5 additions & 3 deletions backend/app/services/implementations/residents_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ def construct_filters(self, query, filters, current_date):
if date_left is not None:
query = query.filter(Residents.date_left <= date_left)

query = query.order_by(Residents.last_modified.desc())

return query

def add_resident(self, resident):
Expand Down Expand Up @@ -203,11 +205,11 @@ def get_residents(self, return_all, page_number, results_per_page, filters=None)
)

if not return_all:
residents_results = residents_results.limit(results_per_page).offset(
residents_results = residents_results.order_by(Residents.last_modified.desc()).limit(results_per_page).offset(
(page_number - 1) * results_per_page
)

residents_results = residents_results.all()
else:
residents_results = residents_results.order_by(Residents.last_modified.desc()).all()

return {
"residents": self.to_residents_json_list(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""add last modified to residents
Revision ID: a1f05c8f324c
Revises: 117790caec65
Create Date: 2024-01-13 20:22:32.969646
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'a1f05c8f324c'
down_revision = '117790caec65'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('log_record_tag', schema=None) as batch_op:
batch_op.drop_constraint('log_record_tag_tag_id_fkey', type_='foreignkey')
batch_op.create_foreign_key(None, 'tags', ['tag_id'], ['tag_id'], ondelete='CASCADE')

with op.batch_alter_table('residents', schema=None) as batch_op:
batch_op.add_column(sa.Column('last_modified', sa.DateTime(), server_default=sa.text('now()'), nullable=False))

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('residents', schema=None) as batch_op:
batch_op.drop_column('last_modified')

with op.batch_alter_table('log_record_tag', schema=None) as batch_op:
batch_op.drop_constraint(None, type_='foreignkey')
batch_op.create_foreign_key('log_record_tag_tag_id_fkey', 'tags', ['tag_id'], ['tag_id'])

# ### end Alembic commands ###
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ const ResidentDirectory = (): React.ReactElement => {
const [residentSelections, setResidentSelections] = useState<SelectLabel[]>(
[],
);
// NOTE: Building 362 will always have ID 2 in the database
const [buildingSelections, setBuildingSelections] = useState<SelectLabel[]>(
[],
[{
label: "362",
value: 2
}],
);
const [statusSelections, setStatusSelections] = useState<StatusLabel[]>([]);
const [startDate, setStartDate] = useState<Date | undefined>();
Expand Down

0 comments on commit 3fe92d3

Please sign in to comment.