mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-04-29 10:10:19 +00:00
featherwiki: Use new utility for handling uploads
Tests: - Raise an error by editing code in create, upload, rename and delete operations. Notice that the details error messages are shown with a drop-down. - Upload a wiki and it works. The name is as expected. - Upload the wiki again notice that the duplicate wiki error is shown. Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net> [sunil: Use new utility for uploading] [sunil: Better error message display in the UI] Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
parent
6dd6f12f5a
commit
ad09964e18
@ -4,9 +4,9 @@
|
|||||||
import pathlib
|
import pathlib
|
||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
|
||||||
import urllib.request
|
import urllib.request
|
||||||
|
|
||||||
|
from plinth import action_utils
|
||||||
from plinth.actions import privileged
|
from plinth.actions import privileged
|
||||||
|
|
||||||
EMPTY_WIKI_FILE = 'https://ftp.freedombox.org/pub/featherwiki/empty.html'
|
EMPTY_WIKI_FILE = 'https://ftp.freedombox.org/pub/featherwiki/empty.html'
|
||||||
@ -51,20 +51,11 @@ def create_wiki(file_name: str):
|
|||||||
|
|
||||||
|
|
||||||
@privileged
|
@privileged
|
||||||
def add_wiki_file(upload_file: str):
|
def add_wiki_file(file_name: str, temporary_file_path: str):
|
||||||
"""Add an uploaded wiki file."""
|
"""Add an uploaded wiki file."""
|
||||||
upload_file_path = pathlib.Path(upload_file)
|
action_utils.move_uploaded_file(temporary_file_path, wiki_dir, file_name,
|
||||||
temp_dir = tempfile.gettempdir()
|
allow_overwrite=False, user='www-data',
|
||||||
if not upload_file_path.is_relative_to(temp_dir):
|
group='www-data', permissions=0o644)
|
||||||
raise Exception('Uploaded file is not in expected temp directory.')
|
|
||||||
|
|
||||||
file_name = _normalize_wiki_file_name(upload_file_path.name)
|
|
||||||
file_path = wiki_dir / file_name
|
|
||||||
if file_path.exists():
|
|
||||||
raise ValueError('Wiki exists')
|
|
||||||
|
|
||||||
shutil.move(upload_file_path, file_path)
|
|
||||||
_set_ownership(file_path)
|
|
||||||
|
|
||||||
|
|
||||||
@privileged
|
@privileged
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
"""Django views for Feather Wiki."""
|
"""Django views for Feather Wiki."""
|
||||||
|
|
||||||
import tempfile
|
|
||||||
|
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.contrib.messages.views import SuccessMessageMixin
|
from django.contrib.messages.views import SuccessMessageMixin
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
@ -14,6 +12,7 @@ from django.views.generic import FormView
|
|||||||
from plinth import app as app_module
|
from plinth import app as app_module
|
||||||
from plinth import views
|
from plinth import views
|
||||||
from plinth.modules import featherwiki
|
from plinth.modules import featherwiki
|
||||||
|
from plinth.views import messages_error
|
||||||
|
|
||||||
from . import privileged
|
from . import privileged
|
||||||
from .forms import CreateWikiForm, RenameWikiForm, UploadWikiForm
|
from .forms import CreateWikiForm, RenameWikiForm, UploadWikiForm
|
||||||
@ -56,9 +55,9 @@ class CreateWikiView(SuccessMessageMixin, FormView):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
messages.error(self.request, DUPLICATE_FILE_ERROR)
|
messages.error(self.request, DUPLICATE_FILE_ERROR)
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
messages.error(
|
messages_error(self.request,
|
||||||
self.request, "{0} {1}".format(
|
_('An error occurred while creating the wiki.'),
|
||||||
_('An error occurred while creating the wiki.'), error))
|
error)
|
||||||
|
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
@ -86,9 +85,9 @@ class RenameWikiView(SuccessMessageMixin, FormView):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
messages.error(self.request, DUPLICATE_FILE_ERROR)
|
messages.error(self.request, DUPLICATE_FILE_ERROR)
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
messages.error(
|
messages_error(self.request,
|
||||||
self.request, "{0} {1}".format(
|
_('An error occurred while renaming the wiki.'),
|
||||||
_('An error occurred while renaming the wiki.'), error))
|
error)
|
||||||
|
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
@ -109,24 +108,15 @@ class UploadWikiView(SuccessMessageMixin, FormView):
|
|||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
"""Add the wiki file on valid form submission."""
|
"""Add the wiki file on valid form submission."""
|
||||||
multipart_file = self.request.FILES['featherwiki-file']
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with tempfile.TemporaryDirectory() as temp_dir:
|
uploaded_file = self.request.FILES['featherwiki-file']
|
||||||
wiki_file_name = temp_dir + '/' + multipart_file.name
|
privileged.add_wiki_file(uploaded_file.name,
|
||||||
with open(wiki_file_name, 'wb+') as wiki_file:
|
uploaded_file.temporary_file_path())
|
||||||
for chunk in multipart_file.chunks():
|
|
||||||
wiki_file.write(chunk)
|
|
||||||
|
|
||||||
privileged.add_wiki_file(wiki_file_name)
|
|
||||||
|
|
||||||
self.success_message = _('Wiki file added.')
|
self.success_message = _('Wiki file added.')
|
||||||
except ValueError:
|
except FileExistsError:
|
||||||
messages.error(self.request, DUPLICATE_FILE_ERROR)
|
messages.error(self.request, DUPLICATE_FILE_ERROR)
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
messages.error(
|
messages_error(self.request, _('Failed to add wiki file.'), error)
|
||||||
self.request, "{0} {1}".format(_('Failed to add wiki file.'),
|
|
||||||
error))
|
|
||||||
return redirect(reverse_lazy('featherwiki:index'))
|
return redirect(reverse_lazy('featherwiki:index'))
|
||||||
|
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
@ -144,10 +134,9 @@ def delete(request, name):
|
|||||||
privileged.delete_wiki(name)
|
privileged.delete_wiki(name)
|
||||||
messages.success(request, _('{name} deleted.').format(name=name))
|
messages.success(request, _('{name} deleted.').format(name=name))
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
messages.error(
|
messages_error(request,
|
||||||
request,
|
_('Could not delete {name}').format(name=name),
|
||||||
_('Could not delete {name}: {error}').format(
|
error)
|
||||||
name=name, error=error))
|
|
||||||
|
|
||||||
return redirect(reverse_lazy('featherwiki:index'))
|
return redirect(reverse_lazy('featherwiki:index'))
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user