alcehmy and event functions updated
This commit is contained in:
@@ -37,7 +37,7 @@ from api_services import (
|
||||
|
||||
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
||||
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
|
||||
from api_library.date_time_actions.date_functions import DateTimeLocal
|
||||
from api_library.date_time_actions.date_functions import DateTimeLocal, system_arrow
|
||||
from api_configs import ApiStatic, Auth
|
||||
from databases.no_sql_models.login_handlers import load_user_with_erp_details
|
||||
|
||||
@@ -51,6 +51,7 @@ from api_validations.validations_request import (
|
||||
OccupantSelection,
|
||||
EmployeeSelection,
|
||||
)
|
||||
from databases.sql_models.building.build import RelationshipEmployee2Build
|
||||
|
||||
|
||||
class AuthenticationLoginEventMethods(MethodToEvent):
|
||||
@@ -98,8 +99,8 @@ class AuthenticationSelectEventMethods(MethodToEvent):
|
||||
def authentication_select_company_or_occupant_type(
|
||||
cls,
|
||||
request: Request,
|
||||
data: typing.Union[EmployeeSelection, OccupantSelection],
|
||||
token_dict: dict = None,
|
||||
data,
|
||||
token_dict: typing.Union[EmployeeSelection, OccupantSelection],
|
||||
):
|
||||
from api_objects import OccupantToken, CompanyToken
|
||||
|
||||
@@ -116,38 +117,39 @@ class AuthenticationSelectEventMethods(MethodToEvent):
|
||||
if selected_company := Companies.find_one(uu_id=data.company_uu_id):
|
||||
department_ids = [
|
||||
department.id
|
||||
for department in Departments.filter_by_active(
|
||||
company_id=selected_company.id
|
||||
for department in Departments.filter_all(
|
||||
Departments.company_id==selected_company.id
|
||||
).data
|
||||
]
|
||||
duties_ids = [
|
||||
duties.id
|
||||
for duties in Duties.filter_active(
|
||||
for duties in Duties.filter_all(
|
||||
Duties.company_id == selected_company.id,
|
||||
Duties.department_id.in_(department_ids),
|
||||
).data
|
||||
]
|
||||
staff_ids = [
|
||||
staff.id
|
||||
for staff in Staff.filter_active(
|
||||
for staff in Staff.filter_all(
|
||||
Staff.duties_id.in_(duties_ids)
|
||||
).data
|
||||
]
|
||||
employee = Employees.filter_active(
|
||||
employee = Employees.filter_one(
|
||||
Employees.people_id == token_user.person_id,
|
||||
Employees.staff_id.in_(staff_ids),
|
||||
).data[0]
|
||||
).data
|
||||
|
||||
reachable_event_list_id, reachable_event_list_uu_id = (
|
||||
Event2Employee.get_event_id_by_employee_id(employee_id=employee.id)
|
||||
)
|
||||
staff = Staff.find_one(id=employee.staff_id)
|
||||
duties = Duties.find_one(id=staff.duties_id)
|
||||
department = Departments.find_one(id=duties.department_id)
|
||||
bulk_id = Duty.find_one(duty_code="BULK")
|
||||
bulk_duty_id = Duties.find_one(
|
||||
company_id=selected_company.id, duties_id=bulk_id.id
|
||||
)
|
||||
staff = Staff.filter_one(Staff.id==employee.staff_id).data
|
||||
duties = Duties.find_one(Duties.id==staff.duties_id).data
|
||||
department = Departments.find_one(Departments.id==duties.department_id).data
|
||||
bulk_id = Duty.filter_one(Duty.duty_code=="BULK").data
|
||||
bulk_duty_id = Duties.filter_one(
|
||||
Duties.company_id==selected_company.id,
|
||||
Duties.duties_id==bulk_id.id
|
||||
).data
|
||||
update_selected_to_redis(
|
||||
request=request,
|
||||
add_payload=CompanyToken(
|
||||
@@ -174,30 +176,36 @@ class AuthenticationSelectEventMethods(MethodToEvent):
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
elif token_user.user_type == 2:
|
||||
occupant_type = OccupantTypes.find_one(uu_id=data.occupant_uu_id)
|
||||
occupant_type = OccupantTypes.filter_one(OccupantTypes.uu_id==data.occupant_uu_id).data
|
||||
if not occupant_type:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Occupant Type is not found",
|
||||
)
|
||||
build_part = BuildParts.find_one(uu_id=data.build_part_uu_id)
|
||||
build_part = BuildParts.filter_one(BuildParts.uu_id==data.build_part_uu_id).data
|
||||
if not build_part:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Build Part is not found",
|
||||
)
|
||||
build = Build.find_one(id=build_part.build_id)
|
||||
related_company = RelationshipEmployee2Build.find_one(member_id=build.id)
|
||||
company_related = Companies.find_one(id=related_company.company_id)
|
||||
responsible_employee = Employees.find_one(id=related_company.employee_id)
|
||||
if selected_occupant_type := BuildLivingSpace.find_one(
|
||||
occupant_type=occupant_type.id,
|
||||
person_id=token_user.person_id,
|
||||
build_parts_id=build_part.id,
|
||||
):
|
||||
build = Build.filter_one(Build.id==build_part.build_id).data
|
||||
related_company = RelationshipEmployee2Build.filter_one(
|
||||
RelationshipEmployee2Build.member_id==build.id
|
||||
).data
|
||||
company_related = Companies.filter_one(
|
||||
Companies.id==related_company.company_id
|
||||
).data
|
||||
responsible_employee = Employees.filter_one(
|
||||
Employees.id==related_company.employee_id
|
||||
).data
|
||||
if selected_occupant_type := BuildLivingSpace.filter_one(
|
||||
BuildLivingSpace.occupant_type==occupant_type.id,
|
||||
BuildLivingSpace.person_id==token_user.person_id,
|
||||
BuildLivingSpace.build_parts_id==build_part.id,
|
||||
).data:
|
||||
reachable_event_list_id, reachable_event_list_uu_id = (
|
||||
Event2Occupant.get_event_id_by_build_living_space_id(
|
||||
build_living_space_id=selected_occupant_type.id
|
||||
Event2Occupant.build_living_space_id==selected_occupant_type.id
|
||||
)
|
||||
)
|
||||
update_selected_to_redis(
|
||||
@@ -242,7 +250,7 @@ class AuthenticationCheckTokenEventMethods(MethodToEvent):
|
||||
|
||||
@classmethod
|
||||
def authentication_login_with_domain_and_creds(
|
||||
cls, request, token_dict: dict = None
|
||||
cls, request, token_dict: typing.Union[EmployeeSelection, OccupantSelection],
|
||||
):
|
||||
if get_object_via_access_key(request=request):
|
||||
return JSONResponse(
|
||||
@@ -263,15 +271,17 @@ class AuthenticationRefreshEventMethods(MethodToEvent):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def authentication_refresh_user_info(cls, request, token_dict: dict = None):
|
||||
def authentication_refresh_user_info(
|
||||
cls, request, token_dict: typing.Union[EmployeeSelection, OccupantSelection],
|
||||
):
|
||||
access_token = str(request.headers.get(Auth.ACCESS_TOKEN_TAG))
|
||||
if token_user := get_object_via_access_key(request=request):
|
||||
if found_user := Users.find_one(uu_id=token_user.get("uu_id")):
|
||||
user_token = UsersTokens.find_one(
|
||||
domain=found_user.domain_name,
|
||||
user_id=found_user.id,
|
||||
token_type="RememberMe",
|
||||
)
|
||||
if found_user := Users.filter_one(Users.uu_id==token_user.get("uu_id")).data:
|
||||
user_token = UsersTokens.filter_one(
|
||||
UsersTokens.domain==found_user.domain_name,
|
||||
UsersTokens.user_id==found_user.id,
|
||||
UsersTokens.token_type=="RememberMe",
|
||||
).data
|
||||
access_dict = {
|
||||
"access_token": access_token,
|
||||
"refresh_token": getattr(user_token, "token", None),
|
||||
@@ -299,11 +309,11 @@ class AuthenticationChangePasswordEventMethods(MethodToEvent):
|
||||
|
||||
@classmethod
|
||||
def authentication_change_password(
|
||||
cls, request, data: ChangePassword, token_dict: dict = None
|
||||
cls, request, data: ChangePassword, token_dict: typing.Union[EmployeeSelection, OccupantSelection],
|
||||
):
|
||||
token_user = get_object_via_access_key(request=request)
|
||||
if token_user.user_type == 1:
|
||||
if found_user := Users.find_one(uu_id=token_user.uu_id):
|
||||
if found_user := Users.filter_one(Users.uu_id==token_user.uu_id).data:
|
||||
if found_user.check_password(data.old_password):
|
||||
found_user.set_password(data.new_password)
|
||||
return JSONResponse(
|
||||
@@ -342,10 +352,12 @@ class AuthenticationCreatePasswordEventMethods(MethodToEvent):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_406_NOT_ACCEPTABLE, detail="Password must match"
|
||||
)
|
||||
if found_user := Users.find_one(password_token=data.password_token):
|
||||
if found_user := Users.filter_one(
|
||||
Users.password_token==data.password_token
|
||||
).data:
|
||||
found_user.create_password(password=data.password)
|
||||
found_user.password_token = None
|
||||
found_user.save()
|
||||
Users.save()
|
||||
send_email_completed = send_email(
|
||||
subject=f"Dear {found_user.user_tag}, your password has been changed.",
|
||||
receivers=[str(found_user.email)],
|
||||
@@ -386,7 +398,9 @@ class AuthenticationDisconnectUserEventMethods(MethodToEvent):
|
||||
):
|
||||
|
||||
if token_user := get_object_via_access_key(request=request):
|
||||
found_user = Users.find_one(uu_id=token_user.get("uu_id"))
|
||||
found_user = Users.filter_one(
|
||||
Users.uu_id==token_user.get("uu_id")
|
||||
).data
|
||||
if not found_user:
|
||||
return JSONResponse(
|
||||
content={
|
||||
@@ -400,7 +414,9 @@ class AuthenticationDisconnectUserEventMethods(MethodToEvent):
|
||||
for key in already_tokens:
|
||||
token_user = json.loads(redis_cli.get(key) or {})
|
||||
redis_cli.delete(key)
|
||||
selected_user = Users.find_one(uu_id=token_user.get("uu_id"))
|
||||
selected_user = Users.filter_one(
|
||||
Users.uu_id==token_user.get("uu_id")
|
||||
).data
|
||||
selected_user.remove_refresher_token(
|
||||
domain=data.domain, disconnect=True
|
||||
)
|
||||
@@ -449,7 +465,9 @@ class AuthenticationLogoutEventMethods(MethodToEvent):
|
||||
token_user = json.loads(redis_cli.get(key) or {})
|
||||
if token_user.get("domain") == data.domain:
|
||||
redis_cli.delete(key)
|
||||
selected_user = Users.find_one(uu_id=token_user.get("uu_id"))
|
||||
selected_user = Users.filter_one(
|
||||
Users.uu_id==token_user.get("uu_id")
|
||||
).data
|
||||
selected_user.remove_refresher_token(domain=data.domain)
|
||||
# UserLogger.log_error(
|
||||
# str(
|
||||
@@ -503,7 +521,9 @@ class AuthenticationRefreshTokenEventMethods(MethodToEvent):
|
||||
content={"completed": False, "message": "Invalid data", "data": {}},
|
||||
status_code=status.HTTP_202_ACCEPTED,
|
||||
)
|
||||
if found_user := Users.find_one(id=token_refresher.user_id):
|
||||
if found_user := Users.filter_one(
|
||||
Users.id==token_refresher.user_id
|
||||
).data:
|
||||
found_user: Users = found_user
|
||||
access_key = save_access_token_to_redis(
|
||||
request=request, found_user=found_user, domain=data.domain
|
||||
@@ -513,7 +533,7 @@ class AuthenticationRefreshTokenEventMethods(MethodToEvent):
|
||||
found_user.last_remote_addr = getattr(
|
||||
request, "remote_addr", None
|
||||
) or request.headers.get("X-Forwarded-For", None)
|
||||
found_user.last_seen = str(DateTimeLocal.now())
|
||||
found_user.last_seen = str(system_arrow.now())
|
||||
# UserLogger.log_error(
|
||||
# str(
|
||||
# dict(
|
||||
@@ -595,7 +615,7 @@ class AuthenticationForgotPasswordEventMethods(MethodToEvent):
|
||||
)
|
||||
|
||||
found_user.password_token = forgot_key
|
||||
found_user.password_token_is_valid = str(DateTimeLocal.shift(days=1))
|
||||
found_user.password_token_is_valid = str(system_arrow.shift(days=1))
|
||||
found_user.save()
|
||||
|
||||
return JSONResponse(
|
||||
@@ -632,13 +652,13 @@ class AuthenticationDownloadAvatarEventMethods(MethodToEvent):
|
||||
"remember_me": found_user.remember_me,
|
||||
"expiry_ends": str(found_user.expiry_ends),
|
||||
"expired_str": str(
|
||||
DateTimeLocal.now()
|
||||
- DateTimeLocal.get(str(found_user.expiry_ends))
|
||||
system_arrow.now()
|
||||
- system_arrow.get(str(found_user.expiry_ends))
|
||||
),
|
||||
"expired_int": int(
|
||||
(
|
||||
DateTimeLocal.now()
|
||||
- DateTimeLocal.get(str(found_user.expiry_ends))
|
||||
system_arrow.now()
|
||||
- system_arrow.get(str(found_user.expiry_ends))
|
||||
).days
|
||||
),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user