Skip to content

Commit 362ec63

Browse files
committed
Proper docstrings added
1 parent 9053e80 commit 362ec63

File tree

4 files changed

+117
-12
lines changed

4 files changed

+117
-12
lines changed

bootstrap_datepicker_plus/__init__.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
"""This module contains the widget classes."""
2+
13
from bootstrap_datepicker_plus._base import BasePickerInput
24

5+
36
__all__ = (
47
'DatePickerInput',
58
'TimePickerInput',
@@ -10,35 +13,81 @@
1013

1114

1215
class DatePickerInput(BasePickerInput):
16+
"""
17+
Widget to display a Date-Picker Calendar on a DateField property.
18+
19+
Args:
20+
- attrs (dict): HTML attributes of rendered HTML input
21+
- format (string): Python DateTime format eg. "%Y-%m-%d"
22+
- options (dict): Options to customize the widget, see README
23+
"""
24+
1325
picker_type = 'DATE'
1426
format = '%m/%d/%Y'
1527
format_key = 'DATE_INPUT_FORMATS'
1628

1729

1830
class TimePickerInput(BasePickerInput):
31+
"""
32+
Widget to display a Time-Picker Calendar on a TimeField property.
33+
34+
Args:
35+
- attrs (dict): HTML attributes of rendered HTML input
36+
- format (string): Python DateTime format eg. "%Y-%m-%d"
37+
- options (dict): Options to customize the widget, see README
38+
"""
39+
1940
picker_type = 'TIME'
2041
format = '%H:%M'
2142
format_key = 'TIME_INPUT_FORMATS'
2243
template_name = 'bootstrap_datepicker_plus/time-picker.html'
2344

2445

2546
class DateTimePickerInput(BasePickerInput):
47+
"""
48+
Widget to display a DateTime-Picker Calendar on a DateTimeField property.
49+
50+
Args:
51+
- attrs (dict): HTML attributes of rendered HTML input
52+
- format (string): Python DateTime format eg. "%Y-%m-%d"
53+
- options (dict): Options to customize the widget, see README
54+
"""
55+
2656
picker_type = 'DATETIME'
2757
format = '%m/%d/%Y %H:%M'
2858
format_key = 'DATETIME_INPUT_FORMATS'
2959

3060

3161
class MonthPickerInput(BasePickerInput):
62+
"""
63+
Widget to display a Month-Picker Calendar on a DateField property.
64+
65+
Args:
66+
- attrs (dict): HTML attributes of rendered HTML input
67+
- format (string): Python DateTime format eg. "%Y-%m-%d"
68+
- options (dict): Options to customize the widget, see README
69+
"""
70+
3271
picker_type = 'MONTH'
3372
format = '%Y-%m-01'
3473
format_key = 'DATE_INPUT_FORMATS'
3574

3675

3776
class YearPickerInput(BasePickerInput):
77+
"""
78+
Widget to display a Year-Picker Calendar on a DateField property.
79+
80+
Args:
81+
- attrs (dict): HTML attributes of rendered HTML input
82+
- format (string): Python DateTime format eg. "%Y-%m-%d"
83+
- options (dict): Options to customize the widget, see README
84+
"""
85+
3886
picker_type = 'YEAR'
3987
format = '%Y-01-01'
4088
format_key = 'DATE_INPUT_FORMATS'
4189

4290
def _link_to(self, linked_picker):
91+
"""Customize the options when linked with other date-time input"""
4392
yformat = self.config['options']['format'].replace('-01-01', '-12-31')
4493
self.config['options']['format'] = yformat

bootstrap_datepicker_plus/_base.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
"""Contains Base Date-Picker input class for widgets of this package."""
3+
24
from json import dumps as json_dumps
35
from bootstrap_datepicker_plus._helpers import (
46
get_base_input,
@@ -7,6 +9,8 @@
79

810

911
class BasePickerInput(get_base_input()):
12+
"""Base Date-Picker input class for widgets of this package."""
13+
1014
template_name = 'bootstrap_datepicker_plus/date-picker.html'
1115
picker_type = 'DATE'
1216
format = '%m/%d/%Y'
@@ -44,6 +48,8 @@ class BasePickerInput(get_base_input()):
4448
)
4549

4650
class Media:
51+
"""JS/CSS resources needed to render the date-picker calendar."""
52+
4753
js = (
4854
'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/'
4955
'moment-with-locales.min.js',
@@ -59,19 +65,20 @@ class Media:
5965

6066
@classmethod
6167
def format_py2js(cls, datetime_format):
62-
''' convert python datetime format to moment format '''
68+
"""Convert python datetime format to moment datetime format."""
6369
for js_format, py_format in cls.format_map:
6470
datetime_format = datetime_format.replace(py_format, js_format)
6571
return datetime_format
6672

6773
@classmethod
6874
def format_js2py(cls, datetime_format):
69-
''' convert moment format to python datetime format '''
75+
"""Convert moment datetime format to python datetime format."""
7076
for js_format, py_format in cls.format_map:
7177
datetime_format = datetime_format.replace(js_format, py_format)
7278
return datetime_format
7379

7480
def __init__(self, attrs=None, format=None, options=None):
81+
"""Initialize the Date-picker widget."""
7582
self.format_param = format
7683
self.options_param = options if options else {}
7784
self.config = self._default_config.copy()
@@ -84,13 +91,15 @@ def __init__(self, attrs=None, format=None, options=None):
8491
super().__init__(attrs, self._calculate_format())
8592

8693
def _calculate_options(self):
94+
"""Calculate and Return the options."""
8795
_options = self._default_options.copy()
8896
_options.update(self.options)
8997
if self.options_param:
9098
_options.update(self.options_param)
9199
return _options
92100

93101
def _calculate_format(self):
102+
"""Calculate and Return the datetime format."""
94103
_format = self.format_param if self.format_param else self.format
95104
if self.config['options'].get('format'):
96105
_format = self.format_js2py(self.config['options'].get('format'))
@@ -99,27 +108,31 @@ def _calculate_format(self):
99108
return _format
100109

101110
def get_context(self, name, value, attrs):
111+
"""Return widget context dictionary."""
102112
context = super().get_context(
103113
name, value, attrs)
104114
context['widget']['attrs']['dp_config'] = json_dumps(self.config)
105115
return context
106116

107117
def start_of(self, event_id):
108-
'''
109-
Set Datepicker as the start-date of a date-range
118+
"""
119+
Set Date-Picker as the start-date of a date-range.
110120
111-
event_id => a user-defined unique string to identify the date-range
112-
'''
121+
Args:
122+
- event_id (string): User-defined unique id for linking two fields
123+
"""
113124
DatePickerDictionary.items[str(event_id)] = self
114125
return self
115126

116127
def end_of(self, event_id, import_options=True):
117-
'''
118-
Set Datepicker as the end-date of a date-range
128+
"""
129+
Set Date-Picker as the end-date of a date-range.
119130
120-
event_id => a user-defined unique string to identify the date-range
121-
import_options => import options from other input, default is TRUE
122-
'''
131+
Args:
132+
- event_id (string): User-defined unique id for linking two fields
133+
- import_options (bool): inherit options from start-date input,
134+
default: TRUE
135+
"""
123136
event_id = str(event_id)
124137
if event_id in DatePickerDictionary.items:
125138
linked_picker = DatePickerDictionary.items[event_id]
@@ -142,4 +155,9 @@ def end_of(self, event_id, import_options=True):
142155
return self
143156

144157
def _link_to(self, linked_picker):
158+
"""
159+
Executed when two date-inputs are linked together.
160+
161+
This method for sub-classes to override to customize the linking.
162+
"""
145163
pass

bootstrap_datepicker_plus/_compatibility.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
"""Contains fallback/missing classes and methods for older django versions."""
3+
24
from functools import lru_cache
35
from pathlib import Path
46

@@ -12,20 +14,28 @@
1214

1315

1416
class BaseRenderer:
17+
"""Missing class from django.forms.renderers module."""
18+
1519
def get_template(self, template_name):
20+
"""Return template name."""
1621
raise NotImplementedError('subclasses must implement get_template()')
1722

1823
def render(self, template_name, context, request=None):
24+
"""Render template from template_name."""
1925
template = self.get_template(template_name)
2026
return template.render(context, request=request).strip()
2127

2228

2329
class EngineMixin:
30+
"""Missing class from django.forms.renderers module."""
31+
2432
def get_template(self, template_name):
33+
"""Return template name."""
2534
return self.engine.get_template(template_name)
2635

2736
@cached_property
2837
def engine(self):
38+
"""Return Render Engine."""
2939
return self.backend({
3040
'APP_DIRS': True,
3141
'DIRS': [str(ROOT / self.backend.app_dirname)],
@@ -36,28 +46,40 @@ def engine(self):
3646

3747
class DjangoTemplateRenderer(EngineMixin, BaseRenderer):
3848
"""
49+
Missing default template renderer from django.forms.renderers module.
50+
3951
Load Django templates from the built-in widget templates in
4052
django/forms/templates and from apps' 'templates' directory.
4153
"""
54+
4255
backend = DjangoTemplates
4356

4457

4558
@lru_cache()
4659
def get_default_renderer():
60+
"""Missing method from django.forms.renderers module."""
4761
return DjangoTemplateRenderer()
4862

4963

5064
class CompatibleDateTimeBaseInput(DateTimeBaseInput):
65+
"""Fallback class containing methods missing in older django version."""
66+
5167
format = '%m/%d/%Y'
5268
format_key = 'DATE_INPUT_FORMATS'
5369
template_name = 'bootstrap_datepicker_plus/date-picker.html'
5470

5571
def format_value(self, value):
72+
"""
73+
Return a value as it should appear when rendered in a template.
74+
75+
Missing method of django.forms.widgets.Widget class
76+
"""
5677
if value == '' or value is None:
5778
return None
5879
return formats.localize_input(value, self.format)
5980

6081
def get_context(self, name, value, attrs):
82+
"""Missing method of django.forms.widgets.Widget class."""
6183
context = {}
6284
context['widget'] = {
6385
'name': name,
@@ -71,11 +93,16 @@ def get_context(self, name, value, attrs):
7193
return context
7294

7395
def render(self, name, value, attrs=None, renderer=None):
74-
"""Render the widget as an HTML string."""
96+
"""
97+
Render the widget as an HTML string.
98+
99+
Missing method of django.forms.widgets.Widget class
100+
"""
75101
context = self.get_context(name, value, attrs)
76102
return self._render(self.template_name, context, renderer)
77103

78104
def _render(self, template_name, context, renderer=None):
105+
"""Missing method of django.forms.widgets.Widget class."""
79106
if renderer is None:
80107
renderer = get_default_renderer()
81108
return mark_safe(renderer.render(template_name, context))

bootstrap_datepicker_plus/_helpers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
"""Contains the helper classes and methods used throughout the project."""
2+
3+
14
def get_base_input(test=False):
5+
"""
6+
Return DateTimeBaseInput class from django.forms.widgets module
7+
8+
Return _compatibility.DateTimeBaseInput class for older django versions.
9+
"""
210
from django.forms.widgets import DateTimeBaseInput
311
if 'get_context' in dir(DateTimeBaseInput) and not test:
412
# django version 1.11 and above
@@ -13,10 +21,13 @@ def get_base_input(test=False):
1321

1422

1523
class DatePickerDictionary:
24+
"""Keeps track of all date-picker input classes."""
25+
1626
_i = 0
1727
items = dict()
1828

1929
@classmethod
2030
def generate_id(cls):
31+
"""Return a unique ID for each date-picker input class."""
2132
cls._i += 1
2233
return 'dp_%s' % cls._i

0 commit comments

Comments
 (0)