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()
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()
def edit_post(request): """Edit a post in a message. Returns: HttpResponse """ try: post_pk = request.GET['message'] except KeyError: raise Http404 post = get_object_or_404(PrivatePost, pk=post_pk) # Only edit last private post tp = get_object_or_404(PrivateTopic, pk=post.privatetopic.pk) last = get_object_or_404(PrivatePost, pk=tp.last_message.pk) if not last.pk == post.pk: raise PermissionDenied g_topic = None if post.position_in_topic == 1: g_topic = get_object_or_404(PrivateTopic, pk=post.privatetopic.pk) # Making sure the user is allowed to do that if post.author != request.user: if not request.user.has_perm('mp.change_post'): raise PermissionDenied elif request.method == 'GET': messages.add_message( request, messages.WARNING, u'Vous éditez ce message en tant que modérateur (auteur : {}).' u' Soyez encore plus prudent lors de l\'édition de celui-ci !' .format(post.author.username)) if request.method == 'POST': # Using the preview button if 'preview' in request.POST: if g_topic: g_topic = PrivateTopic(title=request.POST['title'], subtitle=request.POST['subtitle']) return render_template('messages/edit_post.html', { 'post': post, 'topic': g_topic, 'text': request.POST['text'], }) # The user just sent data, handle them post.text = request.POST['text'] post.update = datetime.now() post.save() # Modifying the thread info if g_topic: g_topic.title = request.POST['title'] g_topic.subtitle = request.POST['subtitle'] g_topic.save() return redirect(post.get_absolute_url()) else: return render_template('messages/edit_post.html', { 'post': post, 'topic': g_topic, 'text': post.text })
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})
def edit_post(request): """Edit a post in a message. Returns: HttpResponse """ try: post_pk = request.GET['message'] except KeyError: raise Http404 post = get_object_or_404(PrivatePost, pk=post_pk) # Only edit last private post tp = get_object_or_404(PrivateTopic, pk=post.privatetopic.pk) last = get_object_or_404(PrivatePost, pk=tp.last_message.pk) if not last.pk == post.pk: raise PermissionDenied g_topic = None if post.position_in_topic == 1: g_topic = get_object_or_404(PrivateTopic, pk=post.privatetopic.pk) # Making sure the user is allowed to do that if post.author != request.user: if not request.user.has_perm('mp.change_post'): raise PermissionDenied elif request.method == 'GET': messages.add_message( request, messages.WARNING, u'Vous éditez ce message en tant que modérateur (auteur : {}).' u' Soyez encore plus prudent lors de l\'édition de celui-ci !'. format(post.author.username)) if request.method == 'POST': # Using the preview button if 'preview' in request.POST: if g_topic: g_topic = PrivateTopic(title=request.POST['title'], subtitle=request.POST['subtitle']) return render_template('messages/edit_post.html', { 'post': post, 'topic': g_topic, 'text': request.POST['text'], }) # The user just sent data, handle them post.text = request.POST['text'] post.update = datetime.now() post.save() # Modifying the thread info if g_topic: g_topic.title = request.POST['title'] g_topic.subtitle = request.POST['subtitle'] g_topic.save() return redirect(post.get_absolute_url()) else: return render_template('messages/edit_post.html', { 'post': post, 'topic': g_topic, 'text': post.text })
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})