• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

monim67 / django-bootstrap-datepicker-plus / 7860847020

11 Feb 2024 08:43AM UTC coverage: 100.0%. Remained the same
7860847020

push

github

monim67
Fix clear input on form reset, fix #110

142 of 142 relevant lines covered (100.0%)

5.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/bootstrap_datepicker_plus/_base.py
1
"""Contains Base Date-Picker input class for widgets of this package."""
5✔
2

3
import warnings
5✔
4
from typing import Any, Dict, Optional, Tuple
5✔
5

6
from django import forms
5✔
7
from django.forms.widgets import DateTimeBaseInput
5✔
8
from typing_extensions import deprecated
5✔
9

10
from ._config import WidgetConfig
5✔
11
from .schemas import InputAttrs, WidgetOptions, WidgetVariant
5✔
12
from .settings import get_widget_settings
5✔
13

14

15
class BasePickerInput(DateTimeBaseInput):
5✔
16
    """Base Date-Picker input class for widgets of this package."""
5✔
17

18
    variant = WidgetVariant.date
5✔
19
    _date_format = "%Y-%m-%d"
5✔
20
    backend_date_format = "YYYY-MM-DD"
5✔
21
    options: Optional[WidgetOptions] = None
5✔
22
    template_name = "bootstrap_datepicker_plus/input.html"
5✔
23

24
    def __init__(
5✔
25
        self,
26
        attrs: Optional[InputAttrs] = None,
27
        format: Optional[str] = None,
28
        options: Optional[WidgetOptions] = None,
29
        range_from: Optional[str] = None,
30
    ):
31
        """Date-picker input widget.
32

33
        Args:
34
            attrs: HTML attributes of rendered HTMLInputElement
35
            format: Deprecated and not in use anymore
36
            options: Options to customize the widget, see README
37
            range_from: Name of input element to link with for range selection
38
        """
39
        if format is not None:
5✔
40
            warnings.warn(
5✔
41
                "The 'format' parameter is ignored, set 'format' in options instead. "
42
                "see https://github.com/monim67/django-bootstrap-datepicker-plus",
43
                category=FutureWarning,
44
            )
45
        settings = get_widget_settings()
5✔
46
        self.template_name = settings.template_name or self.template_name
5✔
47
        self.config = WidgetConfig(
5✔
48
            variant=self.variant,
49
            backend_date_format=self.backend_date_format,
50
            range_from=range_from,
51
        )
52
        self.config.update_options(
5✔
53
            settings.options,
54
            settings.variant_options.get(self.variant),
55
            self.options,
56
            options,
57
        )
58
        super().__init__(attrs, self._date_format)
5✔
59

60
    def build_attrs(
5✔
61
        self, base_attrs: InputAttrs, extra_attrs: Optional[InputAttrs] = None
62
    ) -> InputAttrs:
63
        """Build an attribute dictionary."""
64
        settings = get_widget_settings()
5✔
65
        attrs = {
5✔
66
            **settings.attrs,
67
            **base_attrs,
68
            **(extra_attrs or {}),
69
            "data-dbdp-config": self.config.to_attr_value(),
70
        }
71
        if settings.debug:
5✔
72
            attrs["data-dbdp-debug"] = ""
5✔
73
        return attrs
5✔
74

75
    def get_context(
5✔
76
        self, name: str, value: Any, attrs: Optional[InputAttrs]
77
    ) -> Dict[str, Any]:
78
        """Return widget context dictionary."""
79
        settings = get_widget_settings()
5✔
80
        context = super().get_context(name, value, attrs)
5✔
81
        context["addon_icon_class"] = settings.addon_icon_classes[self.variant]
5✔
82
        return context
5✔
83

84
    @deprecated(
5✔
85
        "Use 'range_from' instead. See https://github.com/monim67/django-bootstrap-datepicker-plus"
86
    )
87
    def start_of(self, event_id: str) -> "BasePickerInput":
5✔
88
        """Set Date-Picker as the start-date of a date-range (Deprecated!!!)."""
89
        warnings.warn(
5✔
90
            "The 'start_of' method is deprecated, use 'range_from' instead. "
91
            "see https://github.com/monim67/django-bootstrap-datepicker-plus",
92
            category=FutureWarning,
93
        )
94
        self.attrs["data-dbdp-start"] = event_id
5✔
95
        return self
5✔
96

97
    @deprecated(
5✔
98
        "Use 'range_from' instead. See https://github.com/monim67/django-bootstrap-datepicker-plus"
99
    )
100
    def end_of(self, event_id: str, import_options: bool = True) -> "BasePickerInput":
5✔
101
        """Set Date-Picker as the end-date of a date-range (Deprecated!!!)."""
102
        warnings.warn(
5✔
103
            "The 'end_of' method is deprecated, use 'range_from' instead. "
104
            "see https://github.com/monim67/django-bootstrap-datepicker-plus",
105
            category=FutureWarning,
106
        )
107
        self.attrs["data-dbdp-end"] = event_id
5✔
108
        return self
5✔
109

110
    @property
5✔
111
    def media(self) -> forms.Media:  # type: ignore
5✔
112
        """Generate widget Media."""
113
        settings = get_widget_settings()
5✔
114
        return forms.Media(
5✔
115
            css={
116
                "all": tuple_exclude_none(
117
                    settings.bootstrap_icon_css_url,
118
                    settings.datetimepicker_css_url,
119
                    settings.app_static_url + "css/datepicker-widget.css",
120
                ),
121
            },
122
            js=tuple_exclude_none(
123
                settings.momentjs_url,
124
                settings.datetimepicker_js_url,
125
                settings.app_static_url + "js/datepicker-widget.js",
126
            ),
127
        )
128

129

130
def tuple_exclude_none(*items: Optional[str]) -> Tuple[str, ...]:
5✔
131
    """Create a tuple removing None values."""
132
    return tuple(item for item in items if item is not None)
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc