Exemplo n.º 1
0
def create_topic(author, forum, title, subtitle, text, key):
    """create topic in forum"""

    (tags, title_only) = get_tag_by_title(title[:80])

    # Creating the thread
    n_topic = Topic()
    n_topic.forum = forum
    n_topic.title = title_only
    n_topic.subtitle = subtitle
    n_topic.pubdate = datetime.now()
    n_topic.author = author
    n_topic.key = key
    n_topic.save()
    n_topic.add_tags(tags)
    n_topic.save()

    # Add the first message
    post = Post()
    post.topic = n_topic
    post.author = author
    post.text = text
    post.text_html = emarkdown(text)
    post.pubdate = datetime.now()
    post.position = 1
    post.save()

    n_topic.last_message = post
    n_topic.save()

    follow(n_topic, user=author)

    return n_topic
Exemplo n.º 2
0
 def perform_follow(topic, user):
     return follow(topic, user)
Exemplo n.º 3
0
def settings_promote(request, user_pk):
    """ Manage the admin right of user. Only super user can access """

    if not request.user.is_superuser:
        raise PermissionDenied

    profile = get_object_or_404(Profile, user__pk=user_pk)
    user = profile.user

    if request.method == "POST":
        form = PromoteMemberForm(request.POST)
        data = dict(form.data.iterlists())

        groups = Group.objects.all()
        usergroups = user.groups.all()

        if 'groups' in data:
            for group in groups:
                if unicode(group.id) in data['groups']:
                    if group not in usergroups:
                        user.groups.add(group)
                        messages.success(
                            request,
                            _(u'{0} appartient maintenant au groupe {1}.').
                            format(user.username, group.name))
                else:
                    if group in usergroups:
                        user.groups.remove(group)
                        messages.warning(
                            request,
                            _(u'{0} n\'appartient maintenant plus au groupe {1}.'
                              ).format(user.username, group.name))
                        topics_followed = Topic.objects.filter(
                            topicfollowed__user=user, forum__group=group)
                        for topic in topics_followed:
                            follow(topic, user)
        else:
            for group in usergroups:
                topics_followed = Topic.objects.filter(
                    topicfollowed__user=user, forum__group=group)
                for topic in topics_followed:
                    follow(topic, user)
            user.groups.clear()
            messages.warning(
                request,
                _(u'{0} n\'appartient (plus ?) à aucun groupe.').format(
                    user.username))

        if 'superuser' in data and u'on' in data['superuser']:
            if not user.is_superuser:
                user.is_superuser = True
                messages.success(
                    request,
                    _(u'{0} est maintenant super-utilisateur.').format(
                        user.username))
        else:
            if user == request.user:
                messages.error(
                    request,
                    _(u'Un super-utilisateur ne peut pas se retirer des super-utilisateurs.'
                      ))
            else:
                if user.is_superuser:
                    user.is_superuser = False
                    messages.warning(
                        request,
                        _(u'{0} n\'est maintenant plus super-utilisateur.').
                        format(user.username))

        if 'activation' in data and u'on' in data['activation']:
            user.is_active = True
            messages.success(
                request,
                _(u'{0} est maintenant activé.').format(user.username))
        else:
            user.is_active = False
            messages.warning(request,
                             _(u'{0} est désactivé.').format(user.username))

        user.save()

        usergroups = user.groups.all()
        bot = get_object_or_404(
            User, username=settings.ZDS_APP['member']['bot_account'])
        msg = _(u'Bonjour {0},\n\n'
                u'Un administrateur vient de modifier les groupes '
                u'auxquels vous appartenez.  \n').format(user.username)
        if len(usergroups) > 0:
            msg = string_concat(
                msg,
                _(u'Voici la liste des groupes dont vous faites dorénavant partie :\n\n'
                  ))
            for group in usergroups:
                msg += u'* {0}\n'.format(group.name)
        else:
            msg = string_concat(msg,
                                _(u'* Vous ne faites partie d\'aucun groupe'))
        msg += u'\n\n'
        if user.is_superuser:
            msg = string_concat(
                msg,
                _(u'Vous avez aussi rejoint le rang des super-utilisateurs. '
                  u'N\'oubliez pas, un grand pouvoir entraîne de grandes responsabilités !'
                  ))
        send_mp(
            bot,
            [user],
            _(u'Modification des groupes'),
            u'',
            msg,
            True,
            True,
        )

        return redirect(profile.get_absolute_url())

    form = PromoteMemberForm(
        initial={
            'superuser': user.is_superuser,
            'groups': user.groups.all(),
            'activation': user.is_active
        })
    return render(request, 'member/settings/promote.html', {
        "usr": user,
        "profile": profile,
        "form": form
    })
Exemplo n.º 4
0
def send_post(
    request,
    topic,
    author,
    text,
    send_by_mail=False,
):
    post = Post()
    post.topic = topic
    post.author = author
    post.update_content(text)
    post.pubdate = datetime.now()
    if topic.last_message is not None:
        post.position = topic.last_message.position + 1
    else:
        post.position = 1
    post.ip_address = get_client_ip(request)
    post.save()

    topic.last_message = post
    topic.save()

    # Send mail
    if send_by_mail:
        subject = u"{} - {} : {}".format(
            settings.ZDS_APP['site']['litteral_name'], _(u'Forum'),
            topic.title)
        from_email = "{} <{}>".format(
            settings.ZDS_APP['site']['litteral_name'],
            settings.ZDS_APP['site']['email_noreply'])

        followers = topic.get_followers_by_email()
        for follower in followers:
            receiver = follower.user
            if receiver == post.author:
                continue
            last_read = TopicRead.objects.filter(topic=topic,
                                                 post__position=post.position -
                                                 1,
                                                 user=receiver).count()
            if last_read > 0:
                context = {
                    'username':
                    receiver.username,
                    'title':
                    topic.title,
                    'url':
                    settings.ZDS_APP['site']['url'] + post.get_absolute_url(),
                    'author':
                    post.author.username,
                    'site_name':
                    settings.ZDS_APP['site']['litteral_name']
                }
                message_html = render_to_string('email/forum/new_post.html',
                                                context)
                message_txt = render_to_string('email/forum/new_post.txt',
                                               context)

                msg = EmailMultiAlternatives(subject, message_txt, from_email,
                                             [receiver.email])
                msg.attach_alternative(message_html, "text/html")
                msg.send()

    # Follow topic on answering
    if not topic.is_followed(user=post.author):
        follow(topic)

    return topic
Exemplo n.º 5
0
def settings_promote(request, user_pk):
    """ Manage the admin right of user. Only super user can access """

    if not request.user.is_superuser:
        raise PermissionDenied

    profile = get_object_or_404(Profile, user__pk=user_pk)
    user = profile.user
    
    if request.method == "POST":
        form = PromoteMemberForm(request.POST)
        data = dict(form.data.iterlists())

        groups = Group.objects.all()
        usergroups = user.groups.all()
        
        if 'groups' in data:
            for group in groups:
                if unicode(group.id) in data['groups']:
                    if group not in usergroups:
                        user.groups.add(group)
                        messages.success(request, u'{0} appartient maintenant au groupe {1}'
                                                    .format(user.username, group.name))
                else:
                    if group in usergroups:
                        user.groups.remove(group)
                        messages.warning(request, u'{0} n\'appartient maintenant plus au groupe {1}'
                                                    .format(user.username, group.name))
                        topics_followed = Topic.objects.filter(topicfollowed__user=user,
                                                               forum__group=group)
                        for topic in topics_followed:
                            follow(topic, user)
        else:
            for group in usergroups:
                topics_followed = Topic.objects.filter(topicfollowed__user=user,
                                                       forum__group=group)
                for topic in topics_followed:
                    follow(topic, user)
            user.groups.clear()
            messages.warning(request, u'{0} n\'appartient (plus ?) à aucun groupe'
                                        .format(user.username))
        
        if 'superuser' in data and u'on' in data['superuser']:
            if not user.is_superuser:
                user.is_superuser = True
                messages.success(request, u'{0} est maintenant super-utilisateur'
                                            .format(user.username))
        else:
            if user == request.user:
                messages.error(request, u'Un super-utilisateur ne peux pas se retirer des super-utilisateur')
            else:
                if user.is_superuser:
                    user.is_superuser = False
                    messages.warning(request, u'{0} n\'est maintenant plus super-utilisateur'
                                                .format(user.username))

        user.save()
        
        usergroups = user.groups.all()
        bot = get_object_or_404(User, username=settings.BOT_ACCOUNT)
        msg = (u'Bonjour {0},\n\n'
               u'Un administrateur vient de modifier les groupes '
               u'auxquels vous appartenez.  \n'.format(user.username))
        if len(usergroups) > 0:
            msg += u'Voici la liste des groupes dont vous faites dorénavant partis :\n\n'
            for group in usergroups:
                msg += u'* {0}\n'.format(group.name)
        else:
            msg += u'* Vous ne faites partis d\'aucun groupe'
        msg += u'\n\n'
        if user.is_superuser:
            msg += (u'Vous avez aussi rejoint le rang des super utilisateurs. '
                    u'N\'oubliez pas, un grand pouvoir entraine de grandes responsabiltiés !')
        send_mp(
            bot,
            [user],
            u'Modification des groupes',
            u'',
            msg,
            True,
            True,
        )
        
        return redirect(profile.get_absolute_url())

    form = PromoteMemberForm(initial={'superuser': user.is_superuser,
                                      'groups': user.groups.all()
                                     })
    
    return render_template('member/settings/promote.html', {
        "usr": user,
        "profile": profile,
        "form": form
        })