def post(self, request): serializer = MessageSerializer( data={'who': request.user.id, 'whom': request.data['id'], 'message': request.data['message']}) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, id): username = jwt.decode(request.headers['Authorization'].split(' ')[1], 'secret', algorithms=['HS256']) data = dict(request.data.items()) data['who'] = NewUser.objects.get(username=username['username']).id data['whom'] = id serializer = MessageSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def get(self, request): user = request.user if 'id' in request.GET: all_messages = Messages.objects.all().filter( Q(who=user, whom__id=request.GET['id']) | Q(who__id=request.GET['id'], whom=user)) all_messages = [MessageSerializer(instance=message, context={'request': request}).data for message in all_messages] return Response({'all_messages': all_messages}, status=status.HTTP_200_OK) else: messages = Messages.objects.raw( ''' SELECT * FROM main_messages AS main WHERE ( main.id = ( SELECT max(id) FROM ( SELECT id FROM main_messages WHERE (who_id = :user_id) and ((whom_id = main.whom_id) or (whom_id = main.who_id)) and (whom_id != :user_id) UNION ALL SELECT id FROM main_messages WHERE (whom_id = :user_id) and ((who_id = main.whom_id) or (who_id = main.who_id)) and (who_id != :user_id) ) ) OR ( main.id = ( SELECT max(id) FROM main_messages WHERE (whom_id = :user_id) and (who_id = :user_id) ) ) ) ORDER BY id DESC ''', {"user_id": user.id} ) all_messages = [MessageSerializer(instance=message, context={'request': request}).data for message in messages] return Response({'all_messages': all_messages}, status=status.HTTP_200_OK)
def get(self, request, format=None): image_msg = "" data = request.query_params standard_data = { "event": data['event'], "oaid": data['oaid'], "fromuid": data['fromuid'], "appid": 0, "msgid": data['msgid'], "message": data['message'], "timestamp": data['timestamp'], "mac": data['mac'] } if 'href' in data.keys(): image_msg = upload_file(request) standard_data['message'] = "image" serializer = WebHookSerializer(data=standard_data) if serializer.is_valid(): web_hook = serializer.save() # user_respond msg = web_hook.message # chatbot_respond if image_msg: respond_text = my_chat_bot.response(text=image_msg)['text'] else: respond_text = my_chat_bot.response(text=msg)['text'] # send message to user if respond_text: chatbot_respond(respond_text, web_hook) respond_serializer = MessageSerializer(data={ "webhook": web_hook.id, "respond": respond_text }) # save respond_message to database if respond_serializer.is_valid(raise_exception=True): respond_serializer.save() return Response({"message": "Received and saved"}, 200) else: return Response({ "message": "Error", "errors": serializer.errors }, 400)
def get(self, request, id): username = jwt.decode(request.headers['Authorization'].split(' ')[1], 'secret', algorithms=['HS256']) user = NewUser.objects.get(username=username['username']) all_messages = Messages.objects.all().filter( Q(who=user, whom__id=id) | Q(who__id=id, whom=user)) all_messages = [ MessageSerializer(instance=message).data for message in all_messages ] return Response({'all_messages': all_messages})
def post(self, request): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) email = serializer.data["email"] user = User.objects.filter(email=email).first() if user is None: serializer = MessageSerializer( data={'message': "登録されたユーザーが見つかりませんでした。"}) serializer.is_valid(raise_exception=True) return Response(serializer.data, status=400) auth = AuthDigit.update_or_create(user=user) auth.send_password_reset_email() serializer = MessageSerializer( data={ 'message': "メールアドレスにパスワードリセット用の確認コードを送信しました。コードを入力してパスワードの再設定を完了させてください。" }) serializer.is_valid(raise_exception=True) return Response(serializer.data)
def post(self, request): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) token = serializer.data["token"] prt = PasswordResetToken.objects.filter(token=token).last() if prt and prt.is_expired(): serializer = MessageSerializer( data={'message': "パスワードリセットの有効期限が切れています。再度発行してください。"}) serializer.is_valid(raise_exception=True) return Response(serializer.data, status=400) elif prt is None or prt.token != token: serializer = MessageSerializer( data={'message': "パスワードリセットトークンが一致しませんでした。"}) serializer.is_valid(raise_exception=True) return Response(serializer.data, status=400) prt.user.set_password(serializer.data["password"]) prt.user.save() prt.delete() serializer = MessageSerializer(data={'message': "パスワードの変更が完了しました。"}) serializer.is_valid(raise_exception=True) return Response(serializer.data)
def post(self, request): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) code = serializer.data["digit"] auth = AuthDigit.objects.filter(code=code).last() if auth and auth.is_expired(): serializer = MessageSerializer( data={'message': "確認コードの有効期限が切れています。再度発行してください。"}) serializer.is_valid(raise_exception=True) return Response(serializer.data, status=400) elif auth is None or auth.code != code: serializer = MessageSerializer( data={'message': "確認コードが一致しませんでした。"}) serializer.is_valid(raise_exception=True) return Response(serializer.data, status=400) prt = PasswordResetToken.update_or_create(auth.user) serializer = TokenSerializer(data={'token': prt.token}) serializer.is_valid(raise_exception=True) auth.delete() return Response(serializer.data)