Skip to content

Commit

Permalink
Merge pull request #11 from Clubs-Council-IIITH/separate_out
Browse files Browse the repository at this point in the history
Separate out Members from clubs microservice
  • Loading branch information
abhiramtilakiiit authored May 17, 2024
2 parents bb431ee + 12d00ef commit a957f34
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 832 deletions.
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,5 @@ Contains code for queries, mutations, types and models for the clubs data and se
_URL_ -> http://clubs/graphql (For using in single gateway)

> ### QUERIES
#### Club
-

#### Members
-
> ### MUTATIONS
#### Club
-

#### Members
-
13 changes: 0 additions & 13 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# get database
db = client[MONGO_DATABASE]
clubsdb = db.clubs
membersdb = db.members

try:
# check if the clubs index exists
Expand All @@ -29,17 +28,5 @@
print("The clubs index was created.")

print(clubsdb.index_information())

# check if the members index exists
if "unique_members" in membersdb.index_information():
print("The members index exists.")
else:
# create the index
membersdb.create_index(
[("cid", 1), ("uid", 1)], unique=True, name="unique_members"
)
print("The members index was created.")

print(membersdb.index_information())
except Exception:
pass
10 changes: 3 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,17 @@
from otypes import Context, PyObjectIdType

# import all queries and mutations
from queries_clubs import queries as queries_clubs
from queries_members import queries as queries_members
from mutations_clubs import mutations as mutations_clubs
from mutations_members import mutations as mutations_members
from queries import queries
from mutations import mutations


# check whether running in debug mode
DEBUG = int(getenv("GLOBAL_DEBUG", 0))

# create query types
queries = queries_clubs + queries_members
Query = create_type("Query", queries)

# create mutation types
mutations = mutations_clubs + mutations_members
Mutation = create_type("Mutation", mutations)


Expand All @@ -49,6 +45,6 @@ async def get_context() -> Context:
app = FastAPI(
debug=DEBUG,
title="CC Clubs Microservice",
desciption="Handles Data of Clubs and Members",
desciption="Handles Data of Clubs",
)
app.include_router(gql_app, prefix="/graphql")
68 changes: 1 addition & 67 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
EmailStr,
Field,
field_validator,
ValidationInfo,
)
from pydantic_core import core_schema
from typing import Any, List
Expand Down Expand Up @@ -65,71 +64,6 @@ class EnumCategories(str, Enum):
other = "other"


class Roles(BaseModel):
rid: str | None = Field(None, description="Unique Identifier for a role")
name: str = Field(..., min_length=1, max_length=99)
start_year: int = Field(..., ge=2010, le=2050)
end_year: int | None = Field(None, gt=2010, le=2051)
approved: bool = False
rejected: bool = False
deleted: bool = False

# Validators
@field_validator("end_year")
def check_end_year(cls, value, info: ValidationInfo):
if value is not None and value < info.data["start_year"]:
return None
return value

@field_validator("rejected")
def check_status(cls, value, info: ValidationInfo):
if info.data["approved"] is True and value is True:
raise ValueError("Role cannot be both approved and rejected")
return value

model_config = ConfigDict(
arbitrary_types_allowed=True,
str_max_length=100,
validate_assignment=True,
validate_default=True,
validate_return=True,
extra="forbid",
str_strip_whitespace=True,
)


class Member(BaseModel):
id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
cid: str = Field(..., description="Club ID")
uid: str = Field(..., description="User ID")
roles: List[Roles] = Field(
..., description="List of Roles for that specific person"
)

poc: bool = Field(default_factory=(lambda: 0 == 1), description="Club POC")

@field_validator("uid", mode="before")
@classmethod
def transform_uid(cls, v):
return v.lower()

# TODO[pydantic]: The following keys were removed: `json_encoders`.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-config for more information.
model_config = ConfigDict(
arbitrary_types_allowed=True,
str_strip_whitespace=True,
str_max_length=600,
validate_assignment=True,
validate_default=True,
validate_return=True,
extra="forbid",
json_encoders={ObjectId: str},
populate_by_name=True,
)

# Separate Coordinator & other members roles option in frontend, for better filtering for all_members_query


class Social(BaseModel):
website: AnyHttpUrl | None = None
instagram: AnyHttpUrl | None = None
Expand Down Expand Up @@ -192,5 +126,5 @@ def _check_email(cls, v):
)


# TO ADD CLUB SUBSCRIPTION MODEL - v2
# TODO: ADD CLUB SUBSCRIPTION MODEL - v2
# ADD Descriptions for non-direct fields
11 changes: 3 additions & 8 deletions mutations_clubs.py → mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import datetime

from db import clubsdb
from utils import update_role, update_events_cid, update_members_cid, getUser
from utils import update_role, update_events_members_cid, getUser

# import all models and types
from otypes import Info
Expand All @@ -16,10 +16,6 @@
SimpleClubType,
)

"""
CLUB MUTATIONS
"""


@strawberry.mutation
def createClub(clubInput: FullClubInput, info: Info) -> SimpleClubType:
Expand Down Expand Up @@ -121,11 +117,10 @@ def editClub(clubInput: FullClubInput, info: Info) -> FullClubType:
if exists["cid"] != club_input["cid"]:
return1 = update_role(exists["cid"], info.context.cookies, role="public")
return2 = update_role(club_input["cid"], info.context.cookies, role="club")
return3 = update_events_cid(
return3 = update_events_members_cid(
exists["cid"], club_input["cid"], cookies=info.context.cookies
)
return4 = update_members_cid(exists["cid"], club_input["cid"])
if not return1 or not return2 or not return3 or not return4:
if not return1 or not return2 or not return3:
raise Exception("Error in updating the role/cid.")

result = Club.parse_obj(clubsdb.find_one({"code": club_input["code"]}))
Expand Down
Loading

0 comments on commit a957f34

Please sign in to comment.