예제 #1
0
    def post(self, accountId):
        data = request.get_json()

        #   Verifying data required for endpoint was given
        if not data or not data.get("vinNumber"):
            return res.badRequestError("Missing data to process request.")

        #   Verifying car given exists in database
        queryCar = Car.query.filter_by(vin=data.get("vinNumber")).first()
        if not queryCar:
            return res.resourceMissing("Car {} does not exist.".format(
                data.get("vinNumber")))

        #   Verifying car given is NOT registered under the given customer
        queryCarUser = CarUser.query.filter_by(car=queryCar.id,
                                               account=accountId).first()
        if queryCarUser:
            return res.resourceExistsError(
                "Car already registered under customer {}.".format(accountId))

        #   Creata the link from user to the registered car in the database
        newCarUserDetails = {"car": queryCar.id, "account": accountId}
        newCarUser, error = caruser_schema.load(newCarUserDetails)
        if error:
            return res.internalServiceError(error)
        db.session.add(newCarUser)
        db.session.commit()
        return res.postSuccess("Car {} registered under customer {}.".format(
            data.get("vinNumber"), accountId))
예제 #2
0
    def post(self, adminId):
        #   Verifying data required was given
        data = request.get_json()
        if not data or not data.get("vin") or not data.get(
                "carmodel") or not data.get("raspbpi"):
            return res.badRequestError("Missing data to process POST request.")

        #   Verifying vin number is not duplicated
        queryvin = Car.query.filter_by(vin=data.get("vin")).first()
        if queryvin:
            return res.resourceExistsError("VIN {} already registered.".format(
                data.get("vin")))

        #   Verifying raspberry pi is not duplicated
        querypi = Car.query.filter_by(raspbpi=data.get("raspbpi")).first()
        if querypi:
            return res.resourceExistsError(
                "Raspberrypi {} already linked to car {}".format(
                    data.get("raspbpi"), querypi.id))

        #   Adding Car to Database
        newCarDetails = {
            "vin": data.get("vin"),
            "carmodel": data.get("carmodel"),
            "raspbpi": data.get("raspbpi")
        }
        newCar, error = car_schema.load(newCarDetails)
        if error:
            return res.badRequestError(error)
        db.session.add(newCar)
        db.session.commit()
        return res.postSuccess("Successful car added to database.", newCar)
예제 #3
0
    def post(self, accountId):
        data = request.get_json()
        print(json.dumps(data, indent=4, sort_keys=True))

        #   Verifying required data is available to proccess request
        if not data or not data.get("appDetails") or not data.get(
                "accountDetails") or not data.get("carDetails"):
            return res.badRequestError("Missing data to process request.")
        dataAppDetails = data.get("appDetails")
        dataAppVersionDetails = dataAppDetails["appversionDetails"]
        dataAccountDetails = data.get("accountDetails")
        dataCarDetails = data.get("carDetails")
        if not dataAppDetails or not dataAppVersionDetails or not dataAccountDetails or not dataCarDetails:
            return res.badRequestError("Missing data to process request.")

        #   Finding requested app package in server
        appReqDownloadPath = os.path.join(
            app.config["UPLOAD_FOLDER"],
            os.path.join(dataAppDetails["appname"],
                         dataAppVersionDetails["version"]))
        if os.path.exists(appReqDownloadPath) == False:
            print("wrong path")
            return res.badRequestError("App executable files not found.")

        #   Saves customer download request
        apprequestDetails = {
            "requesttype": 3,
            "appversion": dataAppVersionDetails["id"],
            "customer": accountId,
            "car": dataCarDetails["id"]
        }
        customerAppRequest, error = apprequest_schema.load(apprequestDetails)
        if error:
            return res.internalServiceError(error)
        customerAppRequest.status = 5  # sets status as download request
        db.session.add(customerAppRequest)
        db.session.commit()

        #   Connect to client vehicle device
        remoteAppDirectory = os.path.join(app.config["PI_REMOTE_DIR"],
                                          dataAppDetails["appname"])
        error, sshI = ssh.SSHconnect(app.config["PI_IP"], 22)
        if error < 0:
            return res.internalServiceError(sshI)

        #   Download and Install app in remote device
        ssh.createAppDirectory(sshI, remoteAppDirectory)
        ssh.sendFile(sshI, appReqDownloadPath,
                     os.path.join(remoteAppDirectory, "App.zip"))
        ssh.installApp(sshI, remoteAppDirectory, "helloworld.py")
        print(sshI)

        return res.postSuccess("App installation successful.")
예제 #4
0
 def post(self):
     #   Verifying request body is not empty
     data = request.get_json()
     if not data or not data.get("username") or not data.get("password"):
         return res.badRequestError("Missing information to login user.")
     #   Verifies user exists in database
     query = Account.query.filter_by(username=data.get("username")).first()
     if not query:
         return res.badRequestError("Incorrect login information.")
     if query.active == False:
         return res.badRequestError("This account is deactivated.")
     if query.password != data.get("password"):
         return res.resourceMissing("Incorrect password for user {}.".format(query.username))
     return res.postSuccess(data=account_schema.dump(query).data)
예제 #5
0
 def post(self):
     data = request.get_json()
     if not data or not data.get("firstname") or not data.get("lastname") or not data.get("username") or not data.get("password"):
         return res.badRequestError("Missing information to process account creation.")
     #   check username is unique
     query = Account.query.filter_by(username=data.get("username")).first()
     if query:
         return res.resourceExistsError("Username {} already taken.".format(data.get("username")))
     
     newAccountDetails = {"firstname": data.get("firstname"), "lastname": data.get("lastname"), "username": data.get("username"), "password": data.get("password") }
     newAccount, error = account_schema.load(newAccountDetails)
     if error:
         return res.internalServiceError(error)
     db.session.add(newAccount)
     db.session.commit()
     return res.postSuccess("Account succesfully created for username {}.".format(newAccount.username), account_schema.dump(newAccount).data)
예제 #6
0
    def post(self):
        data = request.get_json()
        print(request.files)
        print(data)
        if not data or not data.get("accountId") or not data.get(
                "author") or not data.get("appName"):
            return res.badRequestError("Missing data to process request")

        #   Checks if app name already exists
        query = Application.query.filter_by(
            appname=data.get('appName')).first()
        if query is not None:
            return res.resourceExistsError("App name {} already taken".format(
                data.get('appName')))

        #   Validates and saves app data given
        appDetails = {
            "appname": data.get('appName'),
            "author": data.get("author"),
            "description": data.get('appDescription')
        }
        newApp, error = application_schema.load(appDetails)
        if error:
            return res.badRequestError(error)
        db.session.add(newApp)
        db.session.commit()

        #   Create app directory
        ServUtil.createAppDir(
            os.path.join(app.config["UPLOAD_FOLDER"], newApp.appname))

        #   Add permission to developer over created application
        linked, msg = ServUtil.addDevelopertoApp(
            db, {
                "appid": newApp.id,
                "accountid": data.get("accountId")
            })
        if not linked:
            return res.internalServiceError(msg)

        return res.postSuccess(
            "Succesfully created application {}.".format(newApp.appname),
            application_schema.dump(newApp).data)
예제 #7
0
 def post(self):
     data = request.get_json()
     #print(data)
     if not data or not data.get("versionNumber") or not data.get(
             "checksum") or not data.get("accountId") or not data.get(
                 "appversionId") or not data.get(
                     "requestType") or not data.get("appName"):
         return res.badRequestError("Missing data to create app request")
     requestDetails = {
         "developer": data.get("accountId"),
         "appversion": data.get("appversionId"),
         "requesttype": data.get("requestType")
     }
     appRequest, error = apprequest_schema.load(requestDetails)
     print(appRequest)
     if error:
         return res.internalServiceError(error)
     db.session.add(appRequest)
     db.session.commit()
     kabascript = "/nfs/2017/d/dmontoya/42/fordappstore/infrastructure/containers/raspbian/kabascriptest.sh"  # using test kaba for nows
     #   sh run.sh App.zip ya-ya b7709364cb0e7a86416f17fa3ff35b903077e8fa9e94c7e580ad7d57d5a6d509 1
     appName = data.get("appName")
     appName = appName.replace(' ', '\ ')
     appZipPath = os.path.join(app.config["UPLOAD_FOLDER"], appName,
                               data.get("versionNumber"), "App.zip")
     appandversion = "{}-{}".format(data.get("appName"),
                                    data.get("versionNumber"))
     print(data.get("checksum"))
     print(kabascript)
     print(appandversion)
     print(appZipPath)
     print(str(appRequest.id))
     subprocess.call([
         "sh", kabascript, appZipPath, appandversion,
         data.get("checksum"),
         str(appRequest.id)
     ])
     return res.postSuccess("Request succesfully created.",
                            apprequest_schema.dump(appRequest).data)
예제 #8
0
    def post(self, appId):
        data = request.form
        print(data)
        print(request.files)

        #   Verifies data required was sent in request
        if not data or not data.get("appName") or not data.get(
                "versionNumber") or not data.get("checksum") or not data.get(
                    "versionDescription"):
            return res.badRequestError("Missing data to process request.")

        #   Verifies app version file was sent in request
        if 'file' not in request.files:
            return res.badRequestError("Missing app file.")
        file = request.files['file']

        if "icon" not in request.files:
            return res.badRequestError("Send me an icon.")
        icon = request.files['icon']
        iconPath = os.path.join(app.config["UPLOAD_FOLDER"],
                                data.get("appName"), "Icon")
        print(iconPath)
        saved, msg = ServUtil.saveIcon(icon, iconPath)
        if not saved:
            return res.internalServiceError(msg)

        checksum = ServUtil.checksum_sha256(file)

        if data.get("checksum") == checksum:
            print("good checksum")
        else:
            return res.badRequestError("File corrupted.")
        file.seek(0)
        #   Verifies app exists
        queryApp = Application.query.filter_by(id=appId).first()
        if not queryApp:
            return res.resourceMissing("App {} not found.".format(appId))

        queryAppVersion = ApplicationVersion.query.filter_by(
            app=appId, version=data.get("versionNumber")).first()
        if queryAppVersion:
            return res.resourceExistsError(
                "Duplicate {} v.{} has already been submitted for review.".
                format(queryApp.appname, queryAppVersion.version))

        #   Validates data and creates application version
        appVersionDetails = {
            "app": appId,
            "version": data.get("versionNumber"),
            "description": data.get("versionDescription")
        }
        newappVersion, error = applicationversion_schema.load(
            appVersionDetails)
        if error:
            return res.internalServiceError(error)
        db.session.add(newappVersion)
        db.session.commit()

        #   Saves version files
        saved, msg = ServUtil.saveinAppVersion(app, file, data)
        if not saved:
            return res.internalServiceError(message=msg)

        return res.postSuccess(
            "{} v.{} successfully created !".format(queryApp.appname,
                                                    newappVersion.version),
            applicationversion_schema.dump(newappVersion).data)