2021-06-14 19:45:36 +00:00
|
|
|
# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay
|
2020-03-27 12:50:02 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
from json import dumps as json_dumps
|
|
|
|
|
2020-04-13 04:01:27 +00:00
|
|
|
from django.forms.widgets import DateTimeBaseInput, NumberInput, TextInput, Widget
|
2020-03-27 12:50:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AmountInput(NumberInput):
|
|
|
|
"""
|
|
|
|
This input type lets the user type amounts in euros, but forms receive data in cents
|
|
|
|
"""
|
|
|
|
template_name = "note/amount_input.html"
|
|
|
|
|
|
|
|
def format_value(self, value):
|
2020-04-05 07:09:21 +00:00
|
|
|
return None if value is None or value == "" else "{:.02f}".format(int(value) / 100, )
|
2020-03-27 12:50:02 +00:00
|
|
|
|
|
|
|
def value_from_datadict(self, data, files, name):
|
|
|
|
val = super().value_from_datadict(data, files, name)
|
|
|
|
return str(int(100 * float(val))) if val else val
|
|
|
|
|
|
|
|
|
2020-03-29 22:42:32 +00:00
|
|
|
class Autocomplete(TextInput):
|
2020-08-25 13:39:57 +00:00
|
|
|
template_name = "autocomplete_model.html"
|
2020-03-27 15:19:33 +00:00
|
|
|
|
2020-08-03 16:49:15 +00:00
|
|
|
def __init__(self, model, resetable=False, attrs=None):
|
2020-03-29 22:42:32 +00:00
|
|
|
super().__init__(attrs)
|
2020-03-27 15:19:33 +00:00
|
|
|
|
|
|
|
self.model = model
|
2020-08-03 16:49:15 +00:00
|
|
|
self.resetable = resetable
|
2020-03-27 15:19:33 +00:00
|
|
|
self.model_pk = None
|
|
|
|
|
|
|
|
class Media:
|
|
|
|
"""JS/CSS resources needed to render the date-picker calendar."""
|
|
|
|
|
|
|
|
js = ('js/autocomplete_model.js', )
|
|
|
|
|
2020-08-03 16:49:15 +00:00
|
|
|
def get_context(self, name, value, attrs):
|
|
|
|
context = super().get_context(name, value, attrs)
|
|
|
|
context['widget']['resetable'] = self.resetable
|
|
|
|
return context
|
|
|
|
|
2020-03-27 15:19:33 +00:00
|
|
|
def format_value(self, value):
|
|
|
|
if value:
|
|
|
|
self.attrs["model_pk"] = int(value)
|
|
|
|
return str(self.model.objects.get(pk=int(value)))
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
2020-04-13 04:01:27 +00:00
|
|
|
class ColorWidget(Widget):
|
|
|
|
"""
|
|
|
|
Pulled from django-colorfield.
|
|
|
|
Select a color.
|
|
|
|
"""
|
|
|
|
template_name = 'colorfield/color.html'
|
|
|
|
|
|
|
|
class Media:
|
|
|
|
js = [
|
|
|
|
'colorfield/jscolor/jscolor.min.js',
|
|
|
|
'colorfield/colorfield.js',
|
|
|
|
]
|
|
|
|
|
|
|
|
def format_value(self, value):
|
|
|
|
if value is None:
|
|
|
|
value = 0xFFFFFF
|
|
|
|
return "#{:06X}".format(value)
|
|
|
|
|
|
|
|
def value_from_datadict(self, data, files, name):
|
|
|
|
val = super().value_from_datadict(data, files, name)
|
|
|
|
return int(val[1:], 16)
|