Created
March 16, 2020 15:36
-
-
Save Ingco/28230b4c90b47750d6f933fd86153f02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def when_conditions(when_year, when_month, then_val: Any = 1): | |
return When( | |
Q( | |
created_at__month=when_month, | |
created_at__year=when_year, | |
finish_at__isnull=True, | |
) | |
| Q( | |
finish_at__month=when_month, | |
finish_at__year=when_year, | |
), | |
then=then_val, | |
) | |
def get_jobs_with_prefetch(self, year: int, month: int): | |
"""TODO Main""" | |
return ( | |
apps.get_model("production.ProductionOrderJob") | |
.objects_clear | |
.annotate( | |
is_created=Case( | |
when_conditions(year, month, True), | |
default=False, | |
output_field=BooleanField() | |
), | |
is_finished=Case( | |
when_conditions(year, month, True), | |
default=False, | |
output_field=BooleanField() | |
), | |
step_technician=F("production_order__step__technic"), | |
last_created_at=Max( | |
Case( | |
when_conditions(year, month, F("created_at")), | |
output_field=DateTimeField(), | |
) | |
), | |
title=F("production_job__title"), | |
price=F("production_job__price"), | |
) | |
.filter(Q(is_created=True) | Q(is_finished=True)) | |
.values( | |
"pk", | |
"title", | |
"is_created", | |
"is_finished", | |
"step_technician", | |
"technician", | |
"created_at", | |
"last_created_at", | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment