Exemple #1
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
Exemple #2
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