def authenticated_chat(request, model_key, owner_id): """ View for chat which is "read only" for unauthenticated and "writeable" for authenticated users. Requires **page** as **GET** parameter for page number. Last page will be used for default. owner_id: id of the owner model. model_key: one of the **KEY_KEY_TO_CHAT_OWNER** keys. """ ModelClass = KEY_TO_CHAT_OWNER[model_key] owner = get_object_or_404(ModelClass.objects, id=owner_id) if not owner.chat: raise Http404() if request.method == 'POST': if not request.user.is_authenticated(): return HttpResponseForbidden() form = MessageForm(request.POST, request.user, owner.chat) if form.is_valid(): form.save() return redirect('authenticated_chat', model_key=model_key, owner_id=owner.id) else: form = MessageForm() owner.chat.url = reverse('authenticated_chat', kwargs={ 'owner_id': owner_id, 'model_key': model_key, }) owner.chat.rss = reverse('open_rss', kwargs={ 'id': owner_id, 'model_key': model_key, }) return render(request, 'parts/chat_message_list.html', { 'form': form, 'chat': owner.chat, 'page': get_chat_page(owner.chat, request.GET.get('page')), })
def add_message(request, chat_id): form = MessageForm error = {} try: chat = Chat.objects.get(id=chat_id) except Chat.DoesNotExist: raise Http404 if request.method == "POST": form = MessageForm(request.POST) if form.is_valid(): f = form.save(commit=False) f.ip = "1.2.3.4" f.chat = chat f.save() if request.is_ajax(): error['error'] = 'False' return HttpResponse(simplejson.dumps(error)) else: return redirect('/messages/%s/' % (f.chat.id))
def detail(request, event_id): event = get_object_or_404(Event, pk=event_id) if request.method == 'POST' and request.user in event.participants.all(): form = MessageForm(request.POST) if form.is_valid(): new_message = form.save(commit=False) new_message.chat = event.chat new_message.chat.last_activity = timezone.now() new_message.author = request.user new_message.chat.save() new_message.save() return redirect('events:detail', event_id=event_id) else: form = MessageForm() storage = messages.get_messages(request) context = {'event': event, 'messages': storage, 'form': form} return render(request, 'events/detail.html', context)
def create_message(request, chat_id=None): chat = get_object_or_404(Chat, id=chat_id) form = MessageForm(user=request.user, chat=chat, data=request.POST) if form.is_valid(): msg = form.save() return HttpResponseAjax(**msg.to_dict) else: return HttpResponseAjaxError(errors=form.errors)
def send_message(request, user_id): if request.method == 'POST': form = MessageForm(request.POST) message = form.save(commit=False) message.sender = request.user receiver = User.objects.get(id=user_id) message.receiver = receiver message.save() return HttpResponseRedirect(reverse("chat:chat_room"))
def team_chat(request, team): if request.method == 'POST': form = MessageForm(request.POST, request.user, team.chat) if form.is_valid(): form.save() return redirect('team_chat', team_id=team.id) else: form = MessageForm() page = get_chat_page(team.chat, request.REQUEST.get('page')) team.chat.url = reverse('team_chat', kwargs={'team_id': team.id}) team_rss = TeamChatFeed() team.owner_key = request.user.userprofile.external_read_auth team.chat.rss = team_rss.link(team) return render(request, 'parts/chat_message_list.html', { 'chat': team.chat, 'form': form, 'page': page, })
def post(self, request, pk): form = MessageForm(data=request.POST) if form.is_valid(): message = form.save(commit=False) message.chat_id = pk message.author = self.request.user message.save() #new_message.delay(message.id, message.author, message.chat_id) return redirect(reverse('messages', kwargs={'pk': pk}))
def authenticated_chat(request, model_key, owner_id): """ View for chat which is "read only" for unauthenticated and "writeable" for authenticated users. Requires **page** as **GET** parameter for page number. Last page will be used for default. owner_id: id of the owner model. model_key: one of the **KEY_KEY_TO_CHAT_OWNER** keys. """ ModelClass = KEY_TO_CHAT_OWNER[model_key] owner = get_object_or_404(ModelClass.objects, id=owner_id) if not owner.chat: raise Http404() if request.method == 'POST': if not request.user.is_authenticated(): return HttpResponseForbidden() form = MessageForm(request.POST, request.user, owner.chat) if form.is_valid(): form.save() return redirect('authenticated_chat', model_key=model_key, owner_id=owner.id) else: form = MessageForm() owner.chat.url = reverse('authenticated_chat', kwargs={ 'owner_id': owner_id, 'model_key': model_key, }) owner.chat.rss = reverse('open_rss', kwargs={ 'id': owner_id, 'model_key': model_key, }) return render( request, 'parts/chat_message_list.html', { 'form': form, 'chat': owner.chat, 'page': get_chat_page(owner.chat, request.GET.get('page')), })
def post(self, request, pk): chat = get_object_or_404(Chat, id=pk) if request.user in chat.members.all(): form = MessageForm(data=request.POST) if form.is_valid(): message = form.save(commit=False) message.chat_id = pk message.author = self.request.user message.save() new_message.apply_async(args=[ message.id, ], countdown=3600) return redirect(reverse('messages', kwargs={'pk': pk}))
class UserSendPageMessage(TemplateView): """ Пишем сообщения со страниц пользователей или разных списков. Если у пользователя есть друзья, то add_friend_message.html (возможность добавлять друзей), иначе add_message.html """ template_name = None def get(self, request, *args, **kwargs): from users.models import User from common.templates import get_my_template self.template_name = get_my_template("chat/message/add_message.html", request.user, request.META['HTTP_USER_AGENT']) self.user = User.objects.get(pk=self.kwargs["pk"]) return super(UserSendPageMessage, self).get(request, *args, **kwargs) def get_context_data(self, **kwargs): from chat.forms import MessageForm context = super(UserSendPageMessage, self).get_context_data(**kwargs) context["form"] = MessageForm() context["member"] = self.user return context def post(self, request, *args, **kwargs): from users.models import User from chat.models import Message from common.check.user import check_user_can_get_list from chat.forms import MessageForm from django.http import HttpResponse self.form, self.user, connections = MessageForm( request.POST), User.objects.get( pk=self.kwargs["pk"]), request.POST.getlist("chat_items") check_user_can_get_list(request.user, self.user) if request.is_ajax() and self.form.is_valid(): message = self.form.save(commit=False) if request.POST.get('text') or request.POST.get( 'attach_items') or request.POST.get('sticker'): _message = Message.get_or_create_chat_and_send_message( creator=request.user, user=self.user, text=message.text, voice=request.POST.get('voice'), attach=request.POST.getlist('attach_items'), sticker=request.POST.get('sticker')) return HttpResponse() else: from django.http import HttpResponseBadRequest return HttpResponseBadRequest()
def chat(request, pk): ride = get_object_or_404(Ride, pk=pk) if request.user.pk != ride.initiator.pk and request.user.pk != ride.driver.pk: return redirect('profile_summary') if request.method == "POST": form = MessageForm(request.POST) if form.is_valid(): message = form.save(commit=False) message.ride = ride message.user = request.user message.save() return redirect('chat', pk=ride.pk) else: form = MessageForm() return render(request, 'chat.html', {'form': form, 'ride': ride})
def post(self, request, *args, **kwargs): from chat.forms import MessageForm form = MessageForm(request.POST, request.FILES) if request.is_ajax() and form.is_valid( ) and request.user.is_administrator(): from chat.models import Message form_post = form.save(commit=False) Message.get_or_create_manager_chat_and_send_message( creator_pk=request.user.pk, text=form_post.text, voice=request.POST.get('voice'), attach=request.POST.getlist('attach_items')) return HttpResponse() else: return HttpResponseBadRequest()
def on_message(self, message): self.log('User message: {0}'.format(message)) data = {} data['message'] = urllib.unquote(message['message'].encode('utf-8')).decode("utf-8") form = MessageForm(data) if form.is_valid(): object = form.save(commit=False) object.sender_id = message['sender'] object.channel_id = message['channel'] object.save() message.clear() message['room'] = self._get_room_name(object.channel_id) message['action'] = 'new_message' message['result'] = render_to_string('chat/msg_detail.html', {'msg': object}) # parse url match = re.search(r'http://[a-zA-Z0-9]+\.[-a-zA-Z0-9_]+/*', object.message) if match: try: url = metadata_parser.MetadataParser(url=object.message) meta = url.metadata.get('meta') page = url.metadata.get('page') img = meta.get('og:image', None) title = urllib.unquote(page['title'].encode('utf-8')).decode("utf-8") message['result'] = render_to_string('chat/url_parse.html', {'img': img, 'title':title, 'obj': object}) except: pass else: message.clear() message['action'] = 'error' self.emit_to_room_with_me(object.channel_id, 'message', message) return True