Skip to content

fix scheduler, send a welcome email only once per registration, python to 3.9.10 #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ A Fully Async-based backend for [Code Inbox](https://github.com/wiseaidev/code-i
## Development Requirements

- Make (GNU command)
- Python (>= 3.9)
- Python (>= 3.9.10)
- Poetry (1.6)

---
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ packages = [
]

[tool.poetry.dependencies]
python = "^3.10.10"
python = "^3.9.10"
fastapi = "^0.103.1"
uvicorn = {extras = ["standard"], version = "^0.23.2"}
pydantic = {extras = ["email"], version = "^1.10.12"}
Expand Down
1 change: 0 additions & 1 deletion src/nylas/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ async def login_user(token: str, session: AIOSession) -> Dict[str, Any]:
email_address = access_token_obj["email_address"]

user_obj = await find_existed_user(email_address, session)
print(user_obj)

if not user_obj:
await create_user(email_address, session)
Expand Down
4 changes: 0 additions & 4 deletions src/nylas/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ async def send_email(
draft["bcc"] = [{"email": request_body.bcc}]
draft["body"] = request_body.message
draft["from"] = [{"email": current_user.email}]
print(draft)
message = draft.send()
return message

Expand Down Expand Up @@ -205,7 +204,6 @@ async def delete_label(

removed_item = code_app.state.nylas.labels.delete(id=item_id)
if removed_item:
print(f"Removed item: {removed_item}")
return {"message": "Item deleted"}
return {"message": "Item not found"}

Expand Down Expand Up @@ -389,7 +387,6 @@ async def execute_code(
await asyncio.sleep(1)

if asyncio.get_event_loop().time() - start_time > timeout:
print("Timeout")
submission_status_dict[submission_token] = "Timeout"
break

Expand All @@ -403,7 +400,6 @@ async def execute_code(
print(new_response.json())
submission_stdout = new_response.json()["stdout"]
if submission_stdout:
print("Finished")
submission_status_dict[submission_token] = "Finished"
result = new_response.json()
break
Expand Down
2 changes: 2 additions & 0 deletions src/users/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ async def update_user_info(
"full_name": personal_info.full_name,
"bio": personal_info.bio,
"programming_language": personal_info.programming_language,
"schedule": personal_info.schedule,
"welcome": "sent",
"modified_date": datetime.utcnow(),
}
)
Expand Down
7 changes: 7 additions & 0 deletions src/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ class User(Model):
calendar: Optional[str] = Field(
default="", description="User's calendar ID."
)
schedule: Optional[str] = Field(
default="every day", description="User's schedule."
)
welcome: Optional[str] = Field(
default="not sent",
description="User's welcome email status, sent or not sent.",
)
user_status: Optional[UserStatus] = Field(
default=UserStatus.ACTIVE.value, description="User's status."
)
Expand Down
111 changes: 94 additions & 17 deletions src/users/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
# create and use as many Drives as you want!
profile_images = deta.Drive("profile-images")

# Create a dictionary to store scheduler instances for each user
user_schedulers = {}


@router.post(
"/user/logout",
Expand Down Expand Up @@ -162,7 +165,47 @@ async def update_personal_information(
An endpoint for updating users personel info.
"""
try:
from src.main import (
code_app,
)

await users_crud.update_user_info(personal_info, current_user, session)
schedule = personal_info.schedule
language = personal_info.language
email = current_user.email
# Create a new scheduler for the user
scheduler = BackgroundScheduler()
user_schedulers[email] = scheduler
if schedule == "Every hour":
scheduler.add_job(
code_app.state.openai.send_algorithm_email,
"interval",
hours=1,
args=(email, language),
)
elif schedule == "Every day":
scheduler.add_job(
code_app.state.openai.send_algorithm_email,
"interval",
days=1,
args=(email, language),
)
elif schedule == "Every week":
scheduler.add_job(
code_app.state.openai.send_algorithm_email,
"interval",
weeks=1,
args=(email, language),
)
elif schedule == "Every month":
scheduler.add_job(
code_app.state.openai.send_algorithm_email,
"interval",
months=1,
args=(email, language),
)

scheduler.start()
return {
"status_code": 200,
"message": "Your personal information has been updated successfully!",
Expand All @@ -185,35 +228,69 @@ async def update_language(
session: AIOSession = Depends(dependencies.get_db_transactional_session),
) -> Dict[str, Any]:
"""
Set the programming language for a specific user using their access token.
Set the programming language and the emails schedule for a specific user using their access token.
"""
try:
from src.main import (
code_app,
)

scheduler = BackgroundScheduler()
email = current_user.email
language = request_body.language
schedule = request_body.schedule
if email in user_schedulers:
# Use the existing scheduler for the user
scheduler = user_schedulers[email]
else:
# Create a new scheduler for the user
scheduler = BackgroundScheduler()
user_schedulers[email] = scheduler
if current_user.welcome == "not sent":
# send a welcome email in the background
ensure_future(nylas_crud.send_welcome_email(email))
# send an algorithm email in the background
ensure_future(
code_app.state.openai.async_send_algorithm_email(
email, language
)
)
user_info = users_schemas.PersonalInfo(
full_name=current_user.full_name,
bio=current_user.bio,
programming_language=request_body.language,
programming_language=language,
schedule=schedule,
)
await users_crud.update_user_info(user_info, current_user, session)
# send a welcome email in the background
ensure_future(nylas_crud.send_welcome_email(current_user.email))
# send an algorithm email in the background
ensure_future(
code_app.state.openai.async_send_algorithm_email(
current_user.email, request_body.language

if schedule == "Every hour":
scheduler.add_job(
code_app.state.openai.send_algorithm_email,
"interval",
hours=1,
args=(email, language),
)
)
# send an algorithm email every 24 hours
scheduler.add_job(
code_app.state.openai.send_algorithm_email,
"interval",
hours=24,
args=(current_user.email, request_body.language),
)
elif schedule == "Every day":
scheduler.add_job(
code_app.state.openai.send_algorithm_email,
"interval",
days=1,
args=(email, language),
)
elif schedule == "Every week":
scheduler.add_job(
code_app.state.openai.send_algorithm_email,
"interval",
weeks=1,
args=(email, language),
)
elif schedule == "Every month":
scheduler.add_job(
code_app.state.openai.send_algorithm_email,
"interval",
months=1,
args=(email, language),
)

scheduler.start()
return {
"status_code": 200,
Expand Down
13 changes: 13 additions & 0 deletions src/users/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ class PersonalInfo(BaseModel):
programming_language: str = Field(
..., example="python", description="User's programming language."
)
schedule: str = Field(
..., example="Every hour", description="User's schedule."
)


class LogoutSchema(BaseModel):
Expand All @@ -123,3 +126,13 @@ class LanguageSchema(BaseModel):
language: str = Field(
..., example="python", description="User's programming language."
)

schedule: str = Field(
..., example="Every hour", description="User's schedule."
)

welcome: bool = Field(
...,
example=True,
description="A field that indicates whether of not the user should receive a welcome message.",
)