def post(cls, request) -> Response: """ Makes a pitt :return: Response with audio's name """ user_auth = TokenAuthentication() access = user_auth.get(request) try: user_email = access['email'] except KeyError: return Response('You are logged out.', status=200) user = User.objects.get(email_address=user_email) user_id = user.id audio_path = request.data['audio_path'] data = {'user_id': user_id, 'audio_path': audio_path} headers = {"Content-Type": "application/json"} url = 'http://localhost:8118/voice/' try: requests.post(url=url, data=data, headers=headers) response = Response(data['audio_path'], status=200) return response except requests.ConnectionError: return Response('Unable to connect to the server.')
def post(cls, request) -> Response: """ JWT logout :return: Response dict """ TokenAuthentication.get(request) auth_token = get_authorization_header(request).split() access_token = Token.objects.get(access_token=auth_token[0]) access_token.delete() return Response('You are logged out.', status=200)
def get(cls, request) -> Response: """ Used to diaplay all users :return: Response dict """ access = TokenAuthentication.get(request) try: access['email'] except KeyError: return Response('You are logged out.', status=200) all_users = User.objects.all() users_list = [] for user in all_users: user_info = dict( id=user.id, login=user.login, profile=f'http://localhost:8000/finduser/?login={user.login}', ) users_list.append(user_info) returned_data = dict(users_list=users_list, ) return Response(returned_data, status=200)
def delete(cls, request) -> Response: """ Used to delete a subscription on a user :param request: :return: Response dict """ user_auth = TokenAuthentication() access = user_auth.get(request) follower_email = access['email'] follower = User.objects.get(email_address=follower_email) follower_id = follower.id login = request.data['login'] user = User.objects.get(login=login) user_id = user.id user_do_delete = Follower.objects.get(user_id=user_id, follower_id=follower_id) user_do_delete.delete() return Response(login, status=200)
def get(cls, request) -> Response: """ Displays the feed :return: Response with the list of the pitts """ access = TokenAuthentication.get(request) feed_pitts = [] users = [] try: follower_email = access['email'] except KeyError: return Response('You are logged out.', status=200) follower = User.objects.get(email_address=follower_email) for foll in Follower.objects.all(): if foll.follower_id == follower.id: users.append(foll.user_id) for pitt in Pitt.objects.all(): for user_id in users: if pitt.user_id == user_id: user = User.objects.get(id=user_id) pitt_info = [ user.login, f'http://localhost:8000/finduser/?login={user.login}', pitt.audio_decoded, pitt.created_at, pitt.id ] feed_pitts.append(pitt_info) if pitt.user_id == follower.id: pitt_info = [ follower.login, f'http://localhost:8000/finduser/?login={follower.login}', pitt.audio_decoded, pitt.created_at, pitt.id ] feed_pitts.append(pitt_info) feed_pitts.reverse() feed = Paginator(feed_pitts, 3) if 'page' in request.query_params: page_number = request.query_params['page'] else: page_number = 1 try: page = feed.page(page_number) return Response(page.object_list, status=200) except django.core.paginator.EmptyPage: return Response('This page does not exist', status=200)
def get(cls, request) -> Response: """ User profile :return: Response dict with the user's data """ access = TokenAuthentication.get(request) auth_token = get_authorization_header(request) try: user_email = access['email'] except KeyError: return Response('You are logged out.', status=200) user_profile_name = access['name'] user = User.objects.get(email_address=user_email, profile_name=user_profile_name) feed_link = 'http://localhost:8000/feed/' makepitt_link = 'http://localhost:8000/makepitt/' finduser_link = 'http://localhost:8000/finduser/' all_users_link = 'http://localhost:8000/users/' logout_link = 'http://localhost:8000/logout/' delete_link = f'http://localhost:8000/deletenode/?id={user.id}&token={auth_token}' pitts = [] for pitt in Pitt.objects.all(): if pitt.user_id == user.id: pitt_info = (pitt.audio_decoded, pitt.created_at) pitts.append(pitt_info) returned_data = dict( user_data=dict( id=user.id, login=user.login, profile_name=user_profile_name, email_address=user_email, pitts=pitts, ), functions=dict( feed=feed_link, pitt=makepitt_link, find_user=finduser_link, all_users=all_users_link, logout=logout_link, delete_account=delete_link, ), ) return Response(returned_data, status=200)
def post(cls, request) -> Response: """ Used to create a subscription on a user :return: Response dict """ access = TokenAuthentication.get(request) subscription_flag = True try: subscriber_email = access['email'] except KeyError: return Response('You are logged out.', status=200) user_login = request.data['login'] try: find_follower = User.objects.get(email_address=subscriber_email) follower_id = find_follower.id find_user = User.objects.get(login=user_login) except User.DoesNotExist: return Response('User is not found.', status=200) user_id = find_user.id user_email = find_user.email_address follower_login = find_follower.login requests.post( url="https://api.mailgun.net/v3/sandboxb3f7b13f20844cedaf8e5a0e05cdb824.mailgun.org/messages", auth=("api", "3adac155c921fea3423695a3acee1e58-e470a504-39684050"), data={"from": "*****@*****.**", "to": user_email, "subject": "New follower", "text": f'You have one new follower. His login: {follower_login}'} ) try: existing_follower = Follower.objects.get(user_id=user_id, follower_id=follower_id) if existing_follower: return Response('You are already subscribed.', status=200) except Follower.DoesNotExist: Follower.create_follower(user_id, follower_id, subscription_flag) returned_data = dict( user_id=user_id, follower_id=follower_id, subscription_flag=subscription_flag, ) return Response(returned_data, status=200)
def get(cls, request) -> Response: """ Finds user in the DB with the login from the query :param request: :return: Response dict with the user data """ access = TokenAuthentication.get(request) auth_token = get_authorization_header(request).decode('utf-8') data = request.query_params login = data['login'] feed_pitts = [] follow_link = f'http://localhost:8000/follownode/?login={login}&token={auth_token}' unfollow_link = f'http://localhost:8000/follownode/?login={login}&unfollow=True&token={auth_token}' try: user = User.objects.get(login=login) follower = User.objects.get(email_address=access['email']) except User.DoesNotExist: return Response('User is not found.', status=200) except KeyError: return Response('Yiu are logged out.', status=200) for pitt in Pitt.objects.all(): if pitt.user_id == user.id: pitt_info = (pitt.audio_decoded, pitt.created_at) feed_pitts.append(pitt_info) returned_data = dict( id=user.id, login=user.login, email=user.email_address, pitts=feed_pitts, ) try: follower_exists = Follower.objects.get(user_id=user.id, follower_id=follower.id) if follower_exists: returned_data['unfollow_link'] = unfollow_link return Response(returned_data, status=200) except Follower.DoesNotExist: returned_data['follow_link'] = follow_link return Response(returned_data, status=200)
def delete(cls, request) -> Response: """ Deletes a pitt :param request: :return: Response dict """ access = TokenAuthentication.get(request) try: access['email'] except KeyError: return Response('You are logged out.', status=200) try: pitt_id = request.data['id'] pitt = Pitt.objects.get(id=pitt_id) except Pitt.DoesNotExist: return Response('Pitt is not found.') pitt.delete() returned_data = dict(id=pitt_id, ) return Response(returned_data, status=200)