Skip to content

Commit

Permalink
Fixing roommate search
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbyt3r committed Nov 29, 2023
1 parent cdc964f commit ebcdf6b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 158 deletions.
152 changes: 8 additions & 144 deletions backend/tuber/api/hotels.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,14 @@
from tuber.models import *
from tuber.permissions import *
from tuber.database import db
from sqlalchemy import and_, or_, func
from sqlalchemy import and_, or_, not_, func
from sqlalchemy.orm import joinedload, selectinload
import datetime
import os
from tuber.api.util import *
from .room_matcher import rematch_hotel_block, clear_hotel_block
import time


def update_room_request_props(db, reqs, assigned=None, requested=None, approved=None):
if not reqs:
return
room_nights = db.query(HotelRoomNight).filter(
HotelRoomNight.event == reqs[0].event).order_by(HotelRoomNight.date).all()
room_night_lookup = {x.id: x for x in room_nights}
for req in reqs:
req.requested = False
req.approved = False
if not req.declined:
for rnr in req.room_night_requests:
if rnr.requested:
req.requested = True
if not room_night_lookup[rnr.room_night].restricted:
req.approved = True
else:
for rna in req.room_night_approvals:
if rna.room_night == rnr.room_night and rna.approved:
req.approved = True

req.assigned = bool(req.room_night_assignments)
if not assigned is None:
req.assigned = assigned
if not requested is None:
req.requested = requested
if not approved is None:
req.approved = approved


@app.route("/api/event/<int:event>/hotel/<int:hotel_block>/room/<int:room_id>/remove_roommates", methods=["POST"])
def remove_roommates(event, hotel_block, room_id):
if not check_permission(f'hotel_block.{hotel_block}.write', event=event):
Expand Down Expand Up @@ -306,20 +276,23 @@ def room_search(event):
def request_search(event, hotel_block):
if not check_permission("hotel_block.*.read"):
return "", 403

assigned_nights = db.query(RoomNightRequest.id).filter(RoomNightRequest.requested).join(
RoomNightAssignment, and_(RoomNightAssignment.badge == RoomNightRequest.badge,
RoomNightAssignment.room_night == RoomNightRequest.room_night)
).subquery()
reqs = db.query(HotelRoomRequest).filter(
HotelRoomRequest.event == event,
HotelRoomRequest.hotel_block == hotel_block,
HotelRoomRequest.approved == True,
HotelRoomRequest.assigned == False
HotelRoomRequest.room_night_requests.any(and_(RoomNightRequest.requested, not_(RoomNightRequest.id.in_(assigned_nights))))
).join(Badge, Badge.id == HotelRoomRequest.badge).filter(
or_(Badge.search_name.contains(g.data['search_term'].lower()), func.lower(
HotelRoomRequest.notes).contains(g.data['search_term'].lower()))
).order_by(g.data['sort']).offset(int(g.data['offset'])).limit(int(g.data['limit'])).all()
count = db.query(HotelRoomRequest).filter(
HotelRoomRequest.event == event,
HotelRoomRequest.hotel_block == hotel_block,
HotelRoomRequest.approved == True,
HotelRoomRequest.assigned == False
HotelRoomRequest.room_night_requests.any(and_(RoomNightRequest.requested, not_(RoomNightRequest.id.in_(assigned_nights))))
).join(Badge, Badge.id == HotelRoomRequest.badge).filter(
or_(Badge.search_name.contains(g.data['search_term'].lower()), func.lower(
HotelRoomRequest.notes).contains(g.data['search_term'].lower()))
Expand Down Expand Up @@ -666,112 +639,3 @@ def export_passkey(event):
"Content-Disposition": "attachment; filename=rooms.csv",
}
return Response(result, headers=headers)


@app.route("/api/event/<int:event>/hotel/update_requests", methods=["POST"])
def update_requests(event):
if not check_permission("rooming.*.admin"):
return "", 403
updates = {
"requests": {},
"badges": {}
}
badges = db.query(Badge).filter(Badge.event == event).all()
for badge in badges:
if not badge.public_name and badge.printed_name:
badge.public_name = badge.printed_name
updates['badges'][badge.id] = badge.public_name
db.add(badge)
badgeLookup = {x.id: x for x in badges}

room_nights = db.query(HotelRoomNight).filter(
HotelRoomNight.event == event).order_by(HotelRoomNight.date).all()
room_night_lookup = {x.id: x for x in room_nights}
block = db.query(HotelRoomBlock).filter(
HotelRoomBlock.event == event).first()

reqs = db.query(HotelRoomRequest).filter(HotelRoomRequest.event == event).options(
joinedload(HotelRoomRequest.room_night_requests)).all()
for req in reqs:
req.requested = False
req.approved = False
if not req.declined:
for rnr in req.room_night_requests:
if rnr.requested:
req.requested = True
if not room_night_lookup[rnr.room_night].restricted:
req.approved = True
else:
for rna in req.room_night_approvals:
if rna.room_night == rnr.room_night and rna.approved:
req.approved = True

req.assigned = bool(req.room_night_assignments)
updates['requests'][req.id] = {
"requested": req.requested,
"approved": req.approved,
"assigned": req.assigned
}

badge_name = " "
badge = badgeLookup[req.badge]
if badge.public_name:
badge_name = badge.public_name
elif badge.search_name:
badge_name = " ".join([x.capitalize()
for x in badge.search_name.split(" ")])
elif badge.legal_name:
badge_name = badge.legal_name
elif badge.printed_name:
badge_name = badge.printed_name

badge_first_name = badge.first_name
if not badge_first_name:
badge_first_name = badge_name.split(" ")[0]

badge_last_name = badge.last_name
if not badge_last_name:
badge_last_name = badge_name.split(" ", 1)[1]

if not req.first_name:
if badge_first_name:
req.first_name = badge_first_name
updates['requests'][req.id]['first_name'] = req.first_name
if not req.last_name:
if badge_last_name:
req.last_name = badge_last_name
updates['requests'][req.id]['last_name'] = req.last_name

if not req.hotel_block:
req.hotel_block = block.id
db.add(req)

db.commit()
return jsonify(updates), 200


@HotelRoom.onchange
@RoomNightRequest.onchange
@RoomNightAssignment.onchange
@RoomNightApproval.onchange
@HotelRoomRequest.onchange
@HotelRoomNight.onchange
def update_room_request(db, instance, deleted=None):
reqs = []
if request.method == "DELETE" and type(instance) is HotelRoom:
print(f"Deleting hotel room {deleted['name']}")
print(deleted)
reqs = db.query(HotelRoomRequest).filter(
HotelRoomRequest.badge.in_(deleted['roommates'])).all()
elif type(instance) is RoomNightRequest or type(instance) is RoomNightAssignment or type(instance) is RoomNightApproval:
reqs = db.query(HotelRoomRequest).filter(
HotelRoomRequest.badge == instance.badge).all()
elif type(instance) is HotelRoomRequest:
reqs = [instance]
elif type(instance) is HotelRoomNight:
reqs = db.query(HotelRoomRequest).filter(HotelRoomRequest.event == instance.event).options(joinedload(HotelRoomRequest.room_night_requests)).options(
joinedload(HotelRoomRequest.room_night_approvals)).options(joinedload(HotelRoomRequest.room_night_assignments)).all()
update_room_request_props(db, reqs)
if not type(instance) is HotelRoomRequest:
for req in reqs:
db.add(req)
6 changes: 0 additions & 6 deletions backend/tuber/api/room_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ def load_staffers(db, event, hotel_block):
~HotelRoomRequest.room_night_assignments.any(), HotelRoomRequest.hotel_block == hotel_block).all()
staffers = []
for request, badge in requests:
if not request.approved:
continue
staffer = HashNS()

approved = set()
Expand Down Expand Up @@ -260,9 +258,6 @@ def clear_hotel_block(db, event, hotel_block):
for room in rooms:
roommates.extend(room.roommates)
db.delete(room)
for roommate in roommates:
roommate.hotel_room_request[0].assigned = False
db.add(roommate)
db.commit()


Expand Down Expand Up @@ -323,7 +318,6 @@ def rematch_hotel_block(db, event, hotel_block):
assoc = RoomNightAssignment(
event=event, badge=roommate.id, hotel_room=hotel_room.id, room_night=room_night)
db.add(assoc)
roommate.request.assigned = True
db.add(roommate.request)
db.commit()
print(unmapped)
Expand Down
24 changes: 22 additions & 2 deletions backend/tuber/api/uber.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@
'X-Auth-Token': config.uber_api_token
}

@app.route("/api/import_shifts", methods=["POST"])
def import_shifts():
event = config.uber_event
depts = db.query(Department).filter(Department.event == event).all()
for dept in depts:
req = {
"method": "shifts.lookup",
"params": {
"department_id": dept.uber_id
}
}
print(req)
shifts = requests.post(config.uber_api_url, headers=headers, json=req).json()['result']
print(len(shifts))
return "null", 200

@app.route("/api/uber_department")
def get_uber_department():
event = config.uber_event
Expand Down Expand Up @@ -106,13 +122,15 @@ def department_sync():
print("WARNING: ATTENDEE REQUESTED ROOM NIGHTS")
#db.delete(badgelookup[attendee])
else:
print(" not deferred")
if not attendee in badgelookup:
print(f"Missing attendee: {attendee}")
new_badge = create_attendee(uber_model, event)
badgelookup[attendee] = new_badge

badge = badgelookup[attendee]
for dept_name in uber_model['assigned_depts_labels']:
print(f" {dept_name}")
if not dept_name in dept_names and dept_name in uber_depts_names:
new_dept = Department(uber_id=uber_depts_names[dept_name], event=event, name=dept_name)
dept_names[dept_name] = new_dept
Expand All @@ -122,6 +140,7 @@ def department_sync():
badge.departments.append(dept_names[dept_name])
print(f"Adding {badge.public_name} to {dept_name}")

print(" Removing depts")
to_remove = []
for dept in badge.departments:
if not dept.name in uber_model['assigned_depts_labels']:
Expand All @@ -130,8 +149,9 @@ def department_sync():
print(f"Removing {badge.public_name} from {dept.name}")
badge.departments.remove(dept)
db.add(badge)

db.commit()
print("End loop")
db.commit()
print("done")
return "", 200

@app.route("/api/uber_login", methods=["POST"])
Expand Down
6 changes: 0 additions & 6 deletions backend/tuber/models/hotel.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ class HotelRoomRequest(Base):
first_name = Column(String(), nullable=True)
last_name = Column(String(), nullable=True)
declined = Column(Boolean, nullable=True)
# At least one night has been requested
requested = Column(Boolean, nullable=True)
# At least one night is assigned to a room
assigned = Column(Boolean, nullable=True)
# At least one night is requested and approved (inclusive of default nights)
approved = Column(Boolean, nullable=True)
prefer_department = Column(Boolean, nullable=True)
preferred_department = Column(
Integer, ForeignKey('department.id', ondelete="SET NULL"), nullable=True)
Expand Down

0 comments on commit ebcdf6b

Please sign in to comment.