-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.py
127 lines (95 loc) · 2.97 KB
/
queries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
Queries for Clubs
"""
from typing import List
import strawberry
from fastapi.encoders import jsonable_encoder
from db import clubsdb
from models import Club
# import all models and types
from otypes import FullClubType, Info, SimpleClubInput, SimpleClubType
# fetch all active clubs
@strawberry.field
def activeClubs(info: Info) -> List[SimpleClubType]:
"""
Fetches all the currently active clubs and is accessible to all.
Args:
info (Info): User metadata and cookies.
Returns:
List[SimpleClubType]: List of active clubs.
"""
results = clubsdb.find({"state": "active"}, {"_id": 0})
clubs = [
SimpleClubType.from_pydantic(Club.model_validate(result))
for result in results
]
return clubs
@strawberry.field
def allClubs(info: Info) -> List[SimpleClubType]:
"""
Fetches all the clubs
This method returns all the clubs when accessed CC.
When accessed by public, it returns only the active clubs.
Access to both public and CC(Clubs Council).
Args:
info (Info): User metadata and cookies.
Returns:
List[SimpleClubType]: List of all clubs.
"""
user = info.context.user
if user is None:
role = "public"
else:
role = user["role"]
results = []
if role in ["cc"]:
results = clubsdb.find()
else:
results = clubsdb.find({"state": "active"}, {"_id": 0})
clubs = []
for result in results:
clubs.append(SimpleClubType.from_pydantic(Club.model_validate(result)))
return clubs
@strawberry.field
def club(clubInput: SimpleClubInput, info: Info) -> FullClubType:
"""
Fetches all Club Details of a club
Used to get all the details of a deleted/active club by its cid.
Returns deleted clubs also for CC and not for public.
Accessible to both public and CC(Clubs Council).
Args:
clubInput (SimpleClubInput): The club cid.
info (Info): User metadata and cookies.
Returns:
FullClubType: Contains all the club details.
Raises:
Exception: If the club is not found.
Exception: If the club is deleted and the user is not CC.
"""
user = info.context.user
club_input = jsonable_encoder(clubInput)
result = None
club = clubsdb.find_one({"cid": club_input["cid"].lower()}, {"_id": 0})
if not club:
raise Exception("No Club Found")
# check if club is deleted
if club["state"] == "deleted":
# if deleted, check if requesting user is admin
if (user is not None) and (user["role"] in ["cc"]):
result = Club.model_validate(club)
# if not admin, raise error
else:
raise Exception("No Club Found")
# if not deleted, return club
else:
result = Club.model_validate(club)
if result:
return FullClubType.from_pydantic(result)
else:
raise Exception("No Club Result Found")
# register all queries
queries = [
activeClubs,
allClubs,
club,
]