From e28294c81a0dd215968c8717e99657a493c99e3b Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Tue, 20 Dec 2022 09:16:03 +0000 Subject: [PATCH] Hashtag admin simplification --- activities/models/hashtag.py | 6 +-- takahe/urls.py | 9 ---- templates/admin/hashtag_create.html | 26 ---------- templates/admin/hashtag_delete.html | 17 ------- templates/admin/hashtag_edit.html | 4 +- templates/admin/hashtags.html | 17 ++++--- templates/settings/_menu.html | 6 +-- users/views/admin/__init__.py | 7 +-- users/views/admin/federation.py | 6 +-- users/views/admin/hashtags.py | 74 ++++++----------------------- 10 files changed, 36 insertions(+), 136 deletions(-) delete mode 100644 templates/admin/hashtag_create.html delete mode 100644 templates/admin/hashtag_delete.html diff --git a/activities/models/hashtag.py b/activities/models/hashtag.py index a850ec3..078f110 100644 --- a/activities/models/hashtag.py +++ b/activities/models/hashtag.py @@ -116,10 +116,8 @@ class Hashtag(StatorModel): objects = HashtagManager() class urls(urlman.Urls): - root = "/admin/hashtags/" - create = "/admin/hashtags/create/" - edit = "/admin/hashtags/{self.hashtag}/" - delete = "{edit}delete/" + admin = "/admin/hashtags/" + admin_edit = "{admin}{self.hashtag}/" timeline = "/tags/{self.hashtag}/" hashtag_regex = re.compile(r"\B#([a-zA-Z0-9(_)]+\b)(?!;)") diff --git a/takahe/urls.py b/takahe/urls.py index a45be59..85504a2 100644 --- a/takahe/urls.py +++ b/takahe/urls.py @@ -145,19 +145,10 @@ urlpatterns = [ admin.Hashtags.as_view(), name="admin_hashtags", ), - path( - "admin/hashtags/create/", - admin.HashtagCreate.as_view(), - name="admin_hashtags_create", - ), path( "admin/hashtags//", admin.HashtagEdit.as_view(), ), - path( - "admin/hashtags//delete/", - admin.HashtagDelete.as_view(), - ), path( "admin/stator/", admin.Stator.as_view(), diff --git a/templates/admin/hashtag_create.html b/templates/admin/hashtag_create.html deleted file mode 100644 index 2d31cf7..0000000 --- a/templates/admin/hashtag_create.html +++ /dev/null @@ -1,26 +0,0 @@ -{% extends "settings/base.html" %} - -{% block title %}Add hashtag - Admin{% endblock %} - -{% block content %} -
-

Add A hashtag

-

- Use this form to add a hashtag. -

- {% csrf_token %} -
- hashtag Details - {% include "forms/_field.html" with field=form.hashtag %} - {% include "forms/_field.html" with field=form.name_override %} -
-
- Access Control - {% include "forms/_field.html" with field=form.public %} -
-
- Back - -
-
-{% endblock %} diff --git a/templates/admin/hashtag_delete.html b/templates/admin/hashtag_delete.html deleted file mode 100644 index 9aca4e7..0000000 --- a/templates/admin/hashtag_delete.html +++ /dev/null @@ -1,17 +0,0 @@ -{% extends "settings/base.html" %} - -{% block title %}Delete {{ hashtag.hashtag }} - Admin{% endblock %} - -{% block content %} -
- {% csrf_token %} - -

Deleting {{ hashtag.hashtag }}

- -

Please confirm deletion of this hashtag.

-
- Cancel - -
- -{% endblock %} diff --git a/templates/admin/hashtag_edit.html b/templates/admin/hashtag_edit.html index b023dfa..2733a75 100644 --- a/templates/admin/hashtag_edit.html +++ b/templates/admin/hashtag_edit.html @@ -38,8 +38,8 @@
- Back - Delete + Back + View Posts
diff --git a/templates/admin/hashtags.html b/templates/admin/hashtags.html index 4273ac2..7726a93 100644 --- a/templates/admin/hashtags.html +++ b/templates/admin/hashtags.html @@ -4,8 +4,8 @@ {% block content %}
- {% for hashtag in hashtags %} - + {% for hashtag in page_obj %} + {{ hashtag.display_name }} @@ -31,10 +31,15 @@ {% empty %} -

You have no hashtags set up.

+

There are no hashtags yet.

{% endfor %} - - Add a hashtag - +
+ {% if page_obj.has_previous %} + Previous Page + {% endif %} + {% if page_obj.has_next %} + Next Page + {% endif %} +
{% endblock %} diff --git a/templates/settings/_menu.html b/templates/settings/_menu.html index aa05365..478e5ae 100644 --- a/templates/settings/_menu.html +++ b/templates/settings/_menu.html @@ -27,9 +27,6 @@ Reports - - Federation - {% endif %} {% if request.user.admin %}

Administration

@@ -42,6 +39,9 @@ Domains + + Federation + Users diff --git a/users/views/admin/__init__.py b/users/views/admin/__init__.py index c4576b7..2ce82d2 100644 --- a/users/views/admin/__init__.py +++ b/users/views/admin/__init__.py @@ -9,12 +9,7 @@ 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.hashtags import HashtagEdit, Hashtags # noqa from users.views.admin.identities import IdentitiesRoot, IdentityEdit # noqa from users.views.admin.invites import InviteCreate, InvitesRoot, InviteView # noqa from users.views.admin.reports import ReportsRoot, ReportView # noqa diff --git a/users/views/admin/federation.py b/users/views/admin/federation.py index 8850efa..0611b9e 100644 --- a/users/views/admin/federation.py +++ b/users/views/admin/federation.py @@ -4,11 +4,11 @@ from django.shortcuts import get_object_or_404, redirect from django.utils.decorators import method_decorator from django.views.generic import FormView, ListView -from users.decorators import moderator_required +from users.decorators import admin_required from users.models import Domain -@method_decorator(moderator_required, name="dispatch") +@method_decorator(admin_required, name="dispatch") class FederationRoot(ListView): template_name = "admin/federation.html" @@ -33,7 +33,7 @@ class FederationRoot(ListView): return domains -@method_decorator(moderator_required, name="dispatch") +@method_decorator(admin_required, name="dispatch") class FederationEdit(FormView): template_name = "admin/federation_edit.html" diff --git a/users/views/admin/hashtags.py b/users/views/admin/hashtags.py index cdde6c5..3bc6d08 100644 --- a/users/views/admin/hashtags.py +++ b/users/views/admin/hashtags.py @@ -1,33 +1,38 @@ 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 django.views.generic import FormView, ListView from activities.models import Hashtag, HashtagStates from users.decorators import moderator_required @method_decorator(moderator_required, name="dispatch") -class Hashtags(TemplateView): +class Hashtags(ListView): template_name = "admin/hashtags.html" + paginate_by = 30 - def get_context_data(self): - return { - "hashtags": Hashtag.objects.filter().order_by("hashtag"), - "section": "hashtag", + def get(self, request, *args, **kwargs): + self.extra_context = { + "section": "hashtags", } + return super().get(request, *args, **kwargs) + + def get_queryset(self): + return Hashtag.objects.filter().order_by("hashtag") @method_decorator(moderator_required, name="dispatch") -class HashtagCreate(FormView): +class HashtagEdit(FormView): - template_name = "admin/hashtag_create.html" + template_name = "admin/hashtag_edit.html" extra_context = {"section": "hashtags"} class form_class(forms.Form): hashtag = forms.SlugField( help_text="The hashtag without the '#'", + disabled=True, ) name_override = forms.CharField( help_text="Optional - a more human readable hashtag.", @@ -41,14 +46,6 @@ class HashtagCreate(FormView): 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: @@ -59,29 +56,6 @@ class HashtagCreate(FormView): ) 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(moderator_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) @@ -96,7 +70,7 @@ class HashtagEdit(FormView): self.hashtag.name_override = form.cleaned_data["name_override"] self.hashtag.save() Hashtag.transition_perform(self.hashtag, HashtagStates.outdated) - return redirect(Hashtag.urls.root) + return redirect(Hashtag.urls.admin) def get_initial(self): return { @@ -104,23 +78,3 @@ class HashtagEdit(FormView): "name_override": self.hashtag.name_override, "public": self.hashtag.public, } - - -@method_decorator(moderator_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")