Skip to content

Commit

Permalink
Merge branch 'main' into logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sudheshna-donthineni authored Jun 12, 2024
2 parents 218ea92 + ac02ebb commit c282dfa
Show file tree
Hide file tree
Showing 28 changed files with 852 additions and 1,463 deletions.
29 changes: 13 additions & 16 deletions app/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@


def is_response_valid(response, error_message=""):
if response.status_code == 200:
return True
elif response.status_code == 201:
if response.status_code in [200, 201]:
return True
if error_message:
logger.error(error_message)
Expand All @@ -17,29 +15,28 @@ def is_response_valid(response, error_message=""):


def is_response_empty(response_data, return_boolean, error_message=""):
if len(response_data) != 0:
if response_data:
return response_data
if return_boolean:
if error_message:
logger.error(error_message)
raise HTTPException(status_code=404, detail=error_message)
else:
return False
return False
return []


def validate_and_build_query_params(data, valid_query_params):
query_params = {}
for key in data.keys():
if key not in valid_query_params:
logger.error("Query Parameter {key} is not allowed!")
raise HTTPException(
status_code=400, detail="Query Parameter {} is not allowed!".format(key)
)
query_params[key] = data[key]

query_params = {key: data[key] for key in data.keys() if key in valid_query_params}
invalid_params = [key for key in data.keys() if key not in valid_query_params]
if invalid_params:
raise HTTPException(
status_code=400,
detail="Query Parameter(s) {} is not allowed!".format(
", ".join(invalid_params)
),
)
return query_params


def db_request_token():
return {"Authorization": f"Bearer {settings.token}"}
return {"Authorization": f"Bearer {settings.TOKEN}"}
42 changes: 0 additions & 42 deletions app/id_generation.py

This file was deleted.

36 changes: 36 additions & 0 deletions app/id_generation_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from settings import settings
import random
from router import school
import datetime
from dateutil.relativedelta import relativedelta
from request import build_request


class JNVIDGeneration:
def __init__(self, region, school, grade):
self.region = region
self.school = school
self.grade = grade
self.id = (
self.get_class_code()
+ self.get_jnv_code()
+ self.generate_three_digit_code()
)

def get_class_code(self):
graduating_year = datetime.date.today() + relativedelta(
years=12 - int(self.grade)
)
return str(graduating_year.year)[-2:]

def get_jnv_code(self):
return school.get_school(
build_request(
query_params={"region": self.region, "name": self.school_name}
)
)

def generate_three_digit_code(code=""):
for _ in range(3):
code += random.randint(0, 9)
return code
39 changes: 23 additions & 16 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from router import (
auth_group,
auth,
session,
batch,
enrollment_record,
form,
group_session,
group_user,
group,
school,
session_occurrence,
session,
student,
session_group,
teacher,
user_session,
session_occurrence,
batch,
program,
group_type,
form,
user,
teacher,
)
from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI, Request
Expand Down Expand Up @@ -55,8 +57,11 @@ async def log_requests(request: Request, call_next):

origins = [
"http://localhost:8080",
"http://localhost:3000",
"https://staging-auth.avantifellows.org",
"https://auth.avantifellows.org",
"https://staging-gurukul.avantifellows.org",
"https://gurukul.avantifellows.org",
]

app.add_middleware(
Expand All @@ -73,19 +78,21 @@ def authjwt_exception_handler(request: Request, exc: AuthJWTException):
return JSONResponse(status_code=exc.status_code, content={"detail": exc.message})


app.include_router(auth_group.router)
app.include_router(auth.router)
app.include_router(session.router)
app.include_router(batch.router)
app.include_router(enrollment_record.router)
app.include_router(form.router)
app.include_router(group_session.router)
app.include_router(group_user.router)
app.include_router(group.router)
app.include_router(school.router)
app.include_router(session_occurrence.router)
app.include_router(session.router)
app.include_router(student.router)
app.include_router(session_group.router)
app.include_router(teacher.router)
app.include_router(user_session.router)
app.include_router(session_occurrence.router)
app.include_router(batch.router)
app.include_router(program.router)
app.include_router(group_type.router)
app.include_router(form.router)
app.include_router(user.router)
app.include_router(teacher.router)


@app.get("/")
Expand Down
89 changes: 63 additions & 26 deletions app/mapping.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,85 @@
FORM_GROUP_MAPPING = {"HaryanaStudents": "Haryana Student Profile"}
AUTH_GROUP_QUERY_PARAMS = ["id", "name", "locale"]

BATCH_QUERY_PARAMS = [
"id",
"name",
"contact_hours_per_week",
"batch_id",
"parent_id",
"program_id",
"auth_group_id",
]

ENROLLMENT_RECORD_PARAMS = [
"academic_year",
"is_current",
"start_date",
"end_date",
"group_id",
"group_type",
"user_id",
"subject_id",
"grade_id",
]

FORM_SCHEMA_QUERY_PARAMS = ["id", "name", "attributes"]

GROUP_QUERY_PARAMS = ["id", "type", "child_id"]

GROUP_USER_QUERY_PARAMS = ["id", "group_id", "user_id"]

SCHOOL_QUERY_PARAMS = ["id", "name", "school_name", "code", "board_medium"]

SESSION_QUERY_PARAMS = ["id", "name", "session_id"]

STUDENT_QUERY_PARAMS = [
"student_id",
"category",
"stream",
"physically_handicapped",
"annual_family_income",
"monthly_family_income",
"grade",
"grade_id",
"father_name",
"father_phone",
"father_profession",
"father_education_level",
"mother_name",
"mother_phone",
"category",
"stream",
"family_income",
"father_profession",
"father_education_level",
"mother_profession",
"mother_education_level",
"time_of_device_availability",
"has_internet_access",
"primary_smartphone_owner",
"primary_smartphone_owner_profession",
"group",
"guardian_name",
"guardian_relation",
"guardian_phone",
"guardian_education_level",
"guardian_profession",
"has_category_certificate",
"category_certificate",
"physically_handicapped",
"physically_handicapped_certificate",
"annual_family_income",
"monthly_family_income",
"number_of_smartphones",
"family_type",
"number_of_four_wheelers",
"number_of_two_wheelers",
"goes_for_tuition_or_other_coaching",
"know_about_avanti",
"percentage_in_grade_10_science",
"percentage_in_grade_10_math",
"percentage_in_grade_10_english",
"grade_10_marksheet",
"photo",
]

TEACHER_QUERY_PARAMS = ["grade", "designation"]
TEACHER_QUERY_PARAMS = ["teacher_id", "subject_id", "designation"]

USER_QUERY_PARAMS = [
"id",
"first_name",
"middle_name",
"last_name",
"email",
"phone",
Expand All @@ -43,18 +95,3 @@
"country",
"consent_check",
]

ENROLLMENT_RECORD_PARAMS = [
"grade",
"academic_year",
"student_id",
"school_id",
"school_name",
"is_current",
"board_medium",
"date_of_enrollment",
]

SCHOOL_QUERY_PARAMS = ["id", "name", "code"]
GROUP_TYPE_QUERY_PARAMS = ["id", "type", "child_id"]
GROUP_QUERY_PARAMS = ["name", "id", "locale"]
54 changes: 41 additions & 13 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
from typing import Union, Dict, Optional


class AuthGroupResponse(BaseModel):
id: str
name: str
input_schema: Optional[Dict] = {}
locale: Optional[str] = ""
locale_data: Optional[Dict] = {}


class AuthUser(BaseModel):
id: str
type: str
Expand All @@ -10,6 +18,31 @@ class AuthUser(BaseModel):
data: Union[Dict[str, str], None]


class BatchResponse(BaseModel):
id: str
name: str
contact_hours_per_week: Optional[int] = None
batch_id: str
parent_id: Optional[int] = None
start_date: Optional[str] = None
end_date: Optional[str] = None
program_id: Optional[int] = None
auth_group_id: Optional[int] = None


class EnrollmentRecordResponse(BaseModel):
id: str
academic_year: str
is_current: bool
start_date: str
end_date: Optional[str] = None
group_id: int
group_type: str
user_id: int
subject_id: int
grade_id: int


class SessionResponse(BaseModel):
id: Optional[str] = None
session_id: Optional[str] = None
Expand All @@ -24,24 +57,19 @@ class SessionResponse(BaseModel):
is_session_open: bool
type: Optional[str] = None
auth_type: Optional[str] = None
activate_signup: Optional[str] = None
signup_form: Optional[str] = None
id_generation: Optional[str] = None
redirection: Optional[str] = None
pop_up_form: Optional[str] = None
form_schema_id: Optional[str] = None
number_of_fields_in_pop_form: Optional[str] = None


class GroupResponse(BaseModel):
id: str
name: str
input_schema: Optional[Dict] = {}
locale: Optional[str] = ""
locale_data: Optional[Dict] = {}
popup_form: Optional[str] = None
signup_form_id: Optional[str] = None
popup_form_id: Optional[str] = None
session_occurrence_id: Optional[str] = None


class UserSession(BaseModel):
user_id: str
session_id: str
session_occurrence_id: int
is_user_valid: bool
user_activity_type: str
user_type: str
data: Optional[Dict] = {}
Loading

0 comments on commit c282dfa

Please sign in to comment.