Skip to content

Commit

Permalink
Remove copy; restore default as_widget for field
Browse files Browse the repository at this point in the history
  • Loading branch information
slav0nic committed Jun 29, 2015
1 parent cca50c1 commit 56e0072
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions widget_tweaks/templatetags/widget_tweaks.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import re
import sys
import copy
import types
from django.template import Library, Node, Variable, TemplateSyntaxError
register = Library()


PY26 = sys.version_info[:2] == (2, 6)


def silence_without_field(fn):
def wrapped(field, attr):
if not field:
Expand All @@ -23,26 +19,17 @@ def _process_field_attributes(field, attr, process):
attribute = params[0]
value = params[1] if len(params) == 2 else ''

if not PY26:
# See https://bugs.python.org/issue1515.
# It is OK to not do it in Python 2.6 because
# Django versions which require deepcopy here (1.7+)
# don't support Python 2.6 anyways.
field = copy.deepcopy(field)

# decorate field.as_widget method with updated attributes
old_as_widget = field.as_widget

def as_widget(self, widget=None, attrs=None, only_initial=False):
attrs = attrs or {}
process(widget or self.field.widget, attrs, attribute, value)
return old_as_widget(widget, attrs, only_initial)
html = old_as_widget(widget, attrs, only_initial)
self.as_widget = old_as_widget
return html

bound_method = type(old_as_widget)
try:
field.as_widget = bound_method(as_widget, field, field.__class__)
except TypeError: # python 3
field.as_widget = bound_method(as_widget, field)
field.as_widget = types.MethodType(as_widget, field)
return field


Expand Down

0 comments on commit 56e0072

Please sign in to comment.