Exemple #1
0
def message_send(request, username):
    """
        Send a private message to another user.
    """
    userModel = get_user_model()

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

    except userModel.DoesNotExist:
        raise Http404('Invalid username.')

    if request.method == 'POST':
        form = PrivateMessageForm(request.POST)

        if form.is_valid():

            title = form.cleaned_data['title']
            content = form.cleaned_data['content']
            PrivateMessage.objects.create(receiver=user,
                                          sender=request.user,
                                          title=title,
                                          content=content)

            utilities.add_message(request, 'Message sent to {}!'.format(user))

            return HttpResponseRedirect(user.get_url())

    else:
        form = PrivateMessageForm()

    context = {'form': form, 'receiver': user}

    return render(request, 'accounts/send_message.html', context)
Exemple #2
0
def password_changed(request):
    """
        Inform that the password has been changed, and redirect to home.
    """
    utilities.add_message(request, 'Password changed!')

    return HttpResponseRedirect(reverse('home'))
Exemple #3
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 #4
0
def remove_post(request):
    """
        Remove a post.

        Requires a POST request with the following arguments:
            - postIdentifier
    """
    if not request.method == 'POST':
        return HttpResponseNotAllowed(['POST'])

    postIdentifier = request.POST.get('postIdentifier')

    if not postIdentifier:
        return HttpResponseBadRequest("Need a 'postIdentifier' argument.")

    try:
        post = request.user.posts.get(identifier=postIdentifier)

    except Post.DoesNotExist:
        return HttpResponseBadRequest("Invalid 'postIdentifier' argument.")

    post.delete()

    nextUrl = request.GET.get('next')

    if not nextUrl:
        nextUrl = reverse('home')

    utilities.add_message(request, 'Message removed!')

    return JsonResponse({'url': nextUrl})
Exemple #5
0
def message_remove(request, messageId):
    """
        Remove a private message.
    """
    try:
        message = request.user.privatemessage_set.get(id=messageId)

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

    message.delete()
    utilities.add_message(request, 'Message removed!')

    return HttpResponseRedirect(reverse('accounts:message_all'))
Exemple #6
0
def remove_user(request, username):
    """
        Remove an user account (also removes everything associated with it).
    """
    userModel = get_user_model()

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

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

    else:
        utilities.add_message(request, "'{}' user removed!".format(user))
        user.delete()

        return HttpResponseRedirect(reverse('home'))
Exemple #7
0
def disable_user(request, username):
    """
        Enable/disable an user account.
        If the account is disabled, the user won't be able to login.
    """
    userModel = get_user_model()

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

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

    else:
        value = not user.is_active

        # only other staff users can enable/disable staff users
        if user.is_staff:
            if request.user.is_staff:
                user.is_active = value
                user.save(update_fields=['is_active'])

            else:
                return HttpResponseForbidden("Can't disable a staff member.")

        else:
            user.is_active = value
            user.save(update_fields=['is_active'])

        if value:
            message = "'{}' account is now active.".format(user)

        else:
            message = "'{}' account is now disabled.".format(user)

        utilities.add_message(request, message)

        return HttpResponseRedirect(user.get_url())
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 new_account(request):
    """
        Create a new user account.
    """
    if request.method == 'POST':

        form = MyUserCreationForm(request.POST)

        if form.is_valid():

            form.save()
            utilities.add_message(
                request,
                "User '{}' created!".format(form.cleaned_data['username']))

            return HttpResponseRedirect(reverse('accounts:login'))

    else:
        form = MyUserCreationForm()

    context = {'form': form}

    return render(request, 'accounts/new_account.html', context)
Exemple #10
0
def set_moderator(request, username):
    """
        Give/remove moderator rights from an account.
    """
    userModel = get_user_model()

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

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

    user.is_moderator = not user.is_moderator
    user.save(update_fields=['is_moderator'])

    if user.is_moderator:
        message = "'{}' is now a moderator.".format(user)

    else:
        message = "'{}' is not a moderator anymore.".format(user)

    utilities.add_message(request, message)

    return HttpResponseRedirect(user.get_url())
Exemple #11
0
def set_follow(request, username):
    """
        Follow or un-follow an user.
    """
    userModel = get_user_model()

    if username == request.user.username:
        return HttpResponseBadRequest("Can't follow yourself.")

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

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

        # figure out if we're following or un-following
    try:
        request.user.following.get(username=username)

    except userModel.DoesNotExist:
        request.user.following.add(userToFollow)

        utilities.add_message(request, '{} followed'.format(userToFollow))

    else:
        request.user.following.remove(userToFollow)

        utilities.add_message(request, '{} un-followed'.format(userToFollow))

    nextUrl = request.GET.get('next')

    if nextUrl:
        return HttpResponseRedirect(nextUrl)

    else:
        return HttpResponseRedirect(reverse('home'))