-
Notifications
You must be signed in to change notification settings - Fork 52
Feat/Basic Email use #61
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
yammesicka
merged 33 commits into
PythonFreeCourse:develop
from
PureDreamer:feature/email_send
Jan 20, 2021
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
d6ee4f8
Feat/Basic Email use
PureDreamer 2d3fab5
Feat/basic_email_send
PureDreamer 996d190
revert env setting
PureDreamer 68c9209
Changed tests
PureDreamer 9571245
changed file directories
PureDreamer 100438b
added demo templates
PureDreamer 0c76176
Merge remote-tracking branch 'upstream/develop' into feature/email_send
PureDreamer 7d01ce9
Changed to match with current commit
PureDreamer 85a57c8
Update config.py
PureDreamer 4bdb4bb
Update config.py.example
PureDreamer 404561b
Update config.py
PureDreamer ec8b3ab
fixed config
PureDreamer 335a954
Update requirements.txt
PureDreamer 2950fcc
typing email_conf
PureDreamer a086ab3
Pass validation pydantic
PureDreamer 82ab42c
Update config.py.example
PureDreamer 09b0ebe
Update test_email.py
PureDreamer f843285
Update test_email.py
PureDreamer 8a4b5b8
Another Try with fastapi mail
PureDreamer 5292cd1
smtpd mock
PureDreamer 11e87e0
Update test_email.py
PureDreamer a0ff240
Update test_email.py
PureDreamer 4a9366c
try to override error connection
PureDreamer 14168bb
Update test_email.py
PureDreamer b9f6bf0
Update test_email.py
PureDreamer 979e67d
Update test_email.py
PureDreamer ece2d73
update
PureDreamer 0979a94
Update test_email.py
PureDreamer eccb52a
Update test_email.py
PureDreamer 9c5d3ca
conf add plugin
PureDreamer bd760be
Update test_email.py
PureDreamer ff84deb
Update test_email.py
PureDreamer de1c620
Changed requested changes
PureDreamer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from app.config import email_conf | ||
| from app.database.models import Event, User | ||
| from fastapi import BackgroundTasks | ||
| from fastapi_mail import FastMail, MessageSchema | ||
| from sqlalchemy.orm.session import Session | ||
|
|
||
| mail = FastMail(email_conf) | ||
|
|
||
|
|
||
| def send( | ||
| session: Session, event_used: int, user_to_send: int, | ||
| title: str, background_tasks: BackgroundTasks = BackgroundTasks | ||
| ) -> bool: | ||
| """This function is being used to send emails in the background. | ||
| It takes an event and a user and it sends the event to the user. | ||
|
|
||
| Args: | ||
| session(Session): The session to redirect to the database. | ||
| title (str): Title of the email that is being sent. | ||
| event_used (int): Id number of the event that is used. | ||
| user_to_send (int): Id number of user that we want to notify. | ||
| background_tasks (BackgroundTasks): Function from fastapi that lets | ||
| you apply tasks in the background. | ||
|
|
||
| Returns: | ||
| bool: Returns True if the email was sent, else returns False. | ||
| """ | ||
| event_used = session.query(Event).filter( | ||
| Event.id == event_used).first() | ||
| user_to_send = session.query(User).filter( | ||
| User.id == user_to_send).first() | ||
| if not user_to_send or not event_used: | ||
| return False | ||
| message = MessageSchema( | ||
| subject=f"{title} {event_used.title}", | ||
| recipients={"email": [user_to_send.email]}.get("email"), | ||
| body=f"begins at:{event_used.start} : {event_used.content}", | ||
| ) | ||
| background_tasks.add_task(mail.send_message, message) | ||
| return True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from app.database.database import get_db | ||
| from app.internal.email import send as internal_send | ||
| from fastapi import APIRouter, BackgroundTasks, Depends, Form, HTTPException | ||
| from sqlalchemy.orm.session import Session | ||
| from starlette.responses import RedirectResponse | ||
|
|
||
| router = APIRouter( | ||
| prefix="/email", | ||
| tags=["email"], | ||
| responses={404: {"description": "Not found"}}, | ||
| ) | ||
|
|
||
|
|
||
| @router.post("/send") | ||
| async def send( | ||
| db: Session = Depends(get_db), | ||
| send_to: str = "/", | ||
| title: str = Form(...), | ||
| event_used: str = Form(...), | ||
| user_to_send: str = Form(...), | ||
| background_tasks: BackgroundTasks = BackgroundTasks | ||
| ) -> RedirectResponse: | ||
| if not internal_send( | ||
| title=title, event_used=event_used, | ||
| user_to_send=user_to_send, | ||
| background_tasks=background_tasks, session=db): | ||
| raise HTTPException(status_code=404, detail="Couldn't send the email!") | ||
| return RedirectResponse(send_to, status_code=303) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| {% extends "base.html" %} | ||
|
|
||
| {% block content %} | ||
|
|
||
| <div class="container mt-4"> | ||
| <h1>{{message}}</h1> | ||
| </div> | ||
|
|
||
| <div> | ||
| <form action="/email/send" method="post"> | ||
| <!-- Example of how to use sending email | ||
| This is a testing using ids of evenets and users yet with | ||
| simple manipulation you could send it how ever you want --> | ||
| <label for="event_used">Event Id</label><br> | ||
| <input type="text" value="{{ event_used }}" name="event_used"><br> | ||
| <label for="user_to_send">User Id</label><br> | ||
| <input type="text" value="{{user_to_send}}" name="user_to_send"> | ||
| <div> | ||
| <select class="form-control" name="title"> | ||
| <option value="New Event:">New Event</option> | ||
| <option value="Reminder:">Reminder</option> | ||
| <option value="Canceled:">Canceled</option> | ||
| <option value="Updated">Updated</option> | ||
| </select> | ||
| </div> | ||
| <input type="submit" value="Accept"> | ||
| </form> | ||
| {% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,9 @@ | ||
| {% extends "base.html" %} | ||
|
|
||
|
|
||
| {% block content %} | ||
|
|
||
| <div class="container mt-4"> | ||
| <h1>{{message}}</h1> | ||
| </div> | ||
|
|
||
| <div class="container mt-4"> | ||
| <h1>{{message}}</h1> | ||
| </div> | ||
|
|
||
| {% endblock %} | ||
| {% endblock %} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
|
|
||
|
|
||
| from app.internal.email import mail | ||
| from fastapi import BackgroundTasks | ||
|
|
||
|
|
||
| def test_email_send(client, user, event, smtpd): | ||
| mail.config.SUPPRESS_SEND = 1 | ||
| mail.config.MAIL_SERVER = smtpd.hostname | ||
| mail.config.MAIL_PORT = smtpd.port | ||
| mail.config.USE_CREDENTIALS = False | ||
| mail.config.MAIL_TLS = False | ||
| with mail.record_messages() as outbox: | ||
| response = client.post( | ||
| "/email/send", data={ | ||
| "event_used": event.id, "user_to_send": user.id, | ||
| "title": "Testing", | ||
| "background_tasks": BackgroundTasks}) | ||
| assert len(outbox) == 1 | ||
| assert response.ok | ||
|
|
||
|
|
||
| def test_failed_email_send(client, user, event, smtpd): | ||
| mail.config.SUPPRESS_SEND = 1 | ||
| mail.config.MAIL_SERVER = smtpd.hostname | ||
| mail.config.MAIL_PORT = smtpd.port | ||
| with mail.record_messages() as outbox: | ||
| response = client.post( | ||
| "/email/send", data={ | ||
| "event_used": event.id + 1, "user_to_send": user.id, | ||
| "title": "Testing", | ||
| "background_tasks": BackgroundTasks}) | ||
| assert len(outbox) == 0 | ||
| assert not response.ok |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.