File: views.py

package info (click to toggle)
freedombox 26.2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 82,976 kB
  • sloc: python: 48,504; javascript: 1,736; xml: 481; makefile: 290; sh: 167; php: 32
file content (63 lines) | stat: -rw-r--r-- 1,976 bytes parent folder | download | duplicates (6)
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
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Views for BIND module."""

from django.contrib import messages
from django.utils.translation import gettext_lazy as _

from plinth.modules import names
from plinth.views import AppView

from . import privileged
from .forms import BindForm


class BindAppView(AppView):  # pylint: disable=too-many-ancestors
    """A specialized view for configuring Bind."""

    app_id = 'bind'
    form_class = BindForm
    template_name = 'bind.html'

    def get_context_data(self, *args, **kwargs):
        """Get/append information for domains and additional names."""
        context = super().get_context_data(**kwargs)

        served_domains = privileged.get_served_domains()
        context['domains_table'] = []
        for key, val in served_domains.items():
            if key == 'localhost.':
                continue

            context['domains_table'].append({
                'type': 'Domain Name',
                'domain_name': key[:-1:],
                'serving': 'Yes',
                'ip_address': ', '.join(val),
            })

        for item in names.components.DomainName.list():
            context['domains_table'].append({
                'type': item.domain_type.display_name,
                'domain_name': item.name,
                'serving': '-',
                'ip_address': ''
            })

        return context

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

    def form_valid(self, form):
        """Change the configurations of Bind service."""
        data = form.cleaned_data
        old_config = privileged.get_config()

        if old_config['forwarders'] != data['forwarders']:
            privileged.configure(data['forwarders'])
            messages.success(self.request, _('Configuration updated'))

        return super().form_valid(form)