Skip to content

Commit

Permalink
#6: Make the user specify a unique name.
Browse files Browse the repository at this point in the history
If you in your flexselect widget sets a ``unique_name`` property it bypasses the faulty mechanism of magically creating a safe hashed name. Example:

    MyFlexSelectWidget(FlexSelectWidget):
        unique_name = 'my_flexselect_widget' # A name that is allowed to be shown in the HTML.
        ... etc.
  • Loading branch information
runekaagaard committed Sep 17, 2015
1 parent 8c6ca46 commit 2d6325b
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions flexselect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def instance_from_request(request, widget):

class FlexSelectWidget(Select):
instances = {}
unique_name = None

""" Instances of widgets with their hashed names as keys."""

class Media:
Expand All @@ -109,12 +111,15 @@ def _hashed_name(self):
Each widget will be unique by the name of the field and the class name
of the model admin.
"""
salted_string = "".join([
settings.SECRET_KEY,
self.base_field.name,
self.modeladmin.__class__.__name__,
])
return "_%s" % hashlib.sha1(salted_string).hexdigest()
if self.unique_name is not None:
return self.unique_name
else:
salted_string = "".join([
settings.SECRET_KEY,
self.base_field.name,
self.modeladmin.__class__.__name__,
])
return "_%s" % hashlib.sha1(salted_string).hexdigest()

def _get_instance(self):
"""
Expand Down

0 comments on commit 2d6325b

Please sign in to comment.