def post(self, request, pk): qp = request.query_params project_year = get_object_or_404(models.ProjectYear, pk=pk) if qp.get("submit"): project_year.submit(request) # create a new email object email = emails.ProjectYearSubmissionEmail(project_year, request) # send the email object custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) return Response( serializers.ProjectYearSerializer(project_year).data, status.HTTP_200_OK) elif qp.get("unsubmit"): project_year.unsubmit(request) return Response( serializers.ProjectYearSerializer(project_year).data, status.HTTP_200_OK) elif qp.get("add-all-costs"): project_year.add_all_om_costs() project_year.update_modified_by(request.user) serializer = serializers.OMCostSerializer( instance=project_year.omcost_set.all(), many=True) return Response(serializer.data, status.HTTP_200_OK) elif qp.get("remove-empty-costs"): project_year.clear_empty_om_costs() project_year.update_modified_by(request.user) serializer = serializers.OMCostSerializer( instance=project_year.omcost_set.all(), many=True) return Response(serializer.data, status.HTTP_200_OK) raise ValidationError( _("This endpoint cannot be used without a query param"))
def form_valid(self, form): self.object = form.save() # nobody is assigned, assign DJF if self.object.dm_assigned.count() == 0: for u in User.objects.filter(is_superuser=True): self.object.dm_assigned.add(u) # create a new email object email = emails.NewTicketEmail(self.object, self.request) # send the email object custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) messages.success( self.request, "The new ticket has been logged and a confirmation email has been sent!" ) # check to see if a generic file should be appended if form.cleaned_data["generic_file_to_load"]: return HttpResponseRedirect( reverse_lazy("tickets:add_generic_file", kwargs={ "ticket": self.object.id, "type": form.cleaned_data["generic_file_to_load"] })) # if nothing, just go to detail page else: return HttpResponseRedirect(self.get_success_url())
def form_valid(self, form): # retrieve data from form first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] email = form.cleaned_data['email1'] # create a new user my_user = User.objects.create( username=email, first_name=first_name, last_name=last_name, password="******", is_active=True, email=email, ) # only send an email if AAD is not on if not settings.AZURE_AD: email = emails.UserCreationEmail(my_user, self.request) # send the email object custom_send_mail( subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list ) messages.success(self.request, gettext("The user '{}' was created and an email was sent".format(my_user.get_full_name()))) return super().form_valid(form)
def signup(request): if request.method == 'POST': form = forms.SignupForm(request.POST) if form.is_valid(): print(123) user = form.save(commit=False) user.username = user.email user.is_active = False user.save() current_site = get_current_site(request) mail_subject = 'Activate your DM Apps account / Activez votre compte Applications GD' message = render_to_string( 'registration/acc_active_email.html', { 'user': user, 'domain': current_site.domain, 'uid': force_text( urlsafe_base64_encode(force_bytes(user.pk))), 'token': account_activation_token.make_token(user), }) to_email = form.cleaned_data.get('email') from_email = settings.SITE_FROM_EMAIL custom_send_mail( html_message=message, subject=mail_subject, recipient_list=[ to_email, ], from_email=from_email, ) return HttpResponse( 'Please confirm your email address to complete the registration' ) else: form = forms.SignupForm() return render(request, 'registration/signup.html', {'form': form})
def resend_verification_email(request, email): group = Group.objects.get(name='verified') user = User.objects.get(email__iexact=email) try: user.groups.remove(group) except: pass current_site = get_current_site(request) mail_subject = 'Activate your Gulf Region Data Management account.' message = render_to_string( 'registration/acc_active_email.html', { 'user': user, 'domain': current_site.domain, 'uid': force_text(urlsafe_base64_encode(force_bytes(user.pk))), 'token': account_activation_token.make_token(user), }) to_email = user.email from_email = settings.SITE_FROM_EMAIL custom_send_mail( subject=mail_subject, html_message=message, from_email=from_email, recipient_list=to_email, ) return HttpResponse( 'A verification email has been sent. Please check your inbox.')
def manage_trip_warning(trip, request): """ This function will decide if sending an email to NCR is necessary based on 1) the total costs accrued for a trip 2) whether or not a warning has already been sent :param trip: an instance of Trip :return: NoneObject """ # first make sure we are not receiving a NoneObject try: trip.non_res_total_cost except AttributeError: pass else: # If the trip cost is below 10k, make sure the warning field is null and an then do nothing more :) if trip.non_res_total_cost < 10000: if trip.cost_warning_sent: trip.cost_warning_sent = None trip.save() # if the trip is >= 10K, we simply need to send an email to NCR else: if not trip.cost_warning_sent: email = emails.TripCostWarningEmail(trip, request) # # send the email object custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) trip.cost_warning_sent = timezone.now() trip.save()
def send_activation_email(user, request): current_site = get_current_site(request) mail_subject = 'Activate your DM Apps account / Activez votre compte Applications GD' message = render_to_string( 'registration/acc_active_email.html', { 'user': user, 'domain': current_site.domain, 'uid': force_text(urlsafe_base64_encode(force_bytes(user.pk))), 'token': account_activation_token.make_token(user), }) to_email = user.email from_email = settings.SITE_FROM_EMAIL custom_send_mail( html_message=message, subject=mail_subject, recipient_list=[ to_email, ], from_email=from_email, ) messages.success( request, _('A verification email was just send to {email_address}. In order to complete your registration, please follow the link' ' in the message. <br><br>If the email does not appear within 1-2 minutes, please be sure to check your junk mail folder. ' '<br><br>The activation link will only remain valid for a limited period of time.' ).format(email_address=to_email))
def perform_create(self, serializer): staff = serializer.save() staff.project_year.update_modified_by(self.request.user) if staff.user and staff.user.email: email = emails.StaffEmail(staff, "add", self.request) custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list)
def send_approval_email(self, request): if self.approved is not None and not self.notification_email_sent: email = emails.ProjectApprovalEmail(self, request) # send the email object custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) self.notification_email_sent = timezone.now() self.save()
def form_valid(self, form): self.object = form.save() # create a new email object email = emails.NewFileAddedEmail(self.object, self.request) # send the email object custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) return HttpResponseRedirect(reverse('shared_models:close_me'))
def approval_seeker(trip_request, suppress_email=False, request=None): """ This method is meant to seek approvals via email + set reveiwer statuses. It will also set the trip_request status vis a vis __set_request_status__() """ # start by setting the trip_request status... if the trip_request is "denied" OR "draft" or "approved", do not continue if __set_request_status__(trip_request, request): next_reviewer = None email = None for reviewer in trip_request.reviewers.all(): # if the reviewer's status is set to 'queued', they will be our next selection # we should then exit the loop and set the next_reviewer var # if this is a resubmission, there might still be a reviewer whose status is 'pending'. This should be the reviewer if reviewer.status_id == 20 or reviewer.status_id == 1: next_reviewer = reviewer break # if there is a next reviewer, set their status to pending if next_reviewer: next_reviewer.status_id = 1 next_reviewer.status_date = timezone.now() next_reviewer.save() # now, depending on the role of this reviewer, perhaps we want to send an email. # if they are a recommender, rev... if next_reviewer.role_id in [ 1, 2, 3, 4, ] and request: # essentially, just not the RDG or ADM email = emails.ReviewAwaitingEmail(trip_request, next_reviewer, request) elif next_reviewer.role_id in [ 5, 6 ] and request: # if we are going for ADM or RDG signature... email = emails.AdminApprovalAwaitingEmail( trip_request, next_reviewer.role_id, request) if email and not suppress_email: # send the email object custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) # Then, lets set the trip_request status again to account for the fact that something happened __set_request_status__(trip_request, request)
def perform_create(self, serializer): resource = serializer.save(created_by=self.request.user) # decide on who should receive an update for invitee in resource.event.invitees.all(): # only send the email to those who already received an invitation (and where this happened in the past... redundant? ) if invitee.invitation_sent_date and invitee.invitation_sent_date < resource.created_at: email = emails.NewResourceEmail(invitee, resource, self.request) custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list)
def perform_update(self, serializer): staff = get_object_or_404(models.Staff, pk=self.kwargs.get("pk")) old_lead_status = staff.is_lead staff = serializer.save() staff.project_year.update_modified_by(self.request.user) if (old_lead_status is False and staff.is_lead) and (staff.user and staff.user.email): email = emails.StaffEmail(staff, "add", self.request) custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) super().perform_update(serializer)
def send_instructions(request, pk): # create a new email object my_user = models.User.objects.get(pk=pk) email = emails.SendInstructionsEmail(my_user, request) # send the email object custom_send_mail( subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list ) messages.success(request, "An email has been sent to the user with setup instructions!") return HttpResponseRedirect(reverse("shares:user_detail", kwargs={"pk": pk}))
def post(self, request, project_year): project_year = get_object_or_404(models.ProjectYear, pk=project_year) project_year.submit(request) # create a new email object email = emails.ProjectYearSubmissionEmail(project_year, request) # send the email object custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) return Response( serializers.ProjectYearSerializer(project_year).data, status.HTTP_200_OK)
def trip_approval_seeker(trip, request): """ This method is meant to seek approvals via email + set reviewer statuses. """ # start by setting the trip status... if the trip_request is "denied" OR "draft" or "approved", do not continue # Next: if the trip_request is un submitted, it is in 'draft' status # only look for a next reviewer if we are still in a review [id=31] if trip.status_id == 31: next_reviewer = None email = None # look through all the reviewers... see if we can decide on who the next reviewer should be... for reviewer in trip.reviewers.all(): # if the reviewer's status is set to 'queued', they will be our next selection # we should then exit the loop and set the next_reviewer var # CAVEAT: if this is a resubmission, there might still be a reviewer whose status is 'pending'. This should be the reviewer if reviewer.status_id == 24 or reviewer.status_id == 25: next_reviewer = reviewer break # if there is a next reviewer, set their status to pending and send them an email if next_reviewer: next_reviewer.status_id = 25 next_reviewer.status_date = timezone.now() next_reviewer.save() email = emails.TripReviewAwaitingEmail(trip, next_reviewer, request) # send the email object custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) # if no next reviewer was found, the trip's review is complete... else: # THIS IS PROBABLY NOT NECESSARY. # The trip review is complete if all the reviewers' status are set to complete [id=26] # HOWEVER, some reviewers might have been skipped # The total number of reviewers should equal the number of reviewer who completed [id=26] and / or were skipped [id=42]. if trip.reviewers.all().count() == trip.reviewers.filter( status_id__in=[26, 42]).count(): trip.status_id = 32 trip.save()
def post(self, request, pk): """ send the email""" invitee = get_object_or_404(models.Invitee, pk=pk) if not invitee.invitation_sent_date: # send email email = emails.InvitationEmail(invitee, request) custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) invitee.invitation_sent_date = timezone.now() invitee.save() return Response("email sent.", status=status.HTTP_200_OK) return Response("An email has already been sent to this invitee.", status=status.HTTP_400_BAD_REQUEST)
def send_mail(self, subject_template_name, email_template_name, context, from_email, to_email, html_email_template_name=None): """ Send a django.core.mail.EmailMultiAlternatives to `to_email`. """ subject = loader.render_to_string(subject_template_name, context) # Email subject *must not* contain newlines subject = ''.join(subject.splitlines()) body = loader.render_to_string(email_template_name, context) custom_send_mail( html_message=body, subject=subject, from_email=from_email, recipient_list=[to_email] )
def send_resolved_email(request, ticket): # grab a copy of the resource my_ticket = models.Ticket.objects.get(pk=ticket) # create a new email object email = emails.TicketResolvedEmail(my_ticket, request) # send the email object custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) my_ticket.resolved_email_date = timezone.now() my_ticket.save() messages.success(request, "the email has been sent!") return HttpResponseRedirect( reverse('tickets:detail', kwargs={'pk': ticket}))
def form_valid(self, form): self.object = form.save() # nobody is assigned, assign everyone if self.object.dm_assigned.count() == 0: for u in User.objects.filter(is_superuser=True): self.object.dm_assigned.add(u) # create a new email object email = emails.NewTicketEmail(self.object, self.request) # send the email object custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) return HttpResponseRedirect(reverse_lazy('tickets:confirm'))
def form_valid(self, form): object = form.save() models.EntryPerson.objects.create(entry=object, role=1, user_id=self.request.user.id, organization="DFO") # create a new email object email = emails.NewEntryEmail(object, self.request) # send the email object custom_send_mail( subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list ) messages.success(self.request, _("The entry has been submitted and an email has been sent to the Indigenous Hub Coordinator!")) return HttpResponseRedirect(reverse_lazy('ihub:entry_detail', kwargs={"pk": object.id}))
def form_valid(self, form): self.object = form.save() # create a new email object email = emails.NewEntryEmail(self.object, self.request) # send the email object if email.to_list: custom_send_mail( subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list ) if form.cleaned_data["do_another"] == 1: return HttpResponseRedirect(reverse_lazy('scifi:ctrans_new')) else: return HttpResponseRedirect(reverse_lazy('scifi:index'))
def form_valid(self, form): context = { "first_name": form.cleaned_data["first_name"], "last_name": form.cleaned_data["last_name"], "email": form.cleaned_data["email"], "user_id": form.cleaned_data["user_id"], "application": form.cleaned_data["application"], "optional_comment": form.cleaned_data["optional_comment"], } email = emails.RequestAccessEmail(context, self.request) # send the email object custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) messages.success( self.request, message="your request has been sent to the site administrator") return HttpResponseRedirect(reverse('accounts:close_me'))
def form_valid(self, form): self.object = form.save() # create a new email object email = emails.NewFollowUpEmail(self.object, self.request) # send the email object custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) # github if self.object.ticket.github_issue_number: # If a github issue number exists, create this follow up as a comment my_comment = create_or_edit_comment( self.object.created_by_id, self.object.message, self.object.ticket.github_issue_number, ) self.object.github_id = my_comment.id self.object.save() return HttpResponseRedirect(reverse('shared_models:close_me'))
def send(self): custom_send_mail(email_instance=self, )
def __set_request_status__(trip_request, request): """ IF POSSIBLE, THIS SHOULD ONLY BE CALLED BY THE approval_seeker() function. This will look at the reviewers and decide on what the project status should be. Will return False if trip_request is denied or if trip_request is not submitted """ # first order of business, if the trip_request is status "changes requested' do not continue if trip_request.status_id == 16: return False # Next: if the trip_request is unsubmitted, it is in 'draft' status elif not trip_request.submitted: trip_request.status_id = 8 # don't stick around any longer. save the trip_request and leave exit the function trip_request.save() return False else: # if someone denied it at any point, the trip_request is 'denied' and all subsequent reviewers are set to "cancelled" is_denied = False for reviewer in trip_request.reviewers.all(): # if is_denied, all subsequent reviewer statuses should be set to "cancelled" if is_denied: reviewer.status_id = 5 reviewer.save() # if reviewer status is denied, set the is_denied var to true if reviewer.status_id == 3: is_denied = True if is_denied: trip_request.status_id = 10 trip_request.save() # send an email to the trip_request owner email = emails.StatusUpdateEmail(trip_request, request) # # send the email object custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) # don't stick around any longer. save the trip_request and leave exit the function return False # The trip_request should be approved if everyone has approved it. HOWEVER, some reviewers might have been skipped # The total number of reviewers should equal the number of reviewer who approved [id=2] and / or were skipped [id=21]. elif trip_request.reviewers.all().count( ) == trip_request.reviewers.filter(status_id__in=[2, 21]).count(): trip_request.status_id = 11 trip_request.save() # send an email to the trip_request owner email = emails.StatusUpdateEmail(trip_request, request) # # send the email object custom_send_mail(subject=email.subject, html_message=email.message, from_email=email.from_email, recipient_list=email.to_list) # don't stick around any longer. save the trip_request and leave exit the function return False else: for reviewer in trip_request.reviewers.all(): # if a reviewer's status is 'pending', we are waiting on them and the project status should be set accordingly. if reviewer.status_id == 1: # if role is 'reviewer' if reviewer.role_id == 1: trip_request.status_id = 17 # if role is 'recommender' elif reviewer.role_id == 2: trip_request.status_id = 12 # if role is 'ncr reviewer' elif reviewer.role_id == 3: trip_request.status_id = 18 # if role is 'ncr recommender' elif reviewer.role_id == 4: trip_request.status_id = 19 # if role is 'adm' elif reviewer.role_id == 5: trip_request.status_id = 14 # if role is 'rdg' elif reviewer.role_id == 6: trip_request.status_id = 15 trip_request.save() return True