Skip to content
Snippets Groups Projects
Commit 02fa66a7 authored by Carlos Vega's avatar Carlos Vega
Browse files

cleaned a bit the code

parent e98ed3d1
No related branches found
No related tags found
1 merge request!194Issue #195 added tests and changed the post_save function for visit. I didn't…
Pipeline #7571 passed
...@@ -85,30 +85,16 @@ class Visit(models.Model): ...@@ -85,30 +85,16 @@ class Visit(models.Model):
@receiver(post_save, sender=Visit) @receiver(post_save, sender=Visit)
def check_visit_number(sender, instance, **kwargs): def check_visit_number(sender, instance, **kwargs):
# no other solution to ensure the visit_number is in cronological order than to sort the whole list # no other solution to ensure the visit_number is in cronological order than to sort the whole list if there are future visits
visit = instance visit = instance
if visit.subject is not None: #not sure if select_for_update has an effect if visit.subject is not None: #not sure if select_for_update has an effect, the tests work as well without it
visits_before = Visit.objects.select_for_update().filter(subject=visit.subject).filter(datetime_begin__lt=visit.datetime_begin).count() visits_before = Visit.objects.select_for_update().filter(subject=visit.subject).filter(datetime_begin__lt=visit.datetime_begin).count()
# we need to sort the future visits respect to this one, if any # we need to sort the future visits respect to this one, if any
visits = Visit.objects.select_for_update().filter(subject=visit.subject).filter(datetime_begin__gte=visit.datetime_begin).order_by('datetime_begin','datetime_end') visits = Visit.objects.select_for_update().filter(subject=visit.subject).filter(datetime_begin__gte=visit.datetime_begin).order_by('datetime_begin','datetime_end')
with transaction.atomic(): #not sure if has an effect with transaction.atomic(): #not sure if it has an effect, the tests work as well without it
for i, v in enumerate(visits): for i, v in enumerate(visits):
if v.visit_number != (visits_before + i + 1): if v.visit_number != (visits_before + i + 1):
visit_number = (visits_before + i + 1) visit_number = (visits_before + i + 1)
Visit.objects.filter(id=v.id).update(visit_number=visit_number) # does not rise post_save, we avoid recursion Visit.objects.filter(id=v.id).update(visit_number=visit_number) # does not rise post_save, we avoid recursion
if v.id == visit.id: if v.id == visit.id:
visit.visit_number = v.visit_number visit.visit_number = v.visit_number
# @receiver(post_save, sender=Visit)
# def update_visit_number(sender, instance, **kwargs):
# n = kwargs.get('n', 1)
# visit = instance
# if visit.subject is not None:
# count = Visit.objects.filter(subject=visit.subject).filter(datetime_begin__lte=visit.datetime_begin).count()
# if count != visit.visit_number:
# visit.visit_number = count
# visit.save()
# subject_visits = Visit.objects.filter(subject=visit.subject).all()
# for subject_visit in subject_visits:
# update_visit_number(sender, subject_visit, n=(n+1))
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment