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)
def put(self, appversionId): print(appversionId) queryAppVersion = ApplicationVersion.query.filter_by( id=appversionId).first() queryApp = Application.query.filter_by(id=queryAppVersion.app).first() # Ensures app exists in database if not queryAppVersion: return res.resourceMissing( "No record with {} found for any app version.".format( appversionId)) if queryApp.runningversion > queryAppVersion.id: return res.badRequestError("Running version > than this version.") # Ensures app is approved if queryAppVersion.status is not 4: print("awaiting to be approved") return res.resourceExistsError( "App v.{} is still awaiting to be approved.".format( queryAppVersion.version)) queryApp.runningversion = queryAppVersion.id queryAppVersion.status = 2 queryApp.active = True db.session.commit() return res.putSuccess( "{} v.{} launched. Now available in MakitiAppStore !".format( queryApp.appname, queryAppVersion.version))
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))
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)
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)
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)