def list(self): filters = self._filters_parse() filters = self._filters_cleanup(filters) try: filters = self._parse_list_options(filters) entities = self.manager.list(**filters) with_pagination = False require_pagination = filters.get('require_pagination', False) page = filters.get('page', None) page_size = filters.get('page_size', None) if (page and page_size is not None) and require_pagination is True: with_pagination = True count = self.manager.count(**(self._clean_filters(**filters))) except exception.InfoSystemException as exc: return flask.Response(response=exc.message, status=exc.status) except ValueError: raise exception.BadRequest('page or page_size is invalid') collection = self._entities_to_dict( entities, self._get_include_dicts()) response = {self.collection_wrap: collection} if with_pagination: response.update({'pagination': {'page': int(page), 'page_size': int(page_size), 'total': count}}) return flask.Response(response=utils.to_json(response), status=200, mimetype="application/json")
def upload_photo(self, id, **kwargs): try: file = flask.request.files.get('file', None) if not file: raise exception.BadRequest() token = self.get_token(self.get_token_id()) domain_id = self.get_domain_id_from_token(token) user_id = token.user_id if not (domain_id and user_id): raise exception.BadRequest() kwargs['domain_id'] = domain_id kwargs['user_id'] = user_id kwargs['type_image'] = 'UserPhoto' image = self.manager.api.images().create(file=file, **kwargs) kwargs.pop('type_image') kwargs['photo_id'] = image.id self.manager.upload_photo(id=id, **kwargs) response = {'image': image.to_dict()} except exception.InfoSystemException as exc: return flask.Response(response=exc.message, status=exc.status) return flask.Response(response=utils.to_json(response), status=201, mimetype="application/json")
def get_roles(self, id: str): try: roles = self.manager.get_roles(id=id) response = {"roles": [role.to_dict() for role in roles]} except exception.InfoSystemException as exc: return flask.Response(response=exc.message, status=exc.status) return flask.Response(response=utils.to_json(response), status=200, mimetype="application/json")
def update_settings(self, id): try: data = flask.request.get_json() settings = self.manager.update_settings(id=id, **data) response = {'settings': settings} return flask.Response(response=utils.to_json(response), status=200, mimetype="application/json") except exception.InfoSystemException as exc: return flask.Response(response=exc.message, status=exc.status)
def remove_settings(self, id): try: keys = self._get_keys_from_args() kwargs = {'keys': keys} settings = self.manager.remove_settings(id=id, **kwargs) response = {'settings': settings} return flask.Response(response=utils.to_json(response), status=200, mimetype="application/json") except exception.InfoSystemException as exc: return flask.Response(response=exc.message, status=exc.status)
def activate(self, id1, id2): try: token_id = flask.request.headers.get('token') domain = self.manager.activate(token_id=token_id, domain_id=id1, user_admin_id=id2) response = {'domain': domain.to_dict()} return flask.Response(response=utils.to_json(response), status=200, mimetype="application/json") except exception.InfoSystemException as exc: return flask.Response(response=exc.message, status=exc.status)
def update(self, id): data = flask.request.get_json() try: entity = self.manager.update(**data) except exception.InfoSystemException as exc: return flask.Response(response=exc.message, status=exc.status) response = {self.resource_wrap: entity.to_dict()} return flask.Response(response=utils.to_json(response), status=200, mimetype="application/json")
def get(self, id): try: entity = self.manager.get(id=id) include_dicts = self._get_include_dicts() entity_dict = entity.to_dict(include_dict=include_dicts) except exception.InfoSystemException as exc: return flask.Response(response=exc.message, status=exc.status) response = {self.resource_wrap: entity_dict} return flask.Response(response=utils.to_json(response), status=200, mimetype="application/json")
def upload_logo(self, id): try: token = flask.request.headers.get('token') file = flask.request.files.get('file', None) if not file: raise exception.BadRequest('ERROR! File not found in request.') image = self.manager.upload_logo(id=id, token=token, file=file) response = {'image': image.to_dict()} except exception.InfoSystemException as exc: return flask.Response(response=exc.message, status=exc.status) return flask.Response(response=utils.to_json(response), status=201, mimetype="application/json")
def get_nextval(self, id): data = flask.request.get_json() try: if 'name' not in data: raise exception.BadRequest('ERROR! "name" não not defined') response = self.manager.get_nextval(id=id, name=data['name']) except exception.InfoSystemException as exc: return flask.Response(response=exc.message, status=exc.status) response = {'nextval': response} return flask.Response(response=utils.to_json(response), status=200, mimetype="application/json")
def create(self): data = flask.request.get_json() try: if data: exceptions = data.get('exceptions', None) if exceptions: data['exceptions'] = self._get_resources(exceptions) entity = self.manager.create(**data) else: entity = self.manager.create() except exception.InfoSystemException as exc: return flask.Response(response=exc.message, status=exc.status) response = {self.resource_wrap: entity.to_dict()} return flask.Response(response=utils.to_json(response), status=201, mimetype="application/json")