Exemple #1
0
def create_private_topic(recipients, title, subtitle, text):
    """Create a new private topic using the bot.

   Args:
       recipients: list of User instances
       title: title of the private topic
       subtitle: subtitle of the private topic, can be None
       text: content of the private post in markdown

   """

    # Create private topic
    private_topic = PrivateTopic(
        title=title,
        subtitle=subtitle,
        author_id=BOT_USER_PK,
        pubdate=datetime.now())

    # We beed to save our PrivateTopic before adding it participants
    # since it uses a many-to-many field.
    private_topic.save()

    # Add participants
    for user in recipients:
        private_topic.participants.add(user)

    # Save private topic again
    private_topic.save()

    # Create first private post
    private_post = PrivatePost(
        privatetopic=private_topic,
        text=text,
        pubdate=datetime.now(),
        position_in_topic=1,
        author_id=BOT_USER_PK)

    # Save private post
    private_post.save()

    # Finally update private topic
    private_topic.last_message = private_post
    private_topic.save()
Exemple #2
0
def create_private_topic(recipients, title, subtitle, text):
    """Create a new private topic using the bot.

   Args:
       recipients: list of User instances
       title: title of the private topic
       subtitle: subtitle of the private topic, can be None
       text: content of the private post in markdown

   """

    # Create private topic
    private_topic = PrivateTopic(title=title,
                                 subtitle=subtitle,
                                 author_id=BOT_USER_PK,
                                 pubdate=datetime.now())

    # We beed to save our PrivateTopic before adding it participants
    # since it uses a many-to-many field.
    private_topic.save()

    # Add participants
    for user in recipients:
        private_topic.participants.add(user)

    # Save private topic again
    private_topic.save()

    # Create first private post
    private_post = PrivatePost(privatetopic=private_topic,
                               text=text,
                               pubdate=datetime.now(),
                               position_in_topic=1,
                               author_id=BOT_USER_PK)

    # Save private post
    private_post.save()

    # Finally update private topic
    private_topic.last_message = private_post
    private_topic.save()
Exemple #3
0
def answer(request):
    """Add an answer from an user to a message

    Returns:
        HttpResponse

    """
    try:
        topic_pk = request.GET['sujet']
    except KeyError:
        raise Http404

    g_topic = get_object_or_404(PrivateTopic, pk=topic_pk)
    posts = PrivatePost.objects.filter(
        privatetopic=g_topic).order_by('-pubdate')[:3]
    last_post_pk = g_topic.last_message.pk

    # Check permissions
    if not is_participant(request.user, g_topic):
        raise PermissionDenied

    # Check that the user isn't spamming
    if g_topic.antispam(request.user):
        raise PermissionDenied

    # If we just sent data
    if request.method == 'POST':
        data = request.POST
        newpost = last_post_pk != int(data['last_post'])

        # Using the « preview button », the « more » button or new post
        if 'preview' in data or 'more' in data or newpost:
            return render_template('messages/answer.html', {
                'text': data['text'], 'topic': g_topic, 'posts': posts,
                'last_post_pk': last_post_pk, 'newpost': newpost
            })

        # Saving the message
        else:
            form = PrivatePostForm(request.POST)
            if form.is_valid():
                data = form.data

                post = PrivatePost()
                post.privatetopic = g_topic
                post.author = request.user
                post.text = data['text']
                post.pubdate = datetime.now()
                post.position_in_topic = g_topic.get_post_count() + 1
                post.save()

                g_topic.last_message = post
                g_topic.save()

                return redirect(post.get_absolute_url())
            else:
                raise Http404

    else:
        text = ''

        # Using the quote button
        if 'cite' in request.GET:
            post_cite_pk = request.GET['cite']
            post_cite = PrivatePost.objects.get(pk=post_cite_pk)

            if not is_participant(post_cite.author, g_topic):
                raise PermissionDenied

            for line in post_cite.text.splitlines():
                text = text + '> ' + line + '\n'

            text = u'**{0} a écrit :**\n{1}\n'.format(
                post_cite.author.username, text)

        return render_template('messages/answer.html', {
            'topic': g_topic, 'text': text, 'posts': posts,
            'last_post_pk': last_post_pk
        })
Exemple #4
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})
Exemple #5
0
def answer(request):
    """Add an answer from an user to a message

    Returns:
        HttpResponse

    """
    try:
        topic_pk = request.GET['sujet']
    except KeyError:
        raise Http404

    g_topic = get_object_or_404(PrivateTopic, pk=topic_pk)
    posts = PrivatePost.objects.filter(
        privatetopic=g_topic).order_by('-pubdate')[:3]
    last_post_pk = g_topic.last_message.pk

    # Check permissions
    if not is_participant(request.user, g_topic):
        raise PermissionDenied

    # Check that the user isn't spamming
    if g_topic.antispam(request.user):
        raise PermissionDenied

    # If we just sent data
    if request.method == 'POST':
        data = request.POST
        newpost = last_post_pk != int(data['last_post'])

        # Using the « preview button », the « more » button or new post
        if 'preview' in data or 'more' in data or newpost:
            return render_template(
                'messages/answer.html', {
                    'text': data['text'],
                    'topic': g_topic,
                    'posts': posts,
                    'last_post_pk': last_post_pk,
                    'newpost': newpost
                })

        # Saving the message
        else:
            form = PrivatePostForm(request.POST)
            if form.is_valid():
                data = form.data

                post = PrivatePost()
                post.privatetopic = g_topic
                post.author = request.user
                post.text = data['text']
                post.pubdate = datetime.now()
                post.position_in_topic = g_topic.get_post_count() + 1
                post.save()

                g_topic.last_message = post
                g_topic.save()

                return redirect(post.get_absolute_url())
            else:
                raise Http404

    else:
        text = ''

        # Using the quote button
        if 'cite' in request.GET:
            post_cite_pk = request.GET['cite']
            post_cite = PrivatePost.objects.get(pk=post_cite_pk)

            if not is_participant(post_cite.author, g_topic):
                raise PermissionDenied

            for line in post_cite.text.splitlines():
                text = text + '> ' + line + '\n'

            text = u'**{0} a écrit :**\n{1}\n'.format(
                post_cite.author.username, text)

        return render_template(
            'messages/answer.html', {
                'topic': g_topic,
                'text': text,
                'posts': posts,
                'last_post_pk': last_post_pk
            })
Exemple #6
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})