Exemple #1
0
def search(request):
    """
        Search for a post, user or a category.
    """
    if not request.method == 'POST':
        return HttpResponseNotAllowed(['POST'])

    searchText = request.POST.get('SearchText')
    context = {'search': searchText}

    if not searchText or len(searchText) < 3:
        utilities.add_message(
            request, 'The search text needs to have 3 or more characters.')

    else:
        userModel = get_user_model()

        people = userModel.objects.filter(username__icontains=searchText)
        categories = Category.objects.filter(name__icontains=searchText)
        posts = Post.objects.filter(text__icontains=searchText)

        context.update({
            'people': people,
            'categories': categories,
            'posts': posts
        })

    utilities.get_messages(request, context)

    return render(request, 'search.html', context)
Exemple #2
0
def show_categories(request):
    """
        Show a list with all the non-empty categories.
    """
    # show only non-empty categories
    allCategories = Category.objects.annotate(
        posts_count=Count('posts')).filter(
            posts_count__gt=0).order_by('-posts_count')

    paginator = Paginator(allCategories, 10)
    page = request.GET.get('page')

    try:
        categories = paginator.page(page)

    except PageNotAnInteger:
        categories = paginator.page(1)  # first page

    except EmptyPage:
        categories = paginator.page(paginator.num_pages)  # last page

    context = {'categories': categories}
    utilities.get_messages(request, context)

    return render(request, 'categories.html', context)
Exemple #3
0
def show_category(request, categoryName):
    """
        Show all the posts of the given category.
    """
    try:
        category = Category.objects.get(name=categoryName)

    except Category.DoesNotExist:
        raise Http404("Category doesn't exist.")

    allPosts = category.posts.all().order_by('-date_created')
    paginator = Paginator(allPosts, 5)
    page = request.GET.get('page')

    try:
        posts = paginator.page(page)

    except PageNotAnInteger:
        posts = paginator.page(1)  # first page

    except EmptyPage:
        posts = paginator.page(paginator.num_pages)  # last page

    context = {'categoryName': categoryName, 'posts': posts}
    utilities.get_messages(request, context)

    return render(request, 'category.html', context)
Exemple #4
0
def home(request):
    """
        Show the latest posts, either from all the users (if there's no one logged in), or from the users you're following.
    """
    if request.user.is_authenticated():
        following = request.user.following.all()
        posts = Post.objects.filter(
            user__in=following).order_by('-date_created')

    else:
        posts = Post.objects.all()

    paginator = Paginator(posts, 10)
    page = request.GET.get('page')

    try:
        paginatedPosts = paginator.page(page)

    except PageNotAnInteger:
        paginatedPosts = paginator.page(1)  # first page

    except EmptyPage:
        paginatedPosts = paginator.page(paginator.num_pages)  # last page

    context = {'posts': paginatedPosts}

    utilities.get_messages(request, context)

    return render(request, 'home.html', context)
Exemple #5
0
def login(request):
    """
        Login an account.
    """
    context = {}
    utilities.get_messages(request, context)

    return django_login(request, 'accounts/login.html', extra_context=context)
Exemple #6
0
def message_all(request):
    """
        Get a list of all the private messages available.
    """
    messages = request.user.privatemessage_set.all()

    context = {'messages': messages}

    utilities.get_messages(request, context)

    return render(request, 'accounts/check_messages.html', context)
Exemple #7
0
def user_page(request, username, what=None):
    """
        The user page has information about an user account.
        Also where you can change some settings (like the password).
    """
    userModel = get_user_model()

    try:
        user = userModel.objects.get(username=username)

    except userModel.DoesNotExist:
        raise Http404("User doesn't exist.")

    context = {}

    if what == 'images':
        allElements = user.get_posts_with_images()
        context['imagesSelected'] = True

    elif what == 'followers':
        allElements = user.get_followers()
        context['followersSelected'] = True

    elif what == 'following':
        allElements = user.get_following()
        context['followingSelected'] = True

    else:  # 'messages'
        allElements = user.posts.all().order_by('-date_created')
        context['postSelected'] = True

    paginator = Paginator(allElements, 5)
    page = request.GET.get('page')

    try:
        elements = paginator.page(page)

    except PageNotAnInteger:
        elements = paginator.page(1)  # first page

    except EmptyPage:
        elements = paginator.page(paginator.num_pages)  # last page

    context.update({
        'pageUser': user,
        'unreadMessages': user.how_many_unread_messages(),
        'elements': elements,
    })

    utilities.get_messages(request, context)

    return render(request, 'accounts/user_page.html', context)
Exemple #8
0
def show_post(request, identifier):
    """
        Shows the post plus its replies.
        If this post was a reply to other post, then show the parent post and its replies as well.
    """
    try:
        post = Post.objects.get(identifier=identifier)

    except Post.DoesNotExist:
        utilities.add_message(request, "Couldn't open the message.",
                              utilities.MessageType.error)
        return HttpResponseRedirect(reverse('home'))

    posts = []
    replies = []
    parent = post.reply_to

    if parent:
        posts.append(parent)

        for reply in parent.replies.all().order_by('date_created'):
            posts.append(reply)

            if reply == post:
                replies.extend(post.replies.all().order_by('date_created'))

    else:
        posts.append(post)
        replies.extend(post.replies.all().order_by('date_created'))

    context = {
        'posts': posts,
        'replies': replies,
        'selected_post': post,
    }
    utilities.get_messages(request, context)

    return render(request, 'show_post.html', context)
Exemple #9
0
def show_people(request):
    """
        If there's a user logged in, show a list of users that can be followed, otherwise just show a list of all the users.
    """
    userModel = get_user_model()

    if request.user.is_authenticated():
        following = []

        # don't consider the people you're already following
        for people in request.user.following.all():
            following.append(people.username)

            # and yourself as well
        following.append(request.user.username)

        allUsers = userModel.objects.exclude(username__in=following)

    else:
        allUsers = userModel.objects.all()

    paginator = Paginator(allUsers, 10)
    page = request.GET.get('page')

    try:
        users = paginator.page(page)

    except PageNotAnInteger:
        users = paginator.page(1)  # first page

    except EmptyPage:
        users = paginator.page(paginator.num_pages)  # last page

    context = {'users': users}
    utilities.get_messages(request, context)

    return render(request, 'people.html', context)