summaryrefslogtreecommitdiffstats
path: root/users/views/admin.py
blob: d7f23e83c366556144a26dea3d456dc78724664c (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import re

from django import forms
from django.db import models
from django.shortcuts import get_object_or_404, redirect
from django.utils.decorators import method_decorator
from django.views.generic import FormView, RedirectView, TemplateView

from core.models import Config
from users.decorators import admin_required
from users.models import Domain, Identity, User
from users.views.settings import SettingsPage


@method_decorator(admin_required, name="dispatch")
class AdminRoot(RedirectView):
    pattern_name = "admin_basic"


@method_decorator(admin_required, name="dispatch")
class AdminSettingsPage(SettingsPage):
    """
    Shows a settings page dynamically created from our settings layout
    at the bottom of the page. Don't add this to a URL directly - subclass!
    """

    options_class = Config.SystemOptions

    def load_config(self):
        return Config.load_system()

    def save_config(self, key, value):
        Config.set_system(key, value)


class BasicPage(AdminSettingsPage):

    section = "basic"

    options = {
        "site_name": {
            "title": "Site Name",
        },
        "highlight_color": {
            "title": "Highlight Color",
            "help_text": "Used for logo background and other highlights",
        },
        "post_length": {
            "title": "Maximum Post Length",
            "help_text": "The maximum number of characters allowed per post",
        },
        "site_about": {
            "title": "About This Site",
            "help_text": "Displayed on the homepage and the about page",
            "display": "textarea",
        },
        "site_icon": {
            "title": "Site Icon",
            "help_text": "Minimum size 64x64px. Should be square.",
        },
        "site_banner": {
            "title": "Site Banner",
            "help_text": "Must be at least 650px wide. 3:1 ratio of width:height recommended.",
        },
    }

    layout = {
        "Branding": [
            "site_name",
            "site_about",
            "site_icon",
            "site_banner",
            "highlight_color",
        ],
        "Posts": ["post_length"],
    }


@method_decorator(admin_required, name="dispatch")
class DomainsPage(TemplateView):

    template_name = "admin/domains.html"

    def get_context_data(self):
        return {
            "domains": Domain.objects.filter(local=True).order_by("domain"),
            "section": "domains",
        }


@method_decorator(admin_required, name="dispatch")
class DomainCreatePage(FormView):

    template_name = "admin/domain_create.html"
    extra_context = {"section": "domains"}

    class form_class(forms.Form):
        domain = forms.CharField(
            help_text="The domain displayed as part of a user's identity.\nCannot be changed after the domain has been created.",
        )
        service_domain = forms.CharField(
            help_text="Optional - a domain that serves Takahē if it is not running on the main domain.\nCannot be changed after the domain has been created.",
            required=False,
        )
        public = forms.BooleanField(
            help_text="If any user on this server can create identities here",
            widget=forms.Select(choices=[(True, "Public"), (False, "Private")]),
            required=False,
        )

        domain_regex = re.compile(
            r"^((?!-))(xn--)?[a-z0-9][a-z0-9-_]{0,61}[a-z0-9]{0,1}\.(xn--)?([a-z0-9\-]{1,61}|[a-z0-9-]{1,30}\.[a-z]{2,})$"
        )

        def clean_domain(self):
            if not self.domain_regex.match(self.cleaned_data["domain"]):
                raise forms.ValidationError("This does not look like a domain name")
            if Domain.objects.filter(
                models.Q(domain=self.cleaned_data["domain"])
                | models.Q(service_domain=self.cleaned_data["domain"])
            ):
                raise forms.ValidationError("This domain name is already in use")
            return self.cleaned_data["domain"]

        def clean_service_domain(self):
            if not self.cleaned_data["service_domain"]:
                return None
            if not self.domain_regex.match(self.cleaned_data["service_domain"]):
                raise forms.ValidationError("This does not look like a domain name")
            if Domain.objects.filter(
                models.Q(domain=self.cleaned_data["service_domain"])
                | models.Q(service_domain=self.cleaned_data["service_domain"])
            ):
                raise forms.ValidationError("This domain name is already in use")
            if self.cleaned_data.get("domain") == self.cleaned_data["service_domain"]:
                raise forms.ValidationError(
                    "You cannot have the domain and service domain be the same (did you mean to leave service domain blank?)"
                )
            return self.cleaned_data["service_domain"]

    def form_valid(self, form):
        Domain.objects.create(
            domain=form.cleaned_data["domain"],
            service_domain=form.cleaned_data["service_domain"] or None,
            public=form.cleaned_data["public"],
            local=True,
        )
        return redirect(Domain.urls.root)


@method_decorator(admin_required, name="dispatch")
class DomainEditPage(FormView):

    template_name = "admin/domain_edit.html"
    extra_context = {"section": "domains"}

    class form_class(forms.Form):
        domain = forms.CharField(
            help_text="The domain displayed as part of a user's identity.\nCannot be changed after the domain has been created.",
            disabled=True,
        )
        service_domain = forms.CharField(
            help_text="Optional - a domain that serves Takahē if it is not running on the main domain.\nCannot be changed after the domain has been created.",
            disabled=True,
            required=False,
        )
        public = forms.BooleanField(
            help_text="If any user on this server can create identities here",
            widget=forms.Select(choices=[(True, "Public"), (False, "Private")]),
            required=False,
        )

    def dispatch(self, request, domain):
        self.domain = get_object_or_404(
            Domain.objects.filter(local=True), domain=domain
        )
        return super().dispatch(request)

    def get_context_data(self):
        context = super().get_context_data()
        context["domain"] = self.domain
        return context

    def form_valid(self, form):
        self.domain.public = form.cleaned_data["public"]
        self.domain.save()
        return redirect(Domain.urls.root)

    def get_initial(self):
        return {
            "domain": self.domain.domain,
            "service_domain": self.domain.service_domain,
            "public": self.domain.public,
        }


@method_decorator(admin_required, name="dispatch")
class DomainDeletePage(TemplateView):

    template_name = "admin/domain_delete.html"

    def dispatch(self, request, domain):
        self.domain = get_object_or_404(
            Domain.objects.filter(public=True), domain=domain
        )
        return super().dispatch(request)

    def get_context_data(self):
        return {
            "domain": self.domain,
            "num_identities": self.domain.identities.count(),
            "section": "domains",
        }

    def post(self, request):
        if self.domain.identities.exists():
            raise ValueError("Tried to delete domain with identities!")
        self.domain.delete()
        return redirect("/settings/system/domains/")


@method_decorator(admin_required, name="dispatch")
class UsersPage(TemplateView):

    template_name = "admin/users.html"

    def get_context_data(self):
        return {
            "users": User.objects.order_by("email"),
            "section": "users",
        }


@method_decorator(admin_required, name="dispatch")
class IdentitiesPage(TemplateView):

    template_name = "admin/identities.html"

    def get_context_data(self):
        return {
            "identities": Identity.objects.order_by("username"),
            "section": "identities",
        }