1
1
from datetime import datetime
2
2
from functools import partial
3
3
from django .core .paginator import Paginator , InvalidPage
4
- from django .db .models import Q , Max
5
- from django .db .models .expressions import RawSQL
4
+ from django .db .models import Q , Max , Min , Case , When , F
5
+ from django .db .models .functions import Coalesce
6
+ # from django.db.models.expressions import RawSQL
6
7
from django .http import Http404
7
8
from django .shortcuts import render
8
9
from .models import Production , Part , ProductionCompany
9
10
from plays .models import Play
10
11
from places .models import Place
11
12
from people .models import Person
12
13
from aggregates import Concatenate
14
+ from fields import ApproximateDateField
13
15
14
16
15
17
# object is Place, Person, Play, or ProductionCompany
@@ -33,10 +35,13 @@ def productions_filter(object, type, date_filter):
33
35
else :
34
36
raise Exception ('Strange call to productions_filter' )
35
37
38
+ end_date_field = f"{ annotate_extra } end_date"
39
+ press_date_field = f"{ annotate_extra } press_date"
40
+ start_date_field = f"{ annotate_extra } start_date"
36
41
o = o .annotate (
37
- max_end_date = Max (annotate_extra + 'end_date' ),
38
- max_press_date = Max (annotate_extra + 'press_date' ),
39
- max_start_date = Max (annotate_extra + 'start_date' )
42
+ max_end_date = Max (end_date_field ),
43
+ max_press_date = Max (press_date_field ),
44
+ max_start_date = Max (start_date_field )
40
45
)
41
46
filter = (~ Q (max_end_date = '' ) & Q (max_end_date__lt = now )) | Q (max_end_date = '' , max_press_date__lt = now ) | Q (max_end_date = '' , max_press_date__isnull = True , max_start_date__lt = now )
42
47
if filter_extra :
@@ -54,9 +59,10 @@ def productions_filter(object, type, date_filter):
54
59
o = o .annotate (part__role__concatenate = Concatenate ('part__role' , distinct = True ))
55
60
56
61
if date_filter == 'past' :
57
- return o .annotate (best_date = RawSQL ('MIN(IFNULL(productions_place.press_date, IF(productions_place.end_date!="", productions_place.end_date, productions_place.start_date)))' , ())).order_by ('-best_date' )
62
+ return o .annotate (best_date = Min (Coalesce (press_date_field , Case (When (** {end_date_field : "" }, then = F (start_date_field )), default = F (end_date_field )), output_field = ApproximateDateField ()))).order_by ('-best_date' )
63
+ # return o.annotate(best_date=Min(RawSQL('IFNULL(productions_place.press_date, IF(productions_place.end_date!="", productions_place.end_date, productions_place.start_date))', ()))).order_by('-best_date')
58
64
else :
59
- return o .order_by (annotate_extra + 'start_date' , annotate_extra + 'press_date' )
65
+ return o .order_by (start_date_field , press_date_field )
60
66
61
67
62
68
def _prods_one_way (fltr , object , sort_order ):
0 commit comments