Beispiel #1
0
def history(request,
            group_slug=None,
            group_slug_field=None,
            group_qs=None,
            article_qs=ALL_ARTICLES,
            changes_qs=ALL_CHANGES,
            template_name='recentchanges.html',
            template_dir='wiki',
            extra_context=None,
            *args,
            **kw):

    if request.method == 'GET':
        if group_slug is not None:
            group = get_object_or_404(group_qs,
                                      **{group_slug_field: group_slug})
            changes_qs = changes_qs.filter(article__content_type=get_ct(group),
                                           article__object_id=group.id)
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            allow_read = allow_write = True

        if not allow_read:
            return HttpResponseForbidden()

        template_params = {
            'changes': changes_qs.order_by('-modified'),
            'allow_write': allow_write
        }

        #####
        if group_slug is not None:
            #            template_params['group'] = group
            new_article = ArticleClass(title="NewArticle",
                                       content_type=get_ct(group),
                                       object_id=group.id)
        else:
            new_article = ArticleClass(title="NewArticle")
        template_params['new_article'] = new_article
        #####
        #        template_params = {'new_article': new_article,

        if group_slug is not None:
            template_params['group'] = group_slug

        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response('/'.join([template_dir, template_name]),
                                  template_params,
                                  context_instance=RequestContext(request))
    return HttpResponseNotAllowed(['GET'])
Beispiel #2
0
def view_changeset(request, title, revision,
                   group_slug=None, group_slug_field=None, group_qs=None,
                   article_qs=ALL_ARTICLES,
                   changes_qs=ALL_CHANGES,
                   template_name='changeset.html',
                   template_dir='wiki',
                   extra_context=None,
                   is_member=None,
                   is_private=None,
                   *args, **kw):

    if request.method == "GET":
        article_args = {'article__title': title}
        if group_slug is not None:
            group = get_object_or_404(group_qs,**{group_slug_field: group_slug})
            article_args.update({'article__content_type': get_ct(group),
                                 'article__object_id': group.id})
        changeset = get_object_or_404(
            changes_qs,
            revision=int(revision),
            **article_args)

        article_args = {'title': title}
        if group_slug is not None:
            group = get_object_or_404(group_qs,**{group_slug_field: group_slug})
            article_args.update({'content_type': get_ct(group),
                                 'object_id': group.id})
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            allow_read = allow_write = True

        if not allow_read:
            return HttpResponseForbidden()

        article = article_qs.get(**article_args)

        template_params = {'article': article,
                           'article_title': article.title,
                           'changeset': changeset,
                           'allow_write': allow_write}

        if group_slug is not None:
            template_params['group'] = group
        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response('/'.join([template_dir, template_name]),
                                  template_params,
                                  context_instance=RequestContext(request))
    return HttpResponseNotAllowed(['GET'])
Beispiel #3
0
def history(request,
            group_slug=None, group_slug_field=None, group_qs=None,
            article_qs=ALL_ARTICLES, changes_qs=ALL_CHANGES,
            template_name='recentchanges.html',
            template_dir='wiki',
            extra_context=None,
            *args, **kw):

    if request.method == 'GET':
        if  group_slug is not None:
            group = get_object_or_404(group_qs,
                                      **{group_slug_field : group_slug})
            changes_qs = changes_qs.filter(article__content_type=get_ct(group),
                                           article__object_id=group.id)
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            allow_read = allow_write = True

        if not allow_read:
            return HttpResponseForbidden()

        template_params = {'changes': changes_qs.order_by('-modified'),
                           'allow_write': allow_write}


#####
        if group_slug is not None:
#            template_params['group'] = group
            new_article = ArticleClass(title="NewArticle",
                                       content_type=get_ct(group),
                                       object_id=group.id)
        else:
            new_article = ArticleClass(title="NewArticle")
        template_params['new_article'] = new_article
#####
#        template_params = {'new_article': new_article,


        if group_slug is not None:
            template_params['group'] = group_slug

        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response('/'.join([template_dir, template_name]),
                                  template_params,
                                  context_instance=RequestContext(request))
    return HttpResponseNotAllowed(['GET'])
Beispiel #4
0
def revert_to_revision(request,
                       title,
                       group_slug=None,
                       bridge=None,
                       article_qs=ALL_ARTICLES,
                       extra_context=None,
                       is_member=None,
                       is_private=None,
                       *args,
                       **kw):

    if request.method == 'POST':

        revision = int(request.POST['revision'])

        article_args = {'title': title}

        group = None
        if group_slug is not None:
            try:
                group = bridge.get_group(group_slug)
            except ObjectDoesNotExist:
                raise Http404
            # @@@ use bridge instead
            article_args.update({
                'content_type': get_ct(group),
                'object_id': group.id
            })
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            group = None
            allow_read = allow_write = True
            article_args.update({'object_id': None})

        if not (allow_read or allow_write):
            return HttpResponseForbidden()

        article = get_object_or_404(article_qs, **article_args)

        if request.user.is_authenticated():
            article.revert_to(revision, get_real_ip(request), request.user)
        else:
            article.revert_to(revision, get_real_ip(request))

        if request.user.is_authenticated():
            request.user.message_set.create(
                message=u"The article was reverted successfully.")

        url = get_url('wiki_article_history',
                      group,
                      kw={
                          'title': title,
                      },
                      bridge=bridge)

        return redirect_to(request, url)

    return HttpResponseNotAllowed(['POST'])
Beispiel #5
0
    def __init__(self,
                 request,
                 group_slug=None,
                 bridge=None,
                 article_qs=ALL_ARTICLES,
                 changes_qs=ALL_CHANGES,
                 extra_context=None,
                 title_template=u'feeds/history_title.html',
                 description_template=u'feeds/history_description.html',
                 *args,
                 **kw):

        if group_slug is not None:
            try:
                group = bridge.get_group(group_slug)
            except ObjectDoesNotExist:
                raise Http404
            self.changes_qs = changes_qs.filter(
                article__content_type=get_ct(group),
                article__object_id=group.id)
        else:
            self.changes_qs = changes_qs.filter(article__object_id=None)

        self.title_template = get_template(title_template)
        self.description_template = get_template(description_template)
        super(AtomHistoryFeed, self).__init__('', request)
Beispiel #6
0
def stop_observing_article(request,
                           title,
                           group_slug=None,
                           group_slug_field=None,
                           group_qs=None,
                           article_qs=ALL_ARTICLES,
                           template_name='recentchanges.html',
                           template_dir='wiki',
                           extra_context=None,
                           is_member=None,
                           is_private=None,
                           *args,
                           **kw):
    article_args = {'title': title}
    group = None
    if group_slug is not None:
        group = get_object_or_404(group_qs, **{group_slug_field: group_slug})
        article_args.update({
            'content_type': get_ct(group),
            'object_id': group.id
        })
        allow_read = has_read_perm(request.user, group, is_member, is_private)
    else:
        allow_read = True

    if not allow_read:
        return HttpResponseForbidden()

    article = get_object_or_404(article_qs, **article_args)

    if notification.is_observing(article, request.user):
        notification.stop_observing(article, request.user)

    return redirect(article)
Beispiel #7
0
def stop_observing_article(request, title,
                           group_slug=None, group_slug_field=None, group_qs=None,
                           article_qs=ALL_ARTICLES,
                           template_name='recentchanges.html',
                           template_dir='wiki',
                           extra_context=None,
                           is_member=None,
                           is_private=None,
                           *args, **kw):
    if request.method == 'POST':

        article_args = {'title': title}
        group = None
        if group_slug is not None:
            group = get_object_or_404(group_qs,**{group_slug_field: group_slug})
            article_args.update({'content_type': get_ct(group),
                                 'object_id': group.id})
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
        else:
            allow_read = True

        if not allow_read:
            return HttpResponseForbidden()

        article = get_object_or_404(article_qs, **article_args)

        notification.stop_observing(article, request.user)

        url = get_url('wiki_article', group,
                      [article.title], {'title': article.title,
                                        'group_slug': group_slug})

        return redirect_to(request, url)
    return HttpResponseNotAllowed(['POST'])
Beispiel #8
0
    def __init__(
        self,
        title,
        request,
        group_slug=None,
        group_slug_field=None,
        group_qs=None,
        article_qs=ALL_ARTICLES,
        changes_qs=ALL_CHANGES,
        extra_context=None,
        title_template=u"feeds/history_title.html",
        description_template=u"feeds/history_description.html",
        *args,
        **kw
    ):

        if group_slug is not None:
            group = get_object_or_404(group_qs, **{group_slug_field: group_slug})
            self.article_qs = article_qs.filter(content_type=get_ct(group), object_id=group.id)
        else:
            self.article_qs = article_qs

        self.title_template = get_template(title_template)
        self.description_template = get_template(description_template)
        super(AtomArticleHistoryFeed, self).__init__("", request)
Beispiel #9
0
def view_changeset(request,
                   title,
                   revision,
                   group_slug=None,
                   bridge=None,
                   article_qs=ALL_ARTICLES,
                   changes_qs=ALL_CHANGES,
                   template_name='changeset.html',
                   template_dir='wiki',
                   extra_context=None,
                   is_member=None,
                   is_private=None,
                   *args,
                   **kw):

    if request.method == "GET":
        article_args = {'article__title': title}
        if group_slug is not None:
            try:
                group = bridge.get_group(group_slug)
            except ObjectDoesNotExist:
                raise Http404
            # @@@ hmm, need to look into this for the bridge i think
            article_args.update({
                'article__content_type': get_ct(group),
                'article__object_id': group.id
            })
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            group = None
            allow_read = allow_write = True
            article_args.update({'article__object_id': None})

        if not allow_read:
            return HttpResponseForbidden()

        changeset = get_object_or_404(changes_qs.select_related(),
                                      revision=int(revision),
                                      **article_args)

        article = changeset.article

        template_params = {
            'article': article,
            'article_title': article.title,
            'changeset': changeset,
            'allow_write': allow_write
        }

        template_params['group'] = group
        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response(os.path.join(template_dir, template_name),
                                  template_params,
                                  context_instance=RequestContext(request))
    return HttpResponseNotAllowed(['GET'])
Beispiel #10
0
def revert_to_revision(request,
                       title,
                       group_slug=None,
                       group_slug_field=None,
                       group_qs=None,
                       article_qs=ALL_ARTICLES,
                       extra_context=None,
                       is_member=None,
                       is_private=None,
                       *args,
                       **kw):

    if request.method == 'POST':

        revision = int(request.POST['revision'])

        article_args = {'title': title}

        group = None
        if group_slug is not None:
            group = get_object_or_404(group_qs,
                                      **{group_slug_field: group_slug})
            article_args.update({
                'content_type': get_ct(group),
                'object_id': group.id
            })
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            allow_read = allow_write = True

        if not (allow_read or allow_write):
            return HttpResponseForbidden()

        article = get_object_or_404(article_qs, **article_args)

        # Check whether there is another Article with the same name to which this article
        # wants to be reverted to. If so: prevent it and show a message.
        old_title = article.changeset_set.filter(revision=revision +
                                                 1).get().old_title
        try:
            art = Article.objects.exclude(pk=article.pk).get(title=old_title)
        except Article.DoesNotExist:
            # No existing article found -> reverting possible
            if request.user.is_authenticated:
                article.revert_to(revision, request.user)
            else:
                article.revert_to(revision)
            return redirect(article)
        # An article with this name exists
        messages.error(
            request,
            'Reverting not possible because an article with name \'%s\' already exists'
            % old_title)
        return redirect(article)

    return HttpResponseNotAllowed(['POST'])
Beispiel #11
0
def get_articles_by_group(article_qs, group_slug=None,
                          group_slug_field=None, group_qs=None):
    group = None
    if group_slug is not None:
        group = get_object_or_404(group_qs,
                                  **{group_slug_field: group_slug})
        article_qs = article_qs.filter(content_type=get_ct(group),
                                       object_id=group.id)
    return article_qs, group
Beispiel #12
0
def get_articles_by_group(article_qs,
                          group_slug=None,
                          group_slug_field=None,
                          group_qs=None):
    group = None
    if group_slug is not None:
        group = get_object_or_404(group_qs, **{group_slug_field: group_slug})
        article_qs = article_qs.filter(content_type=get_ct(group),
                                       object_id=group.id)
    return article_qs, group
Beispiel #13
0
def article_history(request,
                    title,
                    group_slug=None,
                    group_slug_field=None,
                    group_qs=None,
                    article_qs=ALL_ARTICLES,
                    template_name='history.html',
                    template_dir='wiki',
                    extra_context=None,
                    is_member=None,
                    is_private=None,
                    *args,
                    **kw):

    if request.method == 'GET':

        article_args = {'title': title}
        if group_slug is not None:
            group = get_object_or_404(group_qs,
                                      **{group_slug_field: group_slug})
            article_args.update({
                'content_type': get_ct(group),
                'object_id': group.id
            })
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            allow_read = allow_write = True

        if not allow_read:
            return HttpResponseForbidden()

        article = get_object_or_404(article_qs, **article_args)
        # changes = article.changeset_set.filter(
        #    reverted=False).order_by('-revision')
        changes = article.changeset_set.all().order_by('-revision')

        template_params = {
            'article': article,
            'changes': changes,
            'allow_write': allow_write
        }
        if group_slug is not None:
            template_params['group'] = group
        if extra_context is not None:
            template_params.update(extra_context)

        return render(
            request,
            '/'.join([template_dir, template_name]),
            template_params,
        )

    return HttpResponseNotAllowed(['GET'])
Beispiel #14
0
def article_history(request,
                    title,
                    group_slug=None,
                    bridge=None,
                    article_qs=ALL_ARTICLES,
                    template_name='history.html',
                    template_dir='wiki',
                    extra_context=None,
                    is_member=None,
                    is_private=None,
                    *args,
                    **kw):

    if request.method == 'GET':

        article_args = {'title': title}
        if group_slug is not None:
            try:
                group = bridge.get_group(group_slug)
            except ObjectDoesNotExist:
                raise Http404
            # @@@ use bridge instead
            article_args.update({
                'content_type': get_ct(group),
                'object_id': group.id
            })
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            group = None
            allow_read = allow_write = True
            article_args.update({'object_id': None})

        if not allow_read:
            return HttpResponseForbidden()

        article = get_object_or_404(article_qs, **article_args)
        changes = article.changeset_set.all().order_by('-revision')

        template_params = {
            'article': article,
            'changes': changes,
            'allow_write': allow_write
        }
        template_params['group'] = group
        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response(os.path.join(template_dir, template_name),
                                  template_params,
                                  context_instance=RequestContext(request))

    return HttpResponseNotAllowed(['GET'])
Beispiel #15
0
    def clean_title(self):
        """ Whiteboard title is always the group slug.
        Really, whiteboards don't need titles, but using the slug enforces
        uniqueness... (can relax this if we do full wikis for groups)
        (actually, is this even called, since "title" is in the exclude list?)
        """
        
        ctype = get_ct(self.cleaned_data['content_type'])
        group = ctype.get_object_for_this_type(pk=self.cleaned_data['object_id'])
        self.cleaned_data['title'] = group.slug

        return self.cleaned_data['title']
Beispiel #16
0
def revert_to_revision(request, title,
                       group_slug=None, bridge=None,
                       article_qs=ALL_ARTICLES,
                       extra_context=None,
                       is_member=None,
                       is_private=None,
                       *args, **kw):

    if request.method == 'POST':

        revision = int(request.POST['revision'])

        article_args = {'title': title}

        group = None
        if group_slug is not None:
            try:
                group = bridge.get_group(group_slug)
            except ObjectDoesNotExist:
                raise Http404
            # @@@ use bridge instead
            article_args.update({'content_type': get_ct(group),
                                 'object_id': group.id})
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            group = None
            allow_read = allow_write = True
            article_args.update({'object_id': None})

        if not (allow_read or allow_write):
            return HttpResponseForbidden()

        article = get_object_or_404(article_qs, **article_args)

        if request.user.is_authenticated():
            article.revert_to(revision, get_real_ip(request), request.user)
        else:
            article.revert_to(revision, get_real_ip(request))


        if request.user.is_authenticated():
            request.user.message_set.create(
                message=u"The article was reverted successfully.")
                
        url = get_url('wiki_article_history', group, kw={
            'title': title,
        }, bridge=bridge)
        
        return redirect_to(request, url)

    return HttpResponseNotAllowed(['POST'])
Beispiel #17
0
    def clean_title(self):
        """ Whiteboard title is always the group slug.
        Really, whiteboards don't need titles, but using the slug enforces
        uniqueness... (can relax this if we do full wikis for groups)
        (actually, is this even called, since "title" is in the exclude list?)
        """
        
        ctype = get_ct(self.cleaned_data['content_type'])
        group = ctype.get_object_for_this_type(pk=self.cleaned_data['object_id'])
        self.cleaned_data['title'] = group.slug

        return self.cleaned_data['title']
Beispiel #18
0
def view_article(request, title,
                 ArticleClass=Article, # to create an unsaved instance
                 group_slug=None, group_slug_field=None, group_qs=None,
                 article_qs=ALL_ARTICLES,
                 template_name='view.html',
                 template_dir='wiki',
                 extra_context=None,
                 is_member=None,
                 is_private=None,
                 *args, **kw):

    if request.method == 'GET':
        article_args = {'title': title}
        if group_slug is not None:
            group = get_object_or_404(group_qs,**{group_slug_field: group_slug})
            article_args.update({'content_type': get_ct(group),
                                 'object_id': group.id})
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            allow_read = allow_write = True

        if not allow_read:
            return HttpResponseForbidden()

        try:
            article = article_qs.get(**article_args)
            if notification is not None:
                is_observing = notification.is_observing(article, request.user)
            else:
                is_observing = False
        except ArticleClass.DoesNotExist:
            article = ArticleClass(**article_args)
            is_observing = False

        template_params = {'article': article,
                           'allow_write': allow_write}

        if notification is not None:
            template_params.update({'is_observing': is_observing,
                                    'can_observe': True})

        if group_slug is not None:
            template_params['group'] = group
        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response('/'.join([template_dir, template_name]),
                                  template_params,
                                  context_instance=RequestContext(request))
    return HttpResponseNotAllowed(['GET'])
Beispiel #19
0
def article_list(request,
                 group_slug=None, group_slug_field=None, group_qs=None,
                 article_qs=ALL_ARTICLES,
                 ArticleClass=Article,
                 SearchFormClass=SearchForm,
                 template_name='index.html',
                 template_dir='wiki',
                 extra_context=None,
                 is_member=None,
                 is_private=None,
                 *args, **kw):
    if request.method == 'GET':

        articles, group = get_articles_by_group(
            article_qs, group_slug,
            group_slug_field, group_qs)

        allow_read = has_read_perm(request.user, group, is_member, is_private)
        allow_write = has_write_perm(request.user, group, is_member)

        if not allow_read:
            return HttpResponseForbidden()

        order = request.GET.get('order')

        if order == 'date':
            articles = articles.order_by('-created_at')
        else:
            articles = articles.order_by('title')

        search_form = SearchFormClass()

        template_params = {'articles': articles,
                           'order': order,
                           'search_form': search_form,
                           'allow_write': allow_write}

        if group_slug is not None:
            template_params['group'] = group
            new_article = ArticleClass(title="NewArticle",
                                       content_type=get_ct(group),
                                       object_id=group.id)
        else:
            new_article = ArticleClass(title="NewArticle")
        template_params['new_article'] = new_article
        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response('/'.join([template_dir, template_name]),
                                  template_params,
                                  context_instance=RequestContext(request))
    return HttpResponseNotAllowed(['GET'])
Beispiel #20
0
def view_changeset(request, title, revision,
                   group_slug=None, bridge=None,
                   article_qs=ALL_ARTICLES,
                   changes_qs=ALL_CHANGES,
                   template_name='changeset.html',
                   template_dir='wiki',
                   extra_context=None,
                   is_member=None,
                   is_private=None,
                   *args, **kw):

    if request.method == "GET":
        article_args = {'article__title': title}
        if group_slug is not None:
            try:
                group = bridge.get_group(group_slug)
            except ObjectDoesNotExist:
                raise Http404
            # @@@ hmm, need to look into this for the bridge i think
            article_args.update({'article__content_type': get_ct(group),
                                 'article__object_id': group.id})
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            group = None
            allow_read = allow_write = True
            article_args.update({'article__object_id': None})

        if not allow_read:
            return HttpResponseForbidden()

        changeset = get_object_or_404(
            changes_qs.select_related(),
            revision=int(revision),
            **article_args)

        article = changeset.article

        template_params = {'article': article,
                           'article_title': article.title,
                           'changeset': changeset,
                           'allow_write': allow_write}

        template_params['group'] = group
        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response(os.path.join(template_dir, template_name),
                                  template_params,
                                  context_instance=RequestContext(request))
    return HttpResponseNotAllowed(['GET'])
Beispiel #21
0
def article_list(request,
                 group_slug=None,
                 group_slug_field=None,
                 group_qs=None,
                 article_qs=ALL_ARTICLES,
                 ArticleClass=Article,
                 SearchFormClass=SearchForm,
                 template_name='index.html',
                 template_dir='wiki',
                 extra_context=None,
                 is_member=None,
                 is_private=None,
                 *args,
                 **kw):
    if request.method == 'GET':

        articles, group = get_articles_by_group(article_qs, group_slug,
                                                group_slug_field, group_qs)

        allow_read = has_read_perm(request.user, group, is_member, is_private)
        allow_write = has_write_perm(request.user, group, is_member)

        if not allow_read:
            return HttpResponseForbidden()

        articles = articles.order_by('-created_at')

        search_form = SearchFormClass()

        template_params = {
            'articles': articles,
            'search_form': search_form,
            'allow_write': allow_write
        }

        if group_slug is not None:
            template_params['group'] = group
            new_article = ArticleClass(title="NewArticle",
                                       content_type=get_ct(group),
                                       object_id=group.id)
        else:
            new_article = ArticleClass(title="NewArticle")
        template_params['new_article'] = new_article
        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response('/'.join([template_dir, template_name]),
                                  template_params,
                                  context_instance=RequestContext(request))
    return HttpResponseNotAllowed(['GET'])
Beispiel #22
0
def observe_article(request,
                    title,
                    group_slug=None,
                    bridge=None,
                    article_qs=ALL_ARTICLES,
                    template_name='recentchanges.html',
                    template_dir='wiki',
                    extra_context=None,
                    is_member=None,
                    is_private=None,
                    *args,
                    **kw):
    if request.method == 'POST':

        article_args = {'title': title}
        group = None
        if group_slug is not None:
            try:
                group = bridge.get_group(group_slug)
            except ObjectDoesNotExist:
                raise Http404
            article_args.update({
                'content_type': get_ct(group),
                'object_id': group.id
            })
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
        else:
            group = None
            allow_read = True

        if not allow_read:
            return HttpResponseForbidden()

        article = get_object_or_404(article_qs, **article_args)

        notification.observe(article, request.user,
                             'wiki_observed_article_changed')

        url = get_url('wiki_article',
                      group,
                      kw={
                          'title': article.title,
                      },
                      bridge=bridge)

        return redirect_to(request, url)

    return HttpResponseNotAllowed(['POST'])
Beispiel #23
0
def article_history(request, title,
                    group_slug=None, bridge=None,
                    article_qs=ALL_ARTICLES,
                    template_name='history.html',
                    template_dir='wiki',
                    extra_context=None,
                    is_member=None,
                    is_private=None,
                    *args, **kw):

    if request.method == 'GET':

        article_args = {'title': title}
        if group_slug is not None:
            try:
                group = bridge.get_group(group_slug)
            except ObjectDoesNotExist:
                raise Http404
            # @@@ use bridge instead
            article_args.update({'content_type': get_ct(group),
                                 'object_id': group.id})
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            group = None
            allow_read = allow_write = True
            article_args.update({'object_id': None})

        if not allow_read:
            return HttpResponseForbidden()

        article = get_object_or_404(article_qs, **article_args)
        changes = article.changeset_set.all().order_by('-revision')

        template_params = {'article': article,
                           'changes': changes,
                           'allow_write': allow_write}
        template_params['group'] = group
        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response(os.path.join(template_dir, template_name),
                                  template_params,
                                  context_instance=RequestContext(request))

    return HttpResponseNotAllowed(['GET'])
Beispiel #24
0
def revert_to_revision(
    request,
    title,
    group_slug=None,
    bridge=None,
    article_qs=ALL_WHITEBOARDS,
    extra_context=None,
    is_member=None,
    is_private=None,
    *args,
    **kw
):

    if request.method == "POST":

        revision = int(request.POST["revision"])

        article_args = {"title": title}

        group = None
        if group_slug is not None:
            try:
                group = bridge.get_group(group_slug)
            except ObjectDoesNotExist:
                raise Http404

            # permissions check
            if not group.user_is_member(request.user, admin_override=True):
                return HttpResponseForbidden()

            # @@@ use bridge instead
            article_args.update({"content_type": get_ct(group), "object_id": group.id})

        article = get_object_or_404(article_qs, **article_args)

        if request.user.is_authenticated():
            article.revert_to(revision, get_real_ip(request), request.user)
        else:
            article.revert_to(revision, get_real_ip(request))

        if request.user.is_authenticated():
            request.user.message_set.create(message=u"The article was reverted successfully.")

        url = group.get_absolute_url()
        return redirect_to(request, url)

    return HttpResponseNotAllowed(["POST"])
Beispiel #25
0
def revert_to_revision(request, title,
                       group_slug=None, group_slug_field=None, group_qs=None,
                       article_qs=ALL_ARTICLES,
                       extra_context=None,
                       is_member=None,
                       is_private=None,
                       *args, **kw):

    if request.method == 'POST':

        revision = int(request.POST['revision'])

        article_args = {'title': title}

        group = None
        if group_slug is not None:
            group = get_object_or_404(group_qs,**{group_slug_field: group_slug})
            article_args.update({'content_type': get_ct(group),
                                 'object_id': group.id})
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            allow_read = allow_write = True

        if not (allow_read or allow_write):
            return HttpResponseForbidden()

        article = get_object_or_404(article_qs, **article_args)

        if request.user.is_authenticated():
            article.revert_to(revision, get_real_ip(request), request.user)
        else:
            article.revert_to(revision, get_real_ip(request))


        if request.user.is_authenticated():
            messages.add_message(request, messages.INFO, u"The article was reverted successfully.")

        url = get_url('wiki_article_history', group,
                      [title], {'title': title,
                                'group_slug': group_slug})

        return redirect_to(request, url)

    return HttpResponseNotAllowed(['POST'])
Beispiel #26
0
def revert_to_revision(request, title,
                       group_slug=None, bridge=None,
                       article_qs=ALL_WHITEBOARDS,
                       extra_context=None,
                       is_member=None,
                       is_private=None,
                       *args, **kw):

    if request.method == 'POST':

        revision = int(request.POST['revision'])

        article_args = {'title': title}

        group = None
        if group_slug is not None:
            try:
                group = bridge.get_group(group_slug)
            except ObjectDoesNotExist:
                raise Http404
            
            # permissions check
            if not group.user_is_member(request.user, admin_override=True):
                return HttpResponseForbidden()
        
            # @@@ use bridge instead
            article_args.update({'content_type': get_ct(group),
                                 'object_id': group.id})
            
        article = get_object_or_404(article_qs, **article_args)

        if request.user.is_authenticated():
            article.revert_to(revision, get_real_ip(request), request.user)
        else:
            article.revert_to(revision, get_real_ip(request))


        if request.user.is_authenticated():
            request.user.message_set.create(
                message=u"The article was reverted successfully.")
                
        url = group.get_absolute_url()
        return redirect_to(request, url)

    return HttpResponseNotAllowed(['POST'])
Beispiel #27
0
    def __init__(self, request,
                 group_slug=None, group_slug_field=None, group_qs=None, 
                 article_qs=ALL_ARTICLES, changes_qs=ALL_CHANGES, 
                 extra_context=None, 
                 title_template = u'feeds/history_title.html', 
                 description_template = u'feeds/history_description.html', 
                 *args, **kw):

        if  group_slug is not None:
            group = get_object_or_404(group_qs, 
                                      **{group_slug_field : group_slug})
            self.changes_qs = changes_qs.filter(article__content_type=get_ct(group), 
                                                article__object_id=group.id)
        else:
            self.changes_qs = changes_qs

        self.title_template = title_template
        self.description_template = description_template
        super(RssHistoryFeed, self).__init__('', request)
Beispiel #28
0
def history(request,
            group_slug=None,
            bridge=None,
            article_qs=ALL_ARTICLES,
            changes_qs=ALL_CHANGES,
            template_name='recentchanges.html',
            template_dir='wiki',
            extra_context=None,
            *args,
            **kw):

    if request.method == 'GET':
        if group_slug is not None:
            try:
                group = bridge.get_group(group_slug)
            except ObjectDoesNotExist:
                raise Http404
            changes_qs = changes_qs.filter(article__content_type=get_ct(group),
                                           article__object_id=group.id)
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            group = None
            allow_read = allow_write = True
            changes_qs = changes_qs.filter(article__object_id=None)

        if not allow_read:
            return HttpResponseForbidden()

        template_params = {
            'changes': changes_qs.order_by('-modified'),
            'allow_write': allow_write
        }
        template_params['group'] = group_slug

        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response(os.path.join(template_dir, template_name),
                                  template_params,
                                  context_instance=RequestContext(request))
    return HttpResponseNotAllowed(['GET'])
Beispiel #29
0
def article_history(request, title,
                    group_slug=None, group_slug_field=None, group_qs=None,
                    article_qs=ALL_ARTICLES,
                    template_name='history.html',
                    template_dir='wiki',
                    extra_context=None,
                    is_member=None,
                    is_private=None,
                    *args, **kw):

    if request.method == 'GET':

        article_args = {'title': title}
        if group_slug is not None:
            group = get_object_or_404(group_qs,**{group_slug_field: group_slug})
            article_args.update({'content_type': get_ct(group),
                                 'object_id': group.id})
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            allow_read = allow_write = True

        if not allow_read:
            return HttpResponseForbidden()

        article = get_object_or_404(article_qs, **article_args)
        changes = article.changeset_set.filter(
            reverted=False).order_by('-revision')

        template_params = {'article': article,
                           'changes': changes,
                           'allow_write': allow_write}
        if group_slug is not None:
            template_params['group'] = group
        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response('/'.join([template_dir, template_name]),
                                  template_params,
                                  context_instance=RequestContext(request))

    return HttpResponseNotAllowed(['GET'])
Beispiel #30
0
def observe_article(request, title,
                    group_slug=None, bridge=None,
                    article_qs=ALL_ARTICLES,
                    template_name='recentchanges.html',
                    template_dir='wiki',
                    extra_context=None,
                    is_member=None,
                    is_private=None,
                    *args, **kw):
    if request.method == 'POST':

        article_args = {'title': title}
        group = None
        if group_slug is not None:
            try:
                group = bridge.get_group(group_slug)
            except ObjectDoesNotExist:
                raise Http404
            article_args.update({'content_type': get_ct(group),
                                 'object_id': group.id})
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
        else:
            group = None
            allow_read = True

        if not allow_read:
            return HttpResponseForbidden()

        article = get_object_or_404(article_qs, **article_args)

        notification.observe(article, request.user,
                             'wiki_observed_article_changed')
        
        url = get_url('wiki_article', group, kw={
            'title': article.title,
        }, bridge=bridge)
        
        return redirect_to(request, url)

    return HttpResponseNotAllowed(['POST'])
Beispiel #31
0
    def __init__(self, request,
                 group_slug=None, bridge=None, 
                 article_qs=ALL_ARTICLES, changes_qs=ALL_CHANGES, 
                 extra_context=None, 
                 title_template = u'feeds/history_title.html', 
                 description_template = u'feeds/history_description.html', 
                 *args, **kw):

        if  group_slug is not None:
            try:
                group = bridge.get_group(group_slug)
            except ObjectDoesNotExist:
                raise Http404
            self.changes_qs = changes_qs.filter(article__content_type=get_ct(group), 
                                                article__object_id=group.id)
        else:
            self.changes_qs = changes_qs.filter(article__object_id=None)

        self.title_template = get_template(title_template)
        self.description_template = get_template(description_template)
        super(AtomHistoryFeed, self).__init__('', request)
Beispiel #32
0
def history(request,
            group_slug=None, bridge=None,
            article_qs=ALL_ARTICLES, changes_qs=ALL_CHANGES,
            template_name='recentchanges.html',
            template_dir='wiki',
            extra_context=None,
            *args, **kw):

    if request.method == 'GET':
        if group_slug is not None:
            try:
                group = bridge.get_group(group_slug)
            except ObjectDoesNotExist:
                raise Http404
            changes_qs = changes_qs.filter(article__content_type=get_ct(group),
                                           article__object_id=group.id)
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            group = None
            allow_read = allow_write = True
            changes_qs = changes_qs.filter(article__object_id=None)

        if not allow_read:
            return HttpResponseForbidden()

        template_params = {'changes': changes_qs.order_by('-modified'),
                           'allow_write': allow_write}
        template_params['group'] = group_slug

        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response(os.path.join(template_dir, template_name),
                                  template_params,
                                  context_instance=RequestContext(request))
    return HttpResponseNotAllowed(['GET'])
Beispiel #33
0
def get_articles_for_object(object, article_qs=None):
    if article_qs is None:
        article_qs = ALL_ARTICLES
    return article_qs.filter(content_type=get_ct(object), object_id=object.id)
Beispiel #34
0
def view_article(
        request,
        title,
        revision=None,
        ArticleClass=Article,  # to create an unsaved instance
        group_slug=None,
        group_slug_field=None,
        group_qs=None,
        article_qs=ALL_ARTICLES,
        template_name='view.html',
        template_dir='wiki',
        extra_context=None,
        is_member=None,
        is_private=None,
        *args,
        **kw):

    if request.method == 'GET':
        article_args = {'title': title}
        if group_slug is not None:
            group = get_object_or_404(group_qs,
                                      **{group_slug_field: group_slug})
            article_args.update({
                'content_type': get_ct(group),
                'object_id': group.id
            })
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            allow_read = allow_write = True

        if not allow_read:
            return HttpResponseForbidden()

        is_observing = False
        redirected_from = None
        try:
            article = article_qs.get(**article_args)
            if notification is not None:
                is_observing = notification.is_observing(article, request.user)
        except ArticleClass.DoesNotExist:
            try:
                # try to find an article that once had this title
                article = ChangeSet.objects.filter(
                    old_title=title).order_by('-revision')[0].article
                redirected_from = title
                # if article is not None:
                #    return redirect(article, permanent=True)
            except IndexError:
                article = ArticleClass(**article_args)

        if revision is not None:
            changeset = get_object_or_404(article.changeset_set,
                                          revision=revision)
            article.content = changeset.get_content()

        template_params = {
            'article': article,
            'revision': revision,
            'redirected_from': redirected_from,
            'allow_write': allow_write
        }

        if notification is not None:
            template_params.update({
                'is_observing': is_observing,
                'can_observe': True
            })

        if group_slug is not None:
            template_params['group'] = group
        if extra_context is not None:
            template_params.update(extra_context)

        return render(
            request,
            '/'.join([template_dir, template_name]),
            template_params,
        )
    return HttpResponseNotAllowed(['GET'])
Beispiel #35
0
def edit_article(
        request,
        title,
        group_slug=None,
        group_slug_field=None,
        group_qs=None,
        article_qs=ALL_ARTICLES,
        ArticleClass=Article,  # to get the DoesNotExist exception
        ArticleFormClass=ArticleForm,
        template_name='edit.html',
        template_dir='wiki',
        extra_context=None,
        check_membership=False,
        is_member=None,
        is_private=None,
        *args,
        **kw):

    group = None
    article_args = {'title': title}
    if group_slug is not None:
        group = get_object_or_404(group_qs, **{group_slug_field: group_slug})
        group_ct = get_ct(group)
        article_args.update({'content_type': group_ct, 'object_id': group.id})
        allow_read = has_read_perm(request.user, group, is_member, is_private)
        allow_write = has_write_perm(request.user, group, is_member)
    else:
        allow_read = allow_write = True

    if not allow_write:
        return HttpResponseForbidden()

    try:
        # Try to fetch an existing article
        article = article_qs.get(**article_args)
    except ArticleClass.DoesNotExist:
        # No article found, maybe we have a redirect
        try:
            cs = ChangeSet.objects.filter(old_title=title)[0]
            article = article_qs.get(title=cs.article)
        except IndexError:
            # No Article found and no redirect found
            article = None

    if request.method == 'POST':

        form = ArticleFormClass(request.POST, instance=article)

        form.cache_old_content()
        if form.is_valid():

            if request.user.is_authenticated:
                form.editor = request.user

            if ((article is None) and (group_slug is not None)):
                form.group = group

            new_article, changeset = form.save()

            lock = cache.get(get_valid_cache_key(title))
            if lock is not None:
                # Clean the lock
                cache.delete(get_valid_cache_key(title))

            if notification and not changeset.reverted:
                # Get observers for this article and exclude current editor
                items = notification.ObservedItem.objects.all_for(
                    new_article,
                    'post_save').exclude(user=request.user).iterator()
                users = [o.user for o in items]
                notification.send(
                    users, 'wiki_observed_article_changed', {
                        'editor': request.user,
                        'rev': changeset.revision,
                        'rev_comment': changeset.comment,
                        'article': new_article
                    })

            return redirect(new_article)

    elif request.method == 'GET':
        lock = cache.get(get_valid_cache_key(title))
        if lock is None:
            lock = ArticleEditLock(get_valid_cache_key(title), request)
        lock.create_message(request)
        initial = {}
        if group_slug is not None:
            initial.update({
                'content_type': group_ct.id,
                'object_id': group.id
            })

        if article is None:
            initial.update({'title': title, 'action': 'create'})
            form = ArticleFormClass(initial=initial)
        else:
            initial['action'] = 'edit'
            form = ArticleFormClass(instance=article, initial=initial)
    if not article:
        template_params = {'form': form, 'new_article': True}
    else:
        template_params = {
            'form': form,
            'new_article': False,
            'content_type': ContentType.objects.get_for_model(Article).pk,
            'object_id': article.pk,
            'images': article.all_images(),
            'article': article,
        }

    if group_slug is not None:
        template_params['group'] = group
    if extra_context is not None:
        template_params.update(extra_context)

    return render(
        request,
        '/'.join([template_dir, template_name]),
        template_params,
    )
Beispiel #36
0
def view_changeset(request,
                   title,
                   revision,
                   group_slug=None,
                   group_slug_field=None,
                   group_qs=None,
                   article_qs=ALL_ARTICLES,
                   changes_qs=ALL_CHANGES,
                   template_name='changeset.html',
                   template_dir='wiki',
                   extra_context=None,
                   is_member=None,
                   is_private=None,
                   *args,
                   **kw):

    if request.method == "GET":
        article_args = {'article__title': title}
        if group_slug is not None:
            group = get_object_or_404(group_qs,
                                      **{group_slug_field: group_slug})
            article_args.update({
                'article__content_type': get_ct(group),
                'article__object_id': group.id
            })
        changeset = get_object_or_404(changes_qs,
                                      revision=int(revision),
                                      **article_args)

        article_args = {'title': title}
        if group_slug is not None:
            group = get_object_or_404(group_qs,
                                      **{group_slug_field: group_slug})
            article_args.update({
                'content_type': get_ct(group),
                'object_id': group.id
            })
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            allow_read = allow_write = True

        if not allow_read:
            return HttpResponseForbidden()

        article = article_qs.get(**article_args)

        template_params = {
            'article': article,
            'article_title': article.title,
            'changeset': changeset,
            'allow_write': allow_write
        }

        #####
        if group_slug is not None:
            #            template_params['group'] = group
            new_article = ArticleClass(title="NewArticle",
                                       content_type=get_ct(group),
                                       object_id=group.id)
        else:
            new_article = ArticleClass(title="NewArticle")
        template_params['new_article'] = new_article
        #####
        #        template_params = {'new_article': new_article,

        if group_slug is not None:
            template_params['group'] = group
        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response('/'.join([template_dir, template_name]),
                                  template_params,
                                  context_instance=RequestContext(request))
    return HttpResponseNotAllowed(['GET'])
Beispiel #37
0
def edit_article(
        request,
        title,
        group_slug=None,
        group_slug_field=None,
        group_qs=None,
        article_qs=ALL_ARTICLES,
        ArticleClass=Article,  # to get the DoesNotExist exception
        ArticleFormClass=ArticleForm,
        template_name='edit.html',
        template_dir='wiki',
        extra_context=None,
        check_membership=False,
        is_member=None,
        is_private=None,
        *args,
        **kw):

    group = None
    article_args = {'title': title}
    if group_slug is not None:
        group = get_object_or_404(group_qs, **{group_slug_field: group_slug})
        group_ct = get_ct(group)
        article_args.update({'content_type': group_ct, 'object_id': group.id})
        allow_read = has_read_perm(request.user, group, is_member, is_private)
        allow_write = has_write_perm(request.user, group, is_member)
    else:
        allow_read = allow_write = True

    if not allow_write:
        return HttpResponseForbidden()

    try:
        article = article_qs.get(**article_args)
    except ArticleClass.DoesNotExist:
        article = None

    if request.method == 'POST':

        form = ArticleFormClass(request.POST, instance=article)

        if form.is_valid():

            if request.user.is_authenticated():
                form.editor = request.user
                if article is None:
                    user_message = u"Your article was created successfully."
                else:
                    user_message = u"Your article was edited successfully."
                request.user.message_set.create(message=user_message)

            if ((article is None) and (group_slug is not None)):
                form.group = group

            new_article, changeset = form.save()

            url = get_url('wiki_article', group, [new_article.title], {
                'title': new_article.title,
                'group_slug': group_slug
            })

            return redirect_to(request, url)

    elif request.method == 'GET':
        user_ip = get_real_ip(request)

        lock = cache.get(title, None)
        if lock is None:
            lock = ArticleEditLock(title, request)
        lock.create_message(request)

        initial = {'user_ip': user_ip}
        if group_slug is not None:
            initial.update({
                'content_type': group_ct.id,
                'object_id': group.id
            })

        if article is None:
            initial.update({'title': title, 'action': 'create'})
            form = ArticleFormClass(initial=initial)
        else:
            initial['action'] = 'edit'
            form = ArticleFormClass(instance=article, initial=initial)

    template_params = {'form': form, 'article': article}
    #####
    if group_slug is not None:
        #            template_params['group'] = group
        new_article = ArticleClass(title="NewArticle",
                                   content_type=get_ct(group),
                                   object_id=group.id)
    else:
        new_article = ArticleClass(title="NewArticle")
    template_params['new_article'] = new_article
    #####
    #        template_params = {'new_article': new_article,
    if group_slug is not None:
        template_params['group'] = group
    if extra_context is not None:
        template_params.update(extra_context)

    return render_to_response('/'.join([template_dir, template_name]),
                              template_params,
                              context_instance=RequestContext(request))
Beispiel #38
0
def view_article(
        request,
        title,
        ArticleClass=Article,  # to create an unsaved instance
        group_slug=None,
        group_slug_field=None,
        group_qs=None,
        article_qs=ALL_ARTICLES,
        template_name='view.html',
        template_dir='wiki',
        extra_context=None,
        is_member=None,
        is_private=None,
        *args,
        **kw):

    if request.method == 'GET':
        article_args = {'title': title}
        if group_slug is not None:
            group = get_object_or_404(group_qs,
                                      **{group_slug_field: group_slug})
            article_args.update({
                'content_type': get_ct(group),
                'object_id': group.id
            })
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            allow_read = allow_write = True

        if not allow_read:
            return HttpResponseForbidden()

        try:
            article = article_qs.get(**article_args)
            if notification is not None:
                is_observing = notification.is_observing(article, request.user)
            else:
                is_observing = False
        except ArticleClass.DoesNotExist:
            article = ArticleClass(**article_args)
            is_observing = False

#####
        if group_slug is not None:
            #            template_params['group'] = group
            new_article = ArticleClass(title="NewArticle",
                                       content_type=get_ct(group),
                                       object_id=group.id)
        else:
            new_article = ArticleClass(title="NewArticle")


#        template_params['new_article'] = new_article
#####
        template_params = {
            'new_article': new_article,
            'article': article,
            'allow_write': allow_write
        }

        if notification is not None:
            template_params.update({
                'is_observing': is_observing,
                'can_observe': True
            })

        if group_slug is not None:
            template_params['group'] = group
        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response('/'.join([template_dir, template_name]),
                                  template_params,
                                  context_instance=RequestContext(request))
    return HttpResponseNotAllowed(['GET'])
Beispiel #39
0
def view_changeset(request,
                   title,
                   revision,
                   revision_from=None,
                   group_slug=None,
                   group_slug_field=None,
                   group_qs=None,
                   article_qs=ALL_ARTICLES,
                   changes_qs=ALL_CHANGES,
                   template_name='changeset.html',
                   template_dir='wiki',
                   extra_context=None,
                   is_member=None,
                   is_private=None,
                   *args,
                   **kw):

    if request.method == 'GET':
        article_args = {'article__title': title}
        if group_slug is not None:
            group = get_object_or_404(group_qs,
                                      **{group_slug_field: group_slug})
            article_args.update({
                'article__content_type': get_ct(group),
                'article__object_id': group.id
            })
        changeset = get_object_or_404(changes_qs,
                                      revision=int(revision),
                                      **article_args)

        article_args = {'title': title}
        if group_slug is not None:
            group = get_object_or_404(group_qs,
                                      **{group_slug_field: group_slug})
            article_args.update({
                'content_type': get_ct(group),
                'object_id': group.id
            })
            allow_read = has_read_perm(request.user, group, is_member,
                                       is_private)
            allow_write = has_write_perm(request.user, group, is_member)
        else:
            allow_read = allow_write = True

        if not allow_read:
            return HttpResponseForbidden()

        article = article_qs.get(**article_args)

        if revision_from is None:
            revision_from = int(revision) - 1

        from_value = None
        if int(revision) is not int(revision_from) + 1:
            from_value = revision_from

        template_params = {
            'article': article,
            'article_title': article.title,
            'changeset': changeset,
            'differences': changeset.compare_to(revision_from),
            'from': from_value,
            'to': revision,
            'allow_write': allow_write
        }

        if group_slug is not None:
            template_params['group'] = group
        if extra_context is not None:
            template_params.update(extra_context)

        return render(
            request,
            '/'.join([template_dir, template_name]),
            template_params,
        )
    return HttpResponseNotAllowed(['GET'])
Beispiel #40
0
def edit_article(
        request,
        title,
        group_slug=None,
        bridge=None,
        article_qs=ALL_ARTICLES,
        ArticleClass=Article,  # to get the DoesNotExist exception
        ArticleFormClass=ArticleForm,
        template_name='edit.html',
        template_dir='wiki',
        extra_context=None,
        check_membership=False,
        is_member=None,
        is_private=None,
        *args,
        **kw):

    if group_slug is not None:
        try:
            group = bridge.get_group(group_slug)
        except ObjectDoesNotExist:
            raise Http404
        allow_read = has_read_perm(request.user, group, is_member, is_private)
        allow_write = has_write_perm(request.user, group, is_member)
    else:
        group = None
        allow_read = allow_write = True

    if not allow_write:
        return HttpResponseForbidden()

    try:
        article = article_qs.get_by(title, group)
    except ArticleClass.DoesNotExist:
        article = None

    if request.method == 'POST':

        form = ArticleFormClass(request.POST, instance=article)

        if form.is_valid():

            if request.user.is_authenticated():
                form.editor = request.user
                if article is None:
                    user_message = u"Your article was created successfully."
                else:
                    user_message = u"Your article was edited successfully."
                request.user.message_set.create(message=user_message)

            if ((article is None) and (group_slug is not None)):
                form.group = group

            new_article, changeset = form.save()

            url = get_url('wiki_article',
                          group,
                          kw={
                              'title': new_article.title,
                          },
                          bridge=bridge)

            return redirect_to(request, url)

    elif request.method == 'GET':
        user_ip = get_real_ip(request)

        lock = cache.get(title, None)
        if lock is None:
            lock = ArticleEditLock(title, request)
        lock.create_message(request)

        initial = {'user_ip': user_ip}
        if group_slug is not None:
            # @@@ wikiapp currently handles the group filtering, but we will
            # eventually want to handle that via the bridge.
            initial.update({
                'content_type': get_ct(group).id,
                'object_id': group.id
            })

        if article is None:
            initial.update({'title': title, 'action': 'create'})
            form = ArticleFormClass(initial=initial)
        else:
            initial['action'] = 'edit'
            form = ArticleFormClass(instance=article, initial=initial)

    template_params = {'form': form}

    template_params['group'] = group
    if extra_context is not None:
        template_params.update(extra_context)

    return render_to_response(os.path.join(template_dir, template_name),
                              template_params,
                              context_instance=RequestContext(request))
Beispiel #41
0
def edit_article(request, title,
                 group_slug=None, bridge=None,
                 article_qs=ALL_WHITEBOARDS,
                 ArticleClass=Whiteboard, # to get the DoesNotExist exception
                 ArticleFormClass=WhiteboardForm,
                 template_name='edit_whiteboard.html',
                 template_dir='whiteboard',
                 extra_context=None,
                 check_membership=False,
                 is_member=None,
                 is_private=None,
                 *args, **kw):
    
    if group_slug is not None:
        try:
            group = bridge.get_group(group_slug)
        except ObjectDoesNotExist:
            raise Http404
        
        # permissions check
        if not group.user_is_member(request.user, admin_override=True):
            return HttpResponseForbidden()
        
        try:
            article = article_qs.get_by(title, group)
        except ArticleClass.DoesNotExist:
            article = None

        attach_forms = []
        if request.method == 'POST':
            try:
                attach_count = int(request.POST.get("attach_count", 0))
            except ValueError:
                attach_count = 0

            form = ArticleFormClass(request.POST, instance=article)
            attach_forms = [AttachmentForm(request.POST, request.FILES, prefix=str(x), instance=Attachment()) for x in range(0,attach_count)]
            
            # do not take blank attachment forms into account
            for af in attach_forms:
                if not af.is_valid() and not af['attachment_file'].data:
                    attach_forms.remove(af)
                    attach_count = attach_count - 1

            if form.is_valid() and all([af.is_valid() for af in attach_forms]):

                if request.user.is_authenticated():
                    form.editor = request.user
                    if article is None:
                        user_message = u"Whiteboard was created successfully."
                    else:
                        user_message = u"Whiteboard was edited successfully."
                    request.user.message_set.create(message=user_message)

                if ((article is None) and (group_slug is not None)):
                    form.group = group

                new_article, changeset = form.save()

                for af in attach_forms:
                    attachment = af.save(request, new_article)
            
                # FIXME: is there a more efficient way of finding the parent
                # than running these count() queries? 
                if new_article.topic.count():
                    url = new_article.topic.all()[0].get_absolute_url()
                elif new_article.event.count():
                    url = new_article.event.all()[0].get_absolute_url()
                else:
                    url = group.get_absolute_url() + "#group-whiteboard"
                    
                return redirect_to(request, url)

        elif request.method == 'GET':
            user_ip = get_real_ip(request)
    
            lock = cache.get(title, None)
            if lock is None:
                lock = ArticleEditLock(title, request)
            lock.create_message(request)
    
            initial = {'user_ip': user_ip}
            if group_slug is not None:
                # @@@ wikiapp currently handles the group filtering, but we will
                # eventually want to handle that via the bridge.
                initial.update({'content_type': get_ct(group).id,
                                'object_id': group.id})
    
            if article is None:
                initial.update({'title': title,
                                'action': 'create'})
                form = ArticleFormClass(initial=initial)
            else:
                initial['action'] = 'edit'
                form = ArticleFormClass(instance=article,
                                        initial=initial)
                
    
        template_params = {'form': form}
        template_params['attach_forms'] = attach_forms
    
        template_params['group'] = group
        if extra_context is not None:
            template_params.update(extra_context)
    
        return render_to_response(os.path.join(template_dir, template_name),
                                  template_params,
                                  context_instance=RequestContext(request))
Beispiel #42
0
def edit_article(request, title,
                 group_slug=None, group_slug_field=None, group_qs=None,
                 article_qs=ALL_ARTICLES,
                 ArticleClass=Article, # to get the DoesNotExist exception
                 ArticleFormClass=ArticleForm,
                 template_name='edit.html',
                 template_dir='wiki',
                 extra_context=None,
                 check_membership=False,
                 is_member=None,
                 is_private=None,
                 *args, **kw):

    group = None
    article_args = {'title': title}
    if group_slug is not None:
        group = get_object_or_404(group_qs,**{group_slug_field: group_slug})
        group_ct = get_ct(group)
        article_args.update({'content_type': group_ct,
                             'object_id': group.id})
        allow_read = has_read_perm(request.user, group, is_member,
                                   is_private)
        allow_write = has_write_perm(request.user, group, is_member)
    else:
        allow_read = allow_write = True

    if not allow_write:
        return HttpResponseForbidden()

    try:
        article = article_qs.get(**article_args)
    except ArticleClass.DoesNotExist:
        article = None

    if request.method == 'POST':

        form = ArticleFormClass(request.POST, instance=article)

        if form.is_valid():

            if request.user.is_authenticated():
                form.editor = request.user
                if article is None:
                    user_message = u"Your article was created successfully."
                else:
                    user_message = u"Your article was edited successfully."
                request.user.message_set.create(message=user_message)

            if ((article is None) and (group_slug is not None)):
                form.group = group

            new_article, changeset = form.save()

            url = get_url('wiki_article', group,
                          [new_article.title],
                          {'title': new_article.title,
                           'group_slug': group_slug})

            return redirect_to(request, url)

    elif request.method == 'GET':
        user_ip = get_real_ip(request)

        lock = cache.get(title, None)
        if lock is None:
            lock = ArticleEditLock(title, request)
        lock.create_message(request)

        initial = {'user_ip': user_ip}
        if group_slug is not None:
            initial.update({'content_type': group_ct.id,
                            'object_id': group.id})

        if article is None:
            initial.update({'title': title,
                            'action': 'create'})
            form = ArticleFormClass(initial=initial)
        else:
            initial['action'] = 'edit'
            form = ArticleFormClass(instance=article,
                                    initial=initial)

    template_params = {'form': form}

    if group_slug is not None:
        template_params['group'] = group
    if extra_context is not None:
        template_params.update(extra_context)

    return render_to_response('/'.join([template_dir, template_name]),
                              template_params,
                              context_instance=RequestContext(request))
Beispiel #43
0
def get_articles_for_object(object, article_qs=None):
    if article_qs is None:
        article_qs = ALL_ARTICLES
    return article_qs.filter( content_type=get_ct(object),
                                       object_id=object.id)
Beispiel #44
0
def edit_article(
    request,
    title,
    group_slug=None,
    bridge=None,
    article_qs=ALL_WHITEBOARDS,
    ArticleClass=Whiteboard,  # to get the DoesNotExist exception
    ArticleFormClass=WhiteboardForm,
    template_name="edit_whiteboard.html",
    template_dir="whiteboard",
    extra_context=None,
    check_membership=False,
    is_member=None,
    is_private=None,
    *args,
    **kw
):

    if group_slug is not None:
        try:
            group = bridge.get_group(group_slug)
        except ObjectDoesNotExist:
            raise Http404

        # permissions check
        if not group.user_is_member(request.user, admin_override=True):
            return HttpResponseForbidden()

        try:
            article = article_qs.get_by(title, group)
        except ArticleClass.DoesNotExist:
            article = None

        attach_forms = []
        if request.method == "POST":
            try:
                attach_count = int(request.POST.get("attach_count", 0))
            except ValueError:
                attach_count = 0

            form = ArticleFormClass(request.POST, instance=article)
            attach_forms = [
                AttachmentForm(request.POST, request.FILES, prefix=str(x), instance=Attachment())
                for x in range(0, attach_count)
            ]

            # do not take blank attachment forms into account
            for af in attach_forms:
                if not af.is_valid() and not af["attachment_file"].data:
                    attach_forms.remove(af)
                    attach_count = attach_count - 1

            if form.is_valid() and all([af.is_valid() for af in attach_forms]):

                if request.user.is_authenticated():
                    form.editor = request.user
                    if article is None:
                        user_message = u"Whiteboard was created successfully."
                    else:
                        user_message = u"Whiteboard was edited successfully."
                    request.user.message_set.create(message=user_message)

                if (article is None) and (group_slug is not None):
                    form.group = group

                new_article, changeset = form.save()

                for af in attach_forms:
                    attachment = af.save(request, new_article)

                # FIXME: is there a more efficient way of finding the parent
                # than running these count() queries?
                if new_article.topic.count():
                    url = new_article.topic.all()[0].get_absolute_url()
                elif new_article.event.count():
                    url = new_article.event.all()[0].get_absolute_url()
                else:
                    url = group.get_absolute_url() + "#group-whiteboard"

                return redirect_to(request, url)

        elif request.method == "GET":
            user_ip = get_real_ip(request)

            lock = cache.get(title, None)
            if lock is None:
                lock = ArticleEditLock(title, request)
            lock.create_message(request)

            initial = {"user_ip": user_ip}
            if group_slug is not None:
                # @@@ wikiapp currently handles the group filtering, but we will
                # eventually want to handle that via the bridge.
                initial.update({"content_type": get_ct(group).id, "object_id": group.id})

            if article is None:
                initial.update({"title": title, "action": "create"})
                form = ArticleFormClass(initial=initial)
            else:
                initial["action"] = "edit"
                form = ArticleFormClass(instance=article, initial=initial)

        template_params = {"form": form}
        template_params["attach_forms"] = attach_forms

        template_params["group"] = group
        if extra_context is not None:
            template_params.update(extra_context)

        return render_to_response(
            os.path.join(template_dir, template_name), template_params, context_instance=RequestContext(request)
        )
Beispiel #45
0
def edit_article(request, title,
                 group_slug=None, bridge=None,
                 article_qs=ALL_ARTICLES,
                 ArticleClass=Article, # to get the DoesNotExist exception
                 ArticleFormClass=ArticleForm,
                 template_name='edit.html',
                 template_dir='wiki',
                 extra_context=None,
                 check_membership=False,
                 is_member=None,
                 is_private=None,
                 *args, **kw):

    if group_slug is not None:
        try:
            group = bridge.get_group(group_slug)
        except ObjectDoesNotExist:
            raise Http404
        allow_read = has_read_perm(request.user, group, is_member,
                                   is_private)
        allow_write = has_write_perm(request.user, group, is_member)
    else:
        group = None
        allow_read = allow_write = True


    if not allow_write:
        return HttpResponseForbidden()

    try:
        article = article_qs.get_by(title, group)
    except ArticleClass.DoesNotExist:
        article = None

    if request.method == 'POST':

        form = ArticleFormClass(request.POST, instance=article)

        if form.is_valid():

            if request.user.is_authenticated():
                form.editor = request.user
                if article is None:
                    user_message = u"Your article was created successfully."
                else:
                    user_message = u"Your article was edited successfully."
                request.user.message_set.create(message=user_message)

            if ((article is None) and (group_slug is not None)):
                form.group = group

            new_article, changeset = form.save()
            
            url = get_url('wiki_article', group, kw={
                'title': new_article.title,
            }, bridge=bridge)
            
            return redirect_to(request, url)

    elif request.method == 'GET':
        user_ip = get_real_ip(request)

        lock = cache.get(title, None)
        if lock is None:
            lock = ArticleEditLock(title, request)
        lock.create_message(request)

        initial = {'user_ip': user_ip}
        if group_slug is not None:
            # @@@ wikiapp currently handles the group filtering, but we will
            # eventually want to handle that via the bridge.
            initial.update({'content_type': get_ct(group).id,
                            'object_id': group.id})

        if article is None:
            initial.update({'title': title,
                            'action': 'create'})
            form = ArticleFormClass(initial=initial)
        else:
            initial['action'] = 'edit'
            form = ArticleFormClass(instance=article,
                                    initial=initial)

    template_params = {'form': form}

    template_params['group'] = group
    if extra_context is not None:
        template_params.update(extra_context)

    return render_to_response(os.path.join(template_dir, template_name),
                              template_params,
                              context_instance=RequestContext(request))