11from django import forms
22from django .contrib .auth .models import User
3- from django .db .models import Q
43from django .forms import ValidationError
54from django .forms .widgets import HiddenInput
65from django .http import Http404
1110
1211
1312class CommitFestFilterForm (forms .Form ):
13+ selectize_fields = {
14+ "author" : "/lookups/user" ,
15+ "reviewer" : "/lookups/user" ,
16+ }
17+
1418 text = forms .CharField (max_length = 50 , required = False )
1519 status = forms .ChoiceField (required = False )
1620 targetversion = forms .ChoiceField (required = False )
17- author = forms .ChoiceField (required = False )
18- reviewer = forms .ChoiceField (required = False )
21+ author = forms .ChoiceField (required = False , label = "Author (type to search)" )
22+ reviewer = forms .ChoiceField (required = False , label = "Reviewer (type to search)" )
1923 sortkey = forms .IntegerField (required = False )
2024
21- def __init__ (self , cf , * args , ** kwargs ):
22- super (CommitFestFilterForm , self ).__init__ (* args , ** kwargs )
25+ def __init__ (self , data , * args , ** kwargs ):
26+ super (CommitFestFilterForm , self ).__init__ (data , * args , ** kwargs )
2327
2428 self .fields ["sortkey" ].widget = forms .HiddenInput ()
2529
2630 c = [(- 1 , "* All" )] + list (PatchOnCommitFest ._STATUS_CHOICES )
2731 self .fields ["status" ] = forms .ChoiceField (choices = c , required = False )
2832
29- if cf :
30- q = Q (patch_author__commitfests = cf ) | Q (patch_reviewer__commitfests = cf )
31- else :
32- q = Q ()
33- userchoices = [(- 1 , "* All" ), (- 2 , "* None" ), (- 3 , "* Yourself" )] + [
34- (u .id , "%s %s (%s)" % (u .first_name , u .last_name , u .username ))
35- for u in User .objects .filter (q )
36- .distinct ()
37- .order_by ("first_name" , "last_name" )
38- ]
33+ userchoices = [(- 1 , "* All" ), (- 2 , "* None" ), (- 3 , "* Yourself" )]
34+
35+ print (data )
36+ selected_user_ids = set ()
37+ if data and "author" in data :
38+ try :
39+ selected_user_ids .add (int (data ["author" ]))
40+ except ValueError :
41+ pass
42+
43+ if data and "reviewer" in data :
44+ try :
45+ selected_user_ids .add (int (data ["reviewer" ]))
46+ except ValueError :
47+ pass
48+
49+ if selected_user_ids :
50+ userchoices .extend (
51+ (u .id , f"{ u .first_name } { u .last_name } ({ u .username } )" )
52+ for u in User .objects .filter (pk__in = selected_user_ids )
53+ )
54+ print (userchoices )
55+
3956 self .fields ["targetversion" ] = forms .ChoiceField (
4057 choices = [("-1" , "* All" ), ("-2" , "* None" )]
4158 + [(v .id , v .version ) for v in TargetVersion .objects .all ()],
4259 required = False ,
4360 label = "Target version" ,
4461 )
45- self .fields ["author" ] = forms . ChoiceField ( choices = userchoices , required = False )
46- self .fields ["reviewer" ] = forms . ChoiceField ( choices = userchoices , required = False )
62+ self .fields ["author" ]. choices = userchoices
63+ self .fields ["reviewer" ]. choices = userchoices
4764
4865 for f in (
4966 "status" ,
@@ -54,7 +71,7 @@ def __init__(self, cf, *args, **kwargs):
5471
5572
5673class PatchForm (forms .ModelForm ):
57- selectize_multiple_fields = {
74+ selectize_fields = {
5875 "authors" : "/lookups/user" ,
5976 "reviewers" : "/lookups/user" ,
6077 }
@@ -80,7 +97,7 @@ def __init__(self, *args, **kwargs):
8097 )
8198
8299 # Selectize multiple fields -- don't pre-populate everything
83- for field , url in list (self .selectize_multiple_fields .items ()):
100+ for field , url in list (self .selectize_fields .items ()):
84101 # If this is a postback of a selectize field, it may contain ids that are not currently
85102 # stored in the field. They must still be among the *allowed* values of course, which
86103 # are handled by the existing queryset on the field.
@@ -97,8 +114,8 @@ def __init__(self, *args, **kwargs):
97114 self .fields [field ].queryset = self .fields [field ].queryset .filter (
98115 pk__in = set (vals )
99116 )
100- self .fields [field ].label_from_instance = lambda u : "{} ({})" . format (
101- u . username , u .get_full_name ()
117+ self .fields [field ].label_from_instance = (
118+ lambda u : f" { u .get_full_name ()} ( { u . username } )"
102119 )
103120
104121 # Only allow modifying reviewers and committers if the patch is closed.
0 commit comments