From e6783ede77d22dabb6abb976ac729c96d793c682 Mon Sep 17 00:00:00 2001 From: Shreya Pandit Date: Fri, 3 Mar 2023 14:39:35 +0200 Subject: [PATCH 1/5] email admin about journal response --- api/app/responses/api.py | 29 +++++++- api/app/users/repository.py | 8 ++ ...1cd_assign_action_editor_email_template.py | 73 +++++++++++++++++++ 3 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 api/migrations/versions/cebfdfef31cd_assign_action_editor_email_template.py diff --git a/api/app/responses/api.py b/api/app/responses/api.py index 221c005f9..b333d22c5 100644 --- a/api/app/responses/api.py +++ b/api/app/responses/api.py @@ -106,6 +106,12 @@ def post(self): user = user_repository.get_by_id(user_id) response = response_repository.get_by_id_and_user_id(response.id, user_id) self.send_confirmation(user, response) + + event = event_repository.get_event_by_response_id(response.id) + if event.event_type == EventType.CONTINUOUS_JOURNAL: + all_admin_users = user_repository.get_all_admin() + for admin_user in all_admin_users: + self.send_confirmation(admin_user, response, admin_user=True) except: LOGGER.warn('Failed to send confirmation email for response with ID : {id}, but the response was submitted succesfully'.format(id=response.id)) finally: @@ -152,6 +158,12 @@ def put(self): user = user_repository.get_by_id(user_id) response = response_repository.get_by_id_and_user_id(response.id, user_id) self.send_confirmation(user, response) + + event = event_repository.get_event_by_response_id(response.id) + if event.event_type == EventType.CONTINUOUS_JOURNAL: + all_admin_users = user_repository.get_all_admin() + for admin_user in all_admin_users: + self.send_confirmation(admin_user, response, admin_user=True) except: LOGGER.warn('Failed to send confirmation email for response with ID : {id}, but the response was submitted succesfully'.format(id=response.id)) finally: @@ -191,7 +203,7 @@ def delete(self): return {}, 204 - def send_confirmation(self, user, response): + def send_confirmation(self, user, response, admin_user=None): try: answers = response.answers if not answers: @@ -215,8 +227,9 @@ def send_confirmation(self, user, response): else: event_description = event.get_description('en') - emailer.email_user( - 'confirmation-response-call' if event.event_type == EventType.CALL else 'confirmation-response', + if admin_user: + emailer.email_user( + 'assign-action-editor', template_parameters=dict( event_description=event_description, question_answer_summary=question_answer_summary, @@ -224,6 +237,16 @@ def send_confirmation(self, user, response): event=event, user=user ) + else: + emailer.email_user( + 'confirmation-response-call' if event.event_type == EventType.CALL else 'confirmation-response', + template_parameters=dict( + event_description=event_description, + question_answer_summary=question_answer_summary, + ), + event=event, + user=user + ) except Exception as e: LOGGER.error('Could not send confirmation email for response with id : {response_id} due to: {e}'.format(response_id=response.id, e=e)) diff --git a/api/app/users/repository.py b/api/app/users/repository.py index f2a498a7c..74e620ad9 100644 --- a/api/app/users/repository.py +++ b/api/app/users/repository.py @@ -78,3 +78,11 @@ def get_all_with_responses_or_invited_guests_for(event_id): # both in the filter. .filter(or_(InvitedGuest.event_id==event_id, ApplicationForm.event_id==event_id)) ).all() + + @staticmethod + def get_all_admin(): + return ( + db.session.query(AppUser) + .filter_by(active=True, is_deleted=False, is_admin=True)\ + .all() + ) diff --git a/api/migrations/versions/cebfdfef31cd_assign_action_editor_email_template.py b/api/migrations/versions/cebfdfef31cd_assign_action_editor_email_template.py new file mode 100644 index 000000000..7bfa64b1e --- /dev/null +++ b/api/migrations/versions/cebfdfef31cd_assign_action_editor_email_template.py @@ -0,0 +1,73 @@ +"""Adding email templates for assigning an action editor to a journal application + +Revision ID: cebfdfef31cd +Revises: 6a073dd1e30d +Create Date: 2023-03-02 14:03:29.661901 + +""" + +# revision identifiers, used by Alembic. +revision = 'cebfdfef31cd' +down_revision = '6a073dd1e30d' + +from alembic import op +import sqlalchemy as sa +from sqlalchemy import orm +from sqlalchemy.ext.declarative import declarative_base +from app import db + +Base = declarative_base() + + + +class EmailTemplate(Base): + + __tablename__ = 'email_template' + __table_args__ = {'extend_existing': True} + + id = db.Column(db.Integer(), primary_key=True) + key = db.Column(db.String(50), nullable=False) + event_id = db.Column(db.Integer(), nullable=True) + language = db.Column(db.String(2), nullable=False) + template = db.Column(db.String(), nullable=False) + subject = db.Column(db.String(), nullable=False) + + def __init__(self, key, event_id, subject, template, language): + self.key = key + self.event_id = event_id + self.subject = subject + self.template = template + self.language = language + + +def upgrade(): + Base.metadata.bind = op.get_bind() + session = orm.Session(bind=Base.metadata.bind) + + op.execute("""SELECT setval('email_template_id_seq', (SELECT max(id) FROM email_template));""") + + template = """Dear {title} {firstname} {lastname}, + +A new application for {event_name} was received. Please assign an action editor to this application as soon as possible. + +""" + + session.add(EmailTemplate('assign-action-editor', None, '{event_name} Response Receieved', template, 'en')) + + template = """Dear {title} {firstname} {lastname}, + +A new application {event_name} was received but has not been assigned to an action editor yet. Please assign an action editor to this application as soon as possible. + +""" + + session.add(EmailTemplate('action-editor-not-assigned', None, '{event_name} Action Editor ', template, 'en')) + + session.commit() + + + +def downgrade(): + added_keys = ['assign-action-editor', 'action-editor-not-assigned'] + op.execute("""DELETE FROM email_template WHERE key in ({})""".format(', '.join(["'" + k + "'" for k in added_keys]))) + op.execute("""SELECT setval('email_template_id_seq', (SELECT max(id) FROM email_template));""") + From 951eb46d6410b27069ce8f5fc4f7f24d35557102 Mon Sep 17 00:00:00 2001 From: Shreya Pandit Date: Tue, 14 Mar 2023 14:32:42 +0200 Subject: [PATCH 2/5] changed system admin to event admin --- api/app/events/repository.py | 10 +++++++++- api/app/responses/api.py | 4 ++-- api/app/users/repository.py | 8 -------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/api/app/events/repository.py b/api/app/events/repository.py index 07d233b83..434d61d7b 100644 --- a/api/app/events/repository.py +++ b/api/app/events/repository.py @@ -1,6 +1,6 @@ from datetime import datetime from app import db -from app.events.models import Event, EventFee, EventType +from app.events.models import Event, EventFee, EventType, EventRole from app.organisation.models import Organisation from app.responses.models import Response from app.applicationModel.models import ApplicationForm @@ -72,3 +72,11 @@ def get_event_fees(event_id, event_fee_ids): EventFee.is_active==True) .all() ) + + @staticmethod + def get_all_admin(event_id): + return ( + db.session.query(EventRole).get(event_id) + .filter_by(role='admin')\ + .all() + ) diff --git a/api/app/responses/api.py b/api/app/responses/api.py index b333d22c5..c0b4cb300 100644 --- a/api/app/responses/api.py +++ b/api/app/responses/api.py @@ -109,7 +109,7 @@ def post(self): event = event_repository.get_event_by_response_id(response.id) if event.event_type == EventType.CONTINUOUS_JOURNAL: - all_admin_users = user_repository.get_all_admin() + all_admin_users = event_repository.get_all_admin(event_id=response.id) for admin_user in all_admin_users: self.send_confirmation(admin_user, response, admin_user=True) except: @@ -161,7 +161,7 @@ def put(self): event = event_repository.get_event_by_response_id(response.id) if event.event_type == EventType.CONTINUOUS_JOURNAL: - all_admin_users = user_repository.get_all_admin() + all_admin_users = event_repository.get_all_admin(event_id=response.application_form.event_id) for admin_user in all_admin_users: self.send_confirmation(admin_user, response, admin_user=True) except: diff --git a/api/app/users/repository.py b/api/app/users/repository.py index 74e620ad9..f2a498a7c 100644 --- a/api/app/users/repository.py +++ b/api/app/users/repository.py @@ -78,11 +78,3 @@ def get_all_with_responses_or_invited_guests_for(event_id): # both in the filter. .filter(or_(InvitedGuest.event_id==event_id, ApplicationForm.event_id==event_id)) ).all() - - @staticmethod - def get_all_admin(): - return ( - db.session.query(AppUser) - .filter_by(active=True, is_deleted=False, is_admin=True)\ - .all() - ) From 7f247be3f0547996086ef19fe97acfd18be0254e Mon Sep 17 00:00:00 2001 From: Shreya Pandit Date: Tue, 21 Mar 2023 23:03:37 +0200 Subject: [PATCH 3/5] fixed event admin --- api/app/events/repository.py | 7 ++++--- api/app/responses/api.py | 12 ++++++------ ...bfdfef31cd_assign_action_editor_email_template.py | 2 -- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/api/app/events/repository.py b/api/app/events/repository.py index 434d61d7b..d529b70bf 100644 --- a/api/app/events/repository.py +++ b/api/app/events/repository.py @@ -74,9 +74,10 @@ def get_event_fees(event_id, event_fee_ids): ) @staticmethod - def get_all_admin(event_id): + def get_event_admins(event_id): return ( - db.session.query(EventRole).get(event_id) - .filter_by(role='admin')\ + db.session.query(AppUser) + .join(EventRole, EventRole.user_id == AppUser.id) + .filter_by(event_id=event_id, role='admin') .all() ) diff --git a/api/app/responses/api.py b/api/app/responses/api.py index c0b4cb300..970d453e2 100644 --- a/api/app/responses/api.py +++ b/api/app/responses/api.py @@ -109,9 +109,9 @@ def post(self): event = event_repository.get_event_by_response_id(response.id) if event.event_type == EventType.CONTINUOUS_JOURNAL: - all_admin_users = event_repository.get_all_admin(event_id=response.id) - for admin_user in all_admin_users: - self.send_confirmation(admin_user, response, admin_user=True) + all_admin_event_roles = event_repository.get_event_admins(event_id=event.id) + for admin_event_role in all_admin_event_roles: + self.send_confirmation(admin_event_role.user, response, admin_user=True) except: LOGGER.warn('Failed to send confirmation email for response with ID : {id}, but the response was submitted succesfully'.format(id=response.id)) finally: @@ -161,9 +161,9 @@ def put(self): event = event_repository.get_event_by_response_id(response.id) if event.event_type == EventType.CONTINUOUS_JOURNAL: - all_admin_users = event_repository.get_all_admin(event_id=response.application_form.event_id) - for admin_user in all_admin_users: - self.send_confirmation(admin_user, response, admin_user=True) + all_admin_event_roles = event_repository.get_event_admins(event_id=event.id) + for admin_event_role in all_admin_event_roles: + self.send_confirmation(admin_event_role.user, response, admin_user=True) except: LOGGER.warn('Failed to send confirmation email for response with ID : {id}, but the response was submitted succesfully'.format(id=response.id)) finally: diff --git a/api/migrations/versions/cebfdfef31cd_assign_action_editor_email_template.py b/api/migrations/versions/cebfdfef31cd_assign_action_editor_email_template.py index 7bfa64b1e..68fc812d0 100644 --- a/api/migrations/versions/cebfdfef31cd_assign_action_editor_email_template.py +++ b/api/migrations/versions/cebfdfef31cd_assign_action_editor_email_template.py @@ -44,8 +44,6 @@ def upgrade(): Base.metadata.bind = op.get_bind() session = orm.Session(bind=Base.metadata.bind) - op.execute("""SELECT setval('email_template_id_seq', (SELECT max(id) FROM email_template));""") - template = """Dear {title} {firstname} {lastname}, A new application for {event_name} was received. Please assign an action editor to this application as soon as possible. From 1027e573e187ce14f075dec5154efeadd08f2134 Mon Sep 17 00:00:00 2001 From: Shreya Pandit Date: Wed, 22 Mar 2023 23:35:33 +0200 Subject: [PATCH 4/5] fixed event admin query and email --- api/app/responses/api.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/api/app/responses/api.py b/api/app/responses/api.py index 970d453e2..aff6ea2c8 100644 --- a/api/app/responses/api.py +++ b/api/app/responses/api.py @@ -109,9 +109,9 @@ def post(self): event = event_repository.get_event_by_response_id(response.id) if event.event_type == EventType.CONTINUOUS_JOURNAL: - all_admin_event_roles = event_repository.get_event_admins(event_id=event.id) - for admin_event_role in all_admin_event_roles: - self.send_confirmation(admin_event_role.user, response, admin_user=True) + event_admins = event_repository.get_event_admins(event_id=event.id) + for event_admin in event_admins: + self.send_confirmation(event_admin, response) except: LOGGER.warn('Failed to send confirmation email for response with ID : {id}, but the response was submitted succesfully'.format(id=response.id)) finally: @@ -161,9 +161,9 @@ def put(self): event = event_repository.get_event_by_response_id(response.id) if event.event_type == EventType.CONTINUOUS_JOURNAL: - all_admin_event_roles = event_repository.get_event_admins(event_id=event.id) - for admin_event_role in all_admin_event_roles: - self.send_confirmation(admin_event_role.user, response, admin_user=True) + event_admins = event_repository.get_event_admins(event_id=event.id) + for event_admin in event_admins: + self.send_confirmation(event_admin, response) except: LOGGER.warn('Failed to send confirmation email for response with ID : {id}, but the response was submitted succesfully'.format(id=response.id)) finally: @@ -203,7 +203,7 @@ def delete(self): return {}, 204 - def send_confirmation(self, user, response, admin_user=None): + def send_confirmation(self, user, response): try: answers = response.answers if not answers: @@ -227,7 +227,7 @@ def send_confirmation(self, user, response, admin_user=None): else: event_description = event.get_description('en') - if admin_user: + if user.is_event_admin(event.id): emailer.email_user( 'assign-action-editor', template_parameters=dict( From 8703f5210405134e2ef3e6fe2d0bae23d77602ce Mon Sep 17 00:00:00 2001 From: Shreya Pandit Date: Mon, 27 Mar 2023 21:59:47 +0200 Subject: [PATCH 5/5] fixed template typo --- .../cebfdfef31cd_assign_action_editor_email_template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/migrations/versions/cebfdfef31cd_assign_action_editor_email_template.py b/api/migrations/versions/cebfdfef31cd_assign_action_editor_email_template.py index 68fc812d0..8f06c8cd5 100644 --- a/api/migrations/versions/cebfdfef31cd_assign_action_editor_email_template.py +++ b/api/migrations/versions/cebfdfef31cd_assign_action_editor_email_template.py @@ -54,7 +54,7 @@ def upgrade(): template = """Dear {title} {firstname} {lastname}, -A new application {event_name} was received but has not been assigned to an action editor yet. Please assign an action editor to this application as soon as possible. +A new application for {event_name} was received but has not been assigned to an action editor yet. Please assign an action editor to this application as soon as possible. """