2023-01-10 19:31:50 -08:00
|
|
|
from django import forms
|
|
|
|
from django.conf import settings
|
|
|
|
from django.db import models
|
2023-01-13 14:54:21 -08:00
|
|
|
from django.shortcuts import redirect
|
2023-01-10 19:31:50 -08:00
|
|
|
from django.utils.decorators import method_decorator
|
2023-01-13 14:54:21 -08:00
|
|
|
from django.views.generic import FormView, ListView
|
2023-01-10 19:31:50 -08:00
|
|
|
|
|
|
|
from activities.models import Emoji
|
|
|
|
from users.decorators import moderator_required
|
2023-01-13 14:54:21 -08:00
|
|
|
from users.views.admin.generic import HTMXActionView
|
2023-01-10 19:31:50 -08:00
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(moderator_required, name="dispatch")
|
|
|
|
class EmojiRoot(ListView):
|
|
|
|
|
|
|
|
template_name = "admin/emoji.html"
|
|
|
|
paginate_by = 50
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.query = request.GET.get("query")
|
|
|
|
self.local_only = request.GET.get("local_only")
|
|
|
|
self.extra_context = {
|
|
|
|
"section": "emoji",
|
|
|
|
"query": self.query or "",
|
|
|
|
"local_only": self.local_only,
|
|
|
|
}
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = Emoji.objects.filter().order_by("shortcode", "domain_id")
|
|
|
|
if self.local_only:
|
|
|
|
queryset = queryset.filter(local=True)
|
|
|
|
if self.query:
|
|
|
|
query = self.query.lower().strip().lstrip("@")
|
|
|
|
queryset = queryset.filter(
|
|
|
|
models.Q(shortcode__icontains=query) | models.Q(domain_id=query)
|
|
|
|
)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(moderator_required, name="dispatch")
|
|
|
|
class EmojiCreate(FormView):
|
|
|
|
|
|
|
|
template_name = "admin/emoji_create.html"
|
|
|
|
extra_context = {"section": "emoji"}
|
|
|
|
|
|
|
|
class form_class(forms.Form):
|
|
|
|
shortcode = forms.SlugField(
|
|
|
|
help_text="What users type to use the emoji :likethis:",
|
|
|
|
)
|
|
|
|
image = forms.ImageField(
|
2023-01-14 09:33:36 -08:00
|
|
|
help_text=f"The emoji image\nShould be at least 40 x 40 pixels, and under {settings.SETUP.EMOJI_MAX_IMAGE_FILESIZE_KB}KB",
|
2023-01-10 19:31:50 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
def clean_image(self):
|
|
|
|
data = self.cleaned_data["image"]
|
2023-01-14 09:33:36 -08:00
|
|
|
if data.size > settings.SETUP.EMOJI_MAX_IMAGE_FILESIZE_KB * 1024:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
f"Image filesize is too large (actual: {data.size // 1024}KB)"
|
|
|
|
)
|
|
|
|
if data.image.width < 40 or data.image.height < 40:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
f"Image should at least 40 x 40 pixels (actual: {data.image.width} x {data.image.height} pixels)"
|
|
|
|
)
|
2023-01-10 19:31:50 -08:00
|
|
|
return data
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
Emoji.objects.create(
|
|
|
|
shortcode=form.cleaned_data["shortcode"],
|
|
|
|
file=form.cleaned_data["image"],
|
|
|
|
mimetype=form.cleaned_data["image"].image.get_format_mimetype(),
|
|
|
|
local=True,
|
|
|
|
public=True,
|
|
|
|
)
|
|
|
|
return redirect(Emoji.urls.admin)
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(moderator_required, name="dispatch")
|
2023-01-13 14:54:21 -08:00
|
|
|
class EmojiDelete(HTMXActionView):
|
2023-01-10 19:31:50 -08:00
|
|
|
"""
|
|
|
|
Deletes an emoji
|
|
|
|
"""
|
|
|
|
|
2023-01-13 14:54:21 -08:00
|
|
|
model = Emoji
|
|
|
|
|
|
|
|
def action(self, emoji: Emoji):
|
|
|
|
emoji.delete()
|
2023-01-10 19:31:50 -08:00
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(moderator_required, name="dispatch")
|
2023-01-13 14:54:21 -08:00
|
|
|
class EmojiEnable(HTMXActionView):
|
2023-01-10 19:31:50 -08:00
|
|
|
"""
|
|
|
|
Sets an emoji to be enabled (or not!)
|
|
|
|
"""
|
|
|
|
|
2023-01-13 14:54:21 -08:00
|
|
|
model = Emoji
|
2023-01-10 19:31:50 -08:00
|
|
|
enable = True
|
|
|
|
|
2023-01-13 14:54:21 -08:00
|
|
|
def action(self, emoji: Emoji):
|
|
|
|
emoji.public = self.enable
|
|
|
|
emoji.save()
|
2023-01-14 09:35:20 -08:00
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(moderator_required, name="dispatch")
|
|
|
|
class EmojiCopyLocal(HTMXActionView):
|
|
|
|
"""
|
|
|
|
Duplicates a domain emoji to be local, as long as it is usable and the
|
|
|
|
shortcode is available.
|
|
|
|
"""
|
|
|
|
|
|
|
|
model = Emoji
|
|
|
|
|
|
|
|
def action(self, emoji: Emoji):
|
|
|
|
# Force reload locals cache to avoid potential for shortcode dupes
|
|
|
|
Emoji.locals = Emoji.load_locals()
|
|
|
|
emoji.copy_to_local()
|