File: views.py

package info (click to toggle)
freedombox 26.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,092 kB
  • sloc: python: 48,542; javascript: 1,730; xml: 481; makefile: 290; sh: 137; php: 32
file content (70 lines) | stat: -rw-r--r-- 2,269 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Views for security module."""

from django.contrib import messages
from django.template.response import TemplateResponse
from django.utils.translation import gettext as _

from plinth import action_utils
from plinth.modules import security
from plinth.modules.upgrades import is_backports_requested
from plinth.privileged import service as service_privileged
from plinth.views import AppView

from .forms import SecurityForm


class SecurityAppView(AppView):
    """Show security app main page."""

    app_id = 'security'
    template_name = 'security.html'
    form_class = SecurityForm
    prefix = 'security'

    def get_initial(self):
        """Return the values to fill in the form."""
        initial = super().get_initial()
        initial.update(get_status(self.request))
        return initial

    def get_context_data(self, *args, **kwargs):
        """Add additional context data for template."""
        context = super().get_context_data(*args, **kwargs)
        context['is_backports_requested'] = is_backports_requested()
        return context

    def form_valid(self, form):
        """Apply the changes submitted in the form."""
        _apply_changes(self.request, form.initial, form.cleaned_data)
        return super().form_valid(form)


def get_status(request):
    """Return the current status."""
    return {'fail2ban_enabled': action_utils.service_is_enabled('fail2ban')}


def _apply_changes(request, old_status, new_status):
    """Apply the form changes."""
    if old_status['fail2ban_enabled'] != new_status['fail2ban_enabled']:
        if new_status['fail2ban_enabled']:
            service_privileged.enable('fail2ban')
        else:
            service_privileged.disable('fail2ban')

        messages.success(request, _('Configuration updated.'))


def report(request):
    """Serve the security report page."""
    apps_report = security.get_apps_report()
    return TemplateResponse(
        request, 'security_report.html', {
            'title':
                _('Security Report'),
            'freedombox_report':
                apps_report.pop('freedombox'),
            'apps_report':
                sorted(apps_report.values(), key=lambda app: app['name']),
        })