def notification(self, request, *args, **kwargs): """ a)Request notification and uuid of organization b)Access user uuids for that respective organization c)Send Email to the user's email with notification """ serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) org_uuid = request.data['organization_uuid'] message = request.data['notification_messages'] try: subject = 'Notification Message' context = { 'message': message, } template_name = 'email/coreuser/user_notification.txt' html_template_name = 'email/coreuser/user_notification.html' core_users = CoreUser.objects.filter( organization__organization_uuid=org_uuid) for user in core_users: email_address = user.email send_email(email_address, subject, context, template_name, html_template_name) except Exception as ex: print('Exception: ', ex) return Response( { 'detail': 'The notification were sent successfully on email.', }, status=status.HTTP_200_OK)
def save(self, **kwargs): resetpass_url = urljoin(settings.FRONTEND_URL, settings.RESETPASS_CONFIRM_URL_PATH) resetpass_url = resetpass_url + '{uid}/{token}/' email = self.validated_data["email"] count = 0 for user in CoreUser.objects.filter(email=email, is_active=True): uid = urlsafe_base64_encode(force_bytes(user.pk)) token = default_token_generator.make_token(user) context = { 'password_reset_link': resetpass_url.format(uid=uid, token=token), 'user': user, } # get specific subj and templates for user's organization tpl = EmailTemplate.objects.filter(organization=user.organization, type=TEMPLATE_RESET_PASSWORD).first() if not tpl: tpl = EmailTemplate.objects.filter(organization__name=settings.DEFAULT_ORG, type=TEMPLATE_RESET_PASSWORD).first() if tpl and tpl.template: context = Context(context) text_content = Template(tpl.template).render(context) html_content = Template(tpl.template_html).render(context) if tpl.template_html else None count += send_email_body(email, tpl.subject, text_content, html_content) continue # default subject and templates subject = 'Reset your password' template_name = 'email/coreuser/password_reset.txt' html_template_name = 'email/coreuser/password_reset.html' count += send_email(email, subject, context, template_name, html_template_name) return count
def perform_invite(self, serializer): reg_location = urljoin(settings.FRONTEND_URL, settings.REGISTRATION_URL_PATH) reg_location = reg_location + '?token={}' email_addresses = serializer.validated_data.get('emails') user = self.request.user organization = user.organization registered_emails = CoreUser.objects.filter( email__in=email_addresses).values_list('email', flat=True) links = [] for email_address in email_addresses: if email_address not in registered_emails: # create or update an invitation token = create_invitation_token(email_address, organization) # build the invitation link invitation_link = self.request.build_absolute_uri( reg_location.format(token)) links.append(invitation_link) # create the used context for the E-mail templates context = { 'invitation_link': invitation_link, 'org_admin_name': user.name if hasattr(user, 'coreuser') else '', 'organization_name': organization.name if organization else '' } subject = 'Application Access' # TODO we need to make this dynamic template_name = 'email/coreuser/invitation.txt' html_template_name = 'email/coreuser/invitation.html' send_email(email_address, subject, context, template_name, html_template_name) return links
def invite_event(self, request, *args, **kwargs): """ a)Request Event uuid, Room uuid, Email - Array b)Identify user type and send relevant emails with link to Registered and Unregistered user """ serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) event_uuid = request.data['event_uuid'] room_uuid = request.data['room_uuid'] emails = request.data['emails'] event_name = request.data['event_name'] organization_name = request.data['organization_name'] start_date_time = request.data['start_date_time'] end_date_time = request.data['end_date_time'] invitation_link_list = [] c = Calendar() e = Event() e.name = str(event_name) e.begin = str(start_date_time) e.end = str(end_date_time) e.uid = event_uuid e.organizer = Organizer(common_name=str(organization_name), email=DEFAULT_FROM_EMAIL) c.events.add(e) for email in emails: user = CoreUser.objects.filter(email=email).first() # if the user is registered if user: """ for registered users send them a test email "Registered Users! Welcome to a new event at organization". """ email_address = email organization = str(user.organization) reg_location = urljoin(settings.FRONTEND_URL, settings.EVENT_LOGIN_URL_PATH) reg_location = reg_location + '?token={}' token = create_invitation_token_event(email_address, organization, room_uuid, event_uuid) invitation_link = self.request.build_absolute_uri( reg_location.format(token)) invitation_link_list.append(invitation_link) subject = 'Welcome to event {} at {}'.format( event_name, organization) context = { 'organization_name': organization, 'event_link': invitation_link, 'event_uuid': event_uuid, 'room_uuid': room_uuid, 'event_name': event_name } template_name = 'email/coreuser/invite_event.txt' html_template_name = 'email/coreuser/invite_event.html' send_email(email_address, subject, context, template_name, html_template_name, str(c)) # if the user is not registered else: """ for unregistered users send them a test email "Unregistered Users! Welcome to a new event at organization'". """ email_address = email organization = organization_name reg_location = urljoin(settings.FRONTEND_URL, settings.EVENT_REGISTRATION_URL_PATH) reg_location = reg_location + '?token={}' token = create_invitation_token_event(email_address, organization, room_uuid, event_uuid) # build the invitation link invitation_link = self.request.build_absolute_uri( reg_location.format(token)) invitation_link_list.append(invitation_link) subject = 'Welcome to event {} at {}'.format( event_name, organization) context = { 'organization_name': organization, 'event_link': invitation_link, 'event_uuid': event_uuid, 'room_uuid': room_uuid, 'event_name': event_name } template_name = 'email/coreuser/invite_event.txt' html_template_name = 'email/coreuser/invite_event.html' send_email(email_address, subject, context, template_name, html_template_name, str(c)) return Response( { 'detail': 'The invitations were sent successfully.', 'event_link': [invitation_link_list], }, status=status.HTTP_200_OK)