コード例 #1
0
def end_interlock_session(request, session_id, rfid=None):
    session = InterlockLog.objects.get(pk=session_id)
    if not session.session_complete:
        user = None
        if rfid is not None:
            user = Profile.objects.get(rfid=rfid).user
        else:
            user = session.user

        session.session_complete = True
        session.last_heartbeat = timezone.now()
        session.user_off = user
        session.save()
        session.interlock.checkin()
        on_time = humanize.naturaldelta(
            session.last_heartbeat - session.first_heartbeat
        )
        post_interlock_swipe_to_discord(
            session.user_off.profile.get_full_name(),
            session.interlock.name,
            "deactivated",
            on_time,
        )

        return JsonResponse({"access": True})

    return JsonResponse(
        {
            "access": False,
            "error": "Session already ended.",
            "timestamp": round(time.time()),
        }
    )
コード例 #2
0
ファイル: views.py プロジェクト: snoopen/MemberMatters
def interlock_cron(request):
    timedout_interlocks = InterlockLog.objects.filter(
        last_heartbeat__lt=timezone.now() - timedelta(minutes=2),
        session_complete=False)

    if timedout_interlocks:
        for session in timedout_interlocks:
            session.session_complete = True
            session.save()
            session.interlock.checkin()
            on_time = humanize.naturaldelta(session.last_heartbeat -
                                            session.first_heartbeat)
            post_interlock_swipe_to_discord(
                session.user.profile.get_full_name(),
                session.interlock.name,
                "left_on",
                on_time,
            )

    return HttpResponseRedirect("/")
コード例 #3
0
def check_interlock_access(request, rfid_code=None, interlock_id=None, session_id=None):
    interlock_ip = None

    if session_id is not None:
        session = InterlockLog.objects.get(pk=session_id)
        if not session.session_complete:
            session.heartbeat()
            return JsonResponse({"access": True, "timestamp": round(time.time())})

        else:
            return JsonResponse(
                {
                    "access": False,
                    "error": "Session already ended.",
                    "timestamp": round(time.time()),
                }
            )

    try:
        user = Profile.objects.get(rfid=rfid_code).user

    except ObjectDoesNotExist:
        log_event(
            "Tried to check access for non existent user (or rfid not set).",
            "error",
            request,
        )
        print(
            "Tried to check access on ({}) for non existent user ({}) or rfid not set.".format(
                interlock_id, rfid_code
            )
        )
        return JsonResponse(
            {
                "access": False,
                "error": "Tried to check access for non existent user (or rfid not set).",
                "timestamp": round(time.time()),
            }
        )

    if interlock_id is not None:
        try:
            interlock = Interlock.objects.get(pk=interlock_id)
            interlock.checkin()

        except ObjectDoesNotExist:
            log_event(
                "Tried to check access for non existent interlock.", "error", request
            )
            print(
                "Tried to check access for non existent interlock ({}) user ({}).".format(
                    interlock_id, rfid_code
                )
            )
            return JsonResponse(
                {
                    "access": False,
                    "error": "Tried to check access for non existent interlock.",
                    "timestamp": round(time.time()),
                }
            )

    else:
        interlock_ip = request.META.get("HTTP_X_REAL_IP")

        try:
            interlock = Interlock.objects.get(ip_address=interlock_ip)
            interlock.checkin()

        except ObjectDoesNotExist:
            log_event(
                "Tried to check access for {} interlock but none found.".format(
                    interlock_ip
                ),
                "error",
                request,
            )
            print(
                "Tried to check access for non existent interlock ({}) ip ({}).".format(
                    interlock_id, interlock_ip
                )
            )
            return JsonResponse(
                {
                    "access": False,
                    "error": "Tried to check access for {} interlock but none found.".format(
                        interlock_ip
                    ),
                    "timestamp": round(time.time()),
                }
            )

    if user.profile.state == "active":
        if interlock.locked_out:
            post_interlock_swipe_to_discord(
                user.profile.get_full_name(), interlock.name, "maintenance_lock_out"
            )
            return JsonResponse(
                {
                    "access": False,
                    "error": "Maintenance lockout enabled.",
                    "timestamp": round(time.time()),
                }
            )

        allowed_interlocks = user.profile.interlocks.all()

        if allowed_interlocks:
            if interlock in allowed_interlocks:
                # user has access

                # if the user isn't signed into site and the interlock isn't exempt
                if (
                    not user.profile.is_signed_into_site()
                    and interlock.exempt_signin is False
                ):
                    user.profile.update_last_seen()
                    post_door_swipe_to_discord(
                        user.profile.get_full_name(), interlock.name, "not_signed_in"
                    )
                    return JsonResponse(
                        {"access": False, "name": user.profile.first_name}
                    )

                session = interlock.create_session(user)
                user.profile.update_last_seen()
                post_interlock_swipe_to_discord(
                    user.profile.get_full_name(), interlock.name, "activated"
                )
                if interlock.play_theme:
                    play_theme_song(user)

                return JsonResponse(
                    {
                        "access": True,
                        "session_id": session.id,
                        "timestamp": round(time.time()),
                        "name": user.profile.first_name,
                    }
                )

    # if they are inactive or don't have access
    user.profile.update_last_seen()
    post_interlock_swipe_to_discord(
        user.profile.get_full_name(), interlock.name, "rejected"
    )
    return JsonResponse({"access": False, "name": user.profile.first_name})
コード例 #4
0
def check_door_access(request, rfid_code, door_id=None):
    try:
        user = Profile.objects.get(rfid=rfid_code).user

    except ObjectDoesNotExist:
        log_event(
            "Tried to check access for non existent user (or rfid not set).",
            "error",
            request,
        )
        print(
            "Tried to check access on ({}) for non existent user ({}) or rfid not set.".format(
                door_id, rfid_code
            )
        )
        return JsonResponse(
            {
                "access": False,
                "error": "Tried to check access for non existent user (or rfid not set).",
                "timestamp": round(time.time()),
            }
        )

    if door_id is not None:
        try:
            door = Doors.objects.get(pk=door_id)
            door.checkin()

        except ObjectDoesNotExist:
            log_event("Tried to check access for non existent door.", "error", request)
            print("Tried to check access for non existent door ({}).".format(door_id))
            return JsonResponse(
                {
                    "access": False,
                    "error": "Tried to check access for non existent door",
                    "timestamp": round(time.time()),
                }
            )

    else:
        door_ip = request.META.get("HTTP_X_REAL_IP")

        try:
            door = Doors.objects.get(ip_address=door_ip)
            door.checkin()

        except ObjectDoesNotExist:
            log_event(
                "Tried to check access for door {} but none found. (or IP not set)".format(
                    door_ip
                ),
                "error",
                request,
            )
            print(
                "Tried to check access for non existent door ({}) ip ({}).".format(
                    door_id, door_ip
                )
            )
            return JsonResponse(
                {
                    "access": False,
                    "error": "Tried to check access for door {} but none found. (or IP not set)".format(
                        door_ip
                    ),
                    "timestamp": round(time.time()),
                }
            )

    if user.profile.state == "active":
        allowed_doors = user.profile.doors.all()

        if door.locked_out:
            post_interlock_swipe_to_discord(
                user.profile.get_full_name(), door.name, "maintenance_lock_out"
            )
            return JsonResponse(
                {
                    "access": False,
                    "error": "Maintenance lockout enabled.",
                    "timestamp": round(time.time()),
                }
            )

        if allowed_doors:
            if door in allowed_doors:
                # user has access

                # if the user isn't signed into site and the door isn't exempt from this
                if (
                    not user.profile.is_signed_into_site()
                    and door.exempt_signin is False
                ):
                    user.profile.update_last_seen()
                    post_door_swipe_to_discord(
                        user.profile.get_full_name(), door.name, "not_signed_in"
                    )
                    return JsonResponse(
                        {"access": False, "name": user.profile.first_name}
                    )

                door.log_access(user.id)
                user.profile.update_last_seen()
                post_door_swipe_to_discord(
                    user.profile.get_full_name(), door.name, True
                )
                if door.play_theme:
                    play_theme_song(user)

                return JsonResponse({"access": True, "name": user.profile.first_name})

    # if the are inactive or don't have access
    user.profile.update_last_seen()
    post_door_swipe_to_discord(user.profile.get_full_name(), door.name, False)
    return JsonResponse({"access": False, "name": user.profile.first_name})