summaryrefslogtreecommitdiff
path: root/sbhs_server/slot
diff options
context:
space:
mode:
authorttt2017-05-13 00:29:47 +0530
committerttt2017-05-13 00:29:47 +0530
commitabf599be33b383a6a5baf9493093b2126a622ac8 (patch)
tree4c5ab6e0d935d5e65fabcf0258e4a00dd20a5afa /sbhs_server/slot
downloadSBHS-2018-Rpi-abf599be33b383a6a5baf9493093b2126a622ac8.tar.gz
SBHS-2018-Rpi-abf599be33b383a6a5baf9493093b2126a622ac8.tar.bz2
SBHS-2018-Rpi-abf599be33b383a6a5baf9493093b2126a622ac8.zip
added all server files
Diffstat (limited to 'sbhs_server/slot')
-rw-r--r--sbhs_server/slot/__init__.py0
-rw-r--r--sbhs_server/slot/__init__.pycbin0 -> 143 bytes
-rw-r--r--sbhs_server/slot/admin.py3
-rw-r--r--sbhs_server/slot/migrations/0001_slot_timing_data.py29
-rw-r--r--sbhs_server/slot/migrations/__init__.py0
-rw-r--r--sbhs_server/slot/models.py3
-rw-r--r--sbhs_server/slot/models.pycbin0 -> 197 bytes
-rw-r--r--sbhs_server/slot/tests.py3
-rw-r--r--sbhs_server/slot/views.py76
9 files changed, 114 insertions, 0 deletions
diff --git a/sbhs_server/slot/__init__.py b/sbhs_server/slot/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/sbhs_server/slot/__init__.py
diff --git a/sbhs_server/slot/__init__.pyc b/sbhs_server/slot/__init__.pyc
new file mode 100644
index 0000000..0f4e397
--- /dev/null
+++ b/sbhs_server/slot/__init__.pyc
Binary files differ
diff --git a/sbhs_server/slot/admin.py b/sbhs_server/slot/admin.py
new file mode 100644
index 0000000..8c38f3f
--- /dev/null
+++ b/sbhs_server/slot/admin.py
@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.
diff --git a/sbhs_server/slot/migrations/0001_slot_timing_data.py b/sbhs_server/slot/migrations/0001_slot_timing_data.py
new file mode 100644
index 0000000..158f201
--- /dev/null
+++ b/sbhs_server/slot/migrations/0001_slot_timing_data.py
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+from south.utils import datetime_utils as datetime
+from south.db import db
+from south.v2 import DataMigration
+from django.db import models
+from sbhs_server.tables.models import Slot
+
+class Migration(DataMigration):
+
+ def forwards(self, orm):
+ # "Write your forwards methods here."
+ # Note: Don't use "from appname.models import ModelName".
+ # Use orm.ModelName to refer to models in this application,
+ # and orm['appname.ModelName'] for models in other applications.
+ for i in xrange(24):
+ Slot.objects.create(start_hour=i, end_hour=i, start_minute=0, end_minute=55)
+
+ def backwards(self, orm):
+ # "Write your backwards methods here."
+ for i in xrange(24):
+ Slot.objects.delete(i)
+
+
+ models = {
+
+ }
+
+ complete_apps = ['slot']
+ symmetrical = True
diff --git a/sbhs_server/slot/migrations/__init__.py b/sbhs_server/slot/migrations/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/sbhs_server/slot/migrations/__init__.py
diff --git a/sbhs_server/slot/models.py b/sbhs_server/slot/models.py
new file mode 100644
index 0000000..71a8362
--- /dev/null
+++ b/sbhs_server/slot/models.py
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
diff --git a/sbhs_server/slot/models.pyc b/sbhs_server/slot/models.pyc
new file mode 100644
index 0000000..2f7dc8d
--- /dev/null
+++ b/sbhs_server/slot/models.pyc
Binary files differ
diff --git a/sbhs_server/slot/tests.py b/sbhs_server/slot/tests.py
new file mode 100644
index 0000000..7ce503c
--- /dev/null
+++ b/sbhs_server/slot/tests.py
@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.
diff --git a/sbhs_server/slot/views.py b/sbhs_server/slot/views.py
new file mode 100644
index 0000000..b137f93
--- /dev/null
+++ b/sbhs_server/slot/views.py
@@ -0,0 +1,76 @@
+from django.contrib.auth.decorators import login_required
+from django.shortcuts import render, redirect
+from django.contrib import messages
+from sbhs_server.tables.models import Account, Slot, Booking
+import datetime
+
+LIMIT = 2
+
+@login_required(redirect_field_name=None)
+def new(req):
+ cur_slots = Slot.current_slots(req.user.board.mid)
+ all_slots = Slot.get_free_slots(req.user.board.mid)
+ date = (datetime.datetime.now()).strftime("%Y-%m-%d")
+ return render(req, "slot/new.html", {"all_slots": all_slots, "cur_slots": cur_slots, "nowdate": date})
+
+@login_required(redirect_field_name=None)
+def show(req, date_string):
+ date = datetime.datetime.strptime(date_string, "%Y-%m-%d")
+ all_slots = Slot.get_free_slots_on(date, req.user.board.mid)
+ return render(req, "slot/show.html", {"all_slots": all_slots})
+
+@login_required(redirect_field_name=None)
+def create(req):
+ slot = Slot.objects.get(id=req.POST.get("slot"))
+ date_string = req.POST.get("date")
+ date = datetime.date.today() if date_string == "CURRENT" else datetime.datetime.strptime(date_string, "%Y-%m-%d")
+ all_slots = Slot.get_free_slots(req.user.board.mid) if date_string == "CURRENT" else Slot.get_free_slots_on(date, req.user.board.mid)
+
+ if slot in all_slots:
+ if date_string == "CURRENT":
+ Booking.objects.create(slot=slot, account=req.user, booking_date=date)
+ messages.add_message(req, messages.SUCCESS, "Slot " + str(slot) + " booked successfully.")
+ else:
+ bookings = req.user.booking_set.select_related("slot").filter(booking_date__year=date.year,
+ booking_date__month=date.month,
+ booking_date__day=date.day)
+ if len(bookings) >= LIMIT:
+ messages.add_message(req, messages.ERROR, "Can't book more than " + str(LIMIT) + " slots in a day in advance.")
+ elif len(bookings) < LIMIT:
+ consecutive_check = True
+ for b in bookings:
+ if abs(b.slot.start_hour - slot.start_hour) <= 1:
+ consecutive_check = False
+ break
+ if not consecutive_check:
+ messages.add_message(req, messages.ERROR, "Can't book 2 consecutive slots in a day in advance.")
+ else:
+ Booking.objects.create(slot=slot, account=req.user, booking_date=date)
+ messages.add_message(req, messages.SUCCESS, "Slot " + str(slot) + " booked successfully.")
+ else:
+ messages.add_message(req, messages.ERROR, "Slot " + str(slot) + " already booked.")
+
+ return redirect(index)
+
+@login_required(redirect_field_name=None)
+def index(req):
+ bookings = req.user.booking_set.select_related("slot").order_by("booking_date")
+
+ return render(req, "slot/index.html", {"bookings": reversed(bookings),
+ "now_time": datetime.datetime.now()})
+
+
+@login_required(redirect_field_name=None)
+def delete(req, booking_id):
+ try:
+ booking = Booking.objects.select_related("slot").get(id=booking_id)
+ assert booking.account_id == req.user.id
+ if booking.start_time() > datetime.datetime.now():
+ booking.delete()
+ messages.add_message(req, messages.SUCCESS, "Slot booking deleted successfully.")
+ else:
+ messages.add_message(req, messages.ERROR, "Slot time is over. Cannot delete this booking now.")
+ except:
+ messages.add_message(req, messages.ERROR, "Booking does not exist.")
+
+ return redirect(index)