Skip to content

Commit fda3674

Browse files
authored
Merge pull request #68 from Jeschli/override-attrs
Make settings attributes overridable
2 parents 0f3ee79 + 8999140 commit fda3674

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

martor/widgets.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,32 @@
1414
class MartorWidget(forms.Textarea):
1515

1616
def render(self, name, value, attrs=None, renderer=None, **kwargs):
17-
if attrs is None:
18-
attrs = {}
19-
elif 'class' in attrs:
17+
# Make the settings the default attributes to pass
18+
attributes_to_pass = {
19+
'data-enable-configs': MARTOR_ENABLE_CONFIGS,
20+
'data-upload-url': MARTOR_UPLOAD_URL,
21+
'data-markdownfy-url': MARTOR_MARKDOWNIFY_URL,
22+
'data-search-users-url': MARTOR_SEARCH_USERS_URL,
23+
'data-base-emoji-url': MARTOR_MARKDOWN_BASE_EMOJI_URL
24+
}
25+
26+
# Make sure that the martor value is in the class attr passed in
27+
if 'class' in attrs:
2028
attrs['class'] += ' martor'
2129
else:
22-
attrs.update({'class': 'martor'})
30+
attrs['class'] = 'martor'
31+
32+
# Update and overwrite with the attributes passed in
33+
attributes_to_pass.update(attrs)
2334

24-
attrs['data-enable-configs'] = MARTOR_ENABLE_CONFIGS
25-
attrs['data-upload-url'] = MARTOR_UPLOAD_URL
26-
attrs['data-markdownfy-url'] = MARTOR_MARKDOWNIFY_URL
27-
attrs['data-search-users-url'] = MARTOR_SEARCH_USERS_URL
28-
attrs['data-base-emoji-url'] = MARTOR_MARKDOWN_BASE_EMOJI_URL
35+
# Update and overwrite with any attributes that are on the widget
36+
# itself. This is also the only way we can push something in without
37+
# being part of the render chain.
38+
attributes_to_pass.update(self.attrs)
2939

30-
widget = super(MartorWidget, self).render(name, value, attrs)
40+
widget = super(MartorWidget, self).render(name,
41+
value,
42+
attributes_to_pass)
3143

3244
template = get_template('martor/editor.html')
3345

0 commit comments

Comments
 (0)