From ff75c19c70ce7cc5ad871b94e1e235517f88bbbb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Valentin=20Grou=C3=A8s?= <valentin.groues@uni.lu>
Date: Tue, 21 Mar 2017 14:34:00 +0100
Subject: [PATCH] #66 - show appointments types list as checkboxes

---
 smash/web/forms.py                         | 10 +++++++++-
 smash/web/static/css/smash.css             | 21 +++++++++++++++++++++
 smash/web/templates/_base.html             | 14 ++++++--------
 smash/web/templates/appointments/add.html  |  3 +--
 smash/web/templates/appointments/edit.html |  3 +--
 smash/web/templates/visits/add.html        |  3 +--
 smash/web/templates/visits/details.html    |  2 +-
 smash/web/templatetags/filters.py          |  8 ++++++++
 8 files changed, 48 insertions(+), 16 deletions(-)
 create mode 100644 smash/web/static/css/smash.css

diff --git a/smash/web/forms.py b/smash/web/forms.py
index 514d9ac9..b86fed54 100644
--- a/smash/web/forms.py
+++ b/smash/web/forms.py
@@ -3,7 +3,7 @@ from datetime import datetime
 from django import forms
 from django.forms import ModelForm, Form
 
-from models import Subject, Worker, Appointment, Visit
+from models import Subject, Worker, Appointment, Visit, AppointmentType
 
 """
 Possible redundancy, but if need arises, contents of forms can be easily customized
@@ -131,6 +131,8 @@ class AppointmentEditForm(ModelForm):
     datetime_when = forms.DateTimeField(label='Appointment on (YYYY-MM-DD HH:MM)',
                                         widget=forms.DateTimeInput(DATETIMEPICKER_DATE_ATTRS)
                                         )
+    appointment_types = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple,
+                                                       queryset=AppointmentType.objects.all())
 
     def __init__(self, *args, **kwargs):
         user = kwargs.pop('user', None)
@@ -158,6 +160,8 @@ class AppointmentAddForm(ModelForm):
     datetime_when = forms.DateTimeField(label='Appointment on (YYYY-MM-DD HH:MM)',
                                         widget=forms.DateTimeInput(DATETIMEPICKER_DATE_ATTRS)
                                         )
+    appointment_types = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple,
+                                                       queryset=AppointmentType.objects.all())
 
     def __init__(self, *args, **kwargs):
         user = kwargs.pop('user', None)
@@ -186,6 +190,8 @@ class VisitDetailForm(ModelForm):
                                    )
 
     post_mail_sent = forms.RadioSelect()
+    appointment_types = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple,
+                                                       queryset=AppointmentType.objects.all())
 
     class Meta:
         model = Visit
@@ -200,6 +206,8 @@ class VisitAddForm(ModelForm):
     datetime_end = forms.DateField(label="Visit ends on",
                                    widget=forms.TextInput(attrs=DATEPICKER_DATE_ATTRS)
                                    )
+    appointment_types = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple,
+                                                       queryset=AppointmentType.objects.all())
 
     class Meta:
         model = Visit
diff --git a/smash/web/static/css/smash.css b/smash/web/static/css/smash.css
new file mode 100644
index 00000000..ca1d8dc0
--- /dev/null
+++ b/smash/web/static/css/smash.css
@@ -0,0 +1,21 @@
+.multi-checkboxes ul {
+    list-style: none;
+    column-count: 3;
+    -moz-column-count: 3;
+    -webkit-column-count: 3;
+}
+
+.multi-checkboxes li {
+    margin: 0;
+    padding: 0;
+}
+
+.flag-icon {
+    height: 20px;
+}
+
+.multi-checkboxes .form-control:not(select) {
+    -moz-appearance: initial;
+    -webkit-appearance: initial;
+    appearance: initial;
+}
\ No newline at end of file
diff --git a/smash/web/templates/_base.html b/smash/web/templates/_base.html
index 685769f2..b39a2a39 100644
--- a/smash/web/templates/_base.html
+++ b/smash/web/templates/_base.html
@@ -22,17 +22,14 @@
 
         <link rel="stylesheet" href="{% static 'AdminLTE/css/skins/skin-green.min.css' %}">
 
-        <style>
-            .flag-icon {
-                height: 20px;
-            }
-        </style>
+        <link rel="stylesheet" href="{% static 'css/smash.css' %}">
+
 
         <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
         <!--[if lt IE 9]>
-	<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
-	<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
-  <![endif]-->
+        <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
+        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
+        <![endif]-->
     {% endblock styles %}
 </head>
 <!--
@@ -359,6 +356,7 @@ desired effect
 
             <!-- Default to the left -->
             2017, Parkinson Research Clinic <!--(eg. <small>
+
 		<strong>Copyright &copy; 2016 <a href="#">Company</a>.</strong> All rights reserved.
   </small>)-->
         {% endblock footer %}
diff --git a/smash/web/templates/appointments/add.html b/smash/web/templates/appointments/add.html
index 07b201ac..49336720 100644
--- a/smash/web/templates/appointments/add.html
+++ b/smash/web/templates/appointments/add.html
@@ -38,11 +38,10 @@
 
             <form method="post" action="" class="form-horizontal">
                 {% csrf_token %}
-
                 <div class="box-body">
                     <div class="col-sm-6">
                         {% for field in form %}
-                            <div class="form-group {% if field.errors %}has-error{% endif %}">
+                            <div class="form-group {% if field.errors %}has-error{% endif %} {% if field|is_checkbox %}multi-checkboxes{% endif %}">
                                 <label class="col-sm-4 control-label">
                                     {{ field.label }}
                                 </label>
diff --git a/smash/web/templates/appointments/edit.html b/smash/web/templates/appointments/edit.html
index 93ff194a..eb50e3b8 100644
--- a/smash/web/templates/appointments/edit.html
+++ b/smash/web/templates/appointments/edit.html
@@ -38,11 +38,10 @@
 
                 <div class="box-body">
                     {% for field in form %}
-                        <div class="col-md-6 form-group  {% if field.errors %}has-error{% endif %}">
+                        <div class="col-md-6 form-group  {% if field.errors %}has-error{% endif %} {% if field|is_checkbox %}multi-checkboxes{% endif %}">
                             <label for="{# TODO #}" class="col-sm-4 control-label">
                                 {{ field.label }}
                             </label>
-
                             <div class="col-sm-8">
                                 {{ field|add_class:'form-control' }}
                             </div>
diff --git a/smash/web/templates/visits/add.html b/smash/web/templates/visits/add.html
index d8240143..74a84ab4 100644
--- a/smash/web/templates/visits/add.html
+++ b/smash/web/templates/visits/add.html
@@ -35,11 +35,10 @@
                 <div class="box-body">
                     <div class="col-sm-6">
                         {% for field in form %}
-                            <div class="form-group {% if field.errors %}has-error{% endif %}">
+                            <div class="form-group {% if field.errors %}has-error{% endif %}  {% if field|is_checkbox %}multi-checkboxes{% endif %}">
                                 <label class="col-sm-4 control-label">
                                     {{ field.label }}
                                 </label>
-
                                 <div class="col-sm-8">
                                     {{ field|add_class:'form-control' }}
                                 </div>
diff --git a/smash/web/templates/visits/details.html b/smash/web/templates/visits/details.html
index 5f445495..c215b15e 100644
--- a/smash/web/templates/visits/details.html
+++ b/smash/web/templates/visits/details.html
@@ -41,7 +41,7 @@
                 {% csrf_token %}
                 <div class="box-body">
                     {% for field in vform %}
-                        <div class="col-md-6 form-group  {% if field.errors %}has-error{% endif %}">
+                        <div class="col-md-6 form-group  {% if field.errors %}has-error{% endif %}  {% if field|is_checkbox %}multi-checkboxes{% endif %}">
                             <label for="{# TODO #}" class="col-sm-4 control-label">
                                 {{ field.label }}
                             </label>
diff --git a/smash/web/templatetags/filters.py b/smash/web/templatetags/filters.py
index 2df497f0..bcab4307 100644
--- a/smash/web/templatetags/filters.py
+++ b/smash/web/templatetags/filters.py
@@ -1,5 +1,6 @@
 # See: http://stackoverflow.com/a/18962481
 from django import template
+from django.forms import CheckboxSelectMultiple
 
 register = template.Library()
 
@@ -7,6 +8,8 @@ register = template.Library()
 @register.filter(name='add_class')
 def add_class(value, arg):
     # Get all the field's initial css-classes
+    if isinstance(value.field.widget, CheckboxSelectMultiple):
+        return value
     css_classes = value.field.widget.attrs.get('class', ' ').split(' ')
     # Filter out zero-length class names ('')
     css_classes = filter(lambda x: len(x) > 0, css_classes)
@@ -21,3 +24,8 @@ def add_class(value, arg):
 def disable(value):
     value.field.widget.attrs['disabled'] = 'disabled'
     return value
+
+
+@register.filter(name='is_checkbox')
+def is_checkbox(value):
+    return isinstance(value.field.widget, CheckboxSelectMultiple)
-- 
GitLab