def post(self, request, *args, **kwargs): serializer = MailSerializer(data=request.data) if serializer.is_valid(): subject = serializer.validated_data.get('subject') body = serializer.validated_data.get('body') sender = serializer.validated_data.get('sender') recipient = serializer.validated_data.get('recipient') send_aws.delay(subject, body, sender, recipient) return Response( { 'status': 'success', 'data': { 'message': 'Mail Sent Successfully' } }, status=status.HTTP_200_OK) else: return Response( { 'status': 'failure', 'data': { 'message': 'Incorrect request format.', 'errors': serializer.errors } }, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): serializer = MailSerializer(data=request.data) data_form = {"name" : request.data['name'],"email" :request.data['email'], "phone": request.data['phone'],"content" :request.data['content']} verify_data_form = verify_data(data_form=data_form) if(verify_data_form["isOk"]): if serializer.is_valid(): validated_data = serializer.validated_data name = validated_data.get('name') email =validated_data.get('email') phone = validated_data.get('phone') content =validated_data.get('content') send_mail("[FORM SITE] mail de:{}".format(name),"Numéro de téléphone:{}.\nMessage:{}".format(phone, content) , email, ['*****@*****.**'],) else: verify_data_form["msg"]={"msg":"une erreur est survenue"} verify_data_form['status'] = status.HTTP_400_BAD_REQUEST return Response(verify_data_form["msg"], status=verify_data_form['status'] )
def get(self, request): try: mails = Mail.objects.all() except: return JsonResponse({"LMAOOO": "error occured"}, safe=False) return JsonResponse(MailSerializer(mails, many=True).data, safe=False)
def get(self, request, pk): user = User.objects.get(pk=pk) sent_messages = Message.objects.filter(sender=user, parent=None) received_messages = Message_Recipient.objects.filter( receiver=user).select_related("message").filter( message__parent=None) sent_mails = Message_Recipient.objects.filter( message__in=sent_messages) allMails = (sent_mails | received_messages).order_by("-message__timestamp") data = [] for mail in allMails: mail_status = Status.objects.get(user=user, message=mail.message) lastReply = mail.message.lastReply if not mail_status.isDeleted: if lastReply is not None: data.append({ "parentMail": MailSerializer(mail).data, "lastReply": MailSerializer(lastReply).data, "status": StatusSerializer(mail_status).data }) else: data.append({ "parentMail": MailSerializer(mail).data, "lastReply": MailSerializer(mail).data, "status": StatusSerializer(mail_status).data }) data.sort(key=lambda d: d['lastReply']['message']['timestamp'], reverse=True) return Response(data, status=HTTP_200_OK)
def get(self, request, pk): users = self.get_queryset().exclude(pk=pk) responseData = [] for user in users: lastMail = None userData = UserSerializer(user).data lastSentMail = Message.objects.filter( sender=user).order_by("-timestamp").first() lastReceivedMail = Message_Recipient.objects.filter( receiver=user).order_by("-message__timestamp").first() compare = False if lastSentMail: lastMail = Message_Recipient.objects.get(message=lastSentMail) compare = True if lastReceivedMail: if compare: if lastMail.message.timestamp < lastReceivedMail.message.timestamp: lastMail = lastReceivedMail if lastMail is None: mailData = {"last_email": "", "receiver": ""} else: try: if not Status.objects.get( user=user, message=lastMail.message).isDeleted: mailData = { "last_email": MailSerializer(lastMail).data } except: mailData = {"last_email": "", "receiver": ""} responseData.append({**userData, **mailData}) return Response(responseData, status=HTTP_200_OK)