Esempio n. 1
0
def messages(request):
    title = 'Chats'
    user = request.user
    online_users = see_online_users()
    chats = Chat.objects.filter(users__id=user.pk)

    for chat in chats:
        for message in chat.chat_messages.all():
            if not message.is_read:
                chat.unread_count += 1

    if request.method == 'POST':
        form = NewChatForm(request.POST, request.FILES)
        if form.is_valid():
            new_chat = form.save(commit=False)
            new_chat.owner = user
            new_chat.save()
            new_chat.users.add(user)
            return redirect('messages')
    else:
        form = NewChatForm(None)
    return render(request, 'chats/messages.html', {'online_users': online_users,
                                             'chats': chats,
                                             'form': form,
                                             'title': title})
Esempio n. 2
0
def post_detail(request, pk):
    try:
        title = 'Post Comments'
        online_users = see_online_users()
        post = Post.objects.get(pk=pk)
        if request.user.is_authenticated:
            comments = Comment.objects.filter(post=post)
            if request.method == 'POST':
                form = NewCommentForm(request.POST)
                if form.is_valid():
                    new_comment = form.save(commit=False)
                    new_comment.user = request.user
                    new_comment.parent_id = request.POST.get("parent_id")
                    new_comment.post = post
                    new_comment.save()
                    return redirect('post_detail', pk=post.pk)
            else:
                form = NewCommentForm(instance=None)
            return render(request, 'posts/post_detail.html', {'form': form,
                                                              'post': post,
                                                              'online_users': online_users,
                                                              'comments': comments,
                                                              'title': title})
        else:
            return redirect('login')
    except Exception as ex:
        pass
    return HttpResponse('Post does not exist')
Esempio n. 3
0
def image_detail(request, pk):
    try:
        title = 'Image Detail'
        online_users = see_online_users()
        image = Image.objects.get(pk=pk)
        comments = Comment.objects.filter(picture=image)
        if request.method == 'POST':
            form = NewCommentForm(request.POST)
            if form.is_valid():
                new_comment = form.save(commit=False)
                new_comment.user = request.user
                new_comment.parent_id = request.POST.get("parent_id")
                new_comment.picture = image
                new_comment.save()
                return redirect('image-detail', pk=image.pk)
        else:
            form = NewCommentForm()
        return render(request, 'photoalbums/image_detail.html', {'online_users': online_users,
                                                     'image': image,
                                                     'form': form,
                                                     'comments': comments,
                                                     'title': title})
    except Exception:
        pass
    return HttpResponse('Image does not exist')
Esempio n. 4
0
def discussion(request, pk):
    try:
        discussion = Discussion.objects.get(pk=pk)
        group = discussion.group
        messages = Message.objects.filter(discussion=discussion)
        online_users = see_online_users()
        if request.method == 'POST':
            if request.user in group.users.all():
                form = NewMessageForm(request.POST, request.FILES)
                if form.is_valid():
                    new_message = form.save(commit=False)
                    new_message.discussion = discussion
                    new_message.from_user = request.user
                    new_message.save()
                    form = NewMessageForm()
                    return redirect(request.META.get('HTTP_REFERER', '/'))
            else:
                return HttpResponse(
                    'You must be a member of the group to post messages in this discussion'
                )
        else:
            form = NewMessageForm()
        return render(
            request, 'groups/discussion.html', {
                'form': form,
                'discussion': discussion,
                'messages': messages,
                'online_users': online_users,
                'title': discussion.title
            })
    except Exception:
        pass
    return HttpResponse('Discussion does not exist')
Esempio n. 5
0
def index(request):
    title = 'Home'
    online_users = see_online_users()
    user_groups = Group.objects.filter(users__id=request.user.pk)

    def search_posts(request):
        q = request.GET.get('q')

        if q:
            searched_posts = PostDocument.search().query('match', body=q)
        else:
            searched_posts = ''
        return searched_posts

    searched_posts = search_posts(request)

    if request.user.is_authenticated:
        user = request.user
        user_posts = Post.objects.filter(user=request.user).exclude(is_private=True)
        friends_posts = Post.objects.filter(user__in=user.friends.all()).exclude(is_private=True)
        user_group_posts = Post.objects.filter(group__in=user_groups).exclude(is_private=True)

        posts = friends_posts.union(user_posts, user_group_posts)[::-1]
        posts_paginator = Paginator(posts, 5)
        page_obj = posts_paginator.get_page(request.GET.get('page'))

        if request.method == 'POST':
            form = NewPostForm(request.POST, request.FILES)
            if form.is_valid():
                new_post = form.save(commit=False)
                new_post.user = user
                new_post.save()
                for user in request.user.subscribers.all():
                    notification = Notification.objects.create(type='P', text='New Post in subscribed!', user=user, post=new_post)
                    notification.save()
                return redirect('index')
        else:
            form = NewPostForm(instance=None)

        context = {
            'form': form,
            'posts': posts,
            'searched_posts': searched_posts,
            'online_users': online_users,
            'page_obj': page_obj,
            'title': title,
        }

        return render(request, 'posts/index.html', context=context)
    else:
        return redirect('login')
Esempio n. 6
0
def friends(request, pk):
    try:
        user = User.objects.get(pk=pk)
        title = user.name + 's Friends'
        friends = user.friends.all()
        sent_friend_requests = FriendRequest.objects.filter(
            from_user=request.user)
        received_friend_requests = FriendRequest.objects.filter(
            to_user=request.user)

        online_users = see_online_users()
        return render(
            request, 'friends/friends.html', {
                'user': user,
                'friends': friends,
                'sent_friend_requests': sent_friend_requests,
                'received_friend_requests': received_friend_requests,
                'online_users': online_users,
                'title': title
            })
    except Exception as ex:
        pass
    return HttpResponse('User does not exist')
Esempio n. 7
0
def profile(request, pk):
    try:
        user = User.objects.get(pk=pk)
        title = user.name
        if request.user in user.blocked_users.all():
            return HttpResponse('This user blocked you.')
        else:
            online_users = see_online_users()

            friends = user.friends.all()

            friends_for_display = friends[:5]
            images = Image.objects.filter(
                album__user=user).order_by('-added_at')[:8]
            groups_for_display = Group.objects.filter(users__id=user.pk)[:3]

            user_posts = Post.objects.filter(user=user)
            profile_posts = Post.objects.filter(profile=user)
            posts = user_posts.union(profile_posts).order_by(
                'date_posted').reverse()
            posts_paginator = Paginator(posts, 5)
            page_number = request.GET.get('page')
            page_obj = posts_paginator.get_page(page_number)

            # Check if user is friend:
            button_status = 'none'
            if user not in request.user.friends.all():
                button_status = 'not_friend'
                if len(
                        FriendRequest.objects.filter(from_user=request.user,
                                                     to_user=user)) == 1:
                    button_status = 'friend_request_sent'
            if user in request.user.friends.all():
                button_status = 'friend'

            if request.method == 'POST':
                form = NewPostForm(request.POST, request.FILES)
                if form.is_valid():
                    new_post = form.save(commit=False)
                    new_post.user = request.user
                    new_post.profile = user
                    if request.user == user:
                        new_post.is_private = False
                    else:
                        new_post.is_private = True
                    new_post.save()
                    if request.user != user:
                        notification = Notification.objects.create(
                            type='P',
                            text='New Post in profile!',
                            user=user,
                            post=new_post)
                        notification.save()
                    return redirect(request.META.get('HTTP_REFERER', '/'))
            else:
                form = NewPostForm(instance=None)
            return render(
                request, 'user_profile/profile.html', {
                    'user': user,
                    'friends': friends,
                    'friends_for_display': friends_for_display,
                    'groups_for_display': groups_for_display,
                    'images': images,
                    'posts': posts,
                    'form': form,
                    'button_status': button_status,
                    'online_users': online_users,
                    'page_obj': page_obj,
                    'title': title
                })
    except Exception as ex:
        return HttpResponse(ex)
Esempio n. 8
0
def chat(request, pk):
    def search_messages(request):
        q = request.GET.get('q')

        if q:
            searched_messages = MessageDocument.search().query('match', body=q)
        else:
            searched_messages = ''
        return searched_messages

    searched_messages = search_messages(request)

    online_users = see_online_users()
    chat = Chat.objects.get(pk=pk)
    messages = Message.objects.filter(chat=chat)

    form = NewMessageForm()

    messages_paginator = Paginator(messages, 10)
    page_number = request.GET.get('page')
    page_obj = messages_paginator.get_page(page_number)

    if chat.is_private:
        title = 'Chat'
    else:
        title = chat.title

    if request.method == 'GET':
        for message in messages:
            if message.from_user != request.user:
                message.is_read = True
                message.save()
        for n in Notification.objects.filter(user=request.user, message__in=chat.chat_messages.all()):
            n.delete()

    if request.method == 'POST':
        form = NewMessageForm(request.POST, request.FILES)
        if form.is_valid():
            new_message = form.save(commit=False)
            new_message.from_user = request.user
            new_message.chat = chat
            new_message.save()
            chat.last_message = new_message
            for notification in Notification.objects.filter(message__chat=chat).all():
                notification.delete()
            for user in chat.users.all():
                if user != request.user:
                    notification = Notification.objects.create(type='M',
                                                               text='New Message',
                                                               user=user,
                                                               message=new_message)
                    notification.save()
            chat.save()
            form = NewMessageForm()
            return redirect(reverse('chat', kwargs={'pk': chat.pk}) + '?page=%s' % messages_paginator.num_pages)
    return render(request, 'chats/chat.html', {'messages': messages,
                                               'form': form,
                                               'online_users': online_users,
                                               'chat': chat,
                                               'title': title,
                                               'searched_messages': searched_messages,
                                               'page_obj': page_obj})