diff --git a/directory/models.py b/directory/models.py index 58ae645728..b978f7ed39 100644 --- a/directory/models.py +++ b/directory/models.py @@ -1,6 +1,7 @@ from django.contrib.auth.models import Group from django.core.validators import MaxValueValidator, MinValueValidator from django.db import models, transaction +from django.db.models import Q from jsonfield import JSONField from laboratory.settings import DEATH_RESEARCH_PK, EXCLUDE_TYPE_RESEARCH @@ -745,6 +746,9 @@ def update_lab_research_and_fractions(research_data, need_log_data: bool = False fraction_data = Fractions.normalize_fraction_data(fraction) if service_fractions: current_fraction = service_fractions.filter(pk=fraction["id"]).first() + fsli_is_duplicated = Fractions.check_duplicated_fsli_code(current_fraction.pk, fraction_data["fsli"]) + if not fsli_is_duplicated["ok"]: + return {"ok": False, "message": f"В анализе {fsli_is_duplicated['research_title']} есть такой же код ФСЛИ ({fraction_data['fsli']})"} if current_fraction: if need_log_data: old_log_data["fractions"][current_fraction.pk] = Fractions.as_json(current_fraction) @@ -752,6 +756,9 @@ def update_lab_research_and_fractions(research_data, need_log_data: bool = False if need_log_data: new_log_data["fractions"][current_fraction.pk] = Fractions.as_json(current_fraction) else: + fsli_is_duplicated = Fractions.check_duplicated_fsli_code(-1, fraction_data["fsli"]) + if not fsli_is_duplicated["ok"]: + return {"ok": False, "message": f"В анализе {fsli_is_duplicated['research_title']} есть такой же код ФСЛИ ({fraction_data['fsli']})"} new_fraction = Fractions.create_fraction(fraction_data, service.pk, relation.pk) if need_log_data: new_log_data["fractions"][new_fraction.pk] = Fractions.as_json(new_fraction) @@ -792,6 +799,9 @@ def create_lab_research_and_fractions(research_data, need_log_data: bool = False relation = ReleationsFT.get_or_create_relation(tube) for fraction in tube["fractions"]: fraction_data = Fractions.normalize_fraction_data(fraction) + fsli_is_duplicated = Fractions.check_duplicated_fsli_code(-1, fraction_data["fsli"]) + if not fsli_is_duplicated["ok"]: + return {"ok": False, "pk": new_service.pk, "message": f"В анализе {fsli_is_duplicated['research_title']} есть такой же код ФСЛИ ({fraction_data['fsli']})"} new_fraction = Fractions.create_fraction(fraction_data, new_service.pk, relation.pk) log_data["fractions"][new_fraction.pk] = Fractions.as_json(new_fraction) if need_log_data: @@ -1485,6 +1495,14 @@ def create_fraction(fraction_data, service_pk, relation_pk): new_fraction.save() return new_fraction + @staticmethod + def check_duplicated_fsli_code(fraction_id: int, fsli_code): + fractions = Fractions.objects.filter(fsli=fsli_code).select_related('research').exclude(Q(pk=fraction_id) | Q(fsli=None)) + if fractions.exists(): + first_duplicated: Fractions = fractions.first() + return {"ok": False, "fraction_title": first_duplicated.title, "research_title": first_duplicated.research.title} + return {"ok": True} + class Absorption(models.Model): """ diff --git a/l2-frontend/src/construct/ResearchDetail.vue b/l2-frontend/src/construct/ResearchDetail.vue index 6168a2be7d..a06df629c6 100644 --- a/l2-frontend/src/construct/ResearchDetail.vue +++ b/l2-frontend/src/construct/ResearchDetail.vue @@ -567,6 +567,7 @@ const updateResearch = async () => { await getResearch(); emit('updateResearch'); } else { + await getResearch(); root.$emit('msg', 'error', message); } } else { @@ -586,6 +587,10 @@ const createResearch = async () => { await getResearch(); emit('updateResearch'); } else { + if (pk) { + research.value.pk = pk; + await getResearch(); + } root.$emit('msg', 'error', message); } } else {