Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
Mail service and use case (invite firend) done NOT TESTED (#58)
Browse files Browse the repository at this point in the history
* Mail service and use case (invite firend)
  • Loading branch information
Quimbu committed Dec 23, 2019
1 parent b340f95 commit d07e724
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 4 deletions.
18 changes: 18 additions & 0 deletions thesheriff/application/outlaw/invite_friend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import inject
from thesheriff.domain.mail.mail import Mail
from thesheriff.domain.mail.repository.mail_notification \
import MailNotification
from thesheriff.domain.outlaw.outlaw import Outlaw


class InviteFriend:

@inject.autoparams()
def __init__(self, mail_repository: MailNotification):
self.mail_repository = mail_repository

def execute(self, receiver_mail_address):
sender = Outlaw.get_email()
receiver = receiver_mail_address
mail = Mail.mail_invite_friend(sender, receiver)
self.mail_repository.send(mail)
Empty file.
Empty file.
23 changes: 23 additions & 0 deletions thesheriff/domain/mail/mail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import string


class Mail:
def __init__(self):
sender = ""
receiver = ""
content = ""

def mail_invite_friend(self, sender, receiver):
self.sender = sender
self.receiver = receiver
self.content = "Hello dear " + str(receiver) + "!\n " \
"Do you want to join this awesome game?" + \
"Come on!"

if sender is None:
raise Exception('User address needed to send an invitation')
if receiver is None:
raise Exception('Destination address needed to send an invitation')

def send_notification_raid(self):
return
Empty file.
9 changes: 9 additions & 0 deletions thesheriff/domain/mail/repository/mail_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import abc
from thesheriff.domain.mail.mail import Mail


class MailNotification(abc.ABC):

@abc.abstractmethod
def send(self, mail: Mail):
pass
3 changes: 3 additions & 0 deletions thesheriff/domain/outlaw/outlaw.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ def join_gang(self, gang: Gang) -> NoReturn:
:rtype: NoReturn
"""
self.gangs.append(gang)

def get_email(self):
return self.email
4 changes: 0 additions & 4 deletions thesheriff/domain/outlaw/repository/outlaw_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,3 @@ def update(self, mod_outlaw: Outlaw) -> NoReturn:
@abc.abstractmethod
def remove(self, outlaw_id: int) -> NoReturn:
pass

@abc.abstractmethod
def get_friends(self, outlaw_id: int) -> List[Outlaw]:
pass
21 changes: 21 additions & 0 deletions thesheriff/infrastructure/controllers/outlaw_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from thesheriff.application.outlaw.request.create_outlaw_request import \
CreateOutlawRequest
from thesheriff.domain.outlaw.score import Score
from thesheriff.application.outlaw.invite_friend import InviteFriend


@inject.autoparams()
Expand Down Expand Up @@ -164,4 +165,24 @@ def rate_raid_endpoint(outlaw_id: int, raid_id: int) -> Response:
message = {'status': 201, 'message': 'rated successfully'}
return jsonify(message)

@inject.autoparams()
def invite_friend_blueprint(invite_friend: InviteFriend) -> Blueprint:
"""Sends an email to the receiver
:param invitar_amigo: Object to invite the receiver
:returns: Blueprint
"""

@invite_friend.route("/outlaw/invite_friend/", methods=['POST'])
def invite_friend(receiver_mail_address: str) -> Response:
"""Invite friend will receive a mail from a json payload and will send
to him an invitation mail to join the app
:param
:type :
:return: Response.
"""
invite_friend.execute(receiver_mail_address)
# TODO: Fill json response
message = {''}
return jsonify(message)

return blueprint_outlaw
26 changes: 26 additions & 0 deletions thesheriff/infrastructure/repository/smtp_mail_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import smtplib
import ssl
from thesheriff.domain.mail.repository.mail_notification \
import MailNotification


class SMTPMailRepository:
global port # For SSL
global context
global password

# Create a secure SSL context
port = 465
context = ssl.create_default_context()
# TODO(all): read the password from an environment variable
password = "thesheriff123"

def __init__(self, mail_repository: MailNotification):
self.mail_repository = mail_repository

def send(self, mail):
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) \
as server:
# To send the email, we log into the system with the sender address
server.login(mail.sender, password)
server.sendmail(mail.sender, mail.receiver, mail.content)

0 comments on commit d07e724

Please sign in to comment.