def activate_user(pk, db_session: Session, data): # validate_permission("activate_user", db_session, User) activation = data["activation"] user = get_object_or_404(User, db_session, User.id == pk) user.is_active = str_to_bool(activation) db_session.commit() if user.is_active: return HTTPResponse(*ACTIVATED_MSG) return HTTPResponse(*DEACTIVATED_MSG)
def filter_rooms(db_session: Session): # /rooms/filter?query={"sort":"name-",} rooms_query = db_session.query(Room) query = get_query_from_url("query") filtered_query = MongoFilter(Room, rooms_query, query).filter() result = Paginate(filtered_query, rooms_serializer) return HTTPResponse(status=200, body=result)
def change_password(db_session, data): validate_data(change_password_serializer, data) req_user_json = bottle.request.get_user() current_user = db_session.query(User).get(req_user_json['id']) if current_user.check_password(data['old_password']): current_user.set_password(data['password']) return HTTPResponse(*PASSWORD_CHANGED) raise HTTPError(*OLD_PASSWORD_NOT_VALID)
def __get_uri_query(self): try: filters = bottle.request.query.smart_filters() filter_spec = filters.get("filters") filter_spec = ast.literal_eval(filter_spec) except Exception: raise HTTPResponse(status=404, body="url_is_not_valid") return filter_spec
def new_room(db_session, data): validate_data(data=data, serializer=room_serializer) room = Room() room.name = data["name"] db_session.add(room) db_session.commit() result = room_serializer.dump(db_session.query(Room).get(room.id)) return HTTPResponse(status=status.HTTP_200_OK, body=result)
def set_user_role(pk, db_session: Session, data): validate_data(set_role_serializer, data) # validate_permission("edit_roles", db_session, User) user = get_object_or_404(User, db_session, User.id == pk) user.set_roles(data['roles']) db_session.commit() result = user_serializer.dump(db_session.query(User).get(user.id)) return HTTPResponse(status=status.HTTP_201_CREATED, body=result)
def delete_user(pk, db_session): identity = get_user_identity(db_session) if identity.check_permission("delete_user", User): user = get_object_or_404(User, db_session, User.id == pk) db_session.delete(user) db_session.commit() raise HTTPResponse(status=204, body="Successfully deleted !") raise HTTPError(status=403, body="Access denied")
def filters(self): filter_spec = self.__get_uri_query() query = self.db_session.query(self.model) try: filtered_query = apply_filters(query, filter_spec) except Exception as e: raise HTTPResponse(status=200, body=e.args) result = Paginate(filtered_query, self.serializer) return result
def get_params_from_url(params: list): fields_dict = {} for param in params: search_field = bottle.request.query.smart_filters().get(param) if search_field: fields_dict[str(param)] = str( search_field.encode("ISO-8859-1").decode("utf-8")) else: raise HTTPResponse(status=403, body="not_valid_field") return fields_dict
def add_room_image(db_session, data): validate_data(data=data, serializer=room_image_serializer) room_image = RoomImage() room_image.name = upload(data=data) room_image.room_id = data["room_id"] db_session.add(room_image) db_session.commit() room_image = db_session.query(RoomImage).get(room_image.id) result = room_image_serializer.dump(room_image) return HTTPResponse(status=status.HTTP_200_OK, body=result)
def add_person_image(db_session: Session, data): validate_data(person_image_serializer, data) person_image = PersonImage() person = get_object_or_404(Person, db_session, Person.id == data['person_id']) set_objects_limit(person.person_images, limit=1, session=db_session) person_image.name = upload(data=data) person_image.person_id = data['person_id'] db_session.add(person_image) db_session.commit() result = db_session.query(PersonImage).get(person_image.id) result = person_image_serializer.dump(result) return HTTPResponse(status=200, body=result)
def edit_user(pk, db_session: Session, data): validate_data(user_edit_serializer, data) user = get_object_or_404(User, db_session, User.id == pk) person = data['person'] user.person.name = person['name'] user.person.last_name = person['last_name'] user.person.email = person['email'] user.phone = data['phone'] user.username = data['phone'] db_session.commit() result = user_serializer.dump(db_session.query(User).get(user.id)) return HTTPResponse(status=status.HTTP_201_CREATED, body=result)
def create_user(db_session: Session, data): validate_data(user_serializer, data) is_object_exist_409(User, db_session, User.phone == data['phone']) person = data['person'] person_obj = get_or_create(Person, db_session, name=person['name']) person_obj.name = person['name'] person_obj.last_name = person['last_name'] person_obj.email = person['email'] db_session.add(person_obj) user = get_or_create(User, db_session, phone=data['phone']) user.phone = data['phone'] user.set_roles(data['roles']) user.username = user.get_phone user.set_password(data['password']) user.person = person_obj db_session.add(user) db_session.commit() result = user_serializer.dump(db_session.query(User).get(user.id)) return HTTPResponse(status=201, body=result)
def new_quote(data, db_session): validate_data(data=data, serializer=quote_serializer) author = data['author'] first = author['first'] last = author['last'] content = data['content'] author = get_or_create(Author, db_session, Author.first == first, Author.last == last) if author is None: author = Author(first, last) db_session.add(author) quote = Quote(content, author) db_session.add(quote) db_session.commit() result = quote_serializer.dump(db_session.query(Quote).get(quote.id)) return HTTPResponse(status=200, body=result)
def get_rules(db_session: Session): req_user_json = bottle.request.get_user() current_user = db_session.query(User).get(req_user_json['id']) return HTTPResponse(status=status.HTTP_200_OK, body=get_rules_json(current_user), message="rules")
def get_quotes(db_session): quotes = db_session.query(Quote).all() result = quotes_serializer.dump(quotes) return HTTPResponse(status=200, body=result)
def get_user(pk, db_session): user = get_object_or_404(User, db_session, User.id == pk) result = user_serializer.dump(user) raise HTTPResponse(status=200, body=result)
def get_authors(db_session): authors = db_session.query(Author).all() serializer = AuthorSchema(many=True) result = serializer.dump(authors) return HTTPResponse(status=200, body=result)
def get_quote(pk, db_session): quote = db_session.query(Quote).get(pk) result = quote_serializer.dump(quote) return HTTPResponse(status=200, body=result)
def get_rooms(db_session): rooms = db_session.query(Room).all() result = rooms_serializer.dump(rooms) return HTTPResponse(status=status.HTTP_200_OK, body=result)
def get_room(pk, db_session: Session): room = get_object_or_404(Room, db_session, Room.id == pk) result = room_serializer.dump(room) return HTTPResponse(status=status.HTTP_200_OK, body=result)
def get_rooms(db_session): rooms = db_session.query(Room).all() serializer = RoomSchema(many=True) result = serializer.dump(rooms) return HTTPResponse(status=200, body=result)
def get_roles(): return HTTPResponse(status=200, body=get_roles_json())
def remove_person_image(pk, db_session: Session): person_image = get_object_or_404(Person, db_session, Person.id == pk) db_session.delete(person_image) db_session.commit() return HTTPResponse(*DELETE_IMG)
def get_user(pk, db_session): user = get_object_or_404(User, db_session, User.id == pk) result = user_serializer.dump(user) return HTTPResponse(status=status.HTTP_200_OK, body=result)
def get_users(db_session: Session): users = db_session.query(User).order_by(User.created_date) result = Paginate(users, users_serializer) return HTTPResponse(status=status.HTTP_200_OK, body=result)
def get_current_user(db_session: Session): req_user_json = bottle.request.get_user() current_user = db_session.query(User).get(req_user_json['id']) result = user_serializer.dump(current_user) return HTTPResponse(status=status.HTTP_200_OK, body=result)
def get_author(pk, db_session): author = db_session.query(Author).get(pk) author_result = author_serializer.dump(author) quotes_result = quotes_serializer.dump(author.quotes.all()) author_result.update({"quotes": quotes_result}) return HTTPResponse(status=200, body=author_result)