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(
|
|
|
|
help_text="The emoji image\nShould be at least 40 x 40 pixels, and under 50kb",
|
|
|
|
)
|
|
|
|
|
|
|
|
def clean_image(self):
|
|
|
|
data = self.cleaned_data["image"]
|
|
|
|
if data.size > settings.SETUP.EMOJI_MAX_IMAGE_FILESIZE_KB:
|
|
|
|
raise forms.ValidationError("Image filesize is too large")
|
|
|
|
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()
|