From c866fdd6baf8008b14d5e8ffea1aee177aca9e9b Mon Sep 17 00:00:00 2001 From: Markos Gogoulos Date: Tue, 13 Jun 2023 15:41:13 +0300 Subject: [PATCH] allow tags to contain chars, not only English alphabet (#801) * allow tags to contain chars, not only English alphabet --- files/helpers.py | 8 ++++++++ files/models.py | 7 ++----- files/views.py | 6 +++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/files/helpers.py b/files/helpers.py index 5726df2..948c525 100644 --- a/files/helpers.py +++ b/files/helpers.py @@ -785,3 +785,11 @@ def clean_query(query): query = query.replace(char, "") return query.lower() + + +def get_alphanumeric_only(string): + """Returns a query that contains only alphanumeric characters + This include characters other than the English alphabet too + """ + string = "".join([char for char in string if char.isalnum()]) + return string.lower() diff --git a/files/models.py b/files/models.py index acd38ce..c0bd95f 100644 --- a/files/models.py +++ b/files/models.py @@ -16,7 +16,6 @@ from django.core.files import File from django.db import connection, models from django.db.models.signals import m2m_changed, post_delete, post_save, pre_delete from django.dispatch import receiver -from django.template.defaultfilters import slugify from django.urls import reverse from django.utils import timezone from django.utils.html import strip_tags @@ -1000,10 +999,8 @@ class Tag(models.Model): return True def save(self, *args, **kwargs): - self.title = slugify(self.title[:99]) - strip_text_items = ["title"] - for item in strip_text_items: - setattr(self, item, strip_tags(getattr(self, item, None))) + self.title = helpers.get_alphanumeric_only(self.title) + self.title = self.title[:99] super(Tag, self).save(*args, **kwargs) @property diff --git a/files/views.py b/files/views.py index b3f2cc8..ae08c41 100644 --- a/files/views.py +++ b/files/views.py @@ -9,7 +9,6 @@ from django.core.mail import EmailMessage from django.db.models import Q from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404, render -from django.template.defaultfilters import slugify from drf_yasg import openapi as openapi from drf_yasg.utils import swagger_auto_schema from rest_framework import permissions, status @@ -30,7 +29,7 @@ from cms.permissions import IsAuthorizedToAdd, IsUserOrEditor, user_allowed_to_u from users.models import User from .forms import ContactForm, MediaForm, SubtitleForm -from .helpers import clean_query, produce_ffmpeg_commands +from .helpers import clean_query, get_alphanumeric_only, produce_ffmpeg_commands from .methods import ( check_comment_for_mention, get_user_or_session, @@ -182,7 +181,8 @@ def edit_media(request): media.tags.remove(tag) if form.cleaned_data.get("new_tags"): for tag in form.cleaned_data.get("new_tags").split(","): - tag = slugify(tag) + tag = get_alphanumeric_only(tag) + tag = tag[:99] if tag: try: tag = Tag.objects.get(title=tag)