diff --git a/server/api/fixtures/initial.json b/server/api/fixtures/initial.json index 27af973c..86a41d9f 100644 --- a/server/api/fixtures/initial.json +++ b/server/api/fixtures/initial.json @@ -22,10 +22,10 @@ "password": "pbkdf2_sha256$36000$N92TfR4PrqJ7$DZSbvw4wIWbDjrZ+xfDisnqCNwVoyoAdx/meV7pqkHY=", "last_login": "2018-09-09T00:35:53.883Z", "is_superuser": false, - "username": "mayowa.makinde", - "first_name": "Mayowa", - "last_name": "Makinde", - "email": "mayowa.makinde@andela.com", + "username": "olusola.oguntimehin", + "first_name": "Olusola", + "last_name": "Oguntimehin", + "email": "olusola.oguntimehin@andela.com", "is_staff": false, "is_active": true, "date_joined": "2018-09-09T00:35:28.114Z" @@ -232,4 +232,4 @@ "follower_category": 1 } } -] \ No newline at end of file +] diff --git a/server/graphql_schemas/event/schema.py b/server/graphql_schemas/event/schema.py index f38084d9..03da6dca 100644 --- a/server/graphql_schemas/event/schema.py +++ b/server/graphql_schemas/event/schema.py @@ -4,7 +4,6 @@ from dateutil.parser import parse from django.forms.models import model_to_dict -from django.core.mail import EmailMessage from django.core.exceptions import ObjectDoesNotExist from django.template.loader import get_template from django.utils import timezone @@ -17,6 +16,8 @@ from graphql_schemas.utils.helpers import (is_not_admin, update_instance, send_calendar_invites, + send_event_invite_email, + send_event_invite_emails, raise_calendar_error, not_valid_timezone) from graphql_schemas.utils.hasher import Hasher @@ -86,15 +87,16 @@ def mutate_and_get_payload(cls, root, info, **input): user_profile = AndelaUserProfile.objects.get( user=info.context.user ) + new_event = CreateEvent.create_event( + category, user_profile, **input) + # Send email invites in background + BackgroundTaskWorker.start_work(send_event_invite_emails, + (user_profile, new_event, info.context)) if user_profile.credential and user_profile.credential.valid: - new_event = CreateEvent.create_event( - category, user_profile, **input) # Send calender invite in background BackgroundTaskWorker.start_work(send_calendar_invites, - (user_profile, new_event)) + (user_profile, new_event)) else: - new_event = CreateEvent.create_event( - category, user_profile, **input) CreateEvent.notify_event_in_slack(category, input, new_event) raise_calendar_error(user_profile) @@ -244,25 +246,7 @@ def mutate_and_get_payload(cls, root, info, **input): raise GraphQLError( "User cannot invite self") - invite_hash = Hasher.gen_hash([ - event.id, receiver.user.id, sender.user.id]) - invite_url = info.context.build_absolute_uri( - f"/invite/{invite_hash}") - data_values = { - 'title': event.title, - 'imgUrl': event.featured_image, - 'venue': event.venue, - 'startDate': event.start_date, - 'url': invite_url - } - message = get_template('event_invite.html').render(data_values) - msg = EmailMessage( - f"You have been invited to an event by {sender.user.username}", - message, - to=[receiver_email] - ) - msg.content_subtype = 'html' - sent = msg.send() + sent = send_event_invite_email(sender, event, receiver, info.context) if sent: return cls(message="Event invite delivered") diff --git a/server/graphql_schemas/templates/event_email_invite.html b/server/graphql_schemas/templates/event_email_invite.html new file mode 100644 index 00000000..d4dda6e1 --- /dev/null +++ b/server/graphql_schemas/templates/event_email_invite.html @@ -0,0 +1,175 @@ + + + + + Andela Socials + + + +
+
+ andela-logo +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+ +
+ +
+
+ +
+ +
+ + + + + + + + + + + +
+ + diff --git a/server/graphql_schemas/utils/helpers.py b/server/graphql_schemas/utils/helpers.py index ace09b20..6503dd31 100644 --- a/server/graphql_schemas/utils/helpers.py +++ b/server/graphql_schemas/utils/helpers.py @@ -4,11 +4,15 @@ import dateutil.parser as parser from django.core.exceptions import ValidationError from django.core.files.storage import FileSystemStorage +from django.core import mail +from django.core.exceptions import ObjectDoesNotExist +from django.template.loader import get_template from api.utils.oauth_helper import get_auth_url -from api.models import Interest +from api.models import Interest, AndelaUserProfile from googleapiclient.discovery import build from google.cloud import storage +from graphql_schemas.utils.hasher import Hasher from django.conf import settings @@ -55,8 +59,8 @@ def raise_calendar_error(user_profile): :param user_profile: """ auth_url = get_auth_url(user_profile) - raise UnauthorizedCalendarError(message="Calendar API not authorized", - auth_url=auth_url) + raise UnauthorizedCalendarError( + message="Calendar API not authorized", auth_url=auth_url) async def send_calendar_invites(andela_user, event): @@ -71,9 +75,92 @@ async def send_calendar_invites(andela_user, event): payload = build_event(event, invitee_list) calendar = build('calendar', 'v3', credentials=andela_user.credential) + if settings.ENVIRONMENT == "production": - calendar.events().insert(calendarId='primary', sendNotifications=True, - body=payload).execute() + calendar.events().insert(calendarId='primary', + sendNotifications=True, body=payload).execute() + + +def send_event_invite_email(andela_user, event, receiver, info): + """ + Send single email + :param andela_user: + :param event: + :param receiver + :param info: + """ + sender = andela_user + invite_hash = Hasher.gen_hash([ + event.id, receiver.user.id, sender.user.id]) + invite_url = info.build_absolute_uri( + f"/invite/{invite_hash}") + + data_values = { + 'title': event.title, + 'imgUrl': event.featured_image, + 'venue': event.venue, + 'startDate': parser.parse(str( + event.start_date)).strftime("%a, %b %d %Y %H:%M"), + 'url': invite_url, + 'senderName': sender.user.first_name, + 'receiverName': receiver.user.first_name, + } + message = get_template('event_email_invite.html').render(data_values) + msg = mail.EmailMessage( + f"You have been invited to an event by {sender.user.username}", + message, + to=[receiver.user.email] + ) + msg.content_subtype = 'html' + sent = msg.send() + + return sent + + +async def send_event_invite_emails(andela_user, event, info): + """ + Send mass emails asynchronously + :param andela_user: + :param event: + :param info: + """ + message_list = [] + sender = andela_user + invitees = Interest.objects.filter(follower_category=event.social_event) + invitee_list = [{'email': invitee.follower.user.email} + for invitee in invitees] + + for invitee in invitee_list: + receiver = AndelaUserProfile.objects.get( + user__email=invitee['email']) + invite_hash = Hasher.gen_hash([ + event.id, receiver.user.id, sender.user.id]) + invite_url = info.build_absolute_uri( + f"/invite/{invite_hash}") + data_values = { + 'title': event.title, + 'imgUrl': event.featured_image, + 'venue': event.venue, + 'startDate': parser.parse(str( + event.start_date)).strftime("%a, %b %d %Y %H:%M"), + 'url': invite_url, + 'senderName': sender.user.first_name, + 'receiverName': receiver.user.first_name, + } + message = get_template('event_email_invite.html').render(data_values) + msg = mail.EmailMessage( + f"You have been invited to an event by {sender.user.username}", + message, + sender.user.email, + to=[receiver.user.email], + ) + msg.content_subtype = 'html' + message_list.append(msg) + + connection = mail.get_connection() + connection.open() + connection.send_messages(message_list) + connection.close() def build_event(event, invitees):