Beispiel #1
0
 def post(self, request, partner_id):
     chat = models.ChatModel.objects.filter(
         participants__id=request.user.id).filter(
             participants__id=partner_id)
     partner = MesUser.objects.filter(id=partner_id)
     if not partner:
         chat_page = html_messages(request)
     elif not chat:
         partner = partner[0]
         chat = models.ChatModel.objects.create(
             mongo_collection=request.user.username + '-' +
             partner.username, ).participants.add(request.user, partner)
         chat_page = html_messages(request, partner)
     else:
         chat = chat[0]
         partner = partner[0]
         message = {
             'text': request.POST.get('mes_text'),
             'time': datetime.now().strftime('%d.%m.%Y %H:%M:%S'),
             'sender_id': request.user.id
         }
         mongo.add_message_to_db(self.chats_db_name, chat.mongo_collection,
                                 message)
         messages = mongo.get_messages_from_db(self.chats_db_name,
                                               chat.mongo_collection)
         chat_page = html_messages(request, partner, messages)
     return RusJsonResponse({'chat_page': chat_page})
Beispiel #2
0
 def post(self, request, pk):
     if request.POST.get('wallpost') or request.POST.get('comment'):
         if request.POST.get('wallpost'):
             self.post_wallpost(pk)
         else:
             self.post_comment()
         return redirect('/account/api')
     elif request.POST.get('bookmark'):
         self.post_add_to_bookmarks(pk)
         return redirect('/account/api/bookmarks')
     elif request.POST.get('search'):
         search_acc = request.POST.get('search')
         error = False
         try:
             search_acc = models.AccountModel.objects.get(user__username=search_acc)
             if search_acc and search_acc.user != request.user:
                 search_acc = search_acc.user
             else:
                 error = 'Себя нельзя добавить в закладки'
         except models.AccountModel.DoesNotExist:
             error = 'Нет совпадений'
         html_page = render_to_string(
             'accounts_app/json_bookmarks.html',
             {'user_id': request.user.id, 'error': error, 'search_acc': search_acc},
             request
         )
         return RusJsonResponse({'html_page': html_page})
     return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
Beispiel #3
0
 def get(self, request):
     user_acc = models.AccountModel.objects.get(user=request.user)
     bookmarks = user_acc.bookmarks.all()
     bookmarks_amount = len(bookmarks)
     html_page = render_to_string(
         'accounts_app/json_bookmarks.html',
         {'user_id': user_acc.user.id, 'bookmarks': bookmarks, 'bookmarks_amount': bookmarks_amount},
         request
     )
     return RusJsonResponse({'html_page': html_page})
Beispiel #4
0
 def get(self, request):
     adress = request.META.get('HTTP_REFERER')
     id = int(adress[adress.find('account/') + 8])
     account = models.AccountModel.objects.get(id=id)
     wallposts = account.get_wallposts()
     user_acc = models.AccountModel.objects.get(user=request.user)
     bookmarks = user_acc.bookmarks.all()
     html_page = render_to_string(
         'accounts_app/json_account.html',
         {'account': account, 'wallposts': wallposts, 'bookmarks': bookmarks},
         request
     )
     return RusJsonResponse({'html_page': html_page})
Beispiel #5
0
 def get(self, request):
     chats = chat_app.ChatModel.objects.filter(
         participants=request.user).order_by('-last_message_time')
     partners = list()
     for chat in chats:
         partner = chat.get_partner(request.user)
         partners.append(partner)
     serializer = serializers.MesUserSerializer(partners, many=True)
     for user in serializer.data:
         chat = chats.filter(participants=user['id'])[0]
         user['last_message_text'] = chat.last_message_text
         time = chat.last_message_time
         print(time)
         user['last_message_time'] = time
     print(serializer.data)
     left_chats = render_to_string('main_app/left_chats.html',
                                   {'accounts': serializer.data}, request)
     return RusJsonResponse({'left_chats': left_chats})
Beispiel #6
0
 def get(self, request, partner_id):
     chat = models.ChatModel.objects.filter(
         participants__id=request.user.id).filter(
             participants__id=partner_id)
     partner = MesUser.objects.filter(id=partner_id)
     if not partner:
         chat_page = html_messages(request)
     elif not chat:
         partner = partner[0]
         chat = models.ChatModel.objects.create(
             mongo_collection=request.user.username + '-' +
             partner.username, ).participants.add(request.user, partner)
         chat_page = html_messages(request, partner)
     else:
         chat = chat[0]
         partner = partner[0]
         messages = mongo.get_messages_from_db(self.chats_db_name,
                                               chat.mongo_collection)
         chat_page = html_messages(request, partner, messages)
     return RusJsonResponse({'chat_page': chat_page})