Skip to content

Commit

Permalink
Added a notes field so that the requestor can write notes for the cod…
Browse files Browse the repository at this point in the history
…es. (#594)

* Added a notes field so that the requestor can write notes & specifications for the codes they're ordering

* remove extra blank space from string

* Added new function with create_message_body and add release number in changelog file

* Change the release tag number and fix the quality issues

* Change the changelog message
  • Loading branch information
zamanafzal authored Oct 9, 2019
1 parent 3e673d1 commit bb5a5c7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Change Log
Unreleased
----------
[2.0.3] - 2019-10-09
---------------------

* Add message box to code management page and admin portal

[2.0.2] - 2019-10-07
--------------------
Expand Down
2 changes: 1 addition & 1 deletion enterprise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

from __future__ import absolute_import, unicode_literals

__version__ = "2.0.2"
__version__ = "2.0.3"

default_app_config = "enterprise.apps.EnterpriseConfig" # pylint: disable=invalid-name
32 changes: 32 additions & 0 deletions enterprise/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import absolute_import, unicode_literals

from django.conf import settings
from django.utils.translation import ugettext as _

from enterprise.models import (
EnterpriseCustomerCatalog,
Expand Down Expand Up @@ -53,3 +54,34 @@ def get_enterprise_customer_from_user_id(user_id):
return str(EnterpriseCustomerUser.objects.get(user_id=user_id).enterprise_customer.uuid)
except EnterpriseCustomerUser.DoesNotExist:
return None


def create_message_body(email, enterprise_name, number_of_codes=None, notes=None):
"""
Return the message body with extra information added by user.
"""
if number_of_codes and notes:
body_msg = _('{token_email} from {token_enterprise_name} has requested {token_number_codes} additional '
'codes. Please reach out to them.\nAdditional Notes:\n{token_notes}.').format(
token_email=email,
token_enterprise_name=enterprise_name,
token_number_codes=number_of_codes,
token_notes=notes)
elif number_of_codes:
body_msg = _('{token_email} from {token_enterprise_name} has requested {token_number_codes} additional '
'codes. Please reach out to them.').format(
token_email=email,
token_enterprise_name=enterprise_name,
token_number_codes=number_of_codes)
elif notes:
body_msg = _('{token_email} from {token_enterprise_name} has requested additional '
'codes. Please reach out to them.\nAdditional Notes:\n{token_notes}.').format(
token_email=email,
token_enterprise_name=enterprise_name,
token_notes=notes)
else:
body_msg = _('{token_email} from {token_enterprise_name} has requested additional codes.'
' Please reach out to them.').format(
token_email=email,
token_enterprise_name=enterprise_name)
return body_msg
29 changes: 14 additions & 15 deletions enterprise/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from enterprise.api.filters import EnterpriseCustomerUserFilterBackend, UserFilterBackend
from enterprise.api.throttles import ServiceUserThrottle
from enterprise.api.utils import (
create_message_body,
get_ent_cust_from_report_config_uuid,
get_enterprise_customer_from_catalog_id,
get_enterprise_customer_from_user_id,
Expand Down Expand Up @@ -516,28 +517,30 @@ class CouponCodesView(APIView):
REQUIRED_PARAM_EMAIL = 'email'
REQUIRED_PARAM_ENTERPRISE_NAME = 'enterprise_name'
OPTIONAL_PARAM_NUMBER_OF_CODES = 'number_of_codes'
OPTIONAL_PARAM_NOTES = 'notes'

MISSING_REQUIRED_PARAMS_MSG = "Some required parameter(s) missing: {}"

def get_required_query_params(self, request):
"""
Gets ``email``, ``enterprise_name``, and ``number_of_codes``,
Gets ``email``, ``enterprise_name``, ``number_of_codes``, and ``notes``,
which are the relevant parameters for this API endpoint.
:param request: The request to this endpoint.
:return: The ``email``, ``enterprise_name``, and ``number_of_codes`` from the request.
:return: The ``email``, ``enterprise_name``, ``number_of_codes`` and ``notes`` from the request.
"""
email = get_request_value(request, self.REQUIRED_PARAM_EMAIL, '')
enterprise_name = get_request_value(request, self.REQUIRED_PARAM_ENTERPRISE_NAME, '')
number_of_codes = get_request_value(request, self.OPTIONAL_PARAM_NUMBER_OF_CODES, '')
notes = get_request_value(request, self.OPTIONAL_PARAM_NOTES, '')
if not (email and enterprise_name):
raise CodesAPIRequestError(
self.get_missing_params_message([
(self.REQUIRED_PARAM_EMAIL, bool(email)),
(self.REQUIRED_PARAM_ENTERPRISE_NAME, bool(enterprise_name)),
])
)
return email, enterprise_name, number_of_codes
return email, enterprise_name, number_of_codes, notes

def get_missing_params_message(self, parameter_state):
"""
Expand All @@ -555,7 +558,8 @@ def post(self, request):
>>> {
>>> "email": "bob@alice.com",
>>> "enterprise_name": "IBM",
>>> "number_of_codes": "50"
>>> "number_of_codes": "50",
>>> "notes": "Help notes for codes request",
>>> }
Keys:
Expand All @@ -565,36 +569,31 @@ def post(self, request):
The name of the enterprise requesting more codes.
*number_of_codes*
The number of codes requested.
*notes*
Help notes related to codes request.
"""
try:
email, enterprise_name, number_of_codes = self.get_required_query_params(request)
email, enterprise_name, number_of_codes, notes = self.get_required_query_params(request)
except CodesAPIRequestError as invalid_request:
return Response({'error': str(invalid_request)}, status=HTTP_400_BAD_REQUEST)

subject_line = _('Code Management - Request for Codes by {token_enterprise_name}').format(
token_enterprise_name=enterprise_name
)
msg_with_codes = _('{token_email} from {token_enterprise_name} has requested {token_number_codes} additional '
'codes. Please reach out to them.').format(
token_email=email,
token_enterprise_name=enterprise_name,
token_number_codes=number_of_codes)
msg_without_codes = _('{token_email} from {token_enterprise_name} has requested additional codes.'
' Please reach out to them.').format(
token_email=email,
token_enterprise_name=enterprise_name)
body_msg = create_message_body(email, enterprise_name, number_of_codes, notes)
app_config = apps.get_app_config("enterprise")
from_email_address = app_config.enterprise_integrations_email
cs_email = app_config.customer_success_email
data = {
self.REQUIRED_PARAM_EMAIL: email,
self.REQUIRED_PARAM_ENTERPRISE_NAME: enterprise_name,
self.OPTIONAL_PARAM_NUMBER_OF_CODES: number_of_codes,
self.OPTIONAL_PARAM_NOTES: notes,
}
try:
messages_sent = mail.send_mail(
subject_line,
msg_with_codes if number_of_codes else msg_without_codes,
body_msg,
from_email_address,
[cs_email],
fail_silently=False
Expand Down
29 changes: 27 additions & 2 deletions tests/test_enterprise/api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2003,50 +2003,75 @@ def test_get_catalog_query_not_logged_in(self):
'email': 'johndoe@unknown.com',
'enterprise_name': 'Oracle',
'number_of_codes': '50',
'notes': 'Here are helping notes',
},
{u'email': u'johndoe@unknown.com', u'enterprise_name': u'Oracle', u'number_of_codes': u'50'},
{u'email': u'johndoe@unknown.com', u'enterprise_name': u'Oracle', u'number_of_codes': u'50',
u'notes': u'Here are helping notes'},
200,
None,
True,
u'johndoe@unknown.com from Oracle has requested 50 additional codes. Please reach out to them.'
u'\nAdditional Notes:\nHere are helping notes.'.encode("unicode_escape").decode("utf-8")
),
(
# A valid request without codes
{
'email': 'johndoe@unknown.com',
'enterprise_name': 'Oracle',
'number_of_codes': None,
'notes': 'Here are helping notes',
},
{u'email': u'johndoe@unknown.com', u'enterprise_name': u'Oracle', u'number_of_codes': None},
{u'email': u'johndoe@unknown.com', u'enterprise_name': u'Oracle', u'number_of_codes': None,
u'notes': u'Here are helping notes'},
200,
None,
True,
u'johndoe@unknown.com from Oracle has requested additional codes. Please reach out to them.'
u'\nAdditional Notes:\nHere are helping notes.'.encode("unicode_escape").decode("utf-8")
),
(
# A valid request without notes
{
'email': 'johndoe@unknown.com',
'enterprise_name': 'Oracle',
'number_of_codes': '50',
'notes': None,
},
{u'email': u'johndoe@unknown.com', u'enterprise_name': u'Oracle', u'number_of_codes': u'50',
u'notes': None},
200,
None,
True,
u'johndoe@unknown.com from Oracle has requested 50 additional codes. Please reach out to them.'
),
(
# A bad request due to a missing field
{
'email': 'johndoe@unknown.com',
'number_of_codes': '50',
'notes': 'Here are helping notes',
},
{u'error': u'Some required parameter(s) missing: enterprise_name'},
400,
None,
False,
u'johndoe@unknown.com from Oracle has requested 50 additional codes. Please reach out to them.'
u'\nAdditional Notes:\nHere are helping notes.'.encode("unicode_escape").decode("utf-8")
),
(
# Email send issue
{
'email': 'johndoe@unknown.com',
'enterprise_name': 'Oracle',
'number_of_codes': '50',
'notes': 'Here are helping notes',
},
{u'error': u'Request codes email could not be sent'},
500,
SMTPException(),
True,
u'johndoe@unknown.com from Oracle has requested 50 additional codes. Please reach out to them.'
u'\nAdditional Notes:\nHere are helping notes.'.encode("unicode_escape").decode("utf-8")
)
)
@ddt.unpack
Expand Down

0 comments on commit bb5a5c7

Please sign in to comment.