def put(self): data = request.get_json() role = Role.query.filter_by(id=data['id']).first() role.permissions = json.dumps(data['permissions']) db.session.commit() emit('ROLE_WS_CHANGED', {'data': role.name}, namespace='/' + role.name, broadcast=True) return Result.success()
def put(self, installation_id): c = Installation.query.filter_by(id=installation_id).first() if not c: raise HttpNotFoundException() json = get_fillable(Installation, **request.get_json()) for field, value in json.items(): setattr(c, field, value) data = request.get_json().copy() if 'panels' in data: InstallationPanelModel.query.filter_by( installation_id=installation_id).delete() for panel in data['panels']: db.session.add( InstallationPanelModel(installation_id=installation_id, model_id=panel['id'], quantity=int(panel['quantity']), serials=panel['serials'])) if 'inverters' in data: InstallationInverterModel.query.filter_by( installation_id=installation_id).delete() for inverter in data['inverters']: db.session.add( InstallationInverterModel(installation_id=installation_id, model_id=inverter['id'], quantity=int( inverter['quantity']), serials=inverter['serials'])) db.session.commit() return Result.success(code=201)
def post(self): category = request.form.get('category') name = request.form.get('name') installation_id = request.form.get('installation_id') file = request.files.get('file') if category not in DOCUMENT_CATEGORIES: raise HttpException('Invalid Category') extension = max(guess_all_extensions(file.content_type), key=len) key_name = 'documents/{}/{}'.format( installation_id, hashlib.sha256( (str(datetime.utcnow().timestamp()) + name + extension + installation_id).encode('utf8')).hexdigest() + extension) s3 = Storage(configs.UPLOAD_FILE_BUCKET) inst_doc = InstallationDocument(name=name, installation_id=installation_id, category=category, object_key=key_name) s3.put_new(file.read(), key_name, file.content_type) file.close() db.session.add(inst_doc) db.session.commit() return Result.success()
def put(self, user_id): raw_data = request.get_json() user = User.query.options(joinedload('roles')).filter_by(id=user_id).first() if not user: return Result.error('User does not exist') user.first_name = raw_data['first_name'] user.last_name = raw_data['last_name'] user.roles = [] if raw_data['attributes']: if raw_data['attributes'] and 'access' in raw_data['attributes']: user.attributes.user_access = json.dumps(raw_data['attributes']['access']) if raw_data['attributes'] and 'preferences' in raw_data['attributes']: user.attributes.user_preferences = json.dumps(raw_data['attributes']['preferences']) if raw_data['roles']: for role in Role.query.filter(Role.id.in_( list(map( lambda r: r['id'], raw_data['roles']) ))): user.roles.append(role) db.session.commit() emit('USER_WS_CHANGED', {'data': user.id}, namespace='/' + str(user.id), broadcast=True) return Result.success()
def delete(self): role_id = request.get_json() try: Role.query.filter_by(id=role_id).delete() db.session.commit() except IntegrityError as e: return Result.error('integrity constraint', 409) return Result.success()
def put(self, project_id): project = CustomerProject.query.filter_by(id=project_id).first() if not project: raise HttpNotFoundException() json = get_fillable(CustomerProject, **request.get_json()) for field, value in json.items(): setattr(project, field, value) db.session.commit() return Result.success(code=201)
def put(self): data = request.get_json() if data is None or 'email' not in data: raise HttpException('Missing email') user = User.query.filter_by(email=data['email']).first() if user is not None: send_user_token_email(user, 'Actualiza tu contraseña', 'email/change_password.html') # for security reasons, even if user does not exist, we return a success call return Result.success()
def put(self, customer_id): c = Customer.query.filter_by(id=customer_id).first() if not c: raise HttpNotFoundException() json = get_fillable(Customer, **request.get_json()) for field, value in json.items(): setattr(c, field, value) db.session.commit() return Result.success(code=201)
def delete(self, installation_id): object_key = request.get_json()['object_key'] doc = InstallationDocument.query.filter_by( object_key=object_key, installation_id=installation_id).first() if not doc: raise HttpException('Invalid id') db.session.delete(doc) db.session.commit() s3 = Storage(configs.UPLOAD_FILE_BUCKET) s3.remove(object_key) return Result.success()
def put(self, installation_id): status = InstallationStatus.query.filter_by( installation_id=installation_id).first() if status is None: raise HttpNotFoundException() update = request.get_json().copy() for field, value in update.items(): if isinstance(value, str): setattr(status, field, local_to_utc(value)) elif isinstance(value, bool): setattr(status, field, value) db.session.commit() return Result.success(code=201)
def put(self, installation_id): financing = InstallationFinancing.query.filter_by( installation_id=installation_id).first() if financing is None: raise HttpNotFoundException() data = request.get_json().copy() if 'request_date' in data: data['request_date'] = local_to_utc(data['request_date']) if 'response_date' in data: data['response_date'] = local_to_utc(data['response_date']) for field, value in data.items(): setattr(financing, field, value) db.session.commit() return Result.success(code=201)
def put(self, installation_follow_up_id): data = request.get_json().copy() follow_up = InstallationFollowUp.query.filter_by( id=installation_follow_up_id).first() if follow_up is None: raise HttpNotFoundException() for key, value in data.items(): if key in InstallationFollowUp.fillable: setattr(follow_up, key, value) if 'comment' in data: follow_up.comments.append( InstallationFollowUpComment(user_id=request.user.id, comment=data['comment'])) db.session.commit() return Result.success(code=201)
def post(self): data = request.get_json() ut = UserToken.query.filter_by(token=data['token']).first() if not ut or ut.expires <= datetime.datetime.utcnow(): raise HttpException('Invalid token') if ut.target != request.base_url: raise HttpException('Invalid target') parsed = r'^(?=.*\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*(),.?":{}|<>])' if len(data['pw']) < 6 or not re.match(parsed, data['pw']) or data['pw'] != data['pw2']: raise HttpException('Invalid password') user = ut.user user.password = data['pw'] user.hash_password() ut.expires = datetime.datetime.utcnow() db.session.commit() return Result.success()
def put(self): user = request.user raw_data = request.get_json() if 'first_name' in raw_data: user.first_name = raw_data['first_name'] if 'last_name' in raw_data: user.last_name = raw_data['last_name'] access = user.attributes.access preferences = user.attributes.preferences if raw_data['attributes']: if 'access' in raw_data['attributes']: user.attributes.user_access = json.dumps({**access, **raw_data['attributes']['access']}) if 'preferences' in raw_data['attributes']: user.attributes.user_preferences = json.dumps({**preferences, **raw_data['attributes']['preferences']}) db.session.commit() emit('USER_WS_CHANGED', {'data': user.id}, namespace='/' + str(user.id), broadcast=True) return Result.success()
def put(self, message_id): message = UserMessage.query.filter_by(id=message_id).first() message.read = True db.session.commit() return Result.success()
def post(self): send_message(**request.json) return Result.success()
def delete(self, user_id): user = User.query.options(joinedload('roles')).filter_by(id=user_id).first() user.roles = [] db.session.delete(user) db.session.commit() return Result.success()