コード例 #1
0
ファイル: models.py プロジェクト: progdupeupl/pdp_website
def mark_read(topic, user=None):
    """Mark a topic as read for an user.

    If no user is provided, this will use the current session user.

    Args:
        topic: Topic to be marked as read
        user: User who as read the topic

    """

    if user is None:
        user = get_current_user()

    # We update existing TopicRead or create a new one
    req = TopicRead.objects.filter(topic=topic, user=user)
    if req:
        t = req[0]
    else:
        t = TopicRead(topic=topic, user=user)

    t.post = topic.last_message
    t.save()

    # If the topic is followed, we want to update some cached values
    if topic.is_followed(user):
        template_cache_delete('topbar-topics', [user.username])
        if topic in get_last_topics():
            template_cache_delete('home-forums', [user.username])
コード例 #2
0
def mark_read(topic, user=None):
    """Mark a topic as read for an user.

    If no user is provided, this will use the current session user.

    Args:
        topic: Topic to be marked as read
        user: User who as read the topic

    """

    if user is None:
        user = get_current_user()

    # We update existing TopicRead or create a new one
    req = TopicRead.objects.filter(topic=topic, user=user)
    if req:
        t = req[0]
    else:
        t = TopicRead(topic=topic, user=user)

    t.post = topic.last_message
    t.save()

    # If the topic is followed, we want to update some cached values
    if topic.is_followed(user):
        template_cache_delete('topbar-topics', [user.username])
        if topic in get_last_topics():
            template_cache_delete('home-forums', [user.username])
コード例 #3
0
ファイル: views.py プロジェクト: progdupeupl/pdp_website
def modify_tutorial(request):
    """Modify a tutorial.

    Returns:
        HttpResponse

    """

    tutorial_pk = request.POST['tutorial']
    tutorial = get_object_or_404(Tutorial, pk=tutorial_pk)

    # Validator actions
    if request.user.has_perm('tutorial.change_tutorial'):
        if 'validate' in request.POST:

            # We can't validate a non-pending tutorial
            if not tutorial.is_pending:
                raise PermissionDenied

            tutorial.is_pending = False
            tutorial.is_beta = False
            tutorial.is_visible = True
            tutorial.pubdate = datetime.now()
            tutorial.save()

            # We create a topic on forum for feedback
            if BOT_ENABLED:
                bot.create_tutorial_topic(tutorial)

            # We update home page tutorial cache
            template_cache_delete('home-tutorials')

            return redirect(tutorial.get_absolute_url())

        if 'refuse' in request.POST:
            if not tutorial.is_pending:
                raise PermissionDenied

            tutorial.is_pending = False
            tutorial.save()

            return redirect(tutorial.get_absolute_url())

    # User actions
    if request.user in tutorial.authors.all():
        if 'add_author' in request.POST:
            redirect_url = reverse('pdp.tutorial.views.edit_tutorial') + \
                '?tutoriel={0}'.format(tutorial.pk)

            author_username = request.POST['author']
            author = None
            try:
                author = User.objects.get(username=author_username)
            except User.DoesNotExist:
                return redirect(redirect_url)

            tutorial.authors.add(author)
            tutorial.save()

            return redirect(redirect_url)

        elif 'remove_author' in request.POST:
            redirect_url = reverse('pdp.tutorial.views.edit_tutorial') + \
                '?tutoriel={0}'.format(tutorial.pk)

            # Avoid orphan tutorials
            if tutorial.authors.all().count() <= 1:
                raise PermissionDenied

            author_pk = request.POST['author']
            author = get_object_or_404(User, pk=author_pk)

            tutorial.authors.remove(author)
            tutorial.save()

            return redirect(redirect_url)

        elif 'delete' in request.POST:
            tutorial.delete()
            return redirect('/tutoriels/')

        elif 'pending' in request.POST:
            if tutorial.is_pending:
                raise PermissionDenied

            tutorial.is_pending = True
            tutorial.save()
            return redirect(tutorial.get_absolute_url())

        elif 'beta' in request.POST:
            tutorial.is_beta = not tutorial.is_beta
            tutorial.save()
            return redirect(tutorial.get_absolute_url())

    # No action performed, no rights for request user, or not existing
    # action called.
    raise PermissionDenied
コード例 #4
0
ファイル: views.py プロジェクト: progdupeupl/pdp_website
def edit_tutorial(request):
    """Edit a tutorial.

    Returns:
        HttpResponse

    """
    try:
        tutorial_pk = request.GET['tutoriel']
    except KeyError:
        raise Http404

    tutorial = get_object_or_404(Tutorial, pk=tutorial_pk)

    if request.user not in tutorial.authors.all():
        raise Http404

    if request.method == 'POST':
        form = EditTutorialForm(request.POST, request.FILES)
        if form.is_valid():
            data = form.data
            tutorial.title = data['title']
            tutorial.description = data['description']
            tutorial.introduction = data['introduction']
            tutorial.conclusion = data['conclusion']

            if 'image' in request.FILES:
                tutorial.image = request.FILES['image']

            # Update tags
            tutorial.tags.clear()
            list_tags = data['tags'].split(',')

            # If we don't give any tags the list_tags will be [u''] so we check
            # that list_tags[0] is not null. We add the if list_tags before to
            # avoid IndexError.
            if list_tags and list_tags[0]:
                for tag in list_tags:
                    tutorial.tags.add(tag.strip().lower())

            # Update category
            category = None
            if 'category' in data:
                try:
                    category = TutorialCategory.objects.get(
                        pk=int(data['category'])
                    )
                except ValueError:
                    category = None
                except TutorialCategory.DoesNotExist:
                    category = None

            tutorial.category = category

            tutorial.update = datetime.now()
            tutorial.save()

            # If the tutorial was on the home page, clean cache
            if tutorial in get_last_tutorials():
                template_cache_delete('home-tutorials')

            return redirect(tutorial.get_absolute_url())
    else:
        if not tutorial.category:
            tutorial_category_pk = None
        else:
            tutorial_category_pk = tutorial.category.pk

        # initial value for tags input
        list_tags = ''
        first_tag = True
        for tag in tutorial.tags.all():
            if first_tag:
                first_tag = False
            else:
                list_tags += ', '
            list_tags += tag.__str__()

        form = EditTutorialForm({
            'title': tutorial.title,
            'description': tutorial.description,
            'category': tutorial_category_pk,
            'tags': list_tags,
            'introduction': tutorial.introduction,
            'conclusion': tutorial.conclusion
        })

    return render_template('tutorial/edit_tutorial.html', {
        'tutorial': tutorial, 'form': form
    })