Skip to content

Commit

Permalink
Merge pull request #3339 from mikhailprivalov/new-constructor-laboratory
Browse files Browse the repository at this point in the history
Новый конструктор лаборатории
  • Loading branch information
urchinpro authored Jan 9, 2024
2 parents 97673d7 + 9dc8c7c commit ecf70a4
Show file tree
Hide file tree
Showing 12 changed files with 1,122 additions and 0 deletions.
Empty file added api/construct/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions api/construct/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.urls import path

from . import views

urlpatterns = [
path('laboratory/get-departments', views.get_departments),
path('laboratory/get-tubes', views.get_tubes),
path('laboratory/update-order-research', views.update_order_research),
path('laboratory/update-order-fraction', views.update_order_fraction),
path('laboratory/change-visibility-research', views.change_visibility_research),
path('laboratory/get-research', views.get_research),
path('laboratory/update-research', views.update_research),
]
62 changes: 62 additions & 0 deletions api/construct/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
import simplejson as json
from directory.models import Researches, Fractions
from laboratory.decorators import group_required
from podrazdeleniya.models import Podrazdeleniya
from utils.response import status_response


@login_required
@group_required("Конструктор: Лабораторные исследования")
def get_departments(request):
result = Podrazdeleniya.get_podrazdeleniya(Podrazdeleniya.LABORATORY)
return JsonResponse({"result": result})


@login_required
@group_required("Конструктор: Лабораторные исследования")
def get_tubes(request):
request_data = json.loads(request.body)
result = Researches.get_tubes(request_data["department_id"])
return JsonResponse({"result": result})


@login_required
@group_required("Конструктор: Лабораторные исследования")
def update_order_research(request):
request_data = json.loads(request.body)
result = Researches.update_order(request_data["researchPk"], request_data["researchNearbyPk"], request_data["action"])
return status_response(result)


@login_required
@group_required("Конструктор: Лабораторные исследования")
def update_order_fraction(request):
request_data = json.loads(request.body)
result = Fractions.update_order(request_data["fractionPk"], request_data["fractionNearbyPk"], request_data["action"])
return status_response(result)


@login_required
@group_required("Конструктор: Лабораторные исследования")
def change_visibility_research(request):
request_data = json.loads(request.body)
result = Researches.change_visibility(request_data["researchPk"])
return status_response(result)


@login_required
@group_required("Конструктор: Лабораторные исследования")
def get_research(request):
request_data = json.loads(request.body)
result = Researches.get_research(request_data["researchPk"])
return JsonResponse({"result": result})


@login_required
@group_required("Конструктор: Лабораторные исследования")
def update_research(request):
request_data = json.loads(request.body)
result = Researches.update_lab_research(request_data["research"])
return status_response(result)
1 change: 1 addition & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
path('edit-forms/', include('api.edit_forms.urls')),
path('hospitals/', include('api.hospitals.urls')),
path('cases/', include('api.cases.urls')),
path('construct/', include('api.construct.urls')),
path('get-prices', views.get_prices),
path('get-price-data', views.get_price_data),
path('update-price', views.update_price),
Expand Down
1 change: 1 addition & 0 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,7 @@ def construct_menu_data(request):
pages = [
{"url": "/construct/tubes", "title": "Ёмкости для биоматериала", "access": ["Конструктор: Ёмкости для биоматериала"], "module": None},
{"url": "/construct/researches", "title": "Лабораторные исследования", "access": ["Конструктор: Лабораторные исследования"], "module": None},
{"url": "/ui/construct/laboratory", "title": "Лабораторные исследования(н)", "access": ["Конструктор: Лабораторные исследования"], "module": None},
{
"url": "/ui/construct/descriptive",
"title": "Описательные протоколы и консультации",
Expand Down
142 changes: 142 additions & 0 deletions directory/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,121 @@ def get_site_type_id(self):
return -13
return self.site_type_id

@staticmethod
def as_json(research):
result = {
"pk": research.pk,
"title": research.title,
"hide": research.hide,
"order": research.sort_weight,
}
return result

@staticmethod
def get_tube_data(research_pk: int, need_fractions: bool = False) -> dict:
fractions = Fractions.objects.filter(research_id=research_pk).select_related('relation__tube').order_by('sort_weight')
research_tubes = {}
for fraction in fractions:
if research_tubes.get(fraction.relation_id) and need_fractions:
research_tubes[fraction.relation_id]["fractions"].append(fraction.as_json(fraction))
elif not research_tubes.get(fraction.relation_id):
research_tubes[fraction.relation_id] = {
"pk": fraction.relation_id,
"color": fraction.relation.tube.color,
"title": fraction.relation.tube.title,
}
if need_fractions:
research_tubes[fraction.relation_id]["fractions"] = [fraction.as_json(fraction)]
return research_tubes

@staticmethod
def get_laboratory_researches(podrazdelenie_id: int):
if podrazdelenie_id == -1:
podrazdeleniya = Podrazdeleniya.objects.filter(p_type=Podrazdeleniya.LABORATORY).values_list('pk', flat=True)
researches = Researches.objects.filter(podrazdeleniye_id__in=podrazdeleniya).order_by('pk')
else:
researches = Researches.objects.filter(podrazdeleniye_id=podrazdelenie_id).order_by('pk')
return researches

@staticmethod
def get_tubes(podrazdelenie_id: int):
tubes = {}
researches = Researches.get_laboratory_researches(podrazdelenie_id)
for research in researches:
research_tubes = Researches.get_tube_data(research.pk)
tubes_info = [value for _, value in research_tubes.items()]
tubes_keys = tuple(research_tubes.keys())
if tubes.get(tubes_keys):
tubes[tubes_keys]["researches"].append(research.as_json(research))
else:
tubes[tubes_keys] = {
"researches": [research.as_json(research)],
"tubes": tubes_info,
}

result = [{"researches": sorted(value["researches"], key=lambda d: d["order"]), "tubes": value["tubes"]} for _, value in tubes.items()]
return result

@staticmethod
def update_order(research_pk: int, research_nearby_pk: int, action: str):
research = Researches.objects.get(pk=research_pk)
research_nearby = Researches.objects.get(pk=research_nearby_pk)
if action == 'inc_order':
research.sort_weight += 1
research_nearby.sort_weight -= 1
elif action == 'dec_order':
research.sort_weight -= 1
research_nearby.sort_weight += 1
research.save()
research_nearby.save()
return True

@staticmethod
def change_visibility(research_pk: int):
research = Researches.objects.get(pk=research_pk)
if research.hide:
research.hide = False
else:
research.hide = True
research.save()
return True

@staticmethod
def get_research(research_pk: int):
research = Researches.objects.get(pk=research_pk)
research_tubes = Researches.get_tube_data(research_pk, True)
result = {
"pk": research.pk,
"title": research.title,
"shortTitle": research.short_title,
"code": research.code,
"internalCode": research.internal_code,
"ecpCode": research.ecp_id,
"preparation": research.preparation,
"tubes": [value for _, value in research_tubes.items()],
}
return result

@staticmethod
def update_lab_research(research_data):
research = Researches.objects.get(pk=research_data["pk"])
fractions = Fractions.objects.filter(research_id=research.pk)
for tube in research_data["tubes"]:
for fraction in tube["fractions"]:
current_fractions = fractions.get(pk=fraction["pk"])
current_fractions.title = fraction["title"]
current_fractions.ecp_id = fraction["ecpCode"]
current_fractions.fsli = fraction["fsli"]
current_fractions.save()
research.title = research_data["title"]
research.short_title = research_data["shortTitle"]
research.code = research_data["code"]
research.ecp_id = research_data["ecpCode"]
research.internal_code = research_data["internalCode"]
research.preparation = research_data["preparation"]
research.save()
return True


class HospitalService(models.Model):
TYPES = (
Expand Down Expand Up @@ -903,6 +1018,33 @@ def get_fsli_code(self):
def get_ecp_code(self):
return (self.ecp_id or '').strip()

@staticmethod
def as_json(fraction) -> dict:
result = {
"pk": fraction.pk,
"title": fraction.title,
"unit": fraction.unit,
"variants": fraction.variants.get_variants(),
"order": fraction.sort_weight,
"ecpCode": fraction.ecp_id,
"fsli": fraction.fsli,
}
return result

@staticmethod
def update_order(fraction_pk: int, fraction_nearby_pk: int, action: str):
fraction = Fractions.objects.get(pk=fraction_pk)
fraction_nearby = Fractions.objects.get(pk=fraction_nearby_pk)
if action == 'inc_order':
fraction.sort_weight += 1
fraction_nearby.sort_weight -= 1
elif action == 'dec_order':
fraction.sort_weight -= 1
fraction_nearby.sort_weight += 1
fraction.save()
fraction_nearby.save()
return True

def __str__(self):
return self.research.title + " | " + self.title

Expand Down
Loading

0 comments on commit ecf70a4

Please sign in to comment.