Exemple #1
0
def create(req):
    """ Books a new slot for the user.

        Shows user alters if:
            Slot is booked succesfully.
            User exceeds the limit of number of slots that can be booked in advance for a day.
            User attmpts to book two consecutive slots in a day in advance.
            Requested slot is already booked by another user.

        Input: req:request object.
        Output: HttpResponseRedirect object.
    """
    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)
Exemple #2
0
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
    })
Exemple #3
0
def new(req):
    """ Shows currently available slots.
    
        Input: req:request object.
        Output: HttpResponse object.
    """
    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
    })
Exemple #4
0
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)
Exemple #5
0
def show(req, date_string):
    """ Shows all available slots.
        Input: req:request object.
        Output: HttpResponse object.
    """
    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})
Exemple #6
0
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})
Exemple #7
0
def initiation(req):
    """ Logs in an user for conducting the experiment on the specified board.
        Input: req:request object.
        Output: HttpResponse object.
    """
    username = req.POST.get("username")
    print 'username ', username
    password = req.POST.get("password")
    print 'password ', password
    user = authenticate(username=username, password=password)
    print 'user ', user
    if user is not None:
        if user.is_active:
            user1 = Account.objects.select_related().filter(id=user.id)
            user1 = user1[0]
            user_board = user1.board

            #allows admin to access the temporary offline devices but prohibits the users to do so
            if user_board.online and (not user_board.temp_offline
                                      or user1.is_admin):
                slots = Slot.slots_now()
                slot_ids = [s.id for s in slots]
                now = datetime.datetime.now()
                bookings = user.booking_set.filter(
                    booking_date__year=now.year,
                    booking_date__month=now.month,
                    booking_date__day=now.day,
                    slot_id__in=slot_ids).select_related("slot")
                try:
                    cur_booking = bookings[0]
                    print 'cur_booking ', cur_booking
                    active_slot = cur_booking.slot
                    print 'active_slot ', active_slot
                except:
                    cur_booking = None
                    active_slot = None

                if active_slot is not None:
                    endtime = cur_booking.end_time()
                    if now < endtime:
                        filename = datetime.datetime.strftime(
                            now, "%Y%b%d_%H_%M_%S.txt")
                        logdir = os.path.join(settings.EXPERIMENT_LOGS_DIR,
                                              user.username)
                        if not os.path.exists(logdir):
                            os.makedirs(logdir)

                        f = open(os.path.join(logdir, filename), "a")
                        f.close()

                        LOGIN(req, user)

                        e = Experiment()
                        e.booking = cur_booking
                        e.log = os.path.join(logdir, filename)
                        e.save()

                        key = str(user_board.mid)
                        print 'key ', key
                        settings.boards[key]["experiment_id"] = e.id
                        # global_logfile = settings.SBHS_GLOBAL_LOG_DIR + "/" + key + ".log"
                        # with open(global_logfile, "a") as global_loghandler:
                        #     data = "\n\n===================New experiment====================\nUsername : "******"\nExperiment Id : " + str(e.id) + "\n"
                        #     global_loghandler.write(data)

                        reset(req)

                        STATUS = 1
                        MESSAGE = filename
                    else:
                        reset(req)
                        STATUS = 0
                        MESSAGE = "Slot has ended. Please book the next slot to continue the experiment."
                else:
                    STATUS = 0
                    MESSAGE = "You haven't booked this slot."
            else:
                STATUS = 0
                MESSAGE = "Your SBHS is offline. Please contact the Vlabs team."
        else:
            STATUS = 0
            MESSAGE = "Your account is not activated yet. Please check your email for activation link."
    else:
        STATUS = 0
        MESSAGE = "Invalid username or password"

    return HttpResponse(json.dumps({"STATUS": STATUS, "MESSAGE": MESSAGE}))
Exemple #8
0
def initiation(req):
    username = req.POST.get("username")
    password = req.POST.get("password")
    user = authenticate(username=username, password=password)
    if user is not None:
        if user.is_active:
            user1 = Account.objects.select_related().filter(id=user.id)
            user1 = user1[0]
            user_board = user1.board
            if user_board.online:
                slots = Slot.slots_now()
                slot_ids = [s.id for s in slots]
                now = datetime.datetime.now()
                bookings = user.booking_set.filter(
                    booking_date__year=now.year,
                    booking_date__month=now.month,
                    booking_date__day=now.day,
                    slot_id__in=slot_ids).select_related("slot")
                try:
                    cur_booking = bookings[0]
                    active_slot = cur_booking.slot
                except:
                    cur_booking = None
                    active_slot = None

                if active_slot is not None:
                    endtime = cur_booking.end_time()
                    if now < endtime:
                        filename = datetime.datetime.strftime(
                            now, "%Y%b%d_%H_%M_%S.txt")
                        logdir = os.path.join(settings.EXPERIMENT_LOGS_DIR,
                                              user.username)
                        if not os.path.exists(logdir):
                            os.makedirs(logdir)

                        f = open(os.path.join(logdir, filename), "a")
                        f.close()

                        LOGIN(req, user)

                        e = Experiment()
                        e.booking = cur_booking
                        e.log = os.path.join(logdir, filename)
                        e.save()

                        key = str(user_board.mid)

                        settings.boards[key]["experiment_id"] = e.id
                        reset(req)

                        STATUS = 1
                        MESSAGE = filename
                    else:
                        reset(req)
                        STATUS = 0
                        MESSAGE = "Slot has ended. Please book the next slot to continue the experiment."
                else:
                    STATUS = 0
                    MESSAGE = "You haven't booked this slot."
            else:
                STATUS = 0
                MESSAGE = "Your SBHS is offline. Please contact the Vlabs team."
        else:
            STATUS = 0
            MESSAGE = "Your account is not activated yet. Please check your email for activation link."
    else:
        STATUS = 0
        MESSAGE = "Invalid username or password"

    return HttpResponse(json.dumps({"STATUS": STATUS, "MESSAGE": MESSAGE}))