| 1 | from django.conf import settings |
| 2 | from django.forms import TextInput |
| 3 | from django.template.loader import render_to_string |
| 4 | |
| 5 | |
| 6 | class ColorWidget(TextInput): |
| 7 | template_name = "colorfield/color.html" |
| 8 | |
| 9 | class Media: |
| 10 | if settings.DEBUG: |
| 11 | js = [ |
| 12 | "colorfield/jscolor/jscolor.js", |
| 13 | "colorfield/colorfield.js", |
| 14 | ] |
| 15 | else: |
| 16 | js = [ |
| 17 | "colorfield/jscolor/jscolor.min.js", |
| 18 | "colorfield/colorfield.js", |
| 19 | ] |
| 20 | |
| 21 | def get_context(self, name, value, attrs=None): |
| 22 | context = {} |
| 23 | context.update(self.attrs.copy() or {}) |
| 24 | context.update(attrs or {}) |
| 25 | context.update( |
| 26 | { |
| 27 | "widget": self, |
| 28 | "name": name, |
| 29 | "value": value, |
| 30 | } |
| 31 | ) |
| 32 | if "format" not in context: |
| 33 | context.update({"format": "hex"}) |
| 34 | return context |
| 35 | |
| 36 | def render(self, name, value, attrs=None, renderer=None): |
| 37 | return render_to_string( |
| 38 | self.template_name, self.get_context(name, value, attrs) |
| 39 | ) |