Skip to content

Commit

Permalink
adding email functionality. Users can send emails to other users
Browse files Browse the repository at this point in the history
  • Loading branch information
danilojezernik committed Sep 5, 2024
1 parent a927074 commit b674809
Show file tree
Hide file tree
Showing 10 changed files with 520 additions and 273 deletions.
1 change: 0 additions & 1 deletion src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
app.include_router(media.router, prefix='/media', tags=['Media'])
app.include_router(github.router, prefix='/github', tags=['Github'])
app.include_router(book.router, prefix='/book', tags=['Book'])
# app.include_router(technology.router, prefix='/technology', tags=['Technology'])

# Technologies
app.include_router(angular.router, prefix='/angular', tags=['Angular'])
Expand Down
30 changes: 0 additions & 30 deletions src/database/technology.py

This file was deleted.

20 changes: 20 additions & 0 deletions src/database/users_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import datetime

from src.domain.users_messages import Messages

message_reg = [
Messages(
user_id='user_id_1',
full_name_sender='user_id_1',
email_sender='[email protected]',
message='user_id_1',
datum_vnosa=datetime.datetime.now(),
).dict(by_alias=True),
Messages(
user_id='user_id_2',
full_name_sender='user_id_2',
email_sender='[email protected]',
message='user_id_2',
datum_vnosa=datetime.datetime.now(),
).dict(by_alias=True),
]
10 changes: 4 additions & 6 deletions src/domain/technology.py → src/domain/email_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
from pydantic import BaseModel, Field


class Technology(BaseModel):
class EmailData(BaseModel):
id: Optional[str] = Field(alias='_id', default_factory=lambda: str(ObjectId()))
technology: str
title: str
subtitle: str
vsebina: str
image: str
full_name: str
sender_email: str
message: str
datum_vnosa: datetime.datetime = Field(default_factory=datetime.datetime.now)
14 changes: 14 additions & 0 deletions src/domain/users_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import datetime
from typing import Optional

from bson import ObjectId
from pydantic import BaseModel, Field


class Messages(BaseModel):
id: Optional[str] = Field(alias='_id', default_factory=lambda: str(ObjectId()))
user_id: str
full_name_sender: str
email_sender: str
message: str
datum_vnosa: datetime.datetime = Field(default_factory=datetime.datetime.now)
219 changes: 0 additions & 219 deletions src/routes/technology.py

This file was deleted.

50 changes: 36 additions & 14 deletions src/routes/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

from fastapi import APIRouter, Depends, HTTPException

from src.domain.contact import Contact
from src.domain.email_data import EmailData
from src.domain.user import User
from src.domain.user_profile import UserProfile
from src.services import db
from src.services import db, emails
from src.services.security import get_current_user, pwd_context, make_hash, require_role
from src.template import send_email_reg_template

router = APIRouter()

Expand Down Expand Up @@ -63,22 +66,41 @@ async def get_user_by_id(_id: str) -> User:
return User(**cursor)


@router.post('/send-email', operation_id='send_email_to_registered_user')
async def send_email_registered():
"""
THIS ROUTES ARE PRIVATE
"""


@router.post('/send-email/{user_id}', operation_id='send_email_to_registered_user')
async def send_email_registered_user(
user_id: str,
emailing: EmailData,
current_user: User = Depends(get_current_user)
):
"""
Registered user will be able to send email to a registered user:
1. Only authenticated users will be able to send emails
2. User will only type in his name, email and message
3. Make a new email template for users
4. It will be sent to the user's email that user will want to send to
Registered user sends an email to another registered user.
- user_id: The ID of the user to whom the email will be sent.
- full_name: Full name of the sender.
- sender_email: Email of the sender.
- message: The message to be sent.
"""
return {"message": "message was sent"}

recipient = db.process.user.find_one({'_id': user_id})

"""
THIS ROUTES ARE PRIVATE
"""
if not recipient:
return HTTPException(status_code=404, detail='User not found')

recipient_user = User(**recipient)


body = send_email_reg_template.html(username_receiver=recipient_user.full_name, full_name_sender=emailing.full_name,
message=emailing.message, email=emailing.sender_email)

if not emails.write_email(email_to=recipient_user.email, email_from=emailing.sender_email, subject='Dobil si novo sporočilo s strani DaniloJezernik.com', body=body):
raise HTTPException(status_code=500, detail='Email not sent')

return {"message": "Email successfully sent to " + recipient_user.full_name}

# Get all users from database
@router.get('/admin/', operation_id='get_user_private')
Expand Down Expand Up @@ -252,8 +274,8 @@ async def get_user_profile(current_user: User = Depends(get_current_user)) -> Us
# Fetch the user profile from the database
user_profile = db.process.user.find_one({'_id': current_user.id}, {
'username': 1, 'profession': 1, 'email': 1, 'facebook': 1, 'instagram': 1,
'twitter': 1, 'github': 1, 'www': 1, 'blog_notification': 1, 'full_name' : 1,
'description' :1, 'confirmed': 1
'twitter': 1, 'github': 1, 'www': 1, 'blog_notification': 1, 'full_name': 1,
'description': 1, 'confirmed': 1
})

# If user profile is not found, raise a 404 error
Expand Down
Loading

0 comments on commit b674809

Please sign in to comment.