Projet

Général

Profil

0001-misc-use-Django-native-split-datetime-field-45108.patch

Benjamin Dauvergne, 16 juillet 2020 14:45

Télécharger (5,4 ko)

Voir les différences:

Subject: [PATCH] misc: use Django native split datetime field (#45108)

 chrono/manager/forms.py                       | 16 ++++++----
 .../templates/chrono/splitdatetime.html       |  8 +++++
 .../templates/chrono/widget_datetime.html     |  4 ---
 chrono/manager/widgets.py                     | 30 ++++---------------
 4 files changed, 25 insertions(+), 33 deletions(-)
 create mode 100644 chrono/manager/templates/chrono/splitdatetime.html
 delete mode 100644 chrono/manager/templates/chrono/widget_datetime.html
chrono/manager/forms.py
42 42
)
43 43

  
44 44
from . import widgets
45
from .widgets import DateTimeWidget
45
from .widgets import SplitDateTimeField
46 46

  
47 47

  
48 48
class AgendaAddForm(forms.ModelForm):
......
87 87
        model = Event
88 88
        widgets = {
89 89
            'agenda': forms.HiddenInput(),
90
            'start_datetime': DateTimeWidget(),
91 90
            'publication_date': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
92 91
        }
93 92
        exclude = ['full', 'meeting_type', 'desk', 'slug', 'resources']
93
        field_classes = {
94
            'start_datetime': SplitDateTimeField,
95
        }
94 96

  
95 97

  
96 98
class EventForm(forms.ModelForm):
......
98 100
        model = Event
99 101
        widgets = {
100 102
            'agenda': forms.HiddenInput(),
101
            'start_datetime': DateTimeWidget(),
102 103
            'publication_date': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
103 104
        }
105
        field_classes = {
106
            'start_datetime': SplitDateTimeField,
107
        }
104 108
        exclude = ['full', 'meeting_type', 'desk', 'resources']
105 109

  
106 110

  
......
230 234
        fields = ['desk', 'start_datetime', 'end_datetime', 'label']
231 235
        widgets = {
232 236
            'desk': forms.HiddenInput(),
233
            'start_datetime': DateTimeWidget(),
234
            'end_datetime': DateTimeWidget(),
237
        }
238
        field_classes = {
239
            'start_datetime': SplitDateTimeField,
240
            'end_datetime': SplitDateTimeField,
235 241
        }
236 242

  
237 243
    def clean(self):
chrono/manager/templates/chrono/splitdatetime.html
1
<span class="datetime">
2
{% with widget=widget.subwidgets.0 %}
3
  <input type="date" name="{{ widget.name }}"{% if widget.value != None %} value="{{ widget.value.date }}"{% endif %}{% include "django/forms/widgets/attrs.html" %} />
4
{% endwith %}
5
{% with widget=widget.subwidgets.1 %}
6
    <input type="time" name="{{ widget.name }}" pattern="[0-9]{2}:[0-9]{2}" step="300" {% if widget.value != None %} value="{{ widget.value.time }}"{% endif %}{% include "django/forms/widgets/attrs.html" %} />
7
{% endwith %}
8
</span>
chrono/manager/templates/chrono/widget_datetime.html
1
<span class="datetime">
2
<input type="date" name="{{ widget.name }}$date" {% if widget.value.date != None %} value="{{ widget.value.date }}"{% endif %}{% include "django/forms/widgets/attrs.html" %} />
3
<input type="time" name="{{ widget.name }}$time" pattern="[0-9]{2}:[0-9]{2}" step="300" {% if widget.value.time != None %} value="{{ widget.value.time }}"{% endif %}{% include "django/forms/widgets/attrs.html" %} />
4
</span>
chrono/manager/widgets.py
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17 17

  
18
import datetime
19

  
20
from django.forms.widgets import DateTimeInput, TimeInput, SelectMultiple
21
from django.utils import dateparse
18
from django.forms.fields import SplitDateTimeField
19
from django.forms.widgets import TimeInput, SelectMultiple, SplitDateTimeWidget
22 20
from django.utils.safestring import mark_safe
23 21

  
24 22

  
25
class DateTimeWidget(DateTimeInput):
26
    template_name = 'chrono/widget_datetime.html'
23
class SplitDateTimeWidget(SplitDateTimeWidget):
24
    template_name = 'chrono/splitdatetime.html'
27 25

  
28
    def format_value(self, value):
29
        if value:
30
            x = {
31
                'date': value.date().strftime('%Y-%m-%d'),
32
                'time': value.time().strftime('%H:%M'),
33
            }
34
            return x
35
        return None
36 26

  
37
    def value_from_datadict(self, data, files, name):
38
        date_string = data.get(name + '$date')
39
        time_string = data.get(name + '$time')
40
        if not date_string or not time_string:
41
            return None
42
        date_value = dateparse.parse_date(date_string)
43
        time_value = dateparse.parse_time(time_string)
44
        if not date_value or not time_value:
45
            return None
46
        return datetime.datetime.combine(date_value, time_value)
27
class SplitDateTimeField(SplitDateTimeField):
28
    widget = SplitDateTimeWidget
47 29

  
48 30

  
49 31
class TimeWidget(TimeInput):
50
-