Skip to content

Commit

Permalink
Merge pull request #3271 from MarekBleschke/feature/RAS-1220
Browse files Browse the repository at this point in the history
email with attachments transition action
  • Loading branch information
MarekBleschke committed Jun 26, 2018
2 parents cb7ac4d + 6777436 commit 77c0454
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
19 changes: 19 additions & 0 deletions src/ralph/back_office/models.py
Expand Up @@ -11,6 +11,7 @@
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.exceptions import ImproperlyConfigured
from django.core.mail import EmailMessage
from django.db import models, transaction
from django.forms import ValidationError
from django.template import Context, Template
Expand All @@ -37,6 +38,7 @@
from ralph.lib.transitions.conf import get_report_name_for_transition_id
from ralph.lib.transitions.decorators import transition_action
from ralph.lib.transitions.fields import TransitionField
from ralph.lib.transitions.models import Transition
from ralph.licences.models import BaseObjectLicence, Licence
from ralph.reports.models import Report, ReportLanguage

Expand Down Expand Up @@ -605,6 +607,23 @@ def release_report(cls, instances, requester, transition_id, **kwargs):
language=kwargs['report_language']
)

@classmethod
@transition_action(run_after=['release_report', 'return_report',
'loan_report'])
def send_attachments_to_user(cls, requester, transition_id, **kwargs):
if kwargs.get('attachments'):
transition = Transition.objects.get(pk=transition_id)
email = EmailMessage(
subject='Documents for {}'.format(transition.name),
body='Please see documents provided in attachments '
'for "{}".'.format(transition.name),
from_email=settings.EMAIL_FROM,
to=[requester.email]
)
for attachment in kwargs['attachments']:
email.attach_file(attachment.file.path)
email.send()

@classmethod
@transition_action(
form_fields={
Expand Down
58 changes: 43 additions & 15 deletions src/ralph/back_office/tests/test_models.py
Expand Up @@ -5,6 +5,8 @@
from dj.choices import Country
from django.contrib.auth.models import Permission
from django.contrib.messages.storage.fallback import FallbackStorage
from django.core import mail
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.urlresolvers import reverse
from django.test import override_settings, RequestFactory, SimpleTestCase
from django.utils import timezone
Expand All @@ -18,6 +20,7 @@
DataCenterAssetModelFactory,
ServiceEnvironmentFactory
)
from ralph.attachments.models import Attachment
from ralph.back_office.models import (
_check_assets_owner,
BackOfficeAsset,
Expand Down Expand Up @@ -397,21 +400,6 @@ def test_assign_hostname_skips_hostname_when_its_already_set(self):

self.assertEquals(self.bo_asset.hostname, hostname)

def try_change_status_to_in_progress_during_transition(self):
_, transition, _ = self._create_transition(
model=self.bo_asset,
name='test',
source=[BackOfficeAssetStatus.new.id],
target=BackOfficeAssetStatus.in_progress.id,
actions=['assign_owner']
)
self.bo_asset.run_status_transition(
transition_obj_or_name=transition,
data={'assign_owner__owner': self.user_pl.id},
request=self.request
)
self.assertEqual(self.bo_asset.hostname, 'USPC01001')

def test_return_report_when_user_not_assigned(self):
_, transition, _ = self._create_transition(
model=self.bo_asset,
Expand Down Expand Up @@ -464,6 +452,46 @@ def test_a_report_is_generated(self, mock_method):
self.assertEqual(attachment.original_filename, correct_filename)
self.assertEqual(attachment.file.read(), GENERATED_FILE_CONTENT)

def test_send_attachments_to_user_action_sends_email(self):
bo_asset = BackOfficeAssetFactory(model=self.model)
_, transition, _ = self._create_transition(
model=bo_asset,
name='transition name',
source=[BackOfficeAssetStatus.new.id],
target=BackOfficeAssetStatus.used.id,
actions=['return_report']
)
attachment = Attachment.objects.create(
file=SimpleUploadedFile(
'test_file.pdf',
b'some content',
content_type='application/pdf'
),
uploaded_by=self.user_pl,
)

bo_asset.send_attachments_to_user(
self.user_pl,
transition.id,
attachments=[attachment]
)

self.assertEqual(len(mail.outbox), 1)

def test_send_attachments_to_user_action_dont_send_email_without_attachments(self): # noqa: E501
bo_asset = BackOfficeAssetFactory(model=self.model)
_, transition, _ = self._create_transition(
model=self.bo_asset,
name='transition name',
source=[BackOfficeAssetStatus.new.id],
target=BackOfficeAssetStatus.used.id,
actions=['return_report']
)

bo_asset.send_attachments_to_user(self.user_pl, transition.id)

self.assertEqual(len(mail.outbox), 0)


class CheckerTestCase(SimpleTestCase):
def test_requester_must_be_specified(self):
Expand Down
2 changes: 1 addition & 1 deletion src/ralph/lib/external_services/base.py
Expand Up @@ -22,7 +22,7 @@ def __init__(self, service_name):
def run(self, **kwargs):
"""Run function with params on external service.
Basically this method call external method with params wich it
Basically this method call external method with params which it
accept. You must now about accepted params by external function
and provide it.
Expand Down
1 change: 1 addition & 0 deletions src/ralph/lib/transitions/models.py
Expand Up @@ -423,6 +423,7 @@ def run_field_transition(
action, transition
))
func = getattr(first_instance, action.name)
kwargs['attachments'] = attachments
defaults = _prepare_action_data(
action,
data,
Expand Down

0 comments on commit 77c0454

Please sign in to comment.