Ejemplo n.º 1
0
    def get(self, request, droplet_id):
        params = {}
        request_user = request.user
        user_data = get_user_role(request_user)
        if user_data[0] is False:
            return Response(user_data[1], status=user_data[1]["status"])

        user_data = user_data[1]
        user = user_data["user"]

        try:
            droplet = Droplet.objects.get(droplet_id=droplet_id)
        except Droplet.DoesNotExist:
            params["message"] = "Invalid droplet"
            params["status"] = 404
            return Response(params, status=params["status"])
        # TODO: Only if the droplet is in the teachers class should the
        # TODO: teacher be able to delete it
        if user_data["is_teacher"] is True:
            droplet.delete()
            params["message"] = "Successfully deleted Droplet"
            params["status"] = 200
        else:
            droplets = Droplet.objects.filter(owner=user.user)
            if droplet in droplets:
                droplet.delete()
                params["message"] = "Successfully deleted Droplet"
                params["status"] = 200
            else:
                params["message"] = "User does not own specified Droplet"
                params["status"] = 403
        return Response(params, status=params["status"])
Ejemplo n.º 2
0
    def get(self, request, class_id):
        params = {}
        request_user = request.user
        user_data = get_user_role(request_user)[1]

        if user_data["is_student"] is False:
            params[
                "message"] = "User is not a student, therefore cannot be enrolled in a class"
            params["status"] = 401
            return Response(params, params["status"])

        c = class_exists(class_id)
        if c is None:
            params["message"] = f"Class with id {class_id} does not exist"
            params["status"] = 404
            return Response(params, status=params["status"])

        if c in user_data["user"].classes.all():
            params["message"] = "Student is enrolled."
            params["status"] = 200
        else:
            params["message"] = "Student is not enrolled."
            params["status"] = 401

        return Response(params, params["status"])
Ejemplo n.º 3
0
    def get(self, request, class_id):
        params = {}
        request_user = request.user

        clas = class_exists(class_id)
        if clas is None:
            params["message"] = "Invalid class"
            params["status"] = 404
            return Response(params, status=params["status"])

        user_data = get_user_role(request_user, clas)

        if user_data[0] is False:
            return Response(user_data[1], status=user_data[1]["status"])

        user_data = user_data[1]
        if (
            user_data["is_in_class"] is not True
            and user_data["teaches_class"] is not True
        ):
            params[
                "message"
            ] = "Only teachers of the class or students in the class can view droplet count"
            params["status"] = 403
            return Response(params, status=params["status"])

        droplets = Droplet.objects.filter(class_id=clas)
        params["droplet_count"] = len(droplets)

        return Response(params, status=params.get("status", 200))
Ejemplo n.º 4
0
    def get(self, request, class_id=None):
        params = {}
        request_user = request.user
        user_data = get_user_role(request_user)
        if user_data[0] is False:
            return Response(user_data[1], status=user_data[1]["status"])

        user_data = user_data[1]
        user = user_data["user"]

        droplets = Droplet.objects.filter(owner=user.user)

        if len(droplets) == 0:
            params["message"] = "User has no droplets associated with them"
            params["status"] = 200
            return Response(params, status=params["status"])

        for droplet in droplets:
            droplet.delete()

        params["message"] = "{0} droplets deleted".format(len(droplets))
        params["num_droplets_deleted"] = len(droplets)
        params["status"] = 200

        return Response(params, status=params["status"])
Ejemplo n.º 5
0
 def validate(self, attrs):
     data = super().validate(attrs)
     data.update({
         'user':
         dict(
             pk=self.user.pk,
             full_name=self.user.full_name,
             email=self.user.email,
             role=get_user_role(self.user.pk),
         )
     })
     return data
Ejemplo n.º 6
0
 def validate(self, attrs):
     data = super().validate(attrs)
     decoded_payload = token_backend.decode(data['access'], verify=True)
     user_pk = decoded_payload['user_id']
     user_model = get_user_model()
     user = user_model.objects.get(id=user_pk)
     data.update({
         'user':
         dict(
             pk=user.pk,
             full_name=user.full_name,
             email=user.email,
             role=get_user_role(user.pk),
         )
     })
     return data
Ejemplo n.º 7
0
    def get(self, request, class_id):
        params = {}
        request_user = request.user

        clas = class_exists(class_id)
        if clas is None:
            params["message"] = "Invalid class"
            params["status"] = 404
            return Response(params, status=params["status"])

        user_data = get_user_role(request_user, clas)

        if user_data[0] is False:
            return Response(user_data[1], status=user_data[1]["status"])

        user_data = user_data[1]
        user = user_data["user"]
        if user_data["is_teacher"] is not True:
            params["message"] = "Only teachers can view class droplets"
            params["status"] = 403
            return Response(params, status=params["status"])

        if user_data["teaches_class"] is not True:
            params["message"] = "Only official class teachers can view class droplets"
            params["status"] = 403
            return Response(params, status=params["status"])

        droplets = Droplet.objects.filter(class_id=clas)
        params["droplets"] = []
        for droplet in droplets:
            params["droplets"].append(
                {
                    "name": droplet.name,
                    "owner": str(droplet.owner),
                    "owner_email": droplet.owner.email,
                    "owner_id": droplet.owner.id,
                    "initial_password": droplet.initial_password,
                    "ip_addr": droplet.ip_addr,
                    "class": str(droplet.class_id),
                    "class_id": droplet.class_id.id,
                    "droplet_id": droplet.droplet_id,
                }
            )

        return Response(params, status=params.get("status", 200))
Ejemplo n.º 8
0
    def get(self, request):
        data = get_user_role(request.user)

        params = {"classes": {}}

        if data[0] is False:
            params["message"] = "No classes found"
            params["status"] = 404
            del params["classes"]
            return Response(params, status=params["status"])

        user_data = data[1]

        if user_data["is_student"] is True:
            student = user_data["student_object"]
            classes = list(student.classes.all())
            student_classes = []
            for clas in classes:
                student_classes.append({"name": clas.name, "id": clas.id})
            params["classes"]["student"] = student_classes
        if user_data["is_teacher"] is True:
            teacher = user_data["teacher_object"]
            classes = list(teacher.classes.all())
            teacher_classes = []
            for clas in classes:
                teacher_classes.append({"name": clas.name, "id": clas.id})
            params["classes"]["teacher"] = teacher_classes

        if (params["classes"].get("student", None) is None
                and params["classes"].get("teacher", None) is None):
            params["message"] = "No classes found"
            params["status"] = 404
            del params["classes"]

        params["status"] = 200

        return Response(params, status=params.get("status", 200))
Ejemplo n.º 9
0
 def has_permission(self, request, view):
     user_role = get_user_role(request.user)
     return user_role == self.user_role
Ejemplo n.º 10
0
    def post(self, request):
        params = {}
        request_user = request.user
        class_id = request.data.get("class_id", None)
        passcode = request.data.get("passcode", None)

        if class_id is None or passcode is None:
            params["message"] = "Missing parameters class_id or passcode"
            params["status"] = 400
            return Response(params, params["status"])

        try:
            class_id = int(class_id)
        except ValueError:
            params["message"] = "class_id must be numerical"
            params["status"] = 400
            return Response(params, params["status"])

        c = class_exists(class_id)
        if c is None:
            params["message"] = f"Class with id {class_id} does not exist"
            params["status"] = 404
            return Response(params, status=params["status"])

        if c.allow_registration is False:
            params["message"] = "Class not configured for registration"
            params["status"] = 403
            return Response(params, params["status"])

        user_data = get_user_role(request_user, c)[1]

        print(user_data)

        if user_data["teaches_class"] is True:
            params[
                "message"] = "User teaches this class, therefore cannot enroll"
            params["status"] = 401
            return Response(params, params["status"])

        if user_data["is_in_class"] is True:
            params["message"] = "User is already enrolled in this class"
            params["status"] = 401
            return Response(params, params["status"])

        if user_data["is_student"] is False:
            if user_data["user"] is None:
                student = Student.objects.create(user=request_user)
                student.classes.add(c)
                student.save()
                params["message"] = "User was enrolled in class"
                params["status"] = 200
            elif user_data["is_teacher"] is True:
                student = Student.objects.create(user=user_data["user"].user)
                student.classes.add(c)
                student.save()
                params["message"] = "User was enrolled in class"
                params["status"] = 200
        else:
            student = Student.objects.get(user=user_data["user"].user)
            student.classes.add(c)
            student.save()
            params["message"] = "User was enrolled in class"
            params["status"] = 200

        return Response(params, params["status"])
Ejemplo n.º 11
0
    def get(
        self, request, droplet_id, user_id, class_id,
    ):
        params = {}
        request_user = request.user
        user_data = get_user_role(request_user)
        if user_data[0] is False:
            return Response(user_data[1], status=user_data[1]["status"])

        user_data = user_data[1]
        user = user_data["user"]

        if user_data["is_teacher"] is False:
            params["message"] = "Only Teachers can assign resources"
            params["status"] = 403
            return Response(params, status=params["status"])

        try:
            droplet = Droplet.objects.get(droplet_id=droplet_id)
        except Droplet.DoesNotExist:
            params["message"] = "Droplet does not exist"
            params["status"] = 404
            return Response(params, status=params["status"])

        user_is_student = False
        user_is_teacher = False

        student = None
        teacher = None
        try:
            usr = User.objects.get(id=user_id)
        except User.DoesNotExist:
            params["message"] = "Invalid user"
            params["status"] = 400
            return Response(params, status=params["status"])

        try:
            student = Student.objects.get(user=usr)
            user_is_student = True
        except Student.DoesNotExist:
            pass

        try:
            teacher = Teacher.objects.get(user=usr)
            user_is_teacher = True
        except Teacher.DoesNotExist:
            pass

        if user_is_student is False and user_is_teacher is False:
            params["message"] = "Invalid user"
            params["status"] = 404
            return Response(params, status=params["status"])

        clas = class_exists(class_id)
        if clas is None:
            params["message"] = "Invalid class"
            params["status"] = 404
            return Response(params, status=params["status"])

        if clas not in user.classes.all():
            params["message"] = "Only the official class teacher can assign droplets"
            params["status"] = 403
            return Response(params, status=params["status"])

        recipient = None
        if teacher is None:
            recipient = student
        else:
            recipient = teacher

        if clas not in recipient.classes.all():
            params["message"] = "Recipient is not in specified class"
            params["status"] = 400
            return Response(params, status=params["status"])

        droplet.owner = recipient.user
        droplet.save()

        params["message"] = "Droplet successfully reassigned"
        params["status"] = 200
        return Response(params, status=params["status"])
Ejemplo n.º 12
0
    def get(self, request, droplet_id, power_option):
        options = ["power-off", "power-on", "status"]
        params = {}
        if power_option.lower() not in options:
            params[
                "message"
            ] = "Incorrect power option. Options are power-on, power-off, and status"
            params["status"] = 400
            return Response(params, status=params["status"])
        request_user = request.user
        user_data = get_user_role(request_user)
        if user_data[0] is False:
            return Response(user_data[1], status=user_data[1]["status"])

        user_data = user_data[1]
        user = user_data["user"]

        try:
            droplet = Droplet.objects.get(droplet_id=droplet_id)
        except Droplet.DoesNotExist:
            params["message"] = "Droplet does not exist"
            params["status"] = 404
            return Response(params, status=params["status"])

        if user_data["is_teacher"] is True:
            teacher_classes = Class.objects.filter(teacher=user)
        if user.user.id == droplet.owner.id or (
            user_data["is_teacher"] is True and droplet.class_id in teacher_classes
        ):
            if power_option == "power-off":
                power_off(settings.DO_TOKEN, droplet_id)
                params["message"] = "Droplet powered off"
                params["power_status"] = "off"
                params["status"] = 200
            elif power_option == "power-on":
                power_on(settings.DO_TOKEN, droplet_id)
                params["message"] = "Droplet powered on"
                params["power_status"] = "on"
                params["status"] = 200
            else:
                status = power_status(settings.DO_TOKEN, droplet_id)
                if status is None:
                    params["message"] = "Unable to determine power status"
                    params["status"] = 404
                elif status == "off":
                    params["message"] = "Droplet is powered off"
                    params["power_status"] = "off"
                    params["status"] = 200
                elif status == "active":
                    params["message"] = "Droplet is powered on"
                    params["power_status"] = "on"
                    params["status"] = 200
                else:
                    params["message"] = "Droplet power status is unknown by API"
                    params["power_status"] = status
                    params["status"] = 200

        else:
            params["message"] = "User does not own droplet"
            params["status"] = 403
        return Response(params)
Ejemplo n.º 13
0
    def get(self, request, class_id, count=1):
        """
        Create a droplet inside a specific class
        """
        params = {}
        request_user = request.user

        user_data = get_user_role(request_user)

        # If user data is False then the user is neither a teacher or student
        # and cannot create a droplet.
        if user_data[0] is False:
            return Response(user_data[1], status=user_data[1]["status"])

        user_data = user_data[1]
        user = user_data["user"]

        if count < 1 or count > 20:
            params[
                "message"
            ] = "Invalid amount for count. Can't be less than 1 or greater than 20"
            params["status"] = 400
            return Response(params, status=params["status"])

        clas = class_exists(class_id)
        if clas is None:
            params["message"] = "Invalid class"
            params["status"] = 404
            return Response(params, status=params["status"])

        if clas in user.classes.all():
            droplets = Droplet.objects.filter(owner=user.user, class_id=clas.id)
            if user_data["is_teacher"]:
                if count + len(droplets) > user.droplet_limit:
                    params["status"] = 403
                    params["message"] = "Max number of droplets reached"
                    return Response(params, status=params["status"])
            else:
                if count + len(droplets) > clas.droplet_student_limit:
                    params["status"] = 403
                    params["message"] = "Max number of droplets reached"
                    return Response(params, status=params["status"])

            params["droplets"] = []
            for i in range(0, count):
                droplet = Droplet(owner=user.user, class_id=clas)
                droplet.save()
                droplet_info = model_to_dict(droplet)
                del droplet_info["image"]
                del droplet_info["id"]
                del droplet_info["owner"]
                droplet_info["class"] = droplet.class_id.name
                params["droplets"].append(droplet_info)
                params["droplet_count"] = count

            params["message"] = "Droplet(s) created"
            params["status"] = 200
            # params["droplet-id"].append(droplet_id)
        else:
            params["message"] = "User is not in the class"
            params["status"] = 400
        return Response(params, status=params["status"])
Ejemplo n.º 14
0
    def get(self, request, droplet_id=None):
        params = {}
        request_user = request.user
        user_data = get_user_role(request_user)
        if user_data[0] is False:
            return Response(user_data[1], status=user_data[1]["status"])

        user_data = user_data[1]
        user = user_data["user"]

        if droplet_id is None:
            droplets = Droplet.objects.filter(owner=user.user)
            params["droplets"] = []
            params["droplet_count"] = len(droplets)
            for droplet in droplets:
                params["droplets"].append(
                    {
                        "name": droplet.name,
                        "owner": str(droplet.owner),
                        "owner_email": droplet.owner.email,
                        "owner_id": droplet.owner.id,
                        "initial_password": droplet.initial_password,
                        "ip_addr": droplet.ip_addr,
                        "class": str(droplet.class_id),
                        "class_id": droplet.class_id.id,
                        "droplet_id": droplet.droplet_id,
                    }
                )
            params["status"] = 200
        else:
            try:
                droplet = Droplet.objects.get(droplet_id=droplet_id)
            except Droplet.DoesNotExist:
                params["message"] = "Droplet does not exist"
                params["status"] = 404
                return Response(params, status=params["status"])
            if user_data["is_teacher"] is True:
                teacher_classes = Class.objects.filter(teacher=user)

            # Since we are comparing a User to a Student or teacher, we must
            # get the base class user id for both. User is either a student
            # or teacher, where the droplet owner is a Foreign key to a user.
            if user.user.id == droplet.owner.id or (
                user_data["is_teacher"] is True and droplet.class_id in teacher_classes
            ):
                params["droplet"] = {
                    "name": droplet.name,
                    "owner": str(droplet.owner),
                    "owner_email": droplet.owner.email,
                    "owner_id": droplet.owner.id,
                    "initial_password": droplet.initial_password,
                    "ip_addr": droplet.ip_addr,
                    "class": str(droplet.class_id),
                    "class_id": droplet.class_id.id,
                    "droplet_id": droplet.droplet_id,
                }
                params["status"] = 200
            else:
                params["message"] = "User doesn't have permission to view droplet"
                params["status"] = 403

        return Response(params, status=params["status"])