Repository: dotkom/onlineweb4

Issue:
Extend dashboard DTP with time of day
Currently only the date portion of the datetimepicker is active.

Reconfigure `dashboard.js` to include the time of day as well.



Discussion:
:+1:


Resolving patch:
diff --git a/apps/dashboard/widgets.py b/apps/dashboard/widgets.py
--- a/apps/dashboard/widgets.py
+++ b/apps/dashboard/widgets.py
@@ -5,6 +5,14 @@
 from django.forms.widgets import TextInput
 from django.forms.utils import force_text, format_html
 
+DATEPICKER_WIDGET_STRING = """
+<div class="input-group dp">\r\n
+<span class="input-group-btn datepickerbutton">\r\n
+<a href="#" class="btn btn-primary">\r\n
+<i class="fa fa-calendar fa-lg"></i></a></span>\r\n
+<input class="form-control" id="{}" name="{}" type="text" {} placeholder="Den skal vises fra ..." />\r\n
+</div>\r\n
+"""
 DATETIMEPICKER_WIDGET_STRING = """
 <div class="input-group dtp">\r\n
 <span class="input-group-btn datepickerbutton">\r\n
@@ -14,6 +22,15 @@
 </div>\r\n
 """
 
+TIMEPICKER_WIDGET_STRING = """
+<div class="input-group tp">\r\n
+<span class="input-group-btn datepickerbutton">\r\n
+<a href="#" class="btn btn-primary">\r\n
+<i class="fa fa-calendar fa-lg"></i></a></span>\r\n
+<input class="form-control" id="{}" name="{}" type="text" {} placeholder="Den skal vises fra ..." />\r\n
+</div>\r\n
+"""
+
 
 def widget_generator(klass, fields):
     """
@@ -62,6 +79,41 @@ def multiple_widget_generator(klass_field_tuples):
     return widgets
 
 
+class DatePickerInput(TextInput):
+    """
+    This form widget metaclass mixin activates Bootstrap Datetimepicker
+    """
+
+    def __init__(self, attrs=None):
+        super(DatePickerInput, self).__init__(attrs)
+        self.input_type = 'text'
+
+    def render(self, name, value, attrs=None):
+        """
+        Renders this widget
+        :param name: Name attribute of the input type
+        :param value: Value, if any
+        :param attrs: Dictionary of additional attributes and their values
+        :return: HTML
+        """
+
+        if value is None:
+            value = ''
+
+        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
+        if value != '':
+            final_attrs['value'] = format_html('value="{}"', force_text(self._format_value(value)))
+        else:
+            final_attrs['value'] = ''
+
+        return format_html(
+            DATEPICKER_WIDGET_STRING,
+            force_text(final_attrs['id']),
+            force_text(final_attrs['name']),
+            final_attrs['value']
+        )
+
+
 class DatetimePickerInput(TextInput):
     """
     This form widget metaclass mixin activates Bootstrap Datetimepicker
@@ -95,3 +147,38 @@ def render(self, name, value, attrs=None):
             force_text(final_attrs['name']),
             final_attrs['value']
         )
+
+
+class TimePickerInput(TextInput):
+    """
+    This form widget metaclass mixin activates Bootstrap Datetimepicker
+    """
+
+    def __init__(self, attrs=None):
+        super(TimePickerInput, self).__init__(attrs)
+        self.input_type = 'text'
+
+    def render(self, name, value, attrs=None):
+        """
+        Renders this widget
+        :param name: Name attribute of the input type
+        :param value: Value, if any
+        :param attrs: Dictionary of additional attributes and their values
+        :return: HTML
+        """
+
+        if value is None:
+            value = ''
+
+        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
+        if value != '':
+            final_attrs['value'] = format_html('value="{}"', force_text(self._format_value(value)))
+        else:
+            final_attrs['value'] = ''
+
+        return format_html(
+            TIMEPICKER_WIDGET_STRING,
+            force_text(final_attrs['id']),
+            force_text(final_attrs['name']),
+            final_attrs['value']
+        )
diff --git a/apps/posters/dashboard/views.py b/apps/posters/dashboard/views.py
--- a/apps/posters/dashboard/views.py
+++ b/apps/posters/dashboard/views.py
@@ -112,10 +112,12 @@ def edit(request, order_id=None):
     if order_id and request.user != poster.ordered_by and 'proKom' not in request.user.groups.all():
         raise PermissionDenied
 
+    selected_form = EditPosterForm
+
     if request.POST:
         if poster.title:
-            SelectedForm = EditOtherForm
-        form = SelectedForm(request.POST, instance=poster)
+            selected_form = EditOtherForm
+        form = selected_form(request.POST, instance=poster)
         if form.is_valid():
             form.save()
             return HttpResponseRedirect("../detail/"+str(poster.id))
@@ -124,11 +126,11 @@ def edit(request, order_id=None):
             context["poster"] = poster
 
     else:
-        SelectedForm = EditPosterForm
+        selected_form = EditPosterForm
         if poster.title:
-            SelectedForm = EditOtherForm
+            selected_form = EditOtherForm
 
-        context["form"] = SelectedForm(instance=poster)
+        context["form"] = selected_form(instance=poster)
         context["poster"] = poster
 
     return render(request, 'posters/dashboard/add.html', context)
@@ -209,7 +211,7 @@ def _handle_poster_add(request, form, order_type):
                 'site': '',
                 'order_type': type_name.lower().rstrip(),
                 'num': '%s' % poster.amount,
-                'bongs': ', %s x bong' % poster.bong if poster.bong > 0 else '' if poster.bong > 0 else '',
+                'bongs': ', %s x bong' % poster.bong if poster.bong else '',
                 'ordered_by': poster.ordered_by,
                 'ordered_by_committee': poster.ordered_committee,
                 'id': poster.id,
diff --git a/apps/posters/forms.py b/apps/posters/forms.py
--- a/apps/posters/forms.py
+++ b/apps/posters/forms.py
@@ -4,7 +4,7 @@
 from django.contrib.auth.models import Group
 from django.utils import timezone
 
-from apps.dashboard.widgets import DatetimePickerInput, multiple_widget_generator
+from apps.dashboard.widgets import DatePickerInput, multiple_widget_generator
 from apps.events.models import Event
 from apps.posters.models import Poster
 
@@ -43,7 +43,7 @@ class Meta(object):
         dtp_fields = [('display_from', {})]
 
         widgetlist = [
-            (DatetimePickerInput, dtp_fields),
+            (DatePickerInput, dtp_fields),
         ]
 
         # Multiple widget generator merges results from regular widget_generator into a single widget dict
@@ -70,7 +70,7 @@ class Meta(object):
         dtp_fields = [('display_from', {})]
 
         widgetlist = [
-            (DatetimePickerInput, dtp_fields),
+            (DatePickerInput, dtp_fields),
         ]
 
         # Multiple widget generator merges results from regular widget_generator into a single widget dict
@@ -98,7 +98,7 @@ class Meta(object):
         dtp_fields = [('display_from', {})]
 
         widgetlist = [
-            (DatetimePickerInput, dtp_fields),
+            (DatePickerInput, dtp_fields),
         ]
 
         # Multiple widget generator merges results from regular widget_generator into a single widget dict
@@ -111,9 +111,27 @@ class Meta(object):
         fields = ['event', 'amount', 'bong', 'description',
                   'price', 'display_from', 'comments']
 
+        dtp_fields = [('display_from', {})]
+
+        widgetlist = [
+            (DatePickerInput, dtp_fields),
+        ]
+
+        # Multiple widget generator merges results from regular widget_generator into a single widget dict
+        widgets = multiple_widget_generator(widgetlist)
+
 
 class EditOtherForm(AddOtherForm):
     class Meta(object):
         model = Poster
         fields = ['title', 'amount', 'price', 'description',
                   'display_from', 'comments']
+
+        dtp_fields = [('display_from', {})]
+
+        widgetlist = [
+            (DatePickerInput, dtp_fields),
+        ]
+
+        # Multiple widget generator merges results from regular widget_generator into a single widget dict
+        widgets = multiple_widget_generator(widgetlist)


Test patch: