Beispiel #1
0
def new(request):
    """Creates a new message.

    Returns:
        HttpResponse

    """
    if request.method == 'POST':
        # If the client is using the "preview" button
        if 'preview' in request.POST:
            return render_template('messages/new.html', {
                'recipients': request.POST['recipients'],
                'title': request.POST['title'],
                'subtitle': request.POST['subtitle'],
                'text': request.POST['text'],
                'form': PrivateTopicForm(request.POST),
            })

        form = PrivateTopicForm(request.POST)
        if form.is_valid():
            data = form.data
            # Creating the thread
            n_topic = PrivateTopic()
            n_topic.title = data['title']
            n_topic.subtitle = data['subtitle']
            n_topic.pubdate = datetime.now()
            n_topic.author = request.user
            n_topic.save()

            list_part = data['recipients'].split(',')
            for part in list_part:
                part = part.strip()
                p = get_object_or_404(User, username=part)
                n_topic.participants.add(p)
            n_topic.save()

            # Adding the first message
            post = PrivatePost()
            post.privatetopic = n_topic
            post.author = request.user
            post.text = data['text']
            post.pubdate = datetime.now()
            post.position_in_topic = 1
            post.save()

            n_topic.last_message = post
            n_topic.save()

            # Notify participants by mail
            for user in n_topic.participants.all():
                profile = Profile.objects.all().get(user=user)
                if profile.mail_on_private_message:
                    mail.send_mail_new_private_message(n_topic, user)

            return redirect(n_topic.get_absolute_url())
    else:

        data = {}
        if 'destinataire' in request.GET:
            user_pk = request.GET['destinataire']
            u = get_object_or_404(User, pk=user_pk)
            data['recipients'] = u.username

        form = PrivateTopicForm(data)

    return render_template('messages/new.html', {'form': form})
Beispiel #2
0
def new(request):
    """Creates a new message.

    Returns:
        HttpResponse

    """
    if request.method == 'POST':
        # If the client is using the "preview" button
        if 'preview' in request.POST:
            return render_template(
                'messages/new.html', {
                    'recipients': request.POST['recipients'],
                    'title': request.POST['title'],
                    'subtitle': request.POST['subtitle'],
                    'text': request.POST['text'],
                    'form': PrivateTopicForm(request.POST),
                })

        form = PrivateTopicForm(request.POST)
        if form.is_valid():
            data = form.data
            # Creating the thread
            n_topic = PrivateTopic()
            n_topic.title = data['title']
            n_topic.subtitle = data['subtitle']
            n_topic.pubdate = datetime.now()
            n_topic.author = request.user
            n_topic.save()

            list_part = data['recipients'].split(',')
            for part in list_part:
                part = part.strip()
                p = get_object_or_404(User, username=part)
                n_topic.participants.add(p)
            n_topic.save()

            # Adding the first message
            post = PrivatePost()
            post.privatetopic = n_topic
            post.author = request.user
            post.text = data['text']
            post.pubdate = datetime.now()
            post.position_in_topic = 1
            post.save()

            n_topic.last_message = post
            n_topic.save()

            # Notify participants by mail
            for user in n_topic.participants.all():
                profile = Profile.objects.all().get(user=user)
                if profile.mail_on_private_message:
                    mail.send_mail_new_private_message(n_topic, user)

            return redirect(n_topic.get_absolute_url())
    else:

        data = {}
        if 'destinataire' in request.GET:
            user_pk = request.GET['destinataire']
            u = get_object_or_404(User, pk=user_pk)
            data['recipients'] = u.username

        form = PrivateTopicForm(data)

    return render_template('messages/new.html', {'form': form})