Skip to content

Commit

Permalink
Adds the sites framework to be able to get the full url in the manage…
Browse files Browse the repository at this point in the history
…ment command
  • Loading branch information
dirtycoder committed Dec 21, 2016
1 parent 90d198f commit ca0d6ef
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 24 deletions.
11 changes: 9 additions & 2 deletions pets/meupet/services.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.conf import settings
from django.contrib.sites.models import Site
from django.core import mail
from django.core.urlresolvers import reverse
from django.template.loader import render_to_string
Expand All @@ -16,14 +17,20 @@ def send_request_action_email(pet):
subject = _('Update pet registration')
to = pet.owner.email
template_name = 'meupet/request_action_email.txt'
current_site = Site.objects.get_current()

full_url = 'https://{domain}{path}'.format(
domain=current_site.domain,
path=reverse('meupet:update_register', args=[pet.request_key])
)

context = {
'username': pet.owner.first_name,
'pet': pet.name,
'days': settings.DAYS_TO_STALE_REGISTER,
'status': pet.get_status_display(),
# ToDO: use full url
'link': reverse('meupet:update_register', args=[pet.request_key]),
'link': full_url,
'site_name': current_site.name,
}

return send_email(subject, to, template_name, context)
2 changes: 1 addition & 1 deletion pets/meupet/templates/meupet/request_action_email.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ days or update the status of the pet.{% endblocktrans %}
{% blocktrans %}If no action is taken the registration will be disabled in {{ days }} days.{% endblocktrans %}

{% trans "Best regards," %}
Cadê meu Bicho
{{ site_name }}
29 changes: 8 additions & 21 deletions pets/meupet/tests/test_model_pet.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from unittest import mock
from unittest.mock import patch

from django.core import mail
from django.conf import settings
from django.test import TestCase
from django.utils import timezone
from django.core.urlresolvers import reverse

from model_mommy import mommy

Expand Down Expand Up @@ -56,23 +53,13 @@ def test_expired_pets(self):
self.assertNotIn(new_pet, pets)
self.assertNotIn(staled_pet, pets)

def test_request_action_from_user(self):
"""Should send email requesting action from the user to keep the register active"""
@patch('meupet.services.send_request_action_email')
def test_request_action_from_user(self, mock_send_email):
"""Should call send_request_action_email method from request_action"""
pet = mommy.make(Pet)
pet.request_action()
email = mail.outbox[0]

contents = [
pet.name,
pet.owner.first_name,
str(settings.DAYS_TO_STALE_REGISTER),
reverse('meupet:update_register', args=[pet.request_key]),
pet.get_status_display()
]

for expected in contents:
with self.subTest():
self.assertIn(expected, email.body)
mock_send_email.assert_called_once_with(pet)

def test_request_sent_saved(self):
"""Should set the request_sent date in the pet and keep modified date"""
Expand All @@ -87,7 +74,7 @@ def test_request_sent_saved(self):
self.assertAlmostEqual(pet.request_sent, timezone.now(), delta=timezone.timedelta(seconds=1))

def test_request_action_email_set_activation_success(self):
"""Should set the request_key if the request_action_email succeed"""
"""Should set the request_key if the send_request_action_email succeed"""
pet = mommy.make(Pet, request_key='')

pet.request_action()
Expand All @@ -96,10 +83,10 @@ def test_request_action_email_set_activation_success(self):
self.assertNotEqual('', pet.request_key)

def test_request_action_email_not_set_activation_fail(self):
"""Shouldn't set the request_key if the request_action_email fail"""
"""Shouldn't set the request_key if the send_request_action_email fail"""
pet = mommy.make(Pet, request_key='')

with mock.patch('meupet.services.send_request_action_email') as mock_method:
with patch('meupet.services.send_request_action_email') as mock_method:
mock_method.return_value = False
pet.request_action()

Expand Down
34 changes: 34 additions & 0 deletions pets/meupet/tests/test_services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.conf import settings
from django.contrib.sites.models import Site
from django.core import mail
from django.core.urlresolvers import reverse
from django.test import TestCase

from model_mommy import mommy

from meupet.models import Pet


class ServicesTest(TestCase):
def test_request_action_from_user(self):
"""Validate the information sent in the email"""
pet = mommy.make(Pet)
pet.request_action()
email = mail.outbox[0]
current_site = Site.objects.get_current()

pet_update_url = reverse('meupet:update_register', args=[pet.request_key])
full_url = '%s%s' % (current_site.domain, pet_update_url)

contents = [
pet.name,
pet.owner.first_name,
str(settings.DAYS_TO_STALE_REGISTER),
full_url,
pet.get_status_display(),
current_site.name,
]

for expected in contents:
with self.subTest():
self.assertIn(expected, email.body)
3 changes: 3 additions & 0 deletions pets/pets/settings/prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.sites',
'django.contrib.staticfiles',
)

Expand All @@ -53,6 +54,8 @@

INSTALLED_APPS = DJANGO_APPS + PROJECT_APPS + THIRD_PARTS_APPS

SITE_ID = 1

MIDDLEWARE_CLASSES = (
'opbeat.contrib.django.middleware.OpbeatAPMMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand Down

0 comments on commit ca0d6ef

Please sign in to comment.