Example #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()
Example #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()
Example #3
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})
Example #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})