-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
118 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from django import forms | ||
from django.utils.timezone import localtime | ||
|
||
|
||
class SearchDateForm(forms.Form): | ||
"""Form to search for data, between start and end dates.""" | ||
start = forms.DateField( | ||
required=False, | ||
widget=forms.DateInput( | ||
attrs={ | ||
'class': 'form-control-sm', | ||
'label': 'start', | ||
'name': 'start', | ||
'type': 'date' | ||
} | ||
) | ||
) | ||
end = forms.DateField( | ||
required=False, | ||
widget=forms.DateInput( | ||
attrs={ | ||
'class': 'form-control-sm', | ||
'label': 'end', | ||
'name': 'end', | ||
'type': 'date' | ||
} | ||
) | ||
) | ||
|
||
def get_initial_for_field(self, field, field_name): | ||
# Set date inputs "max" attribute to current time. | ||
field.widget.attrs["max"] = localtime().strftime("%Y-%m-%d") | ||
return super().get_initial_for_field(field, field_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,62 @@ | ||
from datetime import timedelta | ||
|
||
from django.conf import settings | ||
from django.views.generic.base import TemplateView | ||
from django.utils.timezone import localdate | ||
from django.views.generic.edit import FormView | ||
|
||
from ..forms import SearchDateForm | ||
|
||
|
||
class BaseView(TemplateView): | ||
class BaseView(FormView): | ||
"""Base view.""" | ||
http_method_names = ("get",) | ||
# icon = None | ||
dates = initial = {} | ||
default_start = {} | ||
form_class = SearchDateForm | ||
http_method_names = ("get", "post") | ||
template_name = "utility.html" | ||
title = None | ||
|
||
def setup(self, request, *args, **kwargs): | ||
# Set default search start and end datetime values. | ||
|
||
today = localdate() | ||
self.dates["start"] = None | ||
if self.default_start: | ||
self.dates["start"] = today - timedelta(**self.default_start) | ||
self.dates["end"] = today | ||
|
||
return super().setup(request, *args, **kwargs) | ||
|
||
|
||
def form_valid(self, form): | ||
# When valid, use the submitted form search dates. | ||
for when in "start", "end": | ||
if form.cleaned_data[when]: | ||
self.dates[when] = form.cleaned_data[when] | ||
|
||
return self.render_to_response(self.get_context_data(form=form)) | ||
|
||
def get_context_data(self, **kwargs): | ||
# Include start and end dates in context. | ||
context = super().get_context_data(**kwargs) | ||
|
||
for when in "start", "end": | ||
if self.dates[when]: | ||
context[when] = self.dates[when].strftime("%Y-%m-%d") | ||
|
||
context["timezone"] = settings.TIME_ZONE | ||
context["title"] = self.title.replace("_", " ").title() | ||
context["website_title"] = settings.WEBSITE_TITLE | ||
|
||
return context | ||
|
||
def get_initial(self): | ||
# Set initial form search date values. | ||
initial = super().get_initial() | ||
for when in "start", "end": | ||
if self.dates[when]: | ||
initial[when] = self.dates[when].strftime("%Y-%m-%d") | ||
return initial | ||
|
||
def get_success_url(self): | ||
return self.request.path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters