Skip to content

Commit 8e8b19e

Browse files
committed
Refactored names to make better sense.
1 parent 3e7fbc2 commit 8e8b19e

File tree

4 files changed

+35
-34
lines changed

4 files changed

+35
-34
lines changed

flexselect/__init__.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ def choices_from_queryset(queryset):
3232
def choices_from_instance(instance, widget):
3333
"""
3434
Builds choices from a model instance using the widgets queryset() method.
35-
If any of the widgets trigger fields is not defined on the instance or the
35+
If any of the widgets trigger_field fields is not defined on the instance or the
3636
instance itself is None, None is returned.
3737
3838
instance: An instance of the model used on the current admin page.
3939
widget: A widget instance given to the FlexSelectWidget.
4040
"""
4141
try:
42-
for trigger in widget.triggers:
43-
getattr(instance, trigger)
42+
for trigger_field in widget.trigger_fields:
43+
getattr(instance, trigger_field)
4444
except ObjectDoesNotExist:
4545
return [('', widget.empty_choices_text(instance))]
4646

@@ -49,16 +49,16 @@ def choices_from_instance(instance, widget):
4949
def details_from_instance(instance, widget):
5050
"""
5151
Builds html from a model instance using the widgets details() method. If
52-
any of the widgets trigger fields is not defined on the instance or the
52+
any of the widgets trigger_field fields is not defined on the instance or the
5353
instance itself is None, None is returned.
5454
5555
instance: An instance of the model used on the current admin page.
5656
widget: A widget instance given to the FlexSelectWidget.
5757
"""
5858
try:
59-
for trigger in widget.triggers:
60-
getattr(instance, trigger)
61-
related_instance = getattr(instance, widget.db_field.name)
59+
for trigger_field in widget.trigger_fields:
60+
getattr(instance, trigger_field)
61+
related_instance = getattr(instance, widget.base_field.name)
6262
except ObjectDoesNotExist:
6363
return u''
6464
return widget.details(related_instance, instance)
@@ -70,15 +70,15 @@ def instance_from_request(request, widget):
7070
"""
7171
items = dict(request.POST.items())
7272
values = {}
73-
for f in widget.db_field.model._meta.fields:
73+
for f in widget.base_field.model._meta.fields:
7474
if f.name in items:
7575
try:
7676
value = f.formfield().to_python(items[f.name])
7777
if value is not None:
7878
values[f.name] = value
7979
except ValidationError:
8080
pass
81-
return widget.db_field.model(**values)
81+
return widget.base_field.model(**values)
8282

8383
class FlexSelectWidget(Select):
8484
"""
@@ -93,7 +93,7 @@ class FlexSelectWidget(Select):
9393
# First a widget class for the field that should update when other
9494
# fields change must be defined.
9595
class CustomerContactRenderer(object):
96-
triggers = ['client']
96+
trigger_fields = ['client']
9797
empty_choices_text = 'Please update the client field first'
9898
9999
def details(self, instance):
@@ -105,16 +105,16 @@ def queryset(self, instance):
105105
106106
# Then the formfield_for_foreignkey() method of the ModelAdmin must be
107107
# overwritten.
108-
def formfield_for_foreignkey(self, db_field, request, **kwargs):
109-
if db_field.name == "customer_contact":
108+
def formfield_for_foreignkey(self, base_field, request, **kwargs):
109+
if base_field.name == "customer_contact":
110110
kwargs['widget'] = FlexSelectWidget(
111111
# An instance of the widget class defined above.
112112
widget=CustomerContactRenderer()
113-
db_field=db_field,
113+
base_field=base_field,
114114
modeladmin=self,
115115
request=request,
116116
)
117-
return super(CaseAdmin, self).formfield_for_foreignkey(db_field,
117+
return super(CaseAdmin, self).formfield_for_foreignkey(base_field,
118118
request, **kwargs)
119119
"""
120120
instances = {}
@@ -131,7 +131,7 @@ class Media:
131131
def __init__(self, db_field, modeladmin, request, *args,
132132
**kwargs):
133133

134-
self.db_field = db_field
134+
self.base_field = db_field
135135
self.modeladmin = modeladmin
136136
self.request = request
137137

@@ -146,7 +146,7 @@ def _hashed_name(self):
146146
"""
147147
salted_string = "".join([
148148
settings.SECRET_KEY,
149-
self.db_field.name,
149+
self.base_field.name,
150150
self.modeladmin.__class__.__name__,
151151
])
152152
return hashlib.sha1(salted_string).hexdigest()
@@ -167,21 +167,21 @@ def _get_instance(self):
167167

168168
def _build_js(self):
169169
"""
170-
Adds the widgets hashed_name as the key with an array of its triggers
170+
Adds the widgets hashed_name as the key with an array of its trigger_fields
171171
as the value to flexselect.selects.
172172
"""
173173
return """
174174
<script>
175175
var flexselect = flexselect || {};
176-
flexselect.triggers = flexselect.triggers || {};
177-
flexselect.triggers.%(field)s = flexselect.triggers.%(field)s ||
176+
flexselect.trigger_fields = flexselect.trigger_fields || {};
177+
flexselect.trigger_fields.%(field)s = flexselect.trigger_fields.%(field)s ||
178178
[];
179-
flexselect.triggers.%(field)s.push(%(triggers)s);
179+
flexselect.trigger_fields.%(field)s.push(%(trigger_fields)s);
180180
</script>""" % {
181-
'field': self.db_field.name,
182-
'triggers':
181+
'field': self.base_field.name,
182+
'trigger_fields':
183183
",".join('["%s", "%s"]' % (t, self.hashed_name)
184-
for t in self.triggers),
184+
for t in self.trigger_fields),
185185
};
186186

187187

flexselect/static/flexselect/js/flexselect.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
var flexselect = flexselect || {};
55

66
/**
7-
* Binds the flexselect triggers.
7+
* Binds the flexselect trigger_fields.
88
*/
99
flexselect.bind_events = function() {
10-
var triggers = that.flexselect.triggers;
11-
for (base_field in triggers) {
12-
flexselect.bind_base_field(base_field, triggers[base_field][0][1]);
13-
for (j in triggers[base_field])
10+
var trigger_fields = that.flexselect.trigger_fields;
11+
for (base_field in trigger_fields) {
12+
flexselect.bind_base_field(base_field, trigger_fields[base_field][0][1]);
13+
for (j in trigger_fields[base_field])
1414
flexselect.bind_trigger_field(
15-
triggers[base_field][j][0],
16-
triggers[base_field][j][1],
15+
trigger_fields[base_field][j][0],
16+
trigger_fields[base_field][j][1],
1717
base_field
1818
);
1919
}
@@ -31,7 +31,8 @@ flexselect.bind_base_field = function(base_field, hashed_name) {
3131
}
3232

3333
/**
34-
* Binds the change event of a field to the flexselect.trigger_field_changed function.
34+
* Binds the change event of a field to the flexselect.trigger_field_changed
35+
* function.
3536
*/
3637
flexselect.bind_trigger_field = function(trigger_field, hashed_name,
3738
base_field) {
@@ -86,7 +87,7 @@ flexselect.move_after_plussign = function() {
8687
};
8788

8889
/**
89-
* Overrides the original dismissAddAnotherPopup and triggers a change event
90+
* Overrides the original dismissAddAnotherPopup and trigger_fields a change event
9091
* on field after the popup has been added.
9192
*/
9293
var _dismissAddAnotherPopup = dismissAddAnotherPopup;

flexselect/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def wrapper(*args,**kwargs):
2525
@login_required
2626
def trigger_field_changed(request):
2727
"""
28-
Ajax callback called when on of the trigger fields is changed. Returns
28+
Ajax callback called when a trigger field or base field has changed. Returns
2929
html for new options and details for the dependent field as json.
3030
"""
3131
hashed_name = request.POST.__getitem__('hashed_name')

test_flexselect/tests/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __unicode__(self):
1818
return self.name
1919

2020
class CompanyContactPersonWidget(FlexSelectWidget):
21-
triggers = ['client']
21+
trigger_fields = ['client']
2222

2323
def details(self, related_instance, instance):
2424
return """\

0 commit comments

Comments
 (0)