Joseph Nuthalapati 96bd9c8bd4
tiddlywiki: Add new app
TiddlyWiki uses almost the same Apache configuration as Feather Wiki,
with one difference - disabling gzip for the `HEAD` request.

The FreedomBox app for TiddlyWiki is identical to Feather Wiki in
every other aspect.

- Proxy download through freedombox.org. This serves two purposes:

1. Upstream's website cannot track the IP addresses of FreedomBox users.
2. We can update the versions of the empty quine files without making
   code changes in FreedomBox.

[sunil]

- Update description to correct the list of users who can access the app.

- Update logo to adhere to the logo guidelines.

- Minor styling fix.

- Update the copyright on the logo based on information from upstream git
repository.

Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
2024-08-10 17:57:01 -07:00

158 lines
5.3 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""Django views for TiddlyWiki."""
import tempfile
from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.views.generic import FormView
from plinth import app as app_module
from plinth import views
from plinth.modules import tiddlywiki
from . import privileged
from .forms import CreateWikiForm, RenameWikiForm, UploadWikiForm
DUPLICATE_FILE_ERROR = _('A wiki file with the given name already exists.')
class TiddlyWikiAppView(views.AppView):
"""Serve configuration page."""
app_id = 'tiddlywiki'
template_name = 'tiddlywiki_configure.html'
def get_context_data(self, *args, **kwargs):
"""Add wikis to the context data."""
context = super().get_context_data(*args, **kwargs)
context['wikis'] = tiddlywiki.get_wiki_list()
return context
class CreateWikiView(SuccessMessageMixin, FormView):
"""View to create a new repository."""
form_class = CreateWikiForm
prefix = 'tiddlywiki'
template_name = 'form.html'
success_url = reverse_lazy('tiddlywiki:index')
def get_context_data(self, **kwargs):
"""Return additional context for rendering the template."""
context = super().get_context_data(**kwargs)
context['title'] = _('Create Wiki')
return context
def form_valid(self, form):
"""Create the repository on valid form submission."""
try:
privileged.create_wiki(form.cleaned_data['name'])
self.success_message = _('Wiki created.')
except ValueError:
messages.error(self.request, DUPLICATE_FILE_ERROR)
except Exception as error:
messages.error(
self.request, "{0} {1}".format(
_('An error occurred while creating the wiki.'), error))
return super().form_valid(form)
class RenameWikiView(SuccessMessageMixin, FormView):
"""View to edit an existing repository."""
form_class = RenameWikiForm
prefix = 'tiddlywiki'
template_name = 'form.html'
success_url = reverse_lazy('tiddlywiki:index')
def get_context_data(self, **kwargs):
"""Return additional context for rendering the template."""
context = super().get_context_data(**kwargs)
context['title'] = _('Rename Wiki')
return context
def form_valid(self, form):
"""Rename the wiki on valid form submission."""
try:
privileged.rename_wiki(self.kwargs['old_name'],
form.cleaned_data['new_name'])
self.success_message = _('Wiki renamed.')
except ValueError:
messages.error(self.request, DUPLICATE_FILE_ERROR)
except Exception as error:
messages.error(
self.request, "{0} {1}".format(
_('An error occurred while renaming the wiki.'), error))
return super().form_valid(form)
class UploadWikiView(SuccessMessageMixin, FormView):
"""View to upload an existing wiki file."""
form_class = UploadWikiForm
prefix = 'tiddlywiki'
template_name = 'tiddlywiki_upload_file.html'
success_url = reverse_lazy('tiddlywiki:index')
def get_context_data(self, **kwargs):
"""Return additional context for rendering the template."""
context = super().get_context_data(**kwargs)
context['title'] = _('Upload Wiki File')
return context
def form_valid(self, form):
"""Add the wiki file on valid form submission."""
multipart_file = self.request.FILES['tiddlywiki-file']
try:
with tempfile.TemporaryDirectory() as temp_dir:
wiki_file_name = temp_dir + '/' + multipart_file.name
with open(wiki_file_name, 'wb+') as wiki_file:
for chunk in multipart_file.chunks():
wiki_file.write(chunk)
privileged.add_wiki_file(wiki_file_name)
self.success_message = _('Wiki file added.')
except ValueError:
messages.error(self.request, DUPLICATE_FILE_ERROR)
except Exception as error:
messages.error(
self.request, "{0} {1}".format(_('Failed to add wiki file.'),
error))
return redirect(reverse_lazy('tiddlywiki:index'))
return super().form_valid(form)
def delete(request, name):
"""Handle deleting wikis, showing a confirmation dialog first.
On GET, display a confirmation page.
On POST, delete the wiki.
"""
app = app_module.App.get('tiddlywiki')
if request.method == 'POST':
try:
privileged.delete_wiki(name)
messages.success(request, _('{name} deleted.').format(name=name))
except Exception as error:
messages.error(
request,
_('Could not delete {name}: {error}').format(
name=name, error=error))
return redirect(reverse_lazy('tiddlywiki:index'))
return TemplateResponse(request, 'tiddlywiki_delete.html', {
'title': app.info.name,
'name': name
})