From 85e309715778bffef184d0bd9beb8ae8e2d40626 Mon Sep 17 00:00:00 2001
From: Jacek Lebioda <jacek.lebioda@uni.lu>
Date: Tue, 12 Dec 2017 16:10:46 +0100
Subject: [PATCH] Rooms management

---
 smash/web/forms/forms.py                      |  6 +-
 .../equipment_and_rooms/rooms/add.html        | 62 +++++++++++++++++++
 .../equipment_and_rooms/rooms/breadcrumb.html |  2 +-
 .../equipment_and_rooms/rooms/edit.html       | 62 +++++++++++++++++++
 .../equipment_and_rooms/rooms/index.html      | 61 ++++++++++++++++++
 smash/web/templates/sidebar.html              |  3 +-
 smash/web/urls.py                             |  4 ++
 smash/web/views/__init__.py                   |  1 +
 smash/web/views/rooms.py                      | 49 +++++++++++++++
 9 files changed, 247 insertions(+), 3 deletions(-)
 create mode 100644 smash/web/templates/equipment_and_rooms/rooms/add.html
 create mode 100644 smash/web/templates/equipment_and_rooms/rooms/edit.html
 create mode 100644 smash/web/templates/equipment_and_rooms/rooms/index.html
 create mode 100644 smash/web/views/rooms.py

diff --git a/smash/web/forms/forms.py b/smash/web/forms/forms.py
index 1aa33b92..65e60931 100644
--- a/smash/web/forms/forms.py
+++ b/smash/web/forms/forms.py
@@ -7,7 +7,7 @@ from django.forms import ModelForm, Form
 from django.utils.dates import MONTHS
 
 from web.models import Appointment, AppointmentType, AppointmentTypeLink, \
-    Availability, ContactAttempt, FlyingTeam, Holiday, StudySubject, \
+    Availability, ContactAttempt, FlyingTeam, Holiday, StudySubject, Room, \
     Worker, Visit, VoucherType, VoucherTypePrice
 from web.models.constants import SUBJECT_TYPE_CHOICES
 from web.views.notifications import get_filter_locations
@@ -400,3 +400,7 @@ class HolidayAddForm(ModelForm):
             validate_availability_conflict(self, self.cleaned_data, availability)
 
 
+class RoomForm(ModelForm):
+    class Meta:
+        model = Room
+        fields = '__all__'
diff --git a/smash/web/templates/equipment_and_rooms/rooms/add.html b/smash/web/templates/equipment_and_rooms/rooms/add.html
new file mode 100644
index 00000000..f5cd36e3
--- /dev/null
+++ b/smash/web/templates/equipment_and_rooms/rooms/add.html
@@ -0,0 +1,62 @@
+{% extends "_base.html" %}
+{% load static %}
+{% load filters %}
+
+{% block styles %}
+    {{ block.super }}
+    <link rel="stylesheet" href="{% static 'AdminLTE/plugins/awesomplete/awesomplete.css' %}"/>
+{% endblock styles %}
+
+{% block ui_active_tab %}'equipment_and_rooms'{% endblock ui_active_tab %}
+{% block page_header %}New room{% endblock page_header %}
+{% block page_description %}{% endblock page_description %}
+
+{% block title %}{{ block.super }} - Add new room{% endblock %}
+
+{% block breadcrumb %}
+    {% include "equipment_and_rooms/rooms/breadcrumb.html" %}
+{% endblock breadcrumb %}
+
+{% block maincontent %}
+
+        <div class="box box-info">
+            <div class="box-header with-border">
+                <a href="{% url 'web.views.equipment_and_rooms.rooms' %}" class="btn btn-block btn-default">Go back (without changes)</a>
+            </div>
+
+            <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 %}">
+                                <label for="{# TODO #}" class="col-sm-4 control-label">
+                                    {{ field.label }}
+                                </label>
+
+                                <div class="col-sm-8">
+                                    {{ field|add_class:'form-control' }}
+                                </div>
+
+                                {% if field.errors %}
+                                    <span class="help-block">
+                                        {{ field.errors }}
+                                     </span>
+                                {% endif %}
+                            </div>
+                        {% endfor %}
+                    </div>
+                </div><!-- /.box-body -->
+
+                <div class="box-footer">
+                    <div class="col-sm-6">
+                        <button type="submit" class="btn btn-block btn-success">Add</button>
+                    </div>
+                    <div class="col-sm-6">
+                        <a href="{% url 'web.views.equipment_and_rooms.rooms' %}" class="btn btn-block btn-default">Cancel</a>
+                    </div>
+                </div><!-- /.box-footer -->
+            </form>
+        </div>
+    {% endblock %}
diff --git a/smash/web/templates/equipment_and_rooms/rooms/breadcrumb.html b/smash/web/templates/equipment_and_rooms/rooms/breadcrumb.html
index 0aeef037..b2ecb34d 100644
--- a/smash/web/templates/equipment_and_rooms/rooms/breadcrumb.html
+++ b/smash/web/templates/equipment_and_rooms/rooms/breadcrumb.html
@@ -1,3 +1,3 @@
 <li><a href="{% url 'web.views.appointments' %}"><i class="fa fa-dashboard"></i> Dashboard</a></li>
 <li><a href="{% url 'web.views.equipment_and_rooms' %}">Equipment and rooms</a></li>
-<li class="active"><a href="">Rooms</a></li>
+<li class="active"><a href="{% url 'web.views.equipment_and_rooms.rooms' %}">Rooms</a></li>
diff --git a/smash/web/templates/equipment_and_rooms/rooms/edit.html b/smash/web/templates/equipment_and_rooms/rooms/edit.html
new file mode 100644
index 00000000..4a7e9b9e
--- /dev/null
+++ b/smash/web/templates/equipment_and_rooms/rooms/edit.html
@@ -0,0 +1,62 @@
+{% extends "_base.html" %}
+{% load static %}
+{% load filters %}
+
+{% block styles %}
+    {{ block.super }}
+    <link rel="stylesheet" href="{% static 'AdminLTE/plugins/awesomplete/awesomplete.css' %}"/>
+{% endblock styles %}
+
+{% block ui_active_tab %}'equipment_and_rooms'{% endblock ui_active_tab %}
+{% block page_header %}Edit room{% endblock page_header %}
+{% block page_description %}{% endblock page_description %}
+
+{% block title %}{{ block.super }} - Edit room{% endblock %}
+
+{% block breadcrumb %}
+    {% include "equipment_and_rooms/rooms/breadcrumb.html" %}
+{% endblock breadcrumb %}
+
+{% block maincontent %}
+
+        <div class="box box-info">
+            <div class="box-header with-border">
+                <a href="{% url 'web.views.equipment_and_rooms.rooms' %}" class="btn btn-block btn-default">Go back (without changes)</a>
+            </div>
+
+            <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 %}">
+                                <label for="{# TODO #}" class="col-sm-4 control-label">
+                                    {{ field.label }}
+                                </label>
+
+                                <div class="col-sm-8">
+                                    {{ field|add_class:'form-control' }}
+                                </div>
+
+                                {% if field.errors %}
+                                    <span class="help-block">
+                                        {{ field.errors }}
+                                     </span>
+                                {% endif %}
+                            </div>
+                        {% endfor %}
+                    </div>
+                </div><!-- /.box-body -->
+
+                <div class="box-footer">
+                    <div class="col-sm-6">
+                        <button type="submit" class="btn btn-block btn-success">Save</button>
+                    </div>
+                    <div class="col-sm-6">
+                        <a href="{% url 'web.views.equipment_and_rooms.rooms' %}" class="btn btn-block btn-default">Cancel</a>
+                    </div>
+                </div><!-- /.box-footer -->
+            </form>
+        </div>
+    {% endblock %}
diff --git a/smash/web/templates/equipment_and_rooms/rooms/index.html b/smash/web/templates/equipment_and_rooms/rooms/index.html
new file mode 100644
index 00000000..454cd79e
--- /dev/null
+++ b/smash/web/templates/equipment_and_rooms/rooms/index.html
@@ -0,0 +1,61 @@
+{% extends "_base.html" %}
+{% load static %}
+
+{% block styles %}
+    {{ block.super }}
+    <!-- DataTables -->
+    <link rel="stylesheet" href="{% static 'AdminLTE/plugins/datatables/dataTables.bootstrap.css' %}">
+{% endblock styles %}
+
+{% block ui_active_tab %}'equipment_and_rooms'{% endblock ui_active_tab %}
+{% block page_header %}Rooms{% endblock page_header %}
+{% block page_description %}management{% endblock page_description %}
+
+{% block breadcrumb %}
+    {% include "equipment_and_rooms/rooms/breadcrumb.html" %}
+{% endblock breadcrumb %}
+
+{% block maincontent %}
+<div>
+    <a href="{% url 'web.views.equipment_and_rooms.rooms_add' %}" class="btn btn-app">
+        <i class="fa fa-plus"></i>
+        Add new room</a>
+</div>
+
+<div class="box-body">
+    {% if rooms_list %}
+        <table id="table" class="table table-bordered table-striped">
+            <thead>
+            <tr>
+                <th>No.</th>
+                <th>City</th>
+                <th>Address</th>
+                <th>Owner</th>
+                <th>Details</th>
+                <th>Remove</th>
+            </tr>
+            </thead>
+            <tbody>
+            {% for room in rooms_list %}
+                <tr>
+                    <td>{{ forloop.counter }}</td>
+                    <td>{{ room.city }}</td>
+                    <td>{{ room.address }}</td>
+                    <td>{{ room.owner }}</td>
+                    <td>
+                      <a href="{% url 'web.views.equipment_and_rooms.rooms_edit' room.id %}" type="button"
+                             class="btn btn-block btn-default">Details</a></td>
+                    </td>
+                    <td>
+                      <a href="{% url 'web.views.equipment_and_rooms.rooms_delete' room.id %}" type="button"
+                             class="btn btn-block btn-danger">Remove</a></td>
+                    </td>
+                </tr>
+            {% endfor %}
+
+            </tbody>
+        </table>
+    {% else %}
+        <p>No rooms found.</p>
+    {% endif %}
+{% endblock maincontent %}
diff --git a/smash/web/templates/sidebar.html b/smash/web/templates/sidebar.html
index 05498fc5..c883860f 100644
--- a/smash/web/templates/sidebar.html
+++ b/smash/web/templates/sidebar.html
@@ -38,8 +38,9 @@
             </span>
         </a>
         <ul class="treeview-menu">
-              <li><a href="{% url 'web.views.kit_requests' %}">Kit requests</a></li>
               <li><a href="{% url 'web.views.equipment_and_rooms.flying_teams' %}">Flying teams</a></li>
+              <li><a href="{% url 'web.views.kit_requests' %}">Kit requests</a></li>
+              <li><a href="{% url 'web.views.equipment_and_rooms.rooms' %}">Rooms</a></li>
         </ul>
     </li>
 
diff --git a/smash/web/urls.py b/smash/web/urls.py
index c58aa7bb..fef4fade 100644
--- a/smash/web/urls.py
+++ b/smash/web/urls.py
@@ -131,6 +131,10 @@ urlpatterns = [
     url(r'^equipment_and_rooms/flying_teams$', views.flying_teams.flying_teams, name='web.views.equipment_and_rooms.flying_teams'),
     url(r'^equipment_and_rooms/flying_teams/add$', views.flying_teams.flying_teams_add, name='web.views.equipment_and_rooms.flying_teams_add'),
     url(r'^equipment_and_rooms/flying_teams/edit/(?P<flying_team_id>\d+)$', views.flying_teams.flying_teams_edit, name='web.views.equipment_and_rooms.flying_teams_edit'),
+    url(r'^equipment_and_rooms/rooms$', views.rooms.rooms, name='web.views.equipment_and_rooms.rooms'),
+    url(r'^equipment_and_rooms/rooms/add$', views.rooms.rooms_add, name='web.views.equipment_and_rooms.rooms_add'),
+    url(r'^equipment_and_rooms/rooms/edit/(?P<room_id>\d+)$', views.rooms.rooms_edit, name='web.views.equipment_and_rooms.rooms_edit'),
+    url(r'^equipment_and_rooms/rooms/delete/(?P<room_id>\d+)$', views.rooms.rooms_delete, name='web.views.equipment_and_rooms.rooms_delete'),
 
     ####################
     #       MAIL       #
diff --git a/smash/web/views/__init__.py b/smash/web/views/__init__.py
index fedbe5fd..7870edac 100644
--- a/smash/web/views/__init__.py
+++ b/smash/web/views/__init__.py
@@ -82,4 +82,5 @@ import voucher
 import voucher_type
 import voucher_type_price
 import redcap
+import rooms
 import uploaded_files
diff --git a/smash/web/views/rooms.py b/smash/web/views/rooms.py
new file mode 100644
index 00000000..0d14fafb
--- /dev/null
+++ b/smash/web/views/rooms.py
@@ -0,0 +1,49 @@
+# coding=utf-8
+from django.shortcuts import redirect, get_object_or_404
+
+from . import wrap_response
+from ..models import Room
+from ..forms.forms import RoomForm
+
+
+def rooms(request):
+    rooms_list = Room.objects.order_by('-city')
+    context = {
+        'rooms_list': rooms_list
+    }
+
+    return wrap_response(request,
+                         "equipment_and_rooms/rooms/index.html",
+                         context)
+
+
+def rooms_add(request):
+    if request.method == 'POST':
+        form = RoomForm(request.POST)
+        if form.is_valid():
+            form.save()
+            return redirect('web.views.equipment_and_rooms.rooms')
+    else:
+        form = RoomForm()
+
+    return wrap_response(request, 'equipment_and_rooms/rooms/add.html', {'form': form})
+
+
+def rooms_edit(request, room_id):
+    the_room = get_object_or_404(Room, id=room_id)
+    if request.method == 'POST':
+        form = RoomForm(request.POST, instance=the_room)
+        if form.is_valid():
+            form.save()
+            return redirect('web.views.equipment_and_rooms.rooms')
+    else:
+        form = RoomForm(instance=the_room)
+
+    return wrap_response(request, 'equipment_and_rooms/rooms/edit.html', {'form': form})
+
+
+def rooms_delete(request, room_id):
+    the_room = get_object_or_404(Room, id=room_id)
+    the_room.delete()
+
+    return redirect('web.views.equipment_and_rooms.rooms')
-- 
GitLab