Example #1
0
def search_add_related(request, course_id, slug, namespace):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    (article, err) = get_article(request, slug,
                                 namespace if namespace else course_id)
    if err:
        return err

    perm_err = check_permissions(request, article, course, check_read=True)
    if perm_err:
        return perm_err

    search_string = request.GET.get('query', None)
    self_pk = request.GET.get('self', None)
    if search_string:
        results = []
        related = Article.objects.filter(title__istartswith=search_string)
        others = article.related.all()
        if self_pk:
            related = related.exclude(pk=self_pk)
        if others:
            related = related.exclude(related__in=others)
        related = related.order_by('title')[:10]
        for item in related:
            results.append({
                'id': str(item.id),
                'value': item.title,
                'info': item.get_url()
            })
    else:
        results = []

    json = simplejson.dumps({'results': results})
    return HttpResponse(json, mimetype='application/json')
Example #2
0
def random_article(request, course_id=None):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    from random import randint
    num_arts = Article.objects.count()
    article = Article.objects.all()[randint(0, num_arts - 1)]
    return HttpResponseRedirect(wiki_reverse('wiki_view', article, course))
Example #3
0
def random_article(request, course_id=None):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    from random import randint
    num_arts = Article.objects.count()
    article = Article.objects.all()[randint(0, num_arts - 1)]
    return HttpResponseRedirect(wiki_reverse('wiki_view', article, course))
Example #4
0
def view_revision(request, revision_number, article_path, course_id=None):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    (article, err) = get_article(request, article_path, course)
    if err:
        return err

    try:
        revision = Revision.objects.get(counter=int(revision_number),
                                        article=article)
    except:
        d = {'wiki_err_norevision': revision_number}
        update_template_dictionary(d, request, course, article)
        return render_to_response('simplewiki/simplewiki_error.html', d)

    perm_err = check_permissions(request,
                                 article,
                                 course,
                                 check_read=True,
                                 check_deleted=True,
                                 revision=revision)
    if perm_err:
        return perm_err

    d = {}
    update_template_dictionary(d, request, course, article, revision)

    return render_to_response('simplewiki/simplewiki_view.html', d)
Example #5
0
def search_add_related(request, course_id, slug, namespace):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    (article, err) = get_article(request, slug,
                                 namespace if namespace else course_id)
    if err:
        return err

    perm_err = check_permissions(request, article, course, check_read=True)
    if perm_err:
        return perm_err

    search_string = request.GET.get('query', None)
    self_pk = request.GET.get('self', None)
    if search_string:
        results = []
        related = Article.objects.filter(title__istartswith=search_string)
        others = article.related.all()
        if self_pk:
            related = related.exclude(pk=self_pk)
        if others:
            related = related.exclude(related__in=others)
        related = related.order_by('title')[:10]
        for item in related:
            results.append({'id': str(item.id),
                            'value': item.title,
                            'info': item.get_url()})
    else:
        results = []

    json = simplejson.dumps({'results': results})
    return HttpResponse(json, mimetype='application/json')
Example #6
0
def remove_related(request, course_id, namespace, slug, related_id):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    (article, err) = get_article(request, slug,
                                 namespace if namespace else course_id)

    if err:
        return err

    perm_err = check_permissions(request,
                                 article,
                                 course,
                                 check_write=True,
                                 check_locked=True)
    if perm_err:
        return perm_err

    try:
        rel_id = int(related_id)
        rel = Article.objects.get(id=rel_id)
        article.related.remove(rel)
        article.save()
    except:
        pass
    finally:
        return HttpResponseRedirect(
            reverse('wiki_view', args=(article.get_url(), )))
Example #7
0
def revision_feed(request, page=1, namespace=None, course_id=None):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    page_size = 10

    if page is None:
        page = 1
    try:
        p = int(page)
    except ValueError:
        p = 1

    history = Revision.objects.order_by('-revision_date').select_related(
        'revision_user', 'article', 'previous_revision')

    page_count = (history.count() + (page_size - 1)) / page_size
    if p > page_count:
        p = 1
    beginItem = (p - 1) * page_size

    next_page = p + 1 if page_count > p else None
    prev_page = p - 1 if p > 1 else None

    d = {'wiki_page': p,
         'wiki_next_page': next_page,
         'wiki_prev_page': prev_page,
         'wiki_history': history[beginItem:beginItem + page_size],
         'show_delete_revision': request.user.is_superuser,
         'namespace': namespace}
    update_template_dictionary(d, request, course)

    return render_to_response('simplewiki/simplewiki_revision_feed.html', d)
Example #8
0
def revision_feed(request, page=1, namespace=None, course_id=None):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    page_size = 10

    if page is None:
        page = 1
    try:
        p = int(page)
    except ValueError:
        p = 1

    history = Revision.objects.order_by('-revision_date').select_related(
        'revision_user', 'article', 'previous_revision')

    page_count = (history.count() + (page_size - 1)) / page_size
    if p > page_count:
        p = 1
    beginItem = (p - 1) * page_size

    next_page = p + 1 if page_count > p else None
    prev_page = p - 1 if p > 1 else None

    d = {
        'wiki_page': p,
        'wiki_next_page': next_page,
        'wiki_prev_page': prev_page,
        'wiki_history': history[beginItem:beginItem + page_size],
        'show_delete_revision': request.user.is_superuser,
        'namespace': namespace
    }
    update_template_dictionary(d, request, course)

    return render_to_response('simplewiki/simplewiki_revision_feed.html', d)
Example #9
0
def add_related(request, course_id, slug, namespace):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    (article, err) = get_article(request, slug,
                                 namespace if namespace else course_id)
    if err:
        return err

    perm_err = check_permissions(request,
                                 article,
                                 course,
                                 check_write=True,
                                 check_locked=True)
    if perm_err:
        return perm_err

    try:
        related_id = request.POST['id']
        rel = Article.objects.get(id=related_id)
        has_already = article.related.filter(id=related_id).count()
        if has_already == 0 and not rel == article:
            article.related.add(rel)
            article.save()
    except:
        pass
    finally:
        return HttpResponseRedirect(
            reverse('wiki_view', args=(article.get_url(), )))
Example #10
0
def search_articles(request, namespace=None, course_id=None):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    # blampe: We should check for the presence of other popular django search
    # apps and use those if possible. Only fall back on this as a last resort.
    # Adding some context to results (eg where matches were) would also be
    # nice.

    # todo: maybe do some perm checking here

    if request.method == 'GET':
        querystring = request.GET.get('value', '').strip()
    else:
        querystring = ""

    results = Article.objects.all()
    if namespace:
        results = results.filter(namespace__name__exact=namespace)

    if request.user.is_superuser:
        results = results.order_by('current_revision__deleted')
    else:
        results = results.filter(current_revision__deleted=0)

    if querystring:
        for queryword in querystring.split():
            # Basic negation is as fancy as we get right now
            if queryword[0] == '-' and len(queryword) > 1:
                results._search = lambda x: results.exclude(x)
                queryword = queryword[1:]
            else:
                results._search = lambda x: results.filter(x)

            results = results._search(
                Q(current_revision__contents__icontains=queryword)
                | Q(title__icontains=queryword))

    results = results.select_related('current_revision__deleted', 'namespace')

    results = sorted(
        results,
        key=lambda article:
        (article.current_revision.deleted, article.get_path().lower()))

    if len(results) == 1 and querystring:
        return HttpResponseRedirect(
            wiki_reverse('wiki_view', article=results[0], course=course))
    else:
        d = {
            'wiki_search_results': results,
            'wiki_search_query': querystring,
            'namespace': namespace
        }
        update_template_dictionary(d, request, course)
        return render_to_response('simplewiki/simplewiki_searchresults.html',
                                  d)
Example #11
0
def edit(request, article_path, course_id=None):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    (article, err) = get_article(request, article_path, course)
    if err:
        return err

    # Check write permissions
    perm_err = check_permissions(request,
                                 article,
                                 course,
                                 check_write=True,
                                 check_locked=True,
                                 check_deleted=False)
    if perm_err:
        return perm_err

    if wiki_settings.WIKI_ALLOW_TITLE_EDIT:
        EditForm = RevisionFormWithTitle
    else:
        EditForm = RevisionForm

    if request.method == 'POST':
        f = EditForm(request.POST)
        if f.is_valid():
            new_revision = f.save(commit=False)
            new_revision.article = article

            if request.POST.__contains__('delete'):
                if (article.current_revision.deleted == 1
                    ):  # This article has already been deleted. Redirect
                    return HttpResponseRedirect(
                        wiki_reverse('wiki_view', article, course))
                new_revision.contents = ""
                new_revision.deleted = 1
            elif not new_revision.get_diff():
                return HttpResponseRedirect(
                    wiki_reverse('wiki_view', article, course))

            if not request.user.is_anonymous():
                new_revision.revision_user = request.user
            new_revision.save()
            if wiki_settings.WIKI_ALLOW_TITLE_EDIT:
                new_revision.article.title = f.cleaned_data['title']
                new_revision.article.save()
            return HttpResponseRedirect(
                wiki_reverse('wiki_view', article, course))
    else:
        startContents = article.current_revision.contents if (
            article.current_revision.deleted == 0) else 'Headline\n===\n\n'

        f = EditForm({'contents': startContents, 'title': article.title})

    d = {'wiki_form': f}
    update_template_dictionary(d, request, course, article)
    return render_to_response('simplewiki/simplewiki_edit.html', d)
Example #12
0
def view(request, article_path, course_id=None):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    (article, err) = get_article(request, article_path, course)
    if err:
        return err

    perm_err = check_permissions(request, article, course, check_read=True, check_deleted=True)
    if perm_err:
        return perm_err

    d = {}
    update_template_dictionary(d, request, course, article, article.current_revision)
    return render_to_response('simplewiki/simplewiki_view.html', d)
Example #13
0
def search_articles(request, namespace=None, course_id=None):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    # blampe: We should check for the presence of other popular django search
    # apps and use those if possible. Only fall back on this as a last resort.
    # Adding some context to results (eg where matches were) would also be
    # nice.

    # todo: maybe do some perm checking here

    if request.method == 'GET':
        querystring = request.GET.get('value', '').strip()
    else:
        querystring = ""

    results = Article.objects.all()
    if namespace:
        results = results.filter(namespace__name__exact=namespace)

    if request.user.is_superuser:
        results = results.order_by('current_revision__deleted')
    else:
        results = results.filter(current_revision__deleted=0)

    if querystring:
        for queryword in querystring.split():
            # Basic negation is as fancy as we get right now
            if queryword[0] == '-' and len(queryword) > 1:
                results._search = lambda x: results.exclude(x)
                queryword = queryword[1:]
            else:
                results._search = lambda x: results.filter(x)

            results = results._search(Q(current_revision__contents__icontains=queryword) |
                                      Q(title__icontains=queryword))

    results = results.select_related('current_revision__deleted', 'namespace')

    results = sorted(results, key=lambda article: (
        article.current_revision.deleted, article.get_path().lower()))

    if len(results) == 1 and querystring:
        return HttpResponseRedirect(wiki_reverse('wiki_view', article=results[0], course=course))
    else:
        d = {'wiki_search_results': results,
             'wiki_search_query': querystring,
             'namespace': namespace}
        update_template_dictionary(d, request, course)
        return render_to_response('simplewiki/simplewiki_searchresults.html', d)
Example #14
0
def edit(request, article_path, course_id=None):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    (article, err) = get_article(request, article_path, course)
    if err:
        return err

    # Check write permissions
    perm_err = check_permissions(
        request, article, course, check_write=True, check_locked=True, check_deleted=False)
    if perm_err:
        return perm_err

    if wiki_settings.WIKI_ALLOW_TITLE_EDIT:
        EditForm = RevisionFormWithTitle
    else:
        EditForm = RevisionForm

    if request.method == 'POST':
        f = EditForm(request.POST)
        if f.is_valid():
            new_revision = f.save(commit=False)
            new_revision.article = article

            if request.POST.__contains__('delete'):
                if (article.current_revision.deleted == 1):  # This article has already been deleted. Redirect
                    return HttpResponseRedirect(wiki_reverse('wiki_view', article, course))
                new_revision.contents = ""
                new_revision.deleted = 1
            elif not new_revision.get_diff():
                return HttpResponseRedirect(wiki_reverse('wiki_view', article, course))

            if not request.user.is_anonymous():
                new_revision.revision_user = request.user
            new_revision.save()
            if wiki_settings.WIKI_ALLOW_TITLE_EDIT:
                new_revision.article.title = f.cleaned_data['title']
                new_revision.article.save()
            return HttpResponseRedirect(wiki_reverse('wiki_view', article, course))
    else:
        startContents = article.current_revision.contents if (
            article.current_revision.deleted == 0) else 'Headline\n===\n\n'

        f = EditForm({'contents': startContents, 'title': article.title})

    d = {'wiki_form': f}
    update_template_dictionary(d, request, course, article)
    return render_to_response('simplewiki/simplewiki_edit.html', d)
Example #15
0
def root_redirect(request, course_id=None):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    #TODO: Add a default namespace to settings.
    namespace = "edX"

    try:
        root = Article.get_root(namespace)
        return HttpResponseRedirect(reverse('wiki_view', kwargs={'course_id': course_id, 'article_path': root.get_path()}))
    except:
        # If the root is not found, we probably are loading this class for the first time
        # We should make sure the namespace exists so the root article can be created.
        Namespace.ensure_namespace(namespace)

        err = not_found(request, namespace + '/', course)
        return err
Example #16
0
def create(request, article_path, course_id=None):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    article_path_components = article_path.split('/')

    # Ensure the namespace exists
    if not len(article_path_components) >= 1 or len(article_path_components[0]) == 0:
        d = {'wiki_err_no_namespace': True}
        update_template_dictionary(d, request, course)
        return render_to_response('simplewiki/simplewiki_error.html', d)

    namespace = None
    try:
        namespace = Namespace.objects.get(name__exact=article_path_components[0])
    except Namespace.DoesNotExist, ValueError:
        d = {'wiki_err_bad_namespace': True}
        update_template_dictionary(d, request, course)
        return render_to_response('simplewiki/simplewiki_error.html', d)
Example #17
0
def remove_related(request, course_id, namespace, slug, related_id):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    (article, err) = get_article(request, slug, namespace if namespace else course_id)

    if err:
        return err

    perm_err = check_permissions(request, article, course, check_write=True, check_locked=True)
    if perm_err:
        return perm_err

    try:
        rel_id = int(related_id)
        rel = Article.objects.get(id=rel_id)
        article.related.remove(rel)
        article.save()
    except:
        pass
    finally:
        return HttpResponseRedirect(reverse('wiki_view', args=(article.get_url(),)))
Example #18
0
def view_revision(request, revision_number, article_path, course_id=None):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    (article, err) = get_article(request, article_path, course)
    if err:
        return err

    try:
        revision = Revision.objects.get(counter=int(revision_number), article=article)
    except:
        d = {'wiki_err_norevision': revision_number}
        update_template_dictionary(d, request, course, article)
        return render_to_response('simplewiki/simplewiki_error.html', d)

    perm_err = check_permissions(request, article, course, check_read=True, check_deleted=True, revision=revision)
    if perm_err:
        return perm_err

    d = {}
    update_template_dictionary(d, request, course, article, revision)

    return render_to_response('simplewiki/simplewiki_view.html', d)
Example #19
0
def add_related(request, course_id, slug, namespace):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    (article, err) = get_article(request, slug, namespace if namespace else course_id)
    if err:
        return err

    perm_err = check_permissions(request, article, course, check_write=True, check_locked=True)
    if perm_err:
        return perm_err

    try:
        related_id = request.POST['id']
        rel = Article.objects.get(id=related_id)
        has_already = article.related.filter(id=related_id).count()
        if has_already == 0 and not rel == article:
            article.related.add(rel)
            article.save()
    except:
        pass
    finally:
        return HttpResponseRedirect(reverse('wiki_view', args=(article.get_url(),)))
Example #20
0
def history(request, article_path, page=1, course_id=None):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    (article, err) = get_article(request, article_path, course)
    if err:
        return err

    perm_err = check_permissions(request,
                                 article,
                                 course,
                                 check_read=True,
                                 check_deleted=False)
    if perm_err:
        return perm_err

    page_size = 10

    if page is None:
        page = 1
    try:
        p = int(page)
    except ValueError:
        p = 1

    history = Revision.objects.filter(
        article__exact=article).order_by('-counter').select_related(
            'previous_revision__counter', 'revision_user', 'wiki_article')

    if request.method == 'POST':
        if request.POST.__contains__(
                'revision'
        ):  # They selected a version, but they can be either deleting or changing the version
            perm_err = check_permissions(request,
                                         article,
                                         course,
                                         check_write=True,
                                         check_locked=True)
            if perm_err:
                return perm_err

            redirectURL = wiki_reverse('wiki_view', article, course)
            try:
                r = int(request.POST['revision'])
                revision = Revision.objects.get(id=r)
                if request.POST.__contains__('change'):
                    article.current_revision = revision
                    article.save()
                elif request.POST.__contains__('view'):
                    redirectURL = wiki_reverse('wiki_view_revision',
                                               course=course,
                                               kwargs={
                                                   'revision_number':
                                                   revision.counter,
                                                   'article_path':
                                                   article.get_path()
                                               })
                # The rese of these are admin functions
                elif request.POST.__contains__(
                        'delete') and request.user.is_superuser:
                    if (revision.deleted == 0):
                        revision.adminSetDeleted(2)
                elif request.POST.__contains__(
                        'restore') and request.user.is_superuser:
                    if (revision.deleted == 2):
                        revision.adminSetDeleted(0)
                elif request.POST.__contains__(
                        'delete_all') and request.user.is_superuser:
                    Revision.objects.filter(article__exact=article,
                                            deleted=0).update(deleted=2)
                elif request.POST.__contains__('lock_article'):
                    article.locked = not article.locked
                    article.save()
            except Exception as e:
                print str(e)
                pass
            finally:
                return HttpResponseRedirect(redirectURL)
                #
                #
                # <input type="submit" name="delete" value="Delete revision"/>
                # <input type="submit" name="restore" value="Restore revision"/>
                # <input type="submit" name="delete_all" value="Delete all revisions">
                # %else:
                # <input type="submit" name="delete_article" value="Delete all revisions">
                #

    page_count = (history.count() + (page_size - 1)) / page_size
    if p > page_count:
        p = 1
    beginItem = (p - 1) * page_size

    next_page = p + 1 if page_count > p else None
    prev_page = p - 1 if p > 1 else None

    d = {
        'wiki_page': p,
        'wiki_next_page': next_page,
        'wiki_prev_page': prev_page,
        'wiki_history': history[beginItem:beginItem + page_size],
        'show_delete_revision': request.user.is_superuser
    }
    update_template_dictionary(d, request, course, article)

    return render_to_response('simplewiki/simplewiki_history.html', d)
Example #21
0
def history(request, article_path, page=1, course_id=None):
    course = get_opt_course_with_access(request.user, course_id, 'load')

    (article, err) = get_article(request, article_path, course)
    if err:
        return err

    perm_err = check_permissions(
        request, article, course, check_read=True, check_deleted=False)
    if perm_err:
        return perm_err

    page_size = 10

    if page is None:
        page = 1
    try:
        p = int(page)
    except ValueError:
        p = 1

    history = Revision.objects.filter(article__exact=article).order_by(
        '-counter').select_related('previous_revision__counter', 'revision_user', 'wiki_article')

    if request.method == 'POST':
        if request.POST.__contains__('revision'):  # They selected a version, but they can be either deleting or changing the version
            perm_err = check_permissions(
                request, article, course, check_write=True, check_locked=True)
            if perm_err:
                return perm_err

            redirectURL = wiki_reverse('wiki_view', article, course)
            try:
                r = int(request.POST['revision'])
                revision = Revision.objects.get(id=r)
                if request.POST.__contains__('change'):
                    article.current_revision = revision
                    article.save()
                elif request.POST.__contains__('view'):
                    redirectURL = wiki_reverse(
                        'wiki_view_revision', course=course,
                        kwargs={'revision_number': revision.counter, 'article_path': article.get_path()})
                # The rese of these are admin functions
                elif request.POST.__contains__('delete') and request.user.is_superuser:
                    if (revision.deleted == 0):
                        revision.adminSetDeleted(2)
                elif request.POST.__contains__('restore') and request.user.is_superuser:
                    if (revision.deleted == 2):
                        revision.adminSetDeleted(0)
                elif request.POST.__contains__('delete_all') and request.user.is_superuser:
                    Revision.objects.filter(
                        article__exact=article, deleted=0).update(deleted=2)
                elif request.POST.__contains__('lock_article'):
                    article.locked = not article.locked
                    article.save()
            except Exception as e:
                print str(e)
                pass
            finally:
                return HttpResponseRedirect(redirectURL)
                #
                #
                # <input type="submit" name="delete" value="Delete revision"/>
                # <input type="submit" name="restore" value="Restore revision"/>
                # <input type="submit" name="delete_all" value="Delete all revisions">
                # %else:
                # <input type="submit" name="delete_article" value="Delete all revisions">
                #

    page_count = (history.count() + (page_size - 1)) / page_size
    if p > page_count:
        p = 1
    beginItem = (p - 1) * page_size

    next_page = p + 1 if page_count > p else None
    prev_page = p - 1 if p > 1 else None

    d = {'wiki_page': p,
         'wiki_next_page': next_page,
         'wiki_prev_page': prev_page,
         'wiki_history': history[beginItem:beginItem + page_size],
         'show_delete_revision': request.user.is_superuser}
    update_template_dictionary(d, request, course, article)

    return render_to_response('simplewiki/simplewiki_history.html', d)