Esempio n. 1
0
def group_admin():
    wiki_page_num = WikiPage.select().count()
    wiki_file_num = WikiFile.select().count()
    return render_template(
        'wiki/group_admin.html',
        wiki_page_num=wiki_page_num,
        wiki_file_num=wiki_file_num
    )
Esempio n. 2
0
def all_files():
    current_page_number = request.args.get('page', default=1, type=int)
    number_per_page = 100
    query = (WikiFile.select().order_by(WikiFile.id).paginate(
        current_page_number, paginate_by=number_per_page))

    kwargs = dict()
    get_pagination_kwargs(kwargs, query, current_page_number, number_per_page)

    return render_template('wiki/all_files.html', **kwargs)
Esempio n. 3
0
def file(wiki_file_id):
    fn = request.args.get('filename')
    if not fn:
        wiki_file = get_object_or_404(
            WikiFile.select(WikiFile.id, WikiFile.name),
            WikiFile.id == wiki_file_id)
        fn = wiki_file.name

    return send_from_directory(os.path.join(DB_PATH, g.wiki_group),
                               str(wiki_file_id),
                               as_attachment=True,
                               attachment_filename=fn)
Esempio n. 4
0
def handle_upload():
    form = request.form
    wiki_page_id = int(form.get('wiki_page_id', None))
    upload_from_upload_page = form.get('upload_page', None)

    file_markdown, file_html = '', ''
    with db.atomic():
        for i, file in enumerate(request.files.getlist('wiki_file')):
            # create a WikiFile in database and retrieve id
            wiki_file = WikiFile.create(name=file.filename,
                                        mime_type=file.mimetype)
            # save uploaded file with WikiFile.id as filename
            file.save(os.path.join(DB_PATH, g.wiki_group, str(wiki_file.id)))
            wiki_file.size = file.tell()
            wiki_file.save()

            if 'image' in wiki_file.mime_type:
                file_type = 'image'
            else:
                file_type = 'file'
            file_markdown += '\n\n[{}:{}]'.format(file_type, wiki_file.id)
            file_html += '<p>{}</p>'.format(
                render_wiki_file(wiki_file.id,
                                 wiki_file.name,
                                 file_type,
                                 tostring=True))

        if upload_from_upload_page:
            wiki_page = get_object_or_404(
                WikiPage.select(WikiPage.id, WikiPage.markdown,
                                WikiPage.current_version,
                                WikiPage.modified_on),
                WikiPage.id == wiki_page_id)

            diff = make_patch(xstr(wiki_page.markdown),
                              xstr(wiki_page.markdown) + file_markdown)
            WikiPageVersion.create(wiki_page=wiki_page,
                                   diff=diff,
                                   version=wiki_page.current_version,
                                   modified_on=wiki_page.modified_on)

            (WikiPageIndex.update(
                markdown=wiki_page.markdown + file_markdown).where(
                    WikiPageIndex.docid == wiki_page.id).execute())

            (WikiPage.update(markdown=WikiPage.markdown + file_markdown,
                             html=WikiPage.html + file_html,
                             current_version=WikiPage.current_version + 1,
                             modified_on=datetime.utcnow()).where(
                                 WikiPage.id == wiki_page.id).execute())

            return ''
    return file_markdown
Esempio n. 5
0
def file(wiki_file_id):
    wiki_file = get_object_or_404(
        WikiFile.select(WikiFile.id, WikiFile.name),
        WikiFile.id==wiki_file_id
    )

    return send_from_directory(
        os.path.join(current_app.config['DB_PATH'], g.wiki_group),
        str(wiki_file_id),
        as_attachment=True,
        attachment_filename=wiki_file.name
    )
Esempio n. 6
0
def handle_upload():
    form = request.form
    wiki_page_id = int(form.get('wiki_page_id', None))
    upload_from_upload_page = form.get('upload_page', None)

    file_markdown, file_html = '', ''
    with db.atomic():
        for i, file in enumerate(request.files.getlist('wiki_file')):
            # create a WikiFile in database and retrieve id
            wiki_file = WikiFile.create(
                name=file.filename,
                mime_type=file.mimetype
            )
            # save uploaded file with WikiFile.id as filename
            file.save(os.path.join(
                current_app.config['DB_PATH'],
                g.wiki_group,
                str(wiki_file.id)
            ))
            wiki_file.size = file.tell()
            wiki_file.save()

            if 'image' in wiki_file.mime_type:
                file_type = 'image'
            else:
                file_type = 'file'
            file_markdown += '\n\n[{}:{}]'.format(file_type, wiki_file.id)
            file_html += '<p>{}</p>'.format(render_wiki_file(
                wiki_file.id,
                wiki_file.name,
                file_type
            ))

        if upload_from_upload_page:
            wiki_page = get_object_or_404(
                WikiPage.select(
                    WikiPage.id,
                    WikiPage.markdown,
                    WikiPage.current_version,
                    WikiPage.modified_on),
                WikiPage.id==wiki_page_id
            )

            diff = make_patch(xstr(wiki_page.markdown), xstr(wiki_page.markdown)+file_markdown)
            wiki_page.update_db_after_upload(diff, file_markdown, file_html)

            return ''

    return file_markdown
Esempio n. 7
0
    def wiki_file(self, wiki_file_id, wiki_file_type, w, h):
        w = w or 0
        h = h or 0
        w, h = int(w), int(h)

        try:
            wiki_file = WikiFile.get_by_id(int(wiki_file_id))
        except WikiFile.DoesNotExist:
            return

        return render_wiki_file(wiki_file.id,
                                wiki_file.name,
                                wiki_file_type,
                                w=w,
                                h=h)
Esempio n. 8
0
    def handleMatch(self, m):
        _, _, wiki_file_type, wiki_file_id, wh, w, h = [
            m.group(i) for i in range(7)
        ]
        w = w or 0
        h = h or 0
        w, h = int(w), int(h)

        try:
            wiki_file = WikiFile.get_by_id(int(wiki_file_id))
        except WikiFile.DoesNotExist:
            return

        return render_wiki_file(wiki_file.id,
                                wiki_file.name,
                                wiki_file_type,
                                w=w,
                                h=h)
Esempio n. 9
0
def replace_file():
    form = request.form
    file = request.files['wiki_file']
    wiki_file_id = form.get('wiki_file_id', None)
    wiki_file_markdown = '[file:{0}]'.format(wiki_file_id)

    # save uploaded file with WikiFile.id as filename
    file.save(os.path.join(
        current_app.config['DB_PATH'],
        g.wiki_group,
        wiki_file_id
    ))

    wiki_file = WikiFile.get_or_none(WikiFile.id==int(wiki_file_id))

    with db.atomic():
        (WikiFile
         .update(
             name=file.filename,
             mime_type=file.mimetype,
             size=file.tell())
         .where(WikiFile.id==int(wiki_file_id))
         .execute())

        if wiki_file and wiki_file.name != file.filename:
            query = (WikiPage
                     .select(WikiPage.id, WikiPage.markdown)
                     .where(WikiPage.markdown.contains(wiki_file_markdown)))
            for wiki_page in query.execute():
                toc, html = markdown(wiki_page, wiki_page.markdown)
                (WikiPage
                .update(toc=toc, html=html)
                .where(WikiPage.id==wiki_page.id)
                .execute())

    return ''