Coverage for app/logic/searchUsers.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-10-02 17:57 +0000

1from playhouse.shortcuts import model_to_dict 

2from app.models.user import User 

3def searchUsers(query, category=None): 

4 ''' 

5 Search the User table based on the search query and category 

6 

7 MySQL LIKE is case insensitive 

8 ''' 

9 # add wildcards to each piece of the query 

10 splitSearch = query.strip().split() 

11 firstName = splitSearch[0] + "%" 

12 lastName = " ".join(splitSearch[1:]) +"%" 

13 

14 if len(splitSearch) == 1: # search for query in first OR last name 

15 searchWhere = (User.firstName ** firstName | User.lastName ** firstName | User.username ** splitSearch) 

16 else: # search for first AND last name 

17 searchWhere = (User.firstName ** firstName & User.lastName ** lastName) 

18 

19 if category == "instructor": 

20 userWhere = (User.isFaculty | User.isStaff) 

21 elif category == "admin": 

22 userWhere = (User.isCeltsAdmin) 

23 elif category == "studentstaff": 

24 userWhere = (User.isCeltsStudentStaff) 

25 elif category == "celtsLinkAdmin": 

26 userWhere = (User.isFaculty | User.isStaff | User.isCeltsStudentStaff) 

27 else: 

28 userWhere = (User.isStudent) 

29 

30 # Combine into query 

31 searchResults = User.select().where(searchWhere, userWhere) 

32 

33 return { user.username : model_to_dict(user) for user in searchResults }