events updated

This commit is contained in:
2024-12-23 13:07:25 +03:00
parent b8cebd9af4
commit 5223f36da7
17 changed files with 160 additions and 65 deletions

View File

@@ -1,7 +1,9 @@
import datetime
import json
import typing
from typing import Union
import arrow
from fastapi import status
from fastapi.requests import Request
from fastapi.exceptions import HTTPException
@@ -116,9 +118,11 @@ class AuthenticationSelectEventMethods(MethodToEvent):
def authentication_select_company_or_occupant_type(
cls,
request: Request,
data,
token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
data: Union[EmployeeSelection, OccupantSelection],
token_dict: Union[EmployeeTokenObject, OccupantTokenObject],
):
from api_objects import OccupantToken, CompanyToken
if isinstance(token_dict, EmployeeTokenObject):
if data.company_uu_id not in token_dict.companies_uu_id_list:
return JSONResponse(
@@ -141,7 +145,6 @@ class AuthenticationSelectEventMethods(MethodToEvent):
duties.id
for duties in Duties.filter_all(
Duties.company_id == selected_company.id,
Duties.department_id.in_(department_ids),
).data
]
staff_ids = [
@@ -154,7 +157,6 @@ class AuthenticationSelectEventMethods(MethodToEvent):
Employees.people_id == token_dict.person_id,
Employees.staff_id.in_(staff_ids),
).data
reachable_event_list_id = Event2Employee.get_event_id_by_employee_id(
employee_id=employee.id
)
@@ -167,12 +169,11 @@ class AuthenticationSelectEventMethods(MethodToEvent):
department = Departments.filter_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,
bulk_id = Duty.filter_by_one(system=True, duty_code="BULK").data
bulk_duty_id = Duties.filter_by_one(
company_id=selected_company.id,
duties_id=bulk_id.id,
**Duties.valid_record_dict,
).data
update_selected_to_redis(
request=request,
@@ -189,7 +190,6 @@ class AuthenticationSelectEventMethods(MethodToEvent):
employee_id=employee.id,
employee_uu_id=employee.uu_id.__str__(),
reachable_event_list_id=reachable_event_list_id,
# reachable_event_list_uu_id=reachable_event_list_uu_id,
),
)
return JSONResponse(
@@ -200,23 +200,25 @@ class AuthenticationSelectEventMethods(MethodToEvent):
status_code=status.HTTP_200_OK,
)
elif isinstance(token_dict, OccupantTokenObject):
occupant_type = OccupantTypes.filter_one(
OccupantTypes.uu_id == data.occupant_uu_id
occupant_type = OccupantTypes.filter_by_one(
system=True, 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.filter_one(
BuildParts.uu_id == data.build_part_uu_id,
build_part = BuildParts.filter_by_one(
system=True, 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.filter_one(Build.id == build_part.build_id).data
build = Build.filter_one(
Build.id == build_part.build_id,
).data
related_company = RelationshipEmployee2Build.filter_one(
RelationshipEmployee2Build.member_id == build.id,
).data
@@ -233,8 +235,7 @@ class AuthenticationSelectEventMethods(MethodToEvent):
).data:
reachable_event_list_id = (
Event2Occupant.get_event_id_by_build_living_space_id(
Event2Occupant.build_living_space_id
== selected_occupant_type.id
build_living_space_id=selected_occupant_type.id
)
)
update_selected_to_redis(
@@ -254,7 +255,6 @@ class AuthenticationSelectEventMethods(MethodToEvent):
responsible_company_id=company_related.id,
responsible_company_uuid=company_related.uu_id.__str__(),
reachable_event_list_id=reachable_event_list_id,
# reachable_event_list_uu_id=reachable_event_list_uu_id,
),
)
return JSONResponse(
@@ -270,6 +270,7 @@ class AuthenticationSelectEventMethods(MethodToEvent):
)
class AuthenticationCheckTokenEventMethods(MethodToEvent):
event_type = "LOGIN"
@@ -284,10 +285,9 @@ class AuthenticationCheckTokenEventMethods(MethodToEvent):
}
@classmethod
def authentication_login_with_domain_and_creds(
cls,
request,
token_dict: typing.Union[EmployeeSelection, OccupantSelection],
def authentication_check_token_is_valid(
cls,
request,
):
if get_object_via_access_key(request=request):
return JSONResponse(
@@ -652,6 +652,52 @@ class AuthenticationForgotPasswordEventMethods(MethodToEvent):
)
class AuthenticationResetPasswordEventMethods(MethodToEvent):
event_type = "UPDATE"
__event_keys__ = {
"af9e121e-24bb-44ac-a616-471d5754360e": "authentication_reset_password",
}
@classmethod
def authentication_reset_password(cls, data: Forgot):
from sqlalchemy import or_
found_user = Users.query.filter(
or_(
Users.email == str(data.access_key).lower(),
Users.phone_number == str(data.access_key).replace(" ", ""),
),
).first()
if not found_user:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Given access key or domain is not matching with the any user record.",
)
reset_password_token = found_user.reset_password_token(found_user=found_user)
send_email_completed = send_email(
subject=f"Dear {found_user.user_tag}, a password reset request has been received.",
receivers=[str(found_user.email)],
html=change_your_password_template(
user_name=found_user.user_tag,
forgot_link=ApiStatic.forgot_link(forgot_key=reset_password_token),
),
)
if not send_email_completed:
raise found_user.raise_http_exception(
status_code=400, message="Email can not be sent. Try again later"
)
return JSONResponse(
content={
"completed": True,
"message": "Password change link is sent to your email or phone",
"data": found_user.get_dict(),
},
status_code=status.HTTP_200_OK,
)
class AuthenticationDownloadAvatarEventMethods(MethodToEvent):
event_type = "LOGIN"
@@ -666,27 +712,28 @@ class AuthenticationDownloadAvatarEventMethods(MethodToEvent):
}
@classmethod
def authentication_download_avatar(cls, data: Forgot):
if found_user := Users.check_user_exits(
access_key=data.access_key, domain=data.domain
):
def authentication_download_avatar(cls, token_dict: Union[
EmployeeTokenObject, OccupantTokenObject
]):
if found_user := Users.filter_one(Users.id == token_dict.user_id).data:
expired_starts = str(
system_arrow.now() - system_arrow.get(str(found_user.expiry_ends))
)
expired_int = int(
system_arrow.now() - system_arrow.get(str(found_user.expiry_ends)).days
)
expired_int = (
system_arrow.now() - system_arrow.get(str(found_user.expiry_ends))
).days
return JSONResponse(
content={
"completed": True,
"message": "Avatar and profile is shared via user credentials",
"data": {
"last_seen": str(found_user.last_seen),
"full_name": found_user.person.full_name,
"avatar": found_user.avatar,
"remember_me": found_user.remember_me,
"expiry_ends": str(found_user.expiry_ends),
"expired_str": expired_starts,
"expired_int": expired_int,
"expired_int": int(expired_int),
},
},
status_code=status.HTTP_200_OK,
@@ -730,6 +777,9 @@ AuthenticationForgotPasswordEventMethod = AuthenticationForgotPasswordEventMetho
AuthenticationDownloadAvatarEventMethod = AuthenticationDownloadAvatarEventMethods(
action=ActionsSchema(endpoint="/authentication/avatar")
)
AuthenticationResetPasswordEventMethod = AuthenticationResetPasswordEventMethods(
action=ActionsSchema(endpoint="/authentication/reset_password")
)
# UserLogger.log_error(
# str(