def post(cls, request, following_id) -> Dict[str, str]: """ Запрос на добавление подписки :param request: :param following_id: :return: """ client = check_token(request) try: following = Client.objects.get(id=following_id) except Client.DoesNotExist: raise exceptions.InvalidUserError(message='Following does not' ' exists') if Following.objects.filter(follower_id=client.id, following_id=following_id).exists(): raise exceptions.ValidationError(message='Following already ' 'exists', status_code=409) following = Following.create_following(following_id, client.id) client.followings.add(following) return dict(follower_id=client.id, following_id=following_id)
def delete(cls, request, following_id): """ Запрос на удаление подписки :param request: :param following_id: :return: """ client = check_token(request) try: following = Following.objects.get( following_id=following_id, follower_id=client.id, ) except Following.DoesNotExist: raise exceptions.InvalidUserError( message='Following does not exists') following.delete() client.followings.filter(following_id=following_id, follower_id=client.id).delete() try: following = Following.objects.get(following_id=following_id, follower_id=client.id) except Following.DoesNotExist: raise exceptions.InvalidUserError(message='Following deleted', status_code=204)
def delete(cls, request, pitt_id): """ Запрос на удаление pitt'a :param request: :param login: :param pitt_id: :return: """ client = check_token(request) try: pitt = Pitt.objects.get(id=pitt_id) except Pitt.DoesNotExist: raise exceptions.InvalidUserError(message='Ivalid pitt_id') try: Pitt.objects.get(user_id=client.id, id=pitt_id) except Pitt.DoesNotExist: raise exceptions.InvalidUserError(message='Forbidden', status_code=403) pitt.delete() client.pitts.filter(id=pitt_id).delete() try: Pitt.objects.get(id=pitt_id) except Pitt.DoesNotExist: raise exceptions.InvalidUserError(message='Pitt successfully ' 'deleted', status_code=204)
def post(cls, request) -> Dict[str, list]: check_token(request) search_string = request.data['search_string'] result = [] for client in Client.objects.all(): if search_string in client.login: client_data = { 'id': client.id, 'login': client.login, } result.append(client_data) if not result: raise exceptions.NoFollowersError(message='No such users') result = sorted(result, key=lambda i: i['login']) return dict(result=result)
def delete(cls, request, login): """ Запрос на удаление пользователя :param request: :param login: :return: """ check_token(request) try: client = Client.objects.get(login=login) except Client.DoesNotExist: raise exceptions.InvalidUserError() client.delete() try: client = Client.objects.get(login=login) except Client.DoesNotExist: raise exceptions.InvalidUserError(message='Client deleted')
def get(cls, request) -> Dict[str, list]: """ Запрос на получение списка пользователей :param request: :return: """ check_token(request) client_list = [] for client in Client.objects.all(): client_data = {'id': client.id, 'login': client.login, } client_list.append(client_data) if not client_list: raise exceptions.NoFollowersError(message='No such users') client_list = sorted(client_list, key=lambda i: i['login']) return dict(result=client_list)
def post(cls, request) -> Dict[str, str]: """ Запрос на logout пользователя :param request: :return: """ client = check_token(request) exp = datetime.utcnow() + timedelta(seconds=0) payload = create_token_payload(client.id, client.login, client.password, exp) token = token_encode(payload) return dict(login=client.login, token=token)
def post(cls, request) -> Dict[str, str]: """ Запрос на создание питта :param request: :return: """ client = check_token(request) id = client.id filepath = request.data['filepath'] text = speech_transcript(filepath) pitt = Pitt.create_pitt(id, filepath, text) client.pitts.add(pitt) return dict(pitt_id=pitt.id, filepath=filepath, text=text)
def get(cls, request, page) -> Dict[str, list]: """ Запрос на демонстрацию ленты :param request: :param page: :return: """ client = check_token(request) following = client.followings.first() if not following: raise exceptions.NoFollowersError() feed = create_feed(client) paginator = Paginator(feed, 25) if page < paginator.num_pages or page > paginator.num_pages: raise exceptions.PageError else: current_page = paginator.page(page) return dict(feed=current_page.object_list, )