def stripe_payment(): try: data = request.get_json() except Exception: return ErrorResponse(PayloadNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() try: token = data['stripe_refresh_token'] except Exception: return ErrorResponse(PayloadNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() try: charge = stripe.Charge.create(amount=data['amount'], currency=data['currency'], customer=data['stripe_user_id'], source=token) user = User.getUser(user_id=data['uid']) resp = {'id': user.id, 'charge': charge} resp['status'] = 'Charge Created' except Exception: return ErrorResponse(OperationNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() return jsonify(StripePaymentSchema().dump(resp).data)
def update_profile_image(): try: data = request.get_json()['data']['attributes'] except Exception: return ErrorResponse(PayloadNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() if not data['image']: return ErrorResponse(ImageNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() if not data['extension']: return ErrorResponse(ExtensionNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() uid = data['uid'] image = data['image'] extension = data['extension'] try: imageName = saveToImage(imageFile=image, extension=extension) except Exception: return ErrorResponse(ImageNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() fetch_user, imageLink = update_database(uid, imageName) return jsonify(UpdateUserSchema().dump(fetch_user).data)
def uploadImage(): try: data = request.get_json()['imgFile'] image = data['imgFile'] except Exception: return ErrorResponse(PayloadNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() extension = data['extension'] try: imageName = saveToImage(imageFile=image, extension=extension) except Exception: return ErrorResponse(ImageNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() uid = data['uid'] fetch_user = User.getUser(user_id=uid) if fetch_user is None: return ErrorResponse( UserNotFound(uid).message, 422, { 'Content-Type': 'application/json' }).respond() file_upload = File(filename=imageName, filetype='image', uploader=fetch_user) file_upload.save_to_db() return jsonify(ImageFileSchema().dump(file_upload).data)
def upload_manual_data(): try: data = request.get_json()['data']['attributes'] except Exception: return ErrorResponse(PayloadNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() if not data.get('manual_data'): return ErrorResponse(ManualDataNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() uid = data.get('uid') manual_data = data.get('manual_data') fetch_user = User.getUser(user_id=uid) if fetch_user is None: return ErrorResponse( UserNotFound(uid).message, 422, { 'Content-Type': 'application/json' }).respond() try: csvName = saveAsCSV(csvData=manual_data) except Exception: return ErrorResponse(OperationNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() file_upload = File(filename=csvName, filetype='csv', uploader=fetch_user) file_upload.save_to_db() return jsonify(ManualFileSchema().dump(file_upload).data)
def upload_default(): try: data = request.get_json()['data']['attributes'] except Exception: return ErrorResponse(PayloadNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() uid = data.get('uid') image_name = data.get('defaultImage') image_data = None with open( os.path.join(app.config.get('BASE_DIR'), 'badge_backgrounds', image_name), "rb") as image_file: image_data = base64.b64encode(image_file.read()) try: imageName = saveToImage(imageFile=image_data.decode('utf-8'), extension=".png") except Exception as e: print(e) return ErrorResponse(ImageNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() fetch_user = User.getUser(user_id=uid) file_upload = File(filename=imageName, filetype='image', uploader=fetch_user) file_upload.save_to_db() return jsonify(DefImageSchem().dump(file_upload).data)
def changePwd(): try: data = request.get_json()['data']['attributes'] except Exception as e: print(e) return ErrorResponse(PayloadNotFound().message, 422, {'Content-Type': 'application/json'}).respond() token = data['token'] try: decoded_res = jwt.decode(token, app.config['SECRET_KEY']) except Exception as e: print(e) return ErrorResponse(SignatureExpired().message, 422, {'Content-Type': 'application/json'}).respond() user = User.getUser(user_id=decoded_res['id']) if 'pwd' not in data.keys(): return ErrorResponse(PasswordNotFound().message, 422, {'Content-Type': 'application/json'}).respond() pwd = data['pwd'] oldPwd = user.password user.password = generate_password_hash(pwd) user.save_to_db() resp = {'id': token} if update_firebase_password(user.id, pwd): resp['status'] = 'Changed' return jsonify(ResetPasswordOperation().dump(resp).data) else: print('Firebase not uploaded') user.password = oldPwd user.save_to_db() resp['status'] = 'Not Changed' return jsonify(ResetPasswordOperation().dump(resp).data)
def decorated(*args, **kwargs): token = request.headers.get('x-access-token') if not token: return ErrorResponse(PayloadNotFound().message, 422, {'Content-Type': 'application/json'}).respond() try: data = jwt.decode(token, app.config['SECRET_KEY']) if 'adminStatus' in data.keys(): return func(*args, **kwargs) return ErrorResponse(AdminNotFound().message, 422, {'Content-Type': 'application/json'}).respond() except Exception as e: print(e)
def oauth_token(): try: data = request.get_json() except Exception: return ErrorResponse(PayloadNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() try: token = jwt.encode({'user': data.get('username')}, app.config.get('SECRET_KEY')) except Exception: return ErrorResponse(OperationNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() return jsonify(Response(200).generateToken(token.decode('UTF-8')))
def reset_password(): try: data = request.get_json() except Exception: return ErrorResponse(PayloadNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() if data and data['username']: user = User.getUser(data['username']) expire = datetime.datetime.utcnow() + datetime.timedelta(hours=24) token = jwt.encode({ 'id': user.username, 'exp': expire }, app.config.get('SECRET_KEY')) return jsonify(Response(200).generateResetURL(token.decode('UTF-8'))) else: return ErrorResponse(JsonNotFound().message, 422, { 'Content-Type': 'application/json' }).respond()
def background_color(): try: data = request.get_json()['data']['attributes'] bg_color = data['bg_color'] except Exception: return ErrorResponse(PayloadNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() svg2png = SVG2PNG() bg_color = '#' + str(bg_color) user_defined_path = svg2png.do_svg2png(1, bg_color) with open(user_defined_path, "rb") as image_file: image_data = base64.b64encode(image_file.read()) os.remove(user_defined_path) try: imageName = saveToImage(imageFile=image_data.decode('utf-8'), extension=".png") except Exception: return ErrorResponse(ImageNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() uid = data['uid'] fetch_user = User.getUser(user_id=uid) if fetch_user is None: return ErrorResponse( UserNotFound(uid).message, 422, { 'Content-Type': 'application/json' }).respond() file_upload = File(filename=imageName, filetype='image', uploader=fetch_user) file_upload.save_to_db() return jsonify(ColorImageSchema().dump(file_upload).data)
def fileUpload(): try: data = request.json['csvFile'] csv = data['csvFile'] except Exception: return ErrorResponse(PayloadNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() if 'extension' not in data.keys(): return ErrorResponse(ExtensionNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() extension = data['extension'] if extension != 'csv': return ErrorResponse(CSVNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() try: csvName = saveToCSV(csvFile=csv, extension='.csv') except Exception: return ErrorResponse(OperationNotFound().message, 422, { 'Content-Type': 'application/json' }).respond() uid = data.get('uid') fetch_user = User.getUser(user_id=uid) if fetch_user is None: return ErrorResponse( UserNotFound(uid).message, 422, { 'Content-Type': 'application/json' }).respond() file_upload = File(filename=csvName, filetype='csv', uploader=fetch_user) file_upload.save_to_db() return jsonify(CSVUploadSchema().dump(file_upload).data)