def send_message(title, message, user, anonymous): """ Send a message to HS when users posts to the contact form. Don't catch AbakusGroup.DoesNotExist, this notifies us when the group doesn't exist. """ anonymous = anonymous if user.is_authenticated else True abakus_group = AbakusGroup.objects.get(name='Hovedstyret') users = [ membership.user for membership in abakus_group.memberships.select_related('user') ] emails = [user.email_address for user in users] from_name = 'Anonymous' if anonymous else user.full_name from_email = 'Unknown' if anonymous else user.email_address send_email.delay(to_email=emails, context={ 'title': title, 'message': message, 'from_name': from_name, 'from_email': from_email }, subject='Ny henvendelse fra kontaktskjemaet', plain_template='contact/email/contact_form.txt', html_template='contact/email/contact_form.html', from_email=None)
def send_message(title, message, user, anonymous, recipient_group): """ Send a message to HS when users posts to the contact form. Don't catch AbakusGroup.DoesNotExist, this notifies us when the group doesn't exist. """ anonymous = anonymous if user.is_authenticated else True # Handle no recipient group as HS if not recipient_group: recipient_group = AbakusGroup.objects.get(name="Hovedstyret") recipient_emails = get_recipients(recipient_group) from_name = "Anonymous" if anonymous else user.full_name from_email = "Unknown" if anonymous else user.email_address send_email.delay( to_email=recipient_emails, context={ "title": title, "message": message, "from_name": from_name, "from_email": from_email, "recipient_group": recipient_group.__str__(), }, subject= f"Ny henvendelse fra kontaktskjemaet til {recipient_group.__str__()}", plain_template="contact/email/contact_form.txt", html_template="contact/email/contact_form.html", from_email=None, )
def handle_create(self, instance, **kwargs): activity = Activity( actor=instance, verb=CompanyInterestVerb, object=instance, time=instance.created_at, extra_context={}, ) recipients = [ member.user for member in AbakusGroup.objects.get( name="Bedkom").memberships.all() ] self.manager.add_activity(activity, [recipient.pk for recipient in recipients], [NotificationFeed]) for recipient in recipients: notification = CompanyInterestNotification( recipient, company_interest=instance) notification.notify() send_email.delay( to_email=f"bedriftskontakt@{settings.GSUITE_DOMAIN}", context=instance.generate_mail_context(), subject="En ny bedrift har meldt sin interesse", plain_template="companies/email/company_interest.txt", html_template="companies/email/company_interest.html", )
def handle_interest(self, company_interest): activity = Activity(actor=company_interest, verb=CompanyInterestVerb, object=company_interest, time=company_interest.created_at, extra_context={}) recipients = [ member.user for member in AbakusGroup.objects.get( name="Bedkom").memberships.all() ] self.manager.add_activity(activity, [recipient.pk for recipient in recipients], [NotificationFeed]) for recipient in recipients: notification = CompanyInterestNotification( recipient, company_interest=company_interest) notification.notify() send_email.delay( to_email=f'bedriftskontakt@{settings.GSUITE_DOMAIN}', context=company_interest.generate_mail_context(), subject='En ny bedrift har meldt sin interesse', plain_template='companies/email/company_interest.txt', html_template='companies/email/company_interest.html', )
def create(self, request, *args, **kwargs): """ Attempts to create a student confirmation token and email it to the user. """ serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user = request.user if user.is_verified_student(): raise ValidationError(detail="Already confirmed a student username") student_username = serializer.validated_data.get("student_username") token = Registrations.generate_student_confirmation_token( student_username, serializer.validated_data.get("course"), serializer.validated_data.get("member"), ) send_email.delay( to_email=f"{student_username}@{constants.STUDENT_EMAIL_DOMAIN}", context={"token": token, "full_name": user.get_full_name()}, subject="Bekreft student kontoen din på Abakus.no", plain_template="users/email/student_confirmation.txt", html_template="users/email/student_confirmation.html", from_email=None, ) return Response(status=status.HTTP_204_NO_CONTENT)
def handle_failure(self, sender, reason): """ Notify about restricted mail failure. This action is not connected to a specific user. We sends a message to the sender instead of a user. We use the send_mail task directly because of this. """ send_email.delay( to_email=sender, context={"reason": reason}, subject=f"Kunne ikke sende ut begrenset epost", plain_template="restricted/email/process_failure.txt", html_template="restricted/email/process_failure.html", )
def create(self, request, *args, **kwargs): """ Attempts to create a registration token and email it to the user. """ serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) email = serializer.validated_data.get('email') token = Registrations.generate_registration_token(email) send_email.delay( to_email=email, context={ "token": token, }, subject='Velkommen til Abakus.no', plain_template='users/email/registration.txt', html_template='users/email/registration.html', from_email=None ) return Response(status=status.HTTP_202_ACCEPTED)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) email = serializer.validated_data["email"] try: User.objects.get(email__iexact=email) except User.DoesNotExist: raise ValidationError( {"email": "User with that email does not exist"}) token = PasswordReset.generate_reset_token(email) send_email.delay( to_email=email, context={"token": token}, subject="Nullstill ditt passord på abakus.no", plain_template="users/email/reset_password.txt", html_template="users/email/reset_password.html", from_email=None, ) return Response(status=status.HTTP_202_ACCEPTED)
def _delay_mail(self, *args, **kwargs): """ Helper for the send_mail celery task. """ return send_email.delay(*args, **kwargs)