Hashtag admin simplification

This commit is contained in:
Andrew Godwin 2022-12-20 09:16:03 +00:00
parent 39013e0bb5
commit e28294c81a
10 changed files with 36 additions and 136 deletions

View File

@ -116,10 +116,8 @@ class Hashtag(StatorModel):
objects = HashtagManager() objects = HashtagManager()
class urls(urlman.Urls): class urls(urlman.Urls):
root = "/admin/hashtags/" admin = "/admin/hashtags/"
create = "/admin/hashtags/create/" admin_edit = "{admin}{self.hashtag}/"
edit = "/admin/hashtags/{self.hashtag}/"
delete = "{edit}delete/"
timeline = "/tags/{self.hashtag}/" timeline = "/tags/{self.hashtag}/"
hashtag_regex = re.compile(r"\B#([a-zA-Z0-9(_)]+\b)(?!;)") hashtag_regex = re.compile(r"\B#([a-zA-Z0-9(_)]+\b)(?!;)")

View File

@ -145,19 +145,10 @@ urlpatterns = [
admin.Hashtags.as_view(), admin.Hashtags.as_view(),
name="admin_hashtags", name="admin_hashtags",
), ),
path(
"admin/hashtags/create/",
admin.HashtagCreate.as_view(),
name="admin_hashtags_create",
),
path( path(
"admin/hashtags/<hashtag>/", "admin/hashtags/<hashtag>/",
admin.HashtagEdit.as_view(), admin.HashtagEdit.as_view(),
), ),
path(
"admin/hashtags/<hashtag>/delete/",
admin.HashtagDelete.as_view(),
),
path( path(
"admin/stator/", "admin/stator/",
admin.Stator.as_view(), admin.Stator.as_view(),

View File

@ -1,26 +0,0 @@
{% extends "settings/base.html" %}
{% block title %}Add hashtag - Admin{% endblock %}
{% block content %}
<form action="." method="POST">
<h1>Add A hashtag</h1>
<p>
Use this form to add a hashtag.
</p>
{% csrf_token %}
<fieldset>
<legend>hashtag Details</legend>
{% include "forms/_field.html" with field=form.hashtag %}
{% include "forms/_field.html" with field=form.name_override %}
</fieldset>
<fieldset>
<legend>Access Control</legend>
{% include "forms/_field.html" with field=form.public %}
</fieldset>
<div class="buttons">
<a href="{% url "admin_hashtags" %}" class="button secondary left">Back</a>
<button>Create</button>
</div>
</form>
{% endblock %}

View File

@ -1,17 +0,0 @@
{% extends "settings/base.html" %}
{% block title %}Delete <i class="fa-solid fa-hashtag"></i>{{ hashtag.hashtag }} - Admin{% endblock %}
{% block content %}
<form action="." method="POST">
{% csrf_token %}
<h1>Deleting <i class="fa-solid fa-hashtag"></i>{{ hashtag.hashtag }}</h1>
<p>Please confirm deletion of this hashtag.</p>
<div class="buttons">
<a class="button" href="{{ hashtag.urls.edit }}">Cancel</a>
<button class="delete">Confirm Deletion</button>
</div>
{% endblock %}

View File

@ -38,8 +38,8 @@
</div> </div>
</fieldset> </fieldset>
<div class="buttons"> <div class="buttons">
<a href="{{ hashtag.urls.root }}" class="button secondary left">Back</a> <a href="{{ hashtag.urls.admin }}" class="button secondary left">Back</a>
<a href="{{ hashtag.urls.delete }}" class="button delete">Delete</a> <a href="{{ hashtag.urls.timeline }}" class="button secondary">View Posts</a>
<button>Save</button> <button>Save</button>
</div> </div>
</form> </form>

View File

@ -4,8 +4,8 @@
{% block content %} {% block content %}
<section class="icon-menu"> <section class="icon-menu">
{% for hashtag in hashtags %} {% for hashtag in page_obj %}
<a class="option" href="{{ hashtag.urls.edit }}"> <a class="option" href="{{ hashtag.urls.admin_edit }}">
<i class="fa-solid fa-hashtag"></i> <i class="fa-solid fa-hashtag"></i>
<span class="handle"> <span class="handle">
{{ hashtag.display_name }} {{ hashtag.display_name }}
@ -31,10 +31,15 @@
</a> </a>
{% empty %} {% empty %}
<p class="option empty">You have no hashtags set up.</p> <p class="option empty">There are no hashtags yet.</p>
{% endfor %} {% endfor %}
<a href="{% url "admin_hashtags_create" %}" class="option new"> <div class="load-more">
<i class="fa-solid fa-plus"></i> Add a hashtag {% if page_obj.has_previous %}
</a> <a class="button" href=".?page={{ page_obj.previous_page_number }}">Previous Page</a>
{% endif %}
{% if page_obj.has_next %}
<a class="button" href=".?page={{ page_obj.next_page_number }}">Next Page</a>
{% endif %}
</div>
</section> </section>
{% endblock %} {% endblock %}

View File

@ -27,9 +27,6 @@
<a href="{% url "admin_reports" %}" {% if section == "reports" %}class="selected"{% endif %} title="Reports"> <a href="{% url "admin_reports" %}" {% if section == "reports" %}class="selected"{% endif %} title="Reports">
<i class="fa-solid fa-flag"></i> Reports <i class="fa-solid fa-flag"></i> Reports
</a> </a>
<a href="{% url "admin_federation" %}" {% if section == "federation" %}class="selected"{% endif %} title="Federation">
<i class="fa-solid fa-diagram-project"></i> Federation
</a>
{% endif %} {% endif %}
{% if request.user.admin %} {% if request.user.admin %}
<h3>Administration</h3> <h3>Administration</h3>
@ -42,6 +39,9 @@
<a href="{% url "admin_domains" %}" {% if section == "domains" %}class="selected"{% endif %} title="Domains"> <a href="{% url "admin_domains" %}" {% if section == "domains" %}class="selected"{% endif %} title="Domains">
<i class="fa-solid fa-globe"></i> Domains <i class="fa-solid fa-globe"></i> Domains
</a> </a>
<a href="{% url "admin_federation" %}" {% if section == "federation" %}class="selected"{% endif %} title="Federation">
<i class="fa-solid fa-diagram-project"></i> Federation
</a>
<a href="{% url "admin_users" %}" {% if section == "users" %}class="selected"{% endif %} title="Users"> <a href="{% url "admin_users" %}" {% if section == "users" %}class="selected"{% endif %} title="Users">
<i class="fa-solid fa-users"></i> Users <i class="fa-solid fa-users"></i> Users
</a> </a>

View File

@ -9,12 +9,7 @@ from users.views.admin.domains import ( # noqa
Domains, Domains,
) )
from users.views.admin.federation import FederationEdit, FederationRoot # noqa from users.views.admin.federation import FederationEdit, FederationRoot # noqa
from users.views.admin.hashtags import ( # noqa from users.views.admin.hashtags import HashtagEdit, Hashtags # noqa
HashtagCreate,
HashtagDelete,
HashtagEdit,
Hashtags,
)
from users.views.admin.identities import IdentitiesRoot, IdentityEdit # noqa from users.views.admin.identities import IdentitiesRoot, IdentityEdit # noqa
from users.views.admin.invites import InviteCreate, InvitesRoot, InviteView # noqa from users.views.admin.invites import InviteCreate, InvitesRoot, InviteView # noqa
from users.views.admin.reports import ReportsRoot, ReportView # noqa from users.views.admin.reports import ReportsRoot, ReportView # noqa

View File

@ -4,11 +4,11 @@ from django.shortcuts import get_object_or_404, redirect
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.views.generic import FormView, ListView from django.views.generic import FormView, ListView
from users.decorators import moderator_required from users.decorators import admin_required
from users.models import Domain from users.models import Domain
@method_decorator(moderator_required, name="dispatch") @method_decorator(admin_required, name="dispatch")
class FederationRoot(ListView): class FederationRoot(ListView):
template_name = "admin/federation.html" template_name = "admin/federation.html"
@ -33,7 +33,7 @@ class FederationRoot(ListView):
return domains return domains
@method_decorator(moderator_required, name="dispatch") @method_decorator(admin_required, name="dispatch")
class FederationEdit(FormView): class FederationEdit(FormView):
template_name = "admin/federation_edit.html" template_name = "admin/federation_edit.html"

View File

@ -1,33 +1,38 @@
from django import forms from django import forms
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.utils.decorators import method_decorator 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 activities.models import Hashtag, HashtagStates
from users.decorators import moderator_required from users.decorators import moderator_required
@method_decorator(moderator_required, name="dispatch") @method_decorator(moderator_required, name="dispatch")
class Hashtags(TemplateView): class Hashtags(ListView):
template_name = "admin/hashtags.html" template_name = "admin/hashtags.html"
paginate_by = 30
def get_context_data(self): def get(self, request, *args, **kwargs):
return { self.extra_context = {
"hashtags": Hashtag.objects.filter().order_by("hashtag"), "section": "hashtags",
"section": "hashtag",
} }
return super().get(request, *args, **kwargs)
def get_queryset(self):
return Hashtag.objects.filter().order_by("hashtag")
@method_decorator(moderator_required, name="dispatch") @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"} extra_context = {"section": "hashtags"}
class form_class(forms.Form): class form_class(forms.Form):
hashtag = forms.SlugField( hashtag = forms.SlugField(
help_text="The hashtag without the '#'", help_text="The hashtag without the '#'",
disabled=True,
) )
name_override = forms.CharField( name_override = forms.CharField(
help_text="Optional - a more human readable hashtag.", help_text="Optional - a more human readable hashtag.",
@ -41,14 +46,6 @@ class HashtagCreate(FormView):
required=False, 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): def clean_name_override(self):
name_override = self.cleaned_data["name_override"] name_override = self.cleaned_data["name_override"]
if not name_override: if not name_override:
@ -59,29 +56,6 @@ class HashtagCreate(FormView):
) )
return self.cleaned_data["name_override"] 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): def dispatch(self, request, hashtag):
self.hashtag = get_object_or_404(Hashtag.objects, hashtag=hashtag) self.hashtag = get_object_or_404(Hashtag.objects, hashtag=hashtag)
return super().dispatch(request) return super().dispatch(request)
@ -96,7 +70,7 @@ class HashtagEdit(FormView):
self.hashtag.name_override = form.cleaned_data["name_override"] self.hashtag.name_override = form.cleaned_data["name_override"]
self.hashtag.save() self.hashtag.save()
Hashtag.transition_perform(self.hashtag, HashtagStates.outdated) Hashtag.transition_perform(self.hashtag, HashtagStates.outdated)
return redirect(Hashtag.urls.root) return redirect(Hashtag.urls.admin)
def get_initial(self): def get_initial(self):
return { return {
@ -104,23 +78,3 @@ class HashtagEdit(FormView):
"name_override": self.hashtag.name_override, "name_override": self.hashtag.name_override,
"public": self.hashtag.public, "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")