diff --git a/education/main.py b/education/main.py index e71c7d8f9e..c09fc7c279 100644 --- a/education/main.py +++ b/education/main.py @@ -1,5 +1,7 @@ import time +from django.db import transaction + from education.models import LogUpdateMMIS from education.sql_func import get_connection_params, get_enrollees_by_id, get_changes, get_grade_entrance_exams, get_application_by_id, get_achievements_by_id from education.views import update_education_individual, change_encoding_cp1251 @@ -161,15 +163,19 @@ def process_update_enrollees(): } ) enrollees_achievements_data[achievement.ID] = tmp_data.copy() - for person_data in enrollees_person_data: - update_education_individual( - person_data, - user_obj_hospital, - enrollees_application_data.get(person_data.ID, []), - enrollees_grade_data.get(person_data.ID, []), - enrollees_achievements_data.get(person_data.ID, []), - ) - if enrollees_person_data: + try: + with transaction.atomic(): + for person_data in enrollees_person_data: + update_education_individual( + person_data, + user_obj_hospital, + enrollees_application_data.get(person_data.ID, []), + enrollees_grade_data.get(person_data.ID, []), + enrollees_achievements_data.get(person_data.ID, []), + ) + except: + print('Data saving error') + else: current_last_log.save() time.sleep(10) diff --git a/education/views.py b/education/views.py index c7426097b7..77b93663ba 100644 --- a/education/views.py +++ b/education/views.py @@ -15,137 +15,134 @@ def change_encoding_cp1251(text): def update_education_individual(person_data, user_hospital_obj, person_applications, person_grade, person_achievements): - try: - card = Individual.import_from_simple_data( - { - "family": change_encoding_cp1251(person_data.Фамилия), - "name": change_encoding_cp1251(person_data.Имя), - "patronymic": change_encoding_cp1251(person_data.Отчество), - "sex": "м" if "м" in change_encoding_cp1251(person_data.Пол.lower()) else "ж", - "birthday": person_data.Дата_Рождения.strftime("%d.%m.%Y"), - "snils": person_data.СНИЛС if person_data.СНИЛС else "", - }, - user_hospital_obj, - person_data.ID, - "", - "", - filter_mmis_id=True, - ) - tel = person_data.Мобильный if person_data.Мобильный else "" - tel = tel.replace("+7", "") - card.phone = tel - card.save() - result_application = [] - step_app = 0 - for pa in person_applications: - step_app += 1 - pa_mmis_id_application = int(pa.get("Код_Заявления")) - application = ApplicationEducation.objects.filter(card=card, mmis_id=pa_mmis_id_application).first() - education_speciality = EducationSpeciality.objects.filter(mmis_id=int(pa.get("Код_Специальности"))).first() - personal_number = pa.get("НомерЛД") - is_enrolled = pa.get("Зачислен") - is_expelled = pa.get("ОтказалсяОтЗачисления") - date = pa.get("Дата_Подачи") - is_checked = pa.get("Проверено") - facultet_id = pa.get("Факультет") - original = pa.get("Оригинал") or False - facultet = Faculties.objects.filter(mmis_id=facultet_id).first() - if application: - application.speciality = education_speciality - application.personal_number = personal_number - application.is_enrolled = is_enrolled - application.is_expelled = is_expelled - application.date = date - application.is_checked = is_checked - application.facultet = facultet - application.original = original - application.save() + card = Individual.import_from_simple_data( + { + "family": change_encoding_cp1251(person_data.Фамилия), + "name": change_encoding_cp1251(person_data.Имя), + "patronymic": change_encoding_cp1251(person_data.Отчество), + "sex": "м" if "м" in change_encoding_cp1251(person_data.Пол.lower()) else "ж", + "birthday": person_data.Дата_Рождения.strftime("%d.%m.%Y"), + "snils": person_data.СНИЛС if person_data.СНИЛС else "", + }, + user_hospital_obj, + person_data.ID, + "", + "", + filter_mmis_id=True, + ) + tel = person_data.Мобильный if person_data.Мобильный else "" + tel = tel.replace("+7", "") + card.phone = tel + card.save() + result_application = [] + step_app = 0 + for pa in person_applications: + step_app += 1 + pa_mmis_id_application = int(pa.get("Код_Заявления")) + application = ApplicationEducation.objects.filter(card=card, mmis_id=pa_mmis_id_application).first() + education_speciality = EducationSpeciality.objects.filter(mmis_id=int(pa.get("Код_Специальности"))).first() + personal_number = pa.get("НомерЛД") + is_enrolled = pa.get("Зачислен") + is_expelled = pa.get("ОтказалсяОтЗачисления") + date = pa.get("Дата_Подачи") + is_checked = pa.get("Проверено") + facultet_id = pa.get("Факультет") + original = pa.get("Оригинал") or False + facultet = Faculties.objects.filter(mmis_id=facultet_id).first() + if application: + application.speciality = education_speciality + application.personal_number = personal_number + application.is_enrolled = is_enrolled + application.is_expelled = is_expelled + application.date = date + application.is_checked = is_checked + application.facultet = facultet + application.original = original + application.save() + else: + application = ApplicationEducation.objects.create( + mmis_id=pa_mmis_id_application, + card=card, + speciality=education_speciality, + personal_number=personal_number, + is_enrolled=is_enrolled, + is_expelled=is_expelled, + date=date, + is_checked=is_checked, + facultet=facultet, + original=original, + ) + application.save() + result_application.append(application.pk) + + result_exam = [] + step_exam = 0 + for pg in person_grade: + step_exam += 1 + pg_mmis_id = int(pg.get("Код")) + subject_code = int(pg.get("Код_Дисциплины")) + grade = pg.get("Оценка") + if grade == "None": + grade = 0 + type_test_code = int(pg.get("Код_Испытания")) + mmis_id_application = int(pg.get("Код_Заявления")) + subject = Subjects.objects.filter(mmis_id=subject_code).first() + entrance_exam = EntranceExam.objects.filter(card=card, mmis_id=pg_mmis_id).first() + application = ApplicationEducation.objects.filter(card=card, mmis_id=mmis_id_application).first() + if application: + if entrance_exam: + entrance_exam.grade = grade + entrance_exam.subjects = subject + entrance_exam.type_test = ExamType.objects.filter(mmis_id=type_test_code).first() + entrance_exam.application_education = application + entrance_exam.save() else: - application = ApplicationEducation.objects.create( - mmis_id=pa_mmis_id_application, - card=card, - speciality=education_speciality, - personal_number=personal_number, - is_enrolled=is_enrolled, - is_expelled=is_expelled, - date=date, - is_checked=is_checked, - facultet=facultet, - original=original, + entrance_exam = EntranceExam.objects.create( + card=card, mmis_id=pg_mmis_id, grade=grade, subjects=subject, type_test=ExamType.objects.filter(mmis_id=type_test_code).first(), application_education=application ) - application.save() - result_application.append(application.pk) - - result_exam = [] - step_exam = 0 - for pg in person_grade: - step_exam += 1 - pg_mmis_id = int(pg.get("Код")) - subject_code = int(pg.get("Код_Дисциплины")) - grade = pg.get("Оценка") - if grade == "None": - grade = 0 - type_test_code = int(pg.get("Код_Испытания")) - mmis_id_application = int(pg.get("Код_Заявления")) - subject = Subjects.objects.filter(mmis_id=subject_code).first() - entrance_exam = EntranceExam.objects.filter(card=card, mmis_id=pg_mmis_id).first() - application = ApplicationEducation.objects.filter(card=card, mmis_id=mmis_id_application).first() - if application: - if entrance_exam: - entrance_exam.grade = grade - entrance_exam.subjects = subject - entrance_exam.type_test = ExamType.objects.filter(mmis_id=type_test_code).first() - entrance_exam.application_education = application - entrance_exam.save() - else: - entrance_exam = EntranceExam.objects.create( - card=card, mmis_id=pg_mmis_id, grade=grade, subjects=subject, type_test=ExamType.objects.filter(mmis_id=type_test_code).first(), application_education=application - ) - entrance_exam.save() - result_exam.append(entrance_exam.pk) + entrance_exam.save() + result_exam.append(entrance_exam.pk) - result_achievements = [] + result_achievements = [] - for pach in person_achievements: - pach_mmis_id = int(pach.get("Код")) - pach_code = int(pach.get("КодИД")) - achievement_type = AchievementType.objects.filter(mmis_id=pach_code).first() - pach_date = pach.get("ДатаИД") - pach_grade = int(pach.get("БаллИД")) - pach_serial = pach.get("СерияИД") - pach_number = pach.get("НомерИД") - pach_organization = pach.get("ОрганизацияИД") - pach_mmis_id_application = pach.get("Код_Заявления") - app_obj = ApplicationEducation.objects.filter(mmis_id=pach_mmis_id_application).first() - achievement_person = Achievement.objects.filter(card=card, mmis_id=pach_mmis_id).first() - if achievement_person: - achievement_person.type = achievement_type - achievement_person.document_number = pach_number - achievement_person.document_serial = pach_serial - achievement_person.document_date = pach_date - achievement_person.grade = pach_grade - achievement_person.organization = pach_organization - achievement_person.application_source_pk = pach_mmis_id_application - achievement_person.application = app_obj - achievement_person.save() - else: - achievement_person = Achievement.objects.create( - card=card, - mmis_id=pach_mmis_id, - type=achievement_type, - document_number=pach_number, - document_serial=pach_serial, - document_date=pach_date, - grade=pach_grade, - organization=pach_organization, - application_source_pk=pach_mmis_id_application, - application=app_obj, - ) - achievement_person.save() - result_achievements.append(achievement_person.pk) - return {"card": card, "result_application": result_application, "result_exam": result_exam, "result_chievements": result_achievements} - except Exception as e: - return f"Exception: {e}" + for pach in person_achievements: + pach_mmis_id = int(pach.get("Код")) + pach_code = int(pach.get("КодИД")) + achievement_type = AchievementType.objects.filter(mmis_id=pach_code).first() + pach_date = pach.get("ДатаИД") + pach_grade = int(pach.get("БаллИД")) + pach_serial = pach.get("СерияИД") + pach_number = pach.get("НомерИД") + pach_organization = pach.get("ОрганизацияИД") + pach_mmis_id_application = pach.get("Код_Заявления") + app_obj = ApplicationEducation.objects.filter(mmis_id=pach_mmis_id_application).first() + achievement_person = Achievement.objects.filter(card=card, mmis_id=pach_mmis_id).first() + if achievement_person: + achievement_person.type = achievement_type + achievement_person.document_number = pach_number + achievement_person.document_serial = pach_serial + achievement_person.document_date = pach_date + achievement_person.grade = pach_grade + achievement_person.organization = pach_organization + achievement_person.application_source_pk = pach_mmis_id_application + achievement_person.application = app_obj + achievement_person.save() + else: + achievement_person = Achievement.objects.create( + card=card, + mmis_id=pach_mmis_id, + type=achievement_type, + document_number=pach_number, + document_serial=pach_serial, + document_date=pach_date, + grade=pach_grade, + organization=pach_organization, + application_source_pk=pach_mmis_id_application, + application=app_obj, + ) + achievement_person.save() + result_achievements.append(achievement_person.pk) + return {"card": card, "result_application": result_application, "result_exam": result_exam, "result_chievements": result_achievements} def get_all_enrollees(request):