Skip to content

Commit

Permalink
Merge branch 'add-redirect-name' of https://github.com/MasoniteFramew…
Browse files Browse the repository at this point in the history
…ork/core into add-redirect-name
  • Loading branch information
josephmancuso committed Dec 9, 2018
2 parents 0abba72 + 8ed27f8 commit 76e3e61
Show file tree
Hide file tree
Showing 15 changed files with 163 additions and 39 deletions.
10 changes: 9 additions & 1 deletion config/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@
|
"""

DRIVERS = {}
DRIVERS = {
'amqp': {
'username': 'guest',
'password': 'guest',
'host': 'localhost',
'port': '5672',
'channel': 'default',
}
}
8 changes: 7 additions & 1 deletion masonite/commands/QueueWorkCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ def callback(ch, method, properties, body):
job = pickle.loads(body)
obj = job['obj']
args = job['args']
callback = job['callback']
if inspect.isclass(obj):
obj = container.resolve(obj)
obj.handle(*args)

try:
getattr(obj, callback)(*args)
except AttributeError:
obj(*args)

ch.basic_ack(delivery_tag=method.delivery_tag)


Expand Down
13 changes: 13 additions & 0 deletions masonite/drivers/BaseMailDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self, app: App, view: View):
self.message_subject = 'Subject'
self.message_body = None
self.view = view
self._queue = False

def to(self, user_email):
"""Set the user email address who you want to send mail to.
Expand All @@ -37,6 +38,18 @@ def to(self, user_email):
self.to_address = user_email
return self

def queue(self):
"""Set the user email address who you want to send mail to.
Arguments:
user_email {string} -- The user email address.
Returns:
self
"""
self._queue = True
return self

def template(self, template_name, dictionary={}):
"""Create an email from a normal Jinja template.
Expand Down
18 changes: 18 additions & 0 deletions masonite/drivers/MailMailgunDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,29 @@ def send(self, message=None):
Returns:
requests.post -- Returns the response as a requests object.
"""

if self._queue:
from wsgi import container
from masonite import Queue
return container.make(Queue).push(self._send_mail, args=(message,))

return self._send_mail(message)

def _send_mail(self, message):
"""Wrapper around sending mail so it can also be used with queues.
Arguments:
message {string|None} -- The message to be sent passed in from the send method.
Returns:
requests.post
"""
if not message:
message = self.message_body

domain = self.config.DRIVERS['mailgun']['domain']
secret = self.config.DRIVERS['mailgun']['secret']

return requests.post(
"https://api.mailgun.net/v3/{0}/messages".format(domain),
auth=("api", secret),
Expand Down
19 changes: 16 additions & 3 deletions masonite/drivers/MailSmtpDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,20 @@ def send(self, message_contents=None):

self.smtp.login(config['username'], config['password'])

# self.smtp.send_message(message)
self.smtp.sendmail(self.config.FROM['name'],
self.to_address, message.as_string())
if self._queue:
from wsgi import container
from masonite import Queue
container.make(Queue).push(
self._send_mail,
args=(self.config.FROM['name'], self.to_address, message.as_string())
)
return

self._send_mail(self.config.FROM['name'],
self.to_address, message.as_string())

def _send_mail(self, *args):
"""Wrapper around sending mail so it can also be used for queues.
"""
self.smtp.sendmail(*args)
self.smtp.quit()
4 changes: 2 additions & 2 deletions masonite/drivers/QueueAmqpDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _publish(self, body):
delivery_mode=2, # make message persistent
))

def push(self, *objects, args=()):
def push(self, *objects, args=(), callback='handle'):
"""Push objects onto the amqp stack.
Arguments:
Expand All @@ -70,7 +70,7 @@ def push(self, *objects, args=()):
for obj in objects:
# Publish to the channel for each object
try:
self._publish({'obj': obj, 'args': args})
self._publish({'obj': obj, 'args': args, 'callback': callback})
except self.pika.exceptions.ConnectionClosed:
self._connect()
self._publish({'obj': obj, 'args': args})
12 changes: 9 additions & 3 deletions masonite/drivers/QueueAsyncDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, app: App):
"""
self.container = app

def push(self, *objects, args=()):
def push(self, *objects, args=(), callback='handle'):
"""Push objects onto the async stack.
Arguments:
Expand All @@ -29,6 +29,12 @@ def push(self, *objects, args=()):
if inspect.isclass(obj):
obj = self.container.resolve(obj)

thread = threading.Thread(
target=obj.handle, args=args, kwargs={})
try:
thread = threading.Thread(
target=getattr(obj, callback), args=args, kwargs={})
except AttributeError:
# Could be wanting to call only a method asyncronously
thread = threading.Thread(
target=obj, args=args, kwargs={})

thread.start()
2 changes: 2 additions & 0 deletions masonite/queues/ShouldQueue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ShouldQueue:
pass
1 change: 1 addition & 0 deletions masonite/queues/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .Queueable import Queueable
from .ShouldQueue import ShouldQueue
Empty file added tests/__init__.py
Empty file.
Empty file added tests/queues/__init__.py
Empty file.
27 changes: 0 additions & 27 deletions tests/queues/test_async_driver.py

This file was deleted.

55 changes: 55 additions & 0 deletions tests/queues/test_drivers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from masonite.app import App
from masonite.drivers import QueueAsyncDriver, QueueAmqpDriver
from masonite.managers import QueueManager
from config import queue

from masonite.queues.Queueable import Queueable
import os
from masonite.environment import LoadEnvironment, env

LoadEnvironment()


class Job(Queueable):

def handle(self):
print('sending from job handled')
return 'test'

class Random(Queueable):

def send(self):
print('sending from random send method')
return 'test'

def handle(self):
print('sending from random handle method')
return 'test'


class TestAsyncDriver:

def setup_method(self):
self.app = App()

self.app.bind('QueueAsyncDriver', QueueAsyncDriver)
self.app.bind('QueueAmqpDriver', QueueAmqpDriver)
self.app.bind('QueueConfig', queue)
self.app.bind('Queueable', Queueable)
self.app.bind('Container', self.app)
self.app.bind('QueueManager', QueueManager(self.app))
self.drivers = ['async']
if env('RUN_AMQP'):
self.drivers.append('amqp')

def test_async_driver_pushes_to_queue(self):
for driver in self.drivers:
assert self.app.make('QueueManager').driver(driver).push(Job) is None

def test_async_driver_can_run_any_callback_method(self):
for driver in self.drivers:
assert self.app.make('QueueManager').driver(driver).push(Random, callback="send") is None

def test_async_driver_can_run_any_method(self):
for driver in self.drivers:
assert self.app.make('QueueManager').driver(driver).push(Random().send) is None
16 changes: 14 additions & 2 deletions tests/test_mailgun_driver.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from config import mail

import pytest
import os

from masonite import env
from masonite.environment import LoadEnvironment

LoadEnvironment()



from masonite.app import App
from masonite.exceptions import DriverNotFound
from masonite.view import View
from masonite.managers.MailManager import MailManager
from masonite.drivers.MailSmtpDriver import MailSmtpDriver as MailDriver
from masonite.drivers.MailMailgunDriver import MailMailgunDriver as Mailgun

from config import mail

if os.getenv('MAILGUN_SECRET'):

Expand Down Expand Up @@ -36,3 +43,8 @@ def test_mailgun_driver(self):
def test_mail_renders_template(self):
assert 'MasoniteTesting' in MailManager(self.app).driver('mailgun').to(
'idmann509@gmail.com').template('mail/welcome', {'to': 'MasoniteTesting'}).message_body

def test_mail_sends_with_queue_and_without_queue(self):
if env('RUN_MAIL'):
assert MailManager(self.app).driver('mailgun').to('idmann509@gmail.com').send('test queue') == None
assert MailManager(self.app).driver('mailgun').queue().to('idmann509@gmail.com').send('test queue') == None
17 changes: 17 additions & 0 deletions tests/test_managers_mail_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import pytest

from masonite.environment import LoadEnvironment

LoadEnvironment()

from config import mail
from masonite.app import App
from masonite.contracts import MailManagerContract
Expand All @@ -11,6 +15,7 @@
from masonite.managers import MailManager
from masonite.view import View
from masonite.contracts import MailContract
from masonite import env


class MailSmtpDriver:
Expand Down Expand Up @@ -92,6 +97,18 @@ def test_send_mail_with_from(self):

assert MailManager(self.app).driver('smtp').to('idmann509@gmail.com').send_from('masonite@masonite.com').from_address == 'masonite@masonite.com'

def test_send_mail_sends(self):
if env('RUN_MAIL'):
self.app.bind('MailSmtpDriver', MailDriver)

assert MailManager(self.app).driver('smtp').to('idmann509@gmail.com').send('hi')

def test_send_mail_sends_with_queue(self):
if env('RUN_MAIL'):
self.app.bind('MailSmtpDriver', MailDriver)

assert MailManager(self.app).driver('smtp').to('idmann509@gmail.com').queue().send('hi') == None

def test_send_mail_with_subject(self):
self.app.bind('MailSmtpDriver', MailDriver)

Expand Down

0 comments on commit 76e3e61

Please sign in to comment.