From fb8f2d10984bcfa2585dc272b4c85d285b722792 Mon Sep 17 00:00:00 2001 From: Michael Manfre Date: Mon, 28 Nov 2022 23:41:36 -0500 Subject: Hashtags --- users/views/admin/__init__.py | 6 ++ users/views/admin/hashtags.py | 126 ++++++++++++++++++++++++++++++++++++++++++ users/views/admin/settings.py | 10 +++- 3 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 users/views/admin/hashtags.py (limited to 'users/views') diff --git a/users/views/admin/__init__.py b/users/views/admin/__init__.py index d1a4db1..101ca30 100644 --- a/users/views/admin/__init__.py +++ b/users/views/admin/__init__.py @@ -11,6 +11,12 @@ from users.views.admin.domains import ( # noqa Domains, ) from users.views.admin.federation import FederationEdit, FederationRoot # noqa +from users.views.admin.hashtags import ( # noqa + HashtagCreate, + HashtagDelete, + HashtagEdit, + Hashtags, +) from users.views.admin.settings import BasicSettings # noqa diff --git a/users/views/admin/hashtags.py b/users/views/admin/hashtags.py new file mode 100644 index 0000000..90f7a84 --- /dev/null +++ b/users/views/admin/hashtags.py @@ -0,0 +1,126 @@ +from django import forms +from django.shortcuts import get_object_or_404, redirect +from django.utils.decorators import method_decorator +from django.views.generic import FormView, TemplateView + +from activities.models import Hashtag, HashtagStates +from users.decorators import admin_required + + +@method_decorator(admin_required, name="dispatch") +class Hashtags(TemplateView): + + template_name = "admin/hashtags.html" + + def get_context_data(self): + return { + "hashtags": Hashtag.objects.filter().order_by("hashtag"), + "section": "hashtag", + } + + +@method_decorator(admin_required, name="dispatch") +class HashtagCreate(FormView): + + template_name = "admin/hashtag_create.html" + extra_context = {"section": "hashtags"} + + class form_class(forms.Form): + hashtag = forms.SlugField( + help_text="The hashtag without the '#'", + ) + name_override = forms.CharField( + help_text="Optional - a more human readable hashtag.", + required=False, + ) + public = forms.NullBooleanField( + help_text="Should this hashtag appear in the UI", + widget=forms.Select( + choices=[(None, "Unreviewed"), (True, "Public"), (False, "Private")] + ), + required=False, + ) + + def clean_hashtag(self): + hashtag = self.cleaned_data["hashtag"].lstrip("#").lower() + if not Hashtag.hashtag_regex.match("#" + hashtag): + raise forms.ValidationError("This does not look like a hashtag name") + if Hashtag.objects.filter(hashtag=hashtag): + raise forms.ValidationError("This hashtag name is already in use") + return hashtag + + def clean_name_override(self): + name_override = self.cleaned_data["name_override"] + if not name_override: + return None + if self.cleaned_data["hashtag"] != name_override.lower(): + raise forms.ValidationError( + "Name override doesn't match hashtag. Only case changes are allowed." + ) + return self.cleaned_data["name_override"] + + def form_valid(self, form): + Hashtag.objects.create( + hashtag=form.cleaned_data["hashtag"], + name_override=form.cleaned_data["name_override"] or None, + public=form.cleaned_data["public"], + ) + return redirect(Hashtag.urls.root) + + +@method_decorator(admin_required, name="dispatch") +class HashtagEdit(FormView): + + template_name = "admin/hashtag_edit.html" + extra_context = {"section": "hashtags"} + + class form_class(HashtagCreate.form_class): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields["hashtag"].disabled = True + + def clean_hashtag(self): + return self.cleaned_data["hashtag"] + + def dispatch(self, request, hashtag): + self.hashtag = get_object_or_404(Hashtag.objects, hashtag=hashtag) + return super().dispatch(request) + + def get_context_data(self, *args, **kwargs): + context = super().get_context_data(*args, **kwargs) + context["hashtag"] = self.hashtag + return context + + def form_valid(self, form): + self.hashtag.public = form.cleaned_data["public"] + self.hashtag.name_override = form.cleaned_data["name_override"] + self.hashtag.save() + Hashtag.transition_perform(self.hashtag, HashtagStates.outdated) + return redirect(Hashtag.urls.root) + + def get_initial(self): + return { + "hashtag": self.hashtag.hashtag, + "name_override": self.hashtag.name_override, + "public": self.hashtag.public, + } + + +@method_decorator(admin_required, name="dispatch") +class HashtagDelete(TemplateView): + + template_name = "admin/hashtag_delete.html" + + def dispatch(self, request, hashtag): + self.hashtag = get_object_or_404(Hashtag.objects, hashtag=hashtag) + return super().dispatch(request) + + def get_context_data(self): + return { + "hashtag": self.hashtag, + "section": "hashtags", + } + + def post(self, request): + self.hashtag.delete() + return redirect("admin_hashtags") diff --git a/users/views/admin/settings.py b/users/views/admin/settings.py index 44a338f..a9ec78b 100644 --- a/users/views/admin/settings.py +++ b/users/views/admin/settings.py @@ -80,6 +80,10 @@ class BasicSettings(AdminSettingsPage): "help_text": "Usernames that only admins can register for identities. One per line.", "display": "textarea", }, + "hashtag_unreviewed_are_public": { + "title": "Unreviewed Hashtags Are Public", + "help_text": "Public Hashtags may appear in Trending and have a Tags timeline", + }, } layout = { @@ -91,7 +95,11 @@ class BasicSettings(AdminSettingsPage): "highlight_color", ], "Signups": ["signup_allowed", "signup_invite_only", "signup_text"], - "Posts": ["post_length", "content_warning_text"], + "Posts": [ + "post_length", + "content_warning_text", + "hashtag_unreviewed_are_public", + ], "Identities": [ "identity_max_per_user", "identity_min_length", -- cgit v1.2.3