summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcoderick142017-05-19 15:14:33 +0530
committercoderick142017-05-19 15:14:33 +0530
commitf6621a4dc6cc0a7fb502af7df67e52fbadaf1842 (patch)
tree87a74c3174c2172fa38c7401b09eaa898bec5421
parent9b8d5b996acb6a042930ddee479ecf9e40bb4e43 (diff)
downloadSBHS-2018-Rpi-f6621a4dc6cc0a7fb502af7df67e52fbadaf1842.tar.gz
SBHS-2018-Rpi-f6621a4dc6cc0a7fb502af7df67e52fbadaf1842.tar.bz2
SBHS-2018-Rpi-f6621a4dc6cc0a7fb502af7df67e52fbadaf1842.zip
Handle error when no boards are online
-rw-r--r--account/views.py9
-rw-r--r--sbhs_server/tables/models.py21
2 files changed, 24 insertions, 6 deletions
diff --git a/account/views.py b/account/views.py
index 8f11d26..5fbb14a 100644
--- a/account/views.py
+++ b/account/views.py
@@ -60,11 +60,18 @@ def create(req):
return redirect(index)
# try:
+
+ # check if a board could be allocated
+ allocated_board_id = Board.allot_board()
+ if allocated_board_id == -1:
+ messages.add_message(req, messages.ERROR, "Sorry!! No boards online at this moment. Try again in some time.")
+ return redirect(index)
+
account = Account(
name=name,
username=username,
email=email,
- board_id=Board.allot_board(),
+ board_id=allocated_board_id,
last_login=datetime.now().strftime("%Y-%m-%d %H:%M")
)
account.set_password(password)
diff --git a/sbhs_server/tables/models.py b/sbhs_server/tables/models.py
index 2b9e1e6..c25a6d2 100644
--- a/sbhs_server/tables/models.py
+++ b/sbhs_server/tables/models.py
@@ -5,6 +5,7 @@ import random, datetime, os
from sbhs_server.helpers import mailer, simple_encrypt
from django.contrib.auth.models import UserManager
from sbhs_server import settings
+from django.core.exceptions import ObjectDoesNotExist
#from yaksh.models import Profile
# Create your models here.
@@ -35,12 +36,22 @@ class Board(TrashableMixin):
board_num = random.randrange(online_boards_count)
return settings.online_mids[board_num]
else:
- last_allocated_MID = Account.objects.select_related().order_by("-id")[0].board.mid;
online_boards = sorted(settings.online_mids)
- for o in online_boards:
- if o > last_allocated_MID:
- return Board.objects.get(mid=o).id
- return Board.objects.get(mid=online_boards[0]).id
+
+ # When the account table is empty, allocate first board
+ try:
+ last_allocated_MID = Account.objects.select_related().order_by("-id")[0].board.mid;
+ for o in online_boards:
+ if o > last_allocated_MID:
+ return Board.objects.get(mid=o).id
+ except ObjectDoesNotExist:
+ pass
+
+ # check if there is at least one online board
+ try:
+ return Board.objects.get(mid=online_boards[0]).id
+ except Exception as e:
+ return -1
def image_link(self):
return settings.WEBCAM_STATIC_DIR + "image" + str(self.mid) + ".jpeg"