def post(self, request, **kwargs): body_unicode = request.body.decode("utf-8") body = json.loads(body_unicode) username = body["username"] pathway_id = body["pathway_id"] program_uuid = kwargs["uuid"] # verify that the user or an admin is making the request if username != request.user.get_username() and not request.user.is_staff: return JsonResponse({"error": "Permission denied"}, status=403) credential = UserCredential.objects.filter( username=username, status=UserCredential.AWARDED, program_credentials__program_uuid=program_uuid ) program = get_object_or_404(Program, uuid=program_uuid, site=request.site) pathway = get_object_or_404( Pathway, id=pathway_id, programs__uuid=program_uuid, pathway_type=PathwayType.CREDIT.value, ) certificate = get_object_or_404(ProgramCertificate, program_uuid=program_uuid, site=request.site) user = get_object_or_404(User, username=username) public_record, _ = ProgramCertRecord.objects.get_or_create(user=user, program=program) # Make sure we haven't already sent an email with a 'sent' status if UserCreditPathway.objects.filter(user=user, pathway=pathway, status=UserCreditPathwayStatus.SENT).exists(): return JsonResponse({"error": "Pathway email already sent"}, status=400) record_path = reverse("records:public_programs", kwargs={"uuid": public_record.uuid.hex}) record_link = request.build_absolute_uri(record_path) csv_link = urllib.parse.urljoin(record_link, "csv") msg = ProgramCreditRequest(request.site, user.email).personalize( recipient=Recipient(username=None, email_address=pathway.email), language=certificate.language, user_context={ "pathway_name": pathway.name, "program_name": program.title, "record_link": record_link, "user_full_name": request.user.get_full_name() or request.user.username, "program_completed": credential.exists(), "previously_sent": False, "csv_link": csv_link, }, ) ace.send(msg) # Create a record of this email so that we can't send multiple times # If the status was previously changed, we want to reset it to SENT UserCreditPathway.objects.update_or_create( user=user, pathway=pathway, defaults={"status": UserCreditPathwayStatus.SENT}, ) return http.HttpResponse(status=200)
def send_updated_emails_for_program(username, program_certificate): """ If the user has previously sent an email to a pathway org, we want to send an updated one when they finish the program. This function is called from the credentials Program Certificate awarding API """ site = program_certificate.site user = User.objects.get(username=username) program_uuid = program_certificate.program_uuid program = Program.objects.prefetch_related('pathways').get( site=site, uuid=program_uuid) pathways_set = frozenset(program.pathways.all()) user_pathways = UserCreditPathway.objects.select_related('pathway').filter( user=user, pathway__in=pathways_set, status=UserCreditPathwayStatus.SENT) # Return here if the user doesn't have a program cert record try: pcr = ProgramCertRecord.objects.get(program=program, user=user) except ProgramCertRecord.DoesNotExist: logger.exception( "Program Cert Record for user_uuid %s, program_uuid %s does not exist", user.id, program.uuid) return # Send emails for those already marked as "SENT" for user_pathway in user_pathways: pathway = user_pathway.pathway record_path = reverse('records:public_programs', kwargs={'uuid': pcr.uuid.hex}) record_link = site.domain + record_path csv_link = urllib.parse.urljoin(record_link, "csv") msg = ProgramCreditRequest(site).personalize( recipient=Recipient(username=None, email_address=pathway.email), language=program_certificate.language, user_context={ 'pathway_name': pathway.name, 'program_name': program.title, 'record_link': record_link, 'user_full_name': user.get_full_name(), 'program_completed': True, 'previously_sent': True, 'csv_link': csv_link, }, ) ace.send(msg)