def save(request, tutor_application_id): tutor_application_to_save = mdl_attribution.tutor_application.find_by_id(tutor_application_id) form = ApplicationForm(data=request.POST) if form.is_valid(): application_charge_lecturing = allocation_charge_update(request.POST.get('application_charge_lecturing_id'), form['charge_lecturing'].value().replace(',', '.')) application_charge_practical = allocation_charge_update(request.POST.get('application_charge_practical_id'), form['charge_practical'].value().replace(',', '.')) if tutor_application_to_save: tutor_application_to_save.course_summary = form['course_summary'].value() tutor_application_to_save.remark = form['remark'].value() tutor_application_to_save.function = define_tutor_application_function(tutor_application_to_save) tutor_application_to_save.save() application_charge_lecturing.tutor_application = tutor_application_to_save application_charge_practical.tutor_application = tutor_application_to_save queue_sender.send_message(settings.QUEUES.get('QUEUES_NAME').get('ATTRIBUTION'), message_generation.generate_message_from_application_charge(application_charge_lecturing, UPDATE_OPERATION, tutor_application_to_save.function)) queue_sender.send_message(settings.QUEUES.get('QUEUES_NAME').get('ATTRIBUTION'), message_generation.generate_message_from_application_charge(application_charge_practical, UPDATE_OPERATION, tutor_application_to_save.function)) return HttpResponseRedirect(reverse('learning_unit_applications')) else: return render(request, "application_form.html", { 'application': get_application_informations(tutor_application_to_save), 'attributions': get_terminating_charges(get_last_year(), tutor_application_to_save.tutor), 'form': form})
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 migrate_model(app_label_models): """ Send all models obect from the models in the list of tuple to the queue migration :param app_label_models: A list of tuple, each tuple has app_label as key and a list of model_name as value ex : [('base',['person', 'tutor', 'offer']),('dissertation',['offer_proposition', 'adviser'])] """ if hasattr(settings, 'QUEUES'): print('Queue Name : {}'.format( settings.QUEUES.get('QUEUES_NAME').get('MIGRATIONS_TO_PRODUCE'))) print('Models : ') for app_label, model_names in app_label_models: print(' App label : {}'.format(app_label)) for model_name in model_names: print(' Model : {}'.format(model_name)) try: Model = apps.get_model(app_label=app_label, model_name=model_name) except LookupError: print(' Model {} does not exists'.format(model_name)) continue objects = Model.objects.all() print(' Count of objects to send : {}'.format( str(len(objects)))) for entity in objects: try: ser_obj = serialize(entity) queue_sender.send_message( settings.QUEUES.get('QUEUES_NAME').get( 'MIGRATIONS_TO_PRODUCE'), wrap_serialization(ser_obj)) except (ChannelClosed, ConnectionClosed): print('QueueServer is not installed or not launched') else: print('You have to configure queues to use migration script!')
def _process_exam_enrollment_form_submission(off_year, request, stud): data_to_submit = _exam_enrollment_form_submission_message(off_year, request, stud) json_data = json.dumps(data_to_submit) offer_enrol = offer_enrollment.get_by_student_offer(stud, off_year) if json_data and offer_enrol: exam_enrollment_submitted.insert_or_update_document(offer_enrol, json_data) queue_sender.send_message(settings.QUEUES.get('QUEUES_NAME').get('EXAM_ENROLLMENT_FORM_SUBMISSION'), data_to_submit) messages.add_message(request, messages.SUCCESS, _('exam_enrollment_form_submitted')) return response.HttpResponseRedirect(reverse('dashboard_home'))
def delete(self, *args, **kwargs): super(SerializableModel, self).delete(*args, **kwargs) if hasattr(settings, 'QUEUES'): try: ser_obj = serialize(self) queue_sender.send_message(settings.QUEUES.get('QUEUES_NAME').get('MIGRATIONS_TO_PRODUCE'), wrap_serialization(ser_obj, to_delete=True)) except (ChannelClosed, ConnectionClosed): LOGGER.exception('QueueServer is not installed or not launched')
def resend_messages_to_queue(self, request, queryset): for q_exception in queryset: try: queue_sender.send_message(q_exception.queue_name, q_exception.message) q_exception.delete() except Exception: self.message_user(request, 'Message %s not sent to %s.' % (q_exception.pk, q_exception.queue_name), level=messages.ERROR) self.message_user(request, "Messages sent.", level=messages.SUCCESS)
def resend_messages_to_queue(self, request, queryset): counter = 0 for record in queryset: try: queue_sender.send_message(settings.QUEUES.get('QUEUES_NAME').get('EXAM_ENROLLMENT_FORM_SUBMISSION'), record.document) counter += 1 except (ChannelClosed, ConnectionClosed): self.message_user(request, 'Message %s not sent to %s.' % (record.pk, record.queue_name), level=messages.ERROR) self.message_user(request, "{} message(s) sent.".format(counter), level=messages.SUCCESS)
def send_to_queue(instance, to_delete=False): queue_name = settings.QUEUES.get('QUEUES_NAME').get('MIGRATIONS_TO_PRODUCE') serialized_instance = wrap_serialization(serialize(instance, to_delete), to_delete) try: # Try to resend message present in cache message_queue_cache.retry_all_cached_messages() # Send current message queue_sender.send_message(queue_name, serialized_instance) except (ChannelClosed, ConnectionClosed): # Save current message queue cache database for retry later MessageQueueCache.objects.create(queue=queue_name, data=serialized_instance) LOGGER.exception('QueueServer is not installed or not launched')
def publish_to_portal(global_ids=None): tutor_application_list = _compute_list(global_ids) queue_name = settings.QUEUES.get('QUEUES_NAME', {}).get('APPLICATION_OSIS_PORTAL') if queue_name: try: queue_sender.send_message(queue_name, tutor_application_list) except (RuntimeError, pika.exceptions.ConnectionClosed, pika.exceptions.ChannelClosed, pika.exceptions.AMQPError): logger.exception('Could not recompute attributions for portal...') return False return True else: logger.exception('Could not recompute attributions for portal because not queue name ATTRIBUTION_RESPONSE') return False
def send_message(operation, global_id, application): if operation not in (DELETE_OPERATION, UPDATE_OPERATION): raise ValueError('operation_not_supported') queue_name = settings.QUEUES.get('QUEUES_NAME', {}).get('APPLICATION_REQUEST') if queue_name: message_to_send = _convert_to_epc_application(application) message_to_send.update({ 'operation': operation, 'global_id': global_id }) queue_sender.send_message(queue_name, message_to_send) return True return False
def send_json_scores_sheets_to_response_queue(global_id): data = get_json_data_scores_sheets(global_id) 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('SCORE_ENCODING_PDF_RESPONSE') send_message(queue_name, data, connect, channel) except (RuntimeError, pika.exceptions.ConnectionClosed, pika.exceptions.ChannelClosed, pika.exceptions.AMQPError): logger.exception('Could not send back scores_sheets json in response queue for global_id {}'.format(global_id))
def publish_to_portal(global_ids=None): attribution_list = _compute_list(global_ids) queue_name = settings.QUEUES.get('QUEUES_NAME', {}).get('ATTRIBUTION_RESPONSE') if queue_name: try: queue_sender.send_message(queue_name, attribution_list) except (RuntimeError, pika.exceptions.ConnectionClosed, pika.exceptions.ChannelClosed, pika.exceptions.AMQPError): logger.exception('Could not recompute attributions for portal...') return False return True else: logger.exception('Could not recompute attributions for portal because not queue name ATTRIBUTION_RESPONSE') return False
def send_to_queue(instance, to_delete=False): queue_name = settings.QUEUES.get('QUEUES_NAME').get( 'MIGRATIONS_TO_PRODUCE') serialized_instance = wrap_serialization(serialize(instance, to_delete), to_delete) try: # Try to resend message present in cache message_queue_cache.retry_all_cached_messages() # Send current message queue_sender.send_message(queue_name, serialized_instance) except (ChannelClosed, ConnectionClosed): # Save current message queue cache database for retry later MessageQueueCache.objects.create(queue=queue_name, data=serialized_instance) LOGGER.exception('QueueServer is not installed or not launched')
def create_tutor_application_from_attribution(an_attribution): attribution_lecturing_duration = tutor_charge.get_attribution_allocation_charge(an_attribution.tutor, an_attribution.learning_unit_year, component_type.LECTURING) attribution_practical_duration = tutor_charge.get_attribution_allocation_charge(an_attribution.tutor, an_attribution.learning_unit_year, component_type.PRACTICAL_EXERCISES) next_academic_year = mdl_base.academic_year.find_by_year(an_attribution.learning_unit_year.academic_year.year+1) next_learning_unit_years = mdl_base.learning_unit_year.search(next_academic_year, None, an_attribution.learning_unit_year.learning_unit, None) next_learning_unit_year = None if next_learning_unit_years: next_learning_unit_year = next_learning_unit_years[0] a_new_tutor_application = mdl_attribution.tutor_application.TutorApplication() a_new_tutor_application.tutor = an_attribution.tutor a_new_tutor_application.function = an_attribution.function a_new_tutor_application.learning_unit_year = next_learning_unit_year a_new_tutor_application.remark = None a_new_tutor_application.course_summary = None a_new_tutor_application.start_year = next_learning_unit_year.academic_year.year a_new_tutor_application.end_year = next_learning_unit_year.academic_year.year a_new_tutor_application.save() application_charge_lecturing = create_application_charge(a_new_tutor_application, attribution_lecturing_duration, component_type.LECTURING) application_charge_practical = create_application_charge(a_new_tutor_application, attribution_practical_duration, component_type.PRACTICAL_EXERCISES) queue_sender.send_message( settings.QUEUES.get('QUEUES_NAME').get('ATTRIBUTION'), message_generation.generate_message_from_application_charge( application_charge_lecturing, UPDATE_OPERATION, define_tutor_application_function(a_new_tutor_application))) queue_sender.send_message(settings.QUEUES.get('QUEUES_NAME').get('ATTRIBUTION'), message_generation.generate_message_from_application_charge( application_charge_practical, UPDATE_OPERATION, define_tutor_application_function(a_new_tutor_application))) return a_new_tutor_application
def serializable_model_resend_messages_to_queue(self, request, queryset): if hasattr(settings, 'QUEUES') and settings.QUEUES: counter = 0 queue_name = settings.QUEUES.get('QUEUES_NAME').get('MIGRATIONS_TO_PRODUCE') for record in queryset: try: ser_obj = serialize(record, False) queue_sender.send_message(queue_name, wrap_serialization(ser_obj)) counter += 1 except (ChannelClosed, ConnectionClosed): self.message_user(request, 'Message %s not sent to %s.' % (record.pk, queue_name), level=messages.ERROR) self.message_user(request, "{} message(s) sent.".format(counter), level=messages.SUCCESS) else: self.message_user(request, 'No messages sent. No queues defined', level=messages.ERROR)
def save_on_new_learning_unit(request): new_tutor_application = create_tutor_application_from_user_learning_unit_year( request.user, request.POST.get('learning_unit_year_id')) form = ApplicationForm(data=request.POST) if form.is_valid(): if new_tutor_application: new_tutor_application.course_summary = form['course_summary'].value() new_tutor_application.remark = form['remark'].value() new_tutor_application.save() application_charge_lecturing = application_charge_create(new_tutor_application, format_charge(form['charge_lecturing'].value()), component_type.LECTURING) application_charge_practical = application_charge_create(new_tutor_application, format_charge(form['charge_practical'].value()), component_type.PRACTICAL_EXERCISES) new_tutor_application.function = define_tutor_application_function(new_tutor_application) new_tutor_application.save() queue_sender.send_message( settings.QUEUES.get('QUEUES_NAME').get('ATTRIBUTION'), message_generation.generate_message_from_application_charge( application_charge_lecturing, UPDATE_OPERATION, define_tutor_application_function(new_tutor_application))) queue_sender.send_message(settings.QUEUES.get('QUEUES_NAME').get('ATTRIBUTION'), message_generation.generate_message_from_application_charge( application_charge_practical, UPDATE_OPERATION, define_tutor_application_function(new_tutor_application))) return HttpResponseRedirect(reverse('learning_unit_applications')) else: return render(request, "application_form.html", { 'application': get_application_informations(new_tutor_application), 'attributions': get_terminating_charges(get_last_year(), new_tutor_application.tutor), 'form': form})
def serializable_model_resend_messages_to_queue(self, request, queryset): if hasattr(settings, 'QUEUES') and settings.QUEUES: counter = 0 queue_name = settings.QUEUES.get('QUEUES_NAME').get( 'MIGRATIONS_TO_PRODUCE') for record in queryset: try: ser_obj = serialize(record, False) queue_sender.send_message(queue_name, wrap_serialization(ser_obj)) counter += 1 except (ChannelClosed, ConnectionClosed): self.message_user(request, 'Message %s not sent to %s.' % (record.pk, queue_name), level=messages.ERROR) self.message_user(request, "{} message(s) sent.".format(counter), level=messages.SUCCESS) else: self.message_user(request, 'No messages sent. No queues defined', level=messages.ERROR)
def retry_all_cached_messages(): messages_to_retry = get_messages_to_retry() for message in messages_to_retry: queue_sender.send_message(message.queue, message.data) message.delete()