def save_state_changed_and_send_email(admission, connected_user=None): person = admission.person_information.person mails = _get_managers_mails(admission.formation) condition_of_acceptance, registration_required = None, None state_message = get_valid_state_change_message(admission) save_and_create_revision(get_revision_messages(state_message), admission, connected_user) if admission.state in (SUBMITTED, REGISTRATION_SUBMITTED): send_submission_email_to_admission_managers(admission, connected_user) send_submission_email_to_participant(admission, connected_user) return elif admission.state in (ACCEPTED, REJECTED, WAITING, VALIDATED, ACCEPTED_NO_REGISTRATION_REQUIRED): condition_of_acceptance, lower_state, registration_required = _get_datas_from_admission(admission) else: lower_state = 'other' send_email( template_references={ 'html': 'iufc_participant_state_changed_{}_html'.format(lower_state), 'txt': 'iufc_participant_state_changed_{}_txt'.format(lower_state), }, data={ 'template': { 'first_name': admission.person_information.person.first_name, 'last_name': admission.person_information.person.last_name, 'formation': admission.formation, 'state': _(admission.state), 'reason': admission.state_reason if admission.state_reason else '-', 'mails': mails, 'original_state': _(admission._original_state), 'condition_of_acceptance': condition_of_acceptance, 'registration_required': registration_required }, 'subject': { 'state': _(admission.state) } }, receivers=[ message_config.create_receiver( person.id, person.email, None ) ], connected_user=connected_user ) MAIL['text'] = MAIL_MESSAGE % {'receiver': person.email} save_and_create_revision(get_revision_messages(MAIL), admission, connected_user)
def send_invoice_uploaded_email(admission): participant = admission.person_information.person mails = _get_managers_mails(admission.formation) send_email( template_references={ 'html': 'iufc_participant_invoice_uploaded_html', 'txt': 'iufc_participant_invoice_uploaded_txt', }, data={ 'template': { 'formation': admission.formation.acronym, 'mails': mails }, 'subject': {} }, receivers=[ message_config.create_receiver( participant.id, participant.email, None ) ], ) MAIL['text'] = MAIL_MESSAGE % {'receiver': participant.email} + ' : ' + _('Invoice') save_and_create_revision(get_revision_messages(MAIL), admission)
def send_invoice_uploaded_email(admission): mails = _get_managers_mails(admission.formation) receivers = _build_participant_receivers(admission) send_email( template_references={ 'html': 'iufc_participant_invoice_uploaded_html', 'txt': 'iufc_participant_invoice_uploaded_txt', }, data={ 'template': { 'formation': admission.formation.acronym, 'mails': mails, 'student_portal_url': settings.CONTINUING_EDUCATION_STUDENT_PORTAL_URL, 'participant_created_admission': _participant_created_admission(admission), }, 'subject': {} }, receivers=receivers, ) MAIL['text'] = MAIL_MESSAGE % { 'receiver': _get_receivers_emails_as_str(receivers) } + ' : ' + _('Invoice') save_and_create_revision(get_revision_messages(MAIL), admission)
def send_submission_email_to_participant(admission, connected_user): participant = admission.person_information.person mails = _get_managers_mails(admission.formation) send_email( template_references={ 'html': _get_template_reference(admission, receiver='participant', suffix='html'), 'txt': _get_template_reference(admission, receiver='participant', suffix='txt') }, data={ 'template': { 'name': admission.person_information.person.last_name, 'formation': admission.formation.title, 'admission_data': _get_formatted_admission_data(admission), 'mails': mails }, 'subject': {} }, receivers=[ message_config.create_receiver( participant.id, participant.email, None ) ], connected_user=connected_user ) MAIL['text'] = MAIL_MESSAGE % {'receiver': participant.email} save_and_create_revision(get_revision_messages(MAIL), admission, connected_user)
def send_admission_to_queue(request, admission): data = get_json_for_epc(admission) credentials = pika.PlainCredentials(settings.QUEUES.get('QUEUE_USER'), settings.QUEUES.get('QUEUE_PASSWORD')) rabbit_settings = pika.ConnectionParameters( settings.QUEUES.get('QUEUE_URL'), settings.QUEUES.get('QUEUE_PORT'), settings.QUEUES.get('QUEUE_CONTEXT_ROOT'), credentials) try: connect = pika.BlockingConnection(rabbit_settings) channel = connect.channel() queue_name = settings.QUEUES.get('QUEUES_NAME').get('IUFC_TO_EPC') send_message(queue_name, data, connect, channel) admission.ucl_registration_complete = UCLRegistrationState.SENDED.name save_and_create_revision( get_revision_messages(UCL_REGISTRATION_SENDED), admission, request.user) except (RuntimeError, pika.exceptions.ConnectionClosed, pika.exceptions.ChannelClosed, pika.exceptions.AMQPError): logger.exception( _('Could not send admission json with uuid %(uuid)s in queue') % {'uuid': admission.uuid}) display_error_messages( request, _('Could not send admission json with uuid %(uuid)s in queue') % {'uuid': admission.uuid})
def test_participant_created_admission_false(self): Version.objects.all().delete() self.assertFalse(_participant_created_admission(self.admission)) save_and_create_revision(get_revision_messages(ADMISSION_CREATION), self.admission, self.manager.person.user) self.assertFalse(_participant_created_admission(self.admission))
def test_get_versions(self): version_list = get_versions(self.admission) self.assertEqual(len(version_list), 0) i = 1 for msg in VERSION_MESSAGES: save_and_create_revision( get_revision_messages({ 'icon': '', 'text': msg }), self.admission, self.training_manager.user) version_list = get_versions(self.admission) self.assertEqual(len(version_list), i) i += 1
def _update_registration_field_for_list(registrations_ids_list, field_to_update, user): registrations_list = Admission.objects.filter( id__in=registrations_ids_list) registrations_list_states = registrations_list.values_list('state', flat=True) if not all(state == admission_state_choices.REGISTRATION_SUBMITTED for state in registrations_list_states): raise PermissionDenied( _('The registration must be submitted to be validated.')) for registration in registrations_list: if field_to_update == 'registration_file_received': registration.registration_file_received = True save_and_create_revision( get_revision_messages(REGISTRATION_FILE_RECEIVED), registration, user)
def save_role_registered_in_admission(data): data = json.loads(data.decode("utf-8").replace("\'", "\"")) admission = get_object_or_404(Admission, uuid=data['student_case_uuid']) if data['success']: registration_status = data.get('registration_status') admission.ucl_registration_complete = registration_status admission.noma = data.get('registration_id') if registration_status == UCLRegistrationState.INSCRIT.name: message = UCL_REGISTRATION_REGISTERED else: UCL_REGISTRATION_STATE_CHANGED['text'] += admission.get_ucl_registration_complete_display() message = UCL_REGISTRATION_STATE_CHANGED else: admission.ucl_registration_complete = UCLRegistrationState.REJECTED.name admission.ucl_registration_error = data['message'] UCL_REGISTRATION_REJECTED['text'] += admission.get_ucl_registration_error_display() message = UCL_REGISTRATION_REJECTED save_and_create_revision(get_revision_messages(message), admission)
def send_submission_email_to_admission_managers(admission, connected_user): relative_path = reverse('admission_detail', kwargs={'admission_id': admission.id}) # No request here because we are in a post_save formation_url = 'https://{}{}'.format(Site.objects.get_current().domain, relative_path) attachments = _get_attachments(admission.id, MAX_DOCUMENTS_SIZE) receivers = _get_admission_managers_email_receivers(admission) send_email(template_references={ 'html': _get_template_reference(admission, receiver='admin', suffix='html'), 'txt': _get_template_reference(admission, receiver='admin', suffix='txt') }, data={ 'template': { 'first_name': admission.person_information.person.first_name, 'last_name': admission.person_information.person.last_name, 'formation': admission.formation, 'state': _(admission.state), 'formation_link': formation_url, 'admission_data': _get_formatted_admission_data(admission), 'attachment_removed': attachments is None, }, 'subject': { 'formation': admission.formation.acronym, }, 'attachment': attachments }, receivers=receivers, connected_user=connected_user) MAIL['text'] = MAIL_MESSAGE % { 'receiver': _get_receivers_emails_as_str(receivers) } save_and_create_revision( get_revision_messages(MAIL) if receivers else '', admission, connected_user)
def create(self, validated_data): if 'person_information' in validated_data: iufc_person_data = validated_data.pop('person_information') person_data = iufc_person_data.pop('person') Person.objects.filter(email=person_data['email']).update( **person_data) person = Person.objects.get(email=person_data['email']) ContinuingEducationPerson.objects.filter(person=person).update( **iufc_person_data) iufc_person = ContinuingEducationPerson.objects.get(person=person) validated_data['person_information'] = iufc_person if 'address' in validated_data: address_data = validated_data.pop('address') address = Address.objects.create(**address_data) validated_data['address'] = address admission = Admission(**validated_data) admission.residence_address = admission.address admission.billing_address = admission.address save_and_create_revision(get_revision_messages(ADMISSION_CREATION), admission, self.context.get('request').user) return admission
def send_submission_email_to_participant(admission, connected_user): mails = _get_managers_mails(admission.formation) receivers = _build_participant_receivers(admission) send_email(template_references={ 'html': _get_template_reference(admission, receiver='participant', suffix='html'), 'txt': _get_template_reference(admission, receiver='participant', suffix='txt') }, data={ 'template': { 'name': admission.person_information.person.last_name, 'formation': admission.formation.title, 'admission_data': _get_formatted_admission_data(admission), 'mails': mails, 'student_portal_url': settings.CONTINUING_EDUCATION_STUDENT_PORTAL_URL, 'participant_created_admission': _participant_created_admission(admission), }, 'subject': {} }, receivers=receivers, connected_user=connected_user) MAIL['text'] = MAIL_MESSAGE % { 'receiver': _get_receivers_emails_as_str(receivers) } save_and_create_revision(get_revision_messages(MAIL), admission, connected_user)
def _set_archived_state(user, admission, archived_state): if admission: admission.archived = archived_state save_and_create_revision( get_revision_messages(FILE_ARCHIVED) if admission.archived else get_revision_messages(FILE_UNARCHIVED), admission, user)
def admission_form(request, admission_id=None): admission = get_object_or_404(Admission, pk=admission_id) if admission_id else None if admission: can_access_admission(request.user, admission) if admission.is_draft(): raise PermissionDenied selected_person = bool(request.POST.get('person_information', False)) states = admission_state_choices.NEW_ADMIN_STATE[admission.state].get('choices', ()) if admission else None base_person = admission.person_information.person if admission else None base_person_form = PersonForm( data=request.POST or None, instance=base_person, selected_person=selected_person, no_first_name_checked=request.POST.get('no_first_name', False) ) person_information = ContinuingEducationPerson.objects.filter(person=base_person).first() # TODO :: get last admission address if it exists instead of None address = admission.address if admission else None state = admission.state if admission else SUBMITTED adm_form = AdmissionForm( data=request.POST or None, user=request.user, instance=admission, initial={ 'state': state } ) person_form = ContinuingEducationPersonForm( data=request.POST or None, instance=person_information, selected_person=selected_person ) address_form = AddressForm(request.POST or None, instance=address) state = admission.state if admission else None if adm_form.is_valid() and person_form.is_valid() and address_form.is_valid() and base_person_form.is_valid(): if address: address = address_form.save() else: address = Address(**address_form.cleaned_data) address.save() person = person_form.save(commit=False) base_person = base_person_form.save() person.person_id = base_person.pk person.save() admission = adm_form.save(commit=False) admission.address = address admission.billing_address = address admission.residence_address = address if not admission.person_information: admission.person_information = person if admission_id: admission.save() else: save_and_create_revision(get_revision_messages(ADMISSION_CREATION), admission, request.user) if admission.state == DRAFT: return redirect(reverse('admission')) return redirect(reverse('admission_detail', kwargs={'admission_id': admission.pk})) else: errors = list(itertools.product(adm_form.errors, person_form.errors, address_form.errors)) display_errors(request, errors) return render( request, 'admission_form.html', { 'admission': admission, 'admission_form': adm_form, 'person_form': person_form, 'address_form': address_form, 'base_person_form': base_person_form, 'state': state, 'states': states, 'selected_person': selected_person, } )