def delete(self, licenseID):
        license = License.query.filter_by(license_uuid=licenseID).first()
        if license is None:
            return build_response(
                status_code=404,
                error="Not Found",
                description="License ID provided does not exist")

        if license.status == "INACTIVE":
            return build_response(
                status_code=304,
                error="Not Modified",
                description="License ID provided is already cancelled")

        license.status = "INACTIVE"
        if license.license_type == "PRIVATE":
            response = requests.delete(license.validation_url,
                                       timeout=app.config["TIMEOUT"])
            if not response.status_code == 200:
                return build_response(
                    status_code=400,
                    error="Not Allowd",
                    description="Validate URL failed to cancel the license")

        db.session.commit()

        return build_response(status_code=200,
                              data=license.serialize,
                              description="License successfully cancelled")
Exemple #2
0
    def get(self, licenseID):
        license = License.query.get(licenseID)
        if license is None:
            return build_response(status_code=404, error="Not Found", description="License does not exist")

        if validate_license(license):
            return build_response(status_code=200, data=license.serialize, description="License is valid")
        else:
            return build_response(status_code=400, data="", error="License is not valid")
Exemple #3
0
    def get(self, typeID):
        type = Type.query.get(typeID)

        if type is None:
            return build_response(
                status_code=404,
                error="Invalid TypeID",
                description="Type ID provided does not exist")

        return build_response(status_code=200,
                              description="Type successfully retrieved",
                              data=type.serialize)
Exemple #4
0
    def get(self):
        if 'user_uuid' in request.args:
            try:
                user_uuid = request.args.get('user_uuid')
                UUID(user_uuid)
            except:
                self.log_bad_request()
                return build_response(status_code=400, error="Invalid field", description="user_uuid is not valid")
            licenses = License.query.filter_by(user_uuid=user_uuid).all()
        else:
            licenses = License.query.all()

        return build_response(status_code=200, data={"licenses": [o.serialize for o in licenses]}, description="Licenses list successfully retrieved")
Exemple #5
0
    def post(self):
        if 'description' not in request.form or 'duration' not in request.form:
            return build_response(
                status_code=400,
                error="Missing fields",
                description="Missing description or duration argument")

        new_type = Type(request.form['description'], request.form['duration'])
        db.session.add(new_type)
        db.session.commit()

        return build_response(status_code=200,
                              description="Type successfully created",
                              data=new_type.serialize)
Exemple #6
0
    def delete(self, typeID):
        type = Type.query.get(typeID)

        if type is None:
            return build_response(
                status_code=404,
                error="Invalid TypeID",
                description="Type ID provided does not exist")

        type.status = "SUSPENDED"
        db.session.commit()

        return build_response(status_code=200,
                              description="Type successfully deleted",
                              data=type.serialize)
    def head(self, licenseID):
        license = License.query.get(licenseID)
        if license is None:
            return build_response(status_code=404,
                                  error="Not Found",
                                  description="License does not exist")

        if validate_license(license):
            return build_response(status_code=200,
                                  data="",
                                  description="License is valid")
        else:
            return build_response(status_code=400,
                                  data="",
                                  error="License is not valid")
Exemple #8
0
    def delete(self, licenseID):
        license = License.query.filter_by(license_uuid=str(licenseID)).first()
        if license is None:
            return build_response(status_code=404, error="Not Found", description="License ID provided does not exist")

        if license.status == "INACTIVE":
            return build_response(status_code=304, error="Not Modified", description="License ID provided is already cancelled")

        license.status = "INACTIVE"
        if license.license_type == "PRIVATE":
            response = requests.delete(license.validation_url, timeout=app.config["TIMEOUT"])
            if not response.status_code == 200:
                return build_response(status_code=400, error="Not Allowed", description="Validate URL failed to cancel the license")

        db.session.commit()

        return build_response(status_code=200, data=license.serialize, description="License successfully cancelled")
Exemple #9
0
    def head(self, licenseID):
        license = License.query.get(licenseID)
        if license is None:
            return build_response(status_code=404,
                                  error="Not Found",
                                  description="License does not exist")

        if not license.status == "ACTIVE":
            return build_response(status_code=400,
                                  data="",
                                  error="License is not valid")

        if license.startingDate > datetime.now():
            build_response(status_code=400,
                           data="",
                           error="License is not valid")

        if license.expiringDate < datetime.now():
            build_response(status_code=400,
                           data="",
                           error="License is not valid")

        return build_response(status_code=200,
                              data="",
                              description="License is valid")
Exemple #10
0
    def delete(self, licenseID):
        license = License.query.filter_by(license_uuid=licenseID).first()
        if license is None:
            return build_response(
                status_code=404,
                error="Not Found",
                description="License ID provided does not exist")

        if not license.status == "ACTIVE":
            return build_response(
                status_code=304,
                error="Not Valid",
                description="License ID provided is already cancelled")

        license.status = "INACTIVE"
        db.session.commit()

        return build_response(status_code=200,
                              data=license.serialize,
                              description="License successfully cancelled")
Exemple #11
0
    def put(self, licenseID):
        license = License.query.filter_by(license_uuid=licenseID).first()
        if license is None:
            return build_response(
                status_code=404,
                error="Not Found",
                description="License ID provided does not exist")

        if not license.status == "ACTIVE":
            return build_response(
                status_code=400,
                error="Not Valid",
                description="License ID provided is not active")

        license.status = "SUSPENDED"
        db.session.commit()

        return build_response(status_code=200,
                              data=license.serialize,
                              description="License successfully suspended")
Exemple #12
0
    def get(self):
        if 'user_uuid' in request.args:
            licenses = License.query.filter_by(
                user_uuid=request.args.get('user_uuid')).all()
        else:
            licenses = License.query.all()

        return build_response(
            status_code=200,
            data={"licenses": [o.serialize for o in licenses]},
            description="Licenses list successfully retrieved")
Exemple #13
0
    def get(self):
        status = 'all'
        if 'status' in request.args:
            status = request.args.get('status')

        if status == 'all':
            type = Type.query.all()
        elif status == 'active':
            type = Type.query.filter_by(status="ACTIVE").all()
        elif status == 'suspended':
            type = Type.query.filter_by(active="SUSPENDED").all()

        return build_response(status_code=200,
                              description="Types list successfully retrieved",
                              data={"types": [o.serialize for o in type]})
Exemple #14
0
    def post(self, licenseID):
        try:
            license = License.query.get(licenseID)
            if license is None:
                return build_response(
                    status_code=404,
                    error="Not Found",
                    description="License ID provided does not exist")

            if 'user_uuid' not in request.form:
                return build_response(status_code=400,
                                      error="Missing Field",
                                      description="user_uuid was not provided")

            if license.user_uuid != request.form['user_uuid']:
                return build_response(
                    status_code=400,
                    error="Invalid User",
                    description=
                    "user_uuid provided is not the one that owns the license")

            license_type = Type.query.get(license.type_uuid)
            if 'type_uuid' in request.form:
                license_type = Type.query.get(request.form['type_uuid'])
                if not license_type:
                    return build_response(
                        status_code=404,
                        error="Invalid License Type",
                        description=
                        "License type provided to change does not exist")
                license.type_uuid = license_type.type_uuid

            baseExpiringDate = license.expiringDate
            if license.expiringDate < datetime.now():
                baseExpiringDate = datetime.now()

            license.expiringDate = baseExpiringDate + timedelta(
                days=license_type.duration)

        except:
            return build_response(
                status_code=400,
                error="Invalid arguments",
                description="Arguments provided were invalid")

        db.session.commit()

        return build_response(status_code=200,
                              data=license.serialize,
                              description="License successfully renewed")
Exemple #15
0
    def post(self):
        try:
            service_uuid = request.form['service_uuid']
            user_uuid = request.form['user_uuid']

            license = License.query.filter_by(
                user_uuid=user_uuid, service_uuid=service_uuid).first()
            if license != None:
                return build_response(
                    status_code=400,
                    error="Already exists",
                    description=
                    "License for that user to that service already exists",
                    data=license.serialize)

            license_type = "PUBLIC"
            if 'license_type' in request.form:
                if request.form["license_type"].upper(
                ) in License.license_types:
                    license_type = request.form["license_type"].upper()
                else:
                    return build_response(
                        status_code=400,
                        error="Invalid field",
                        description="License type parameter was invalid")

            validation_url = None
            if license_type == "PRIVATE":
                if 'validation_url' not in request.form:
                    return build_response(
                        status_code=400,
                        error="Missing fields",
                        description=
                        "Missing validation_url field for private license type"
                    )
                else:
                    validation_url = request.form['validation_url']

            status = "ACTIVE"
            if 'status' in request.form:
                if request.form["status"].upper() in License.valid_status:
                    status = request.form["status"].upper()
                else:
                    return build_response(
                        status_code=400,
                        error="Invalid field",
                        description="Status parameter was invalid")

            new_license = License(service_uuid, user_uuid,
                                  request.form.get('description'),
                                  validation_url, status, license_type)

        except:
            return build_response(
                status_code=400,
                error="Missing fields",
                description="Missing service_uuid or user_uuid argument")

        db.session.add(new_license)
        db.session.commit()
        return build_response(status_code=200,
                              data=new_license.serialize,
                              description="License successfully created")
Exemple #16
0
    def post(self):
        try:
            content=json.loads(request.data)
            service_uuid = content['service_uuid']
            user_uuid = content['user_uuid']
            try:
                UUID(service_uuid)
            except:
                self.log_bad_request()
                return build_response(status_code=400, error="Invalid field", description="service_uuid is not valid")
            try:
                UUID(user_uuid)
            except:
                self.log_bad_request()
                return build_response(status_code=400, error="Invalid field", description="user_uuid is not valid")

            try:
                license = License.query.filter_by( user_uuid=user_uuid,
                                                   service_uuid=service_uuid
                                                 ).first()
            except:
                self.log_bad_request()
                return build_response(status_code=500, error="Connection error", description="Could not connect to database")

            if license != None:
                return build_response(status_code=409, error="Already exists", description="License for that user to that service already exists", data=license.serialize)

            license_type = "PUBLIC"
            if 'license_type' in content:
                if content['license_type'].upper() in License.license_types:
                    license_type = content['license_type'].upper()
                else:
                    return build_response(status_code=400, error="Invalid field", description="License type parameter was invalid")

            validation_url = None
            if license_type == "PRIVATE":
                if 'validation_url' not in content:
                    return build_response(status_code=400, error="Missing fields", description="Missing validation_url field for private license type")
                else:
                    validation_url = content['validation_url']

            status = "ACTIVE"
            if 'status' in content:
                if content["status"].upper() in License.valid_status:
                    status = content["status"].upper()
                else:
                    return build_response(status_code=400, error="Invalid field", description="Status parameter was invalid")

            if 'description' in content:
                description = content['description']
            else:
                description = None

            new_license = License(  service_uuid,
                                    user_uuid,
                                    description,
                                    validation_url,
                                    status,
                                    license_type)

        except:
            self.log_bad_request()
            return build_response(status_code=400, error="Missing fields", description="Missing service_uuid or user_uuid argument")

        db.session.add(new_license)
        db.session.commit()
        return build_response(status_code=201, data=new_license.serialize, description="License successfully created")
    def test_build_response(self):
        response = app.build_response(202)
        self.assertEqual(response, {"status": "success"})

        response = app.build_response(500)
        self.assertEqual(response, {"status": "error"})
Exemple #18
0
    def post(self):
        try:
            service_uuid = request.form['service_uuid']
            user_uuid = request.form['user_uuid']
            type_uuid = request.form['type_uuid']

            license = License.query.filter_by(user_uuid=user_uuid,
                                              service_uuid=service_uuid,
                                              type_uuid=type_uuid).first()
            if license != None:
                return build_response(
                    status_code=304,
                    error="Already exists",
                    description=
                    "License for that user to that service of this type already exists",
                    data=license.serialize)

            license_type = Type.query.filter_by(
                type_uuid=request.form['type_uuid']).first()
            if license_type is None:
                return build_response(
                    status_code=404,
                    error="Not Found",
                    description="License type provided does not exist")
            if license_type.status == "SUSPENDED":
                return build_response(
                    status_code=400,
                    error="Suspended",
                    description="License type required exists but is not active"
                )

            startingDate = datetime.now()
            if not (request.form.get('startingDate') is None):
                startingDate = datetime.strptime(
                    str(request.form.get('startingDate')), "%d-%m-%Y %H:%M")

            expiringDate = startingDate + timedelta(days=license_type.duration)

            status = "ACTIVE"
            if 'status' in request.form:
                if request.form["status"].upper() in License.valid_status:
                    status = request.form["status"].upper()
                else:
                    return build_response(
                        status_code=400,
                        error="Invalid field",
                        description="Status parameter was invalid")

            new_license = License(license_type.type_uuid, service_uuid,
                                  request.form['user_uuid'],
                                  request.form.get('description'),
                                  startingDate, expiringDate, status)

        except:
            return build_response(
                status_code=400,
                error="Missing fields",
                description=
                "Missing type_uuid, service_uuid or user_uuid argument")

        db.session.add(new_license)
        db.session.commit()
        return build_response(status_code=200,
                              data=new_license.serialize,
                              description="License successfully created")