Search code examples
pythondjangovalidationfilteringdjango-q

How to get the second to last most recent timestamped record in Django/Python?


I have a form from a model that keeps track of enter/leave times. I am trying to add constraints to make the data more accurate. Currently, when someone "Enters", it creates a record, saves the time in the "time_in" DateTimeField and then redirects. If the person then tries to enter again, it creates a new record with a new timestamp.

What I'm trying to add now is that, if the person has a previous entry without an exit timestamp (time_out) then that record (which would be the second to last most recent entry), would be flagged by updating the time_exceptions field to 'N'.

Currently, it changes all the fields to 'N', regardless of whether there's an exit or not, as shown below.

NOTE I reduced the amount of code in form_valid, hence the leave area part is not there. It was just a lot of filtering based on other fields and it didn't seem too relevant.

views.py

class EnterExitArea(CreateView):
    model = EmployeeWorkAreaLog
    template_name = "operations/enter_exit_area.html"
    form_class = WarehouseForm

    def form_valid(self, form):
        emp_num = form.cleaned_data['employee_number']
        area = form.cleaned_data['work_area']
        station = form.cleaned_data['station_number']

        if 'enter_area' in self.request.POST:
            form.save()
            EmployeeWorkAreaLog.objects.filter((Q(employee_number=emp_num) & Q(work_area=area) & Q(time_out__isnull=True) & Q(time_in__isnull=True)) & (Q(station_number=station) | Q(station_number__isnull=True))).update(time_in=datetime.now())

            if EmployeeWorkAreaLog.objects.filter(Q(employee_number=emp_num)).count() > 1:
                EmployeeWorkAreaLog.objects.filter((Q(employee_number=emp_num) & Q(work_area=area) & Q(time_out__isnull=True)) & (Q(station_number=station) | Q(station_number__isnull=True))).update(time_exceptions='N')

            return HttpResponseRedirect(self.request.path_info)

I tried the following, but I get a expected string or bytes-like object and while it still creates a new record before crashing, it does not update the time_exceptions of the second to most recent to N.

        if 'enter_area' in self.request.POST:
            form.save()
            EmployeeWorkAreaLog.objects.filter((Q(employee_number=emp_num) & Q(work_area=area) & Q(time_out__isnull=True) & Q(time_in__isnull=True)) & (Q(station_number=station) | Q(station_number__isnull=True))).update(time_in=datetime.now())

            if EmployeeWorkAreaLog.objects.filter(Q(employee_number=emp_num) & Q(time_out__isnull=True) & Q(time_exceptions="")).count() > 1:
                recent = EmployeeWorkAreaLog.objects.filter(employee_number=emp_num, work_area=area, station_number=station, time_out__isnull=True).order_by('-time_in')[1]

                EmployeeWorkAreaLog.objects.filter((Q(employee_number=emp_num) & Q(work_area=area) & Q(time_in=recent) & Q(time_out__isnull=True)) & (Q(station_number=station) | Q(station_number__isnull=True))).update(time_exceptions='N')

            return HttpResponseRedirect(self.request.path_info)

models.py

class EmployeeWorkAreaLog(TimeStampedModel, SoftDeleteModel, models.Model):
    employee_number = models.ForeignKey(Salesman, on_delete=models.SET_NULL, help_text="Employee #", null=True, blank=False)
    work_area = models.ForeignKey(WorkArea, on_delete=models.SET_NULL, null=True, blank=False, help_text="Work Area", related_name="work_area")
    station_number = models.ForeignKey(StationNumber, on_delete=models.SET_NULL, 
    time_exceptions = models.CharField(max_length=2, blank=True)
    time_in = models.DateTimeField(help_text="Time in", null=True, blank=True)
    time_out = models.DateTimeField(blank=True, help_text="Time out", null=True)

    def __str__(self):
        return self.employee_number

traceback:

Internal Server Error: /operations/enter-exit-area/
Traceback (most recent call last):
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\views\generic\edit.py", line 172, in post
    return super().post(request, *args, **kwargs)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\views\generic\edit.py", line 142, in post
    return self.form_valid(form)
  File "C:\Users\mkusneco\apps.rsrgroup.com\apps\operations\views.py", line 45, in form_valid
    Q(station_number=station) | Q(station_number__isnull=True))).update(time_exceptions='N')
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\db\models\query.py", line 844, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\db\models\query.py", line 862, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\db\models\sql\query.py", line 1263, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\db\models\sql\query.py", line 1281, in _add_q
    current_negated, allow_joins, split_subq)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\db\models\sql\query.py", line 1287, in _add_q
    split_subq=split_subq,
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\db\models\sql\query.py", line 1225, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\db\models\sql\query.py", line 1096, in build_lookup
    lookup = lookup_class(lhs, rhs)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\db\models\lookups.py", line 20, in __init__
    self.rhs = self.get_prep_lookup()
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\db\models\lookups.py", line 70, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\db\models\fields\__init__.py", line 1408, in get_prep_value
    value = super().get_prep_value(value)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\db\models\fields\__init__.py", line 1268, in get_prep_value
    return self.to_python(value)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\db\models\fields\__init__.py", line 1369, in to_python
    parsed = parse_datetime(value)
  File "C:\Users\mkusneco\appsve\lib\site-packages\django\utils\dateparse.py", line 106, in parse_datetime
    match = datetime_re.match(value)
TypeError: expected string or bytes-like object

Solution

  • You assign a EmployeeWorkAreaLog instance to recent, however you use it in condition as Q(time_in=recent), which is clearly wrong, since the condition needs to contain a simple value / datetime / string, not an object.

    So you could fix it by using Q(time_in=recent.time_in), but that's not very robust, if the user had some sort of duplicate entries. It would be better to use instance's pk, so instead of time_in condition, you'd use Q(pk=recent.pk). But that's still not the best solution - what if there is more than one such entry? Why update only the second one and not the third, etc?

    So I think instead of selecting the second entry (using [1]), you should select the first one, and exclude this one from the update queryset, e.g.:

    first = EmployeeWorkAreaLog.objects.filter(employee_number=emp_num, work_area=area, station_number=station, time_out__isnull=True).order_by('-time_in').first()
    EmployeeWorkAreaLog.objects.filter((Q(employee_number=emp_num) & Q(work_area=area) & Q(time_out__isnull=True)) & (Q(station_number=station) | Q(station_number__isnull=True))).exclude(pk=first.pk).update(time_exceptions='N')