def post(self, request, chat_id): form = MessageForm(data=request.POST) if form.is_valid(): message = form.save(commit=False) message.chat_id = chat_id message.author = request.user message.save() return redirect(reverse('messages', kwargs={'chat_id': chat_id}))
def get(self, request, chat_id): chat = Dialog.objects.get(id=chat_id) return render(request, 'users/messages.html', { 'user_profile': request.user, 'chat': chat, 'form': MessageForm() })
def dispatch(self, request, *args, **kwargs): self.opponent = None self.dialog = None self.form = None if 'user_id' in kwargs: self.opponent = get_object_or_404(get_user_model(), pk=kwargs['user_id']) self.dialog = Dialog.objects.get_or_create(request.user, self.opponent) if not self.dialog: raise Http404 self.form = MessageForm(request.POST or None) return super(DialogView, self).dispatch(request, *args, **kwargs)
def create(request): form = MessageForm(request.POST) if form.is_valid(): session = boto3.session.Session() s3_client = session.client( service_name='s3', endpoint_url='http://hb.bizmrg.com', aws_access_key_id='6Da62vVLUi6AKbFnnRoeA3', aws_secret_access_key= 'gDYg4Bu15yUpNYGKmmpiVNGvLRWhUAJ3m1GGRvg8KTbU', ) user = User.objects.get(username=request.POST['username']) chats = Member.objects.filter(user=user) for chat in chats: op = Member.objects.filter(chat=chat.chat).exclude(user=user).get() if op.user.username == request.POST['opponent']: result = chat.chat break message = Message(content=request.POST['content'], chat=result, user=user, added_at=request.POST['date']) message.save() response = [message.id] result.last_message = '(' + request.POST['attach_type'] + ') ' +\ request.POST['content'] if request.POST['attach_type'] != 'none' else request.POST['content'] result.save() chat.last_read_message = Message.objects.filter(chat=result).filter( user=op.user).last() chat.save() attachs = {} attachs['type'] = request.POST['attach_type'] if request.POST['attach_type'] == 'geolocation': attachment = Attachment( chat=result, user=user, message=message, attach_type='geolocation', url=request.POST['content'], ) attachment.save() attachs['url'] = attachment.url response.append(attachment.id) else: attachs['url'] = [] for file in request.FILES: key = 'attachments/' + request.POST[ 'attach_type'] + result.topic + '/' + str(hash(file)) s3_client.put_object( Bucket='tsyrkov_messanger_bucket', Key=key, Body=request.FILES[file], ) attachment = Attachment( chat=result, user=user, message=message, attach_type=request.POST['attach_type'], url=key, ) attachment.save() attachs['url'].append( s3_client.generate_presigned_url( 'get_object', Params={ 'Bucket': 'tsyrkov_messanger_bucket', 'Key': attachment.url, }, ExpiresIn=3600)) response.append(attachment.id) # send message to client if attachs['type'] == 'audio': attachs['url'] = attachs['url'][0] avatar = s3_client.generate_presigned_url( 'get_object', Params={ 'Bucket': 'tsyrkov_messanger_bucket', 'Key': message.user.avatar, }, ExpiresIn=3600) command_1 = { 'method': 'publish', 'params': { 'channel': op.user.username + '_with_' + user.username, 'data': { 'avatar': avatar, 'opponent': message.user.username, 'topic': result.topic, 'author': message.user.username, 'last_message': result.last_message, 'read': False, 'date': 'T'.join(message.added_at.split(' ')), 'message': message.content, 'attachments': attachs, } } } command_2 = { 'method': 'publish', 'params': { 'channel': op.user.username, 'data': { 'avatar': avatar, 'opponent': message.user.username, 'topic': result.topic, 'author': message.user.username, 'last_message': result.last_message, 'read': False, 'date': 'T'.join(message.added_at.split(' ')), 'message': message.content, 'attachments': attachs, } } } headers = { 'Content-Type': 'application/json', 'Authorization': 'apikey ' + API_KEY, } requests.post(url='http://localhost:9000/api', data=json.dumps(command_1), headers=headers) requests.post(url='http://localhost:9000/api', data=json.dumps(command_2), headers=headers) return JsonResponse({'success': response}) return JsonResponse({'errors': form.errors}, status=400)