forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgets.py
More file actions
112 lines (92 loc) · 3.87 KB
/
widgets.py
File metadata and controls
112 lines (92 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
widgets for django-form-utils
parts of this code taken from http://www.djangosnippets.org/snippets/934/
- thanks baumer1122
"""
import os
import posixpath
from django import forms
from django.conf import settings
from django.utils.functional import curry
from django.utils.safestring import mark_safe
from django.core.files.uploadedfile import SimpleUploadedFile as UploadedFile
from form_utils.settings import JQUERY_URL, FORM_UTILS_MEDIA_URL
try:
from sorl.thumbnail.main import DjangoThumbnail
def thumbnail(image_path, width, height):
t = DjangoThumbnail(relative_source=image_path, requested_size=(width,height))
return u'<img src="%s" alt="%s" />' % (t.absolute_url, image_path)
except ImportError:
def thumbnail(image_path, width, height):
absolute_url = posixpath.join(settings.MEDIA_URL, image_path)
return u'<img src="%s" alt="%s" />' % (absolute_url, image_path)
class ImageWidget(forms.FileInput):
template = '%(input)s<br />%(image)s'
def __init__(self, attrs=None, template=None, width=200, height=200):
if template is not None:
self.template = template
self.width = width
self.height = height
super(ImageWidget, self).__init__(attrs)
def render(self, name, value, attrs=None):
input_html = super(forms.FileInput, self).render(name, value, attrs)
if hasattr(value, 'width') and hasattr(value, 'height'):
image_html = thumbnail(value.name, self.width, self.height)
output = self.template % {'input': input_html,
'image': image_html}
else:
output = input_html
return mark_safe(output)
class ClearableFileInput(forms.MultiWidget):
default_file_widget_class = forms.FileInput
template = '%(input)s Clear: %(checkbox)s'
def __init__(self, file_widget=None,
attrs=None, template=None):
if template is not None:
self.template = template
file_widget = file_widget or self.default_file_widget_class()
super(ClearableFileInput, self).__init__(
widgets=[file_widget, forms.CheckboxInput()],
attrs=attrs)
def render(self, name, value, attrs=None):
if isinstance(value, list):
self.value = value[0]
else:
self.value = value
return super(ClearableFileInput, self).render(name, value, attrs)
def decompress(self, value):
# the clear checkbox is never initially checked
return [value, None]
def format_output(self, rendered_widgets):
if self.value:
return self.template % {'input': rendered_widgets[0],
'checkbox': rendered_widgets[1]}
return rendered_widgets[0]
root = lambda path: posixpath.join(FORM_UTILS_MEDIA_URL, path)
class AutoResizeTextarea(forms.Textarea):
"""
A Textarea widget that automatically resizes to accomodate its contents.
"""
class Media:
js = (JQUERY_URL,
root('form_utils/js/jquery.autogrow.js'),
root('form_utils/js/autoresize.js'))
def __init__(self, *args, **kwargs):
attrs = kwargs.setdefault('attrs', {})
try:
attrs['class'] = "%s autoresize" % (attrs['class'],)
except KeyError:
attrs['class'] = 'autoresize'
attrs.setdefault('cols', 80)
attrs.setdefault('rows', 5)
super(AutoResizeTextarea, self).__init__(*args, **kwargs)
class InlineAutoResizeTextarea(AutoResizeTextarea):
def __init__(self, *args, **kwargs):
attrs = kwargs.setdefault('attrs', {})
try:
attrs['class'] = "%s inline" % (attrs['class'],)
except KeyError:
attrs['class'] = 'inline'
attrs.setdefault('cols', 40)
attrs.setdefault('rows', 2)
super(InlineAutoResizeTextarea, self).__init__(*args, **kwargs)