-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbriefs.py
91 lines (74 loc) · 3.5 KB
/
briefs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
import six
from flask import abort, current_app, render_template
from flask_login import current_user
from dmapiclient.audit import AuditTypes
from dmutils.email import send_email
from dmutils.email.exceptions import EmailError
def get_brief(data_api_client, brief_id, allowed_statuses=None):
if allowed_statuses is None:
allowed_statuses = []
brief = data_api_client.get_brief(brief_id)['briefs']
if allowed_statuses and brief['status'] not in allowed_statuses:
abort(404)
return brief
def is_supplier_eligible_for_brief(data_api_client, supplier_id, brief):
return data_api_client.is_supplier_eligible_for_brief(supplier_id, brief['id'])
def send_brief_clarification_question(data_api_client, brief, clarification_question):
# Email the question to brief owners
email_body = render_template(
"emails/brief_clarification_question.html",
brief_id=brief['id'],
brief_name=brief['title'],
publish_by_date=brief['clarificationQuestionsPublishedBy'],
framework_slug=brief['frameworkSlug'],
lot_slug=brief['lotSlug'],
message=clarification_question,
)
try:
send_email(
to_email_addresses=get_brief_user_emails(brief),
email_body=email_body,
api_key=current_app.config['DM_MANDRILL_API_KEY'],
subject=u"You’ve received a new supplier question about ‘{}’".format(brief['title']),
from_email=current_app.config['CLARIFICATION_EMAIL_FROM'],
from_name="{} Supplier".format(brief['frameworkName']),
tags=["brief-clarification-question"]
)
except EmailError as e:
current_app.logger.error(
"Brief question email failed to send. error={error} supplier_id={supplier_id} brief_id={brief_id}",
extra={'error': six.text_type(e), 'supplier_id': current_user.supplier_id, 'brief_id': brief['id']}
)
abort(503, "Clarification question email failed to send")
data_api_client.create_audit_event(
audit_type=AuditTypes.send_clarification_question,
user=current_user.email_address,
object_type="briefs",
object_id=brief['id'],
data={"question": clarification_question, "briefId": brief['id'], "supplierId": current_user.supplier_id})
# Send the supplier a copy of the question
supplier_email_body = render_template(
"emails/brief_clarification_question_confirmation.html",
brief_id=brief['id'],
brief_name=brief['title'],
framework_framework=brief['frameworkFramework'],
message=clarification_question
)
try:
send_email(
to_email_addresses=[current_user.email_address],
email_body=supplier_email_body,
api_key=current_app.config['DM_MANDRILL_API_KEY'],
subject=u"Your question about ‘{}’".format(brief['title']),
from_email=current_app.config['CLARIFICATION_EMAIL_FROM'],
from_name=current_app.config['CLARIFICATION_EMAIL_NAME'],
tags=["brief-clarification-question-confirmation"]
)
except EmailError as e:
current_app.logger.error(
"Brief question supplier email failed to send. error={error} supplier_id={supplier_id} brief_id={brief_id}",
extra={'error': six.text_type(e), 'supplier_id': current_user.supplier_id, 'brief_id': brief['id']}
)
def get_brief_user_emails(brief):
return [user['emailAddress'] for user in brief['users'] if user['active']]