summaryrefslogtreecommitdiffstats
path: root/users/views/settings.py
blob: c3c166b1a19585e97860bb1fc81cf10fe80fac5b (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
from functools import partial
from typing import ClassVar, Dict

from django import forms
from django.shortcuts import redirect
from django.utils.decorators import method_decorator
from django.views.generic import FormView, RedirectView
from PIL import Image, ImageOps

from core.models import Config
from users.decorators import identity_required


@method_decorator(identity_required, name="dispatch")
class SettingsRoot(RedirectView):
    url = "/settings/interface/"


@method_decorator(identity_required, name="dispatch")
class SettingsPage(FormView):
    """
    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.IdentityOptions
    template_name = "settings/settings.html"
    section: ClassVar[str]
    options: Dict[str, Dict[str, str]]

    def get_form_class(self):
        # Create the fields dict from the config object
        fields = {}
        for key, details in self.options.items():
            config_field = self.options_class.__fields__[key]
            if config_field.type_ is bool:
                form_field = partial(
                    forms.BooleanField,
                    widget=forms.Select(
                        choices=[(True, "Enabled"), (False, "Disabled")]
                    ),
                )
            elif config_field.type_ is str:
                form_field = forms.CharField
            else:
                raise ValueError(f"Cannot render settings type {config_field.type_}")
            fields[key] = form_field(
                label=details["title"],
                help_text=details.get("help_text", ""),
                required=details.get("required", False),
            )
        # Create a form class dynamically (yeah, right?) and return that
        return type("SettingsForm", (forms.Form,), fields)

    def load_config(self):
        return Config.load_identity(self.request.identity)

    def save_config(self, key, value):
        Config.set_identity(self.request.identity, key, value)

    def get_initial(self):
        config = self.load_config()
        initial = {}
        for key in self.options.keys():
            initial[key] = getattr(config, key)
        return initial

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

    def form_valid(self, form):
        # Save each key
        for field in form:
            self.save_config(
                field.name,
                form.cleaned_data[field.name],
            )
        return redirect(".")


class InterfacePage(SettingsPage):

    section = "interface"

    options = {
        "toot_mode": {
            "title": "I Will Toot As I Please",
            "help_text": "If enabled, changes all 'Post' buttons to 'Toot!'",
        }
    }


@method_decorator(identity_required, name="dispatch")
class ProfilePage(FormView):
    """
    Lets the identity's profile be edited
    """

    template_name = "settings/profile.html"

    class form_class(forms.Form):
        name = forms.CharField(max_length=500)
        summary = forms.CharField(widget=forms.Textarea, required=False)
        icon = forms.ImageField(required=False)
        image = forms.ImageField(required=False)

    def get_initial(self):
        return {
            "name": self.request.identity.name,
            "summary": self.request.identity.summary,
        }

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

    def form_valid(self, form):
        # Update identity name and summary
        self.request.identity.name = form.cleaned_data["name"]
        self.request.identity.summary = form.cleaned_data["summary"]
        # Resize images
        icon = form.cleaned_data.get("icon")
        image = form.cleaned_data.get("image")
        if icon:
            resized_image = ImageOps.fit(Image.open(icon), (400, 400))
            icon.open()
            resized_image.save(icon)
            self.request.identity.icon = icon
        if image:
            resized_image = ImageOps.fit(Image.open(image), (1500, 500))
            image.open()
            resized_image.save(image)
            self.request.identity.image = image
        self.request.identity.save()
        return redirect(".")