From ad27a712a60cd6eb8f4861d9adbd30ae3b9af758 Mon Sep 17 00:00:00 2001 From: gzbender Date: Sun, 20 Jan 2019 22:11:56 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=83=20=D1=81?= =?UTF-8?q?=D1=81=D1=8B=D0=BB=D0=BE=D0=BA=20=D0=BD=D0=B0=20=D0=BA=D1=83?= =?UTF-8?q?=D1=80=D1=81=D1=8B=20=D0=B2=20=D0=B0=D0=B4=D0=BC=D0=B8=D0=BD?= =?UTF-8?q?=D0=BA=D0=B5,=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D1=82=D1=8C=20?= =?UTF-8?q?=D0=B2=D1=81=D0=B5=20=D1=81=D1=81=D1=8B=D0=BB=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BA=D1=83=D1=80=D1=81=D1=8B=20=D0=BF=D0=BE=20?= =?UTF-8?q?slug=20(=D0=BC=D0=B5=D0=BD=D0=B5=D0=B4=D0=B6=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D1=82=20=D0=BA=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=D0=BE=D0=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/course/management/__init__.py | 0 apps/course/management/commands/__init__.py | 0 .../commands/update_courses_slug.py | 20 +++++++++++++++++++ apps/course/models.py | 18 ++++++++--------- 4 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 apps/course/management/__init__.py create mode 100644 apps/course/management/commands/__init__.py create mode 100644 apps/course/management/commands/update_courses_slug.py diff --git a/apps/course/management/__init__.py b/apps/course/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/course/management/commands/__init__.py b/apps/course/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/course/management/commands/update_courses_slug.py b/apps/course/management/commands/update_courses_slug.py new file mode 100644 index 00000000..24999a2e --- /dev/null +++ b/apps/course/management/commands/update_courses_slug.py @@ -0,0 +1,20 @@ +from unidecode import unidecode + +from django.core.management.base import BaseCommand, CommandError +from django.db.models import Q +from django.utils.text import slugify +from apps.course.models import Course + + +class Command(BaseCommand): + help = 'Upload users to Roistat' + + def handle(self, *args, **options): + courses = Course.objects.filter(Q(slug__isnull=True) | Q(slug='')) + for course in courses: + course.slug = slugify(unidecode(course.title[:90])) + try: + course.save() + except: + course.slug += str(course.id) + course.save() diff --git a/apps/course/models.py b/apps/course/models.py index 42a1dd81..b11156f4 100644 --- a/apps/course/models.py +++ b/apps/course/models.py @@ -1,10 +1,13 @@ import arrow from random import shuffle from uuid import uuid4 +from unidecode import unidecode + from django.db import models from django.core.exceptions import ValidationError from django.utils import timezone from django.utils.timezone import now +from django.utils.text import slugify from django.contrib.auth import get_user_model from django.urls import reverse_lazy from django.conf import settings @@ -119,17 +122,14 @@ class Course(BaseModel, DeactivatedMixin): def __str__(self): return str(self.id) + ' ' + self.title - # def save(self, *args, **kwargs): - # if not self.slug: - # self.slug = slugify( - # self.title[:90], - # allow_unicode=True - # ) + def save(self, *args, **kwargs): + if not self.slug: + self.slug = slugify(unidecode(self.title[:90])) - # if Course.objects.filter(slug=self.slug).exclude(id=self.id).exists(): - # self.slug += '_' + str(uuid4())[:6] + if Course.objects.filter(slug=self.slug).exclude(id=self.id).exists(): + self.slug += str(uuid4())[-3:] - # return super().save() + return super().save() @property def url(self): From 81bcc7e56a91f3f2907356f0533f78c2814f7b03 Mon Sep 17 00:00:00 2001 From: gzbender Date: Mon, 21 Jan 2019 15:17:27 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=83=20=D1=81?= =?UTF-8?q?=D1=81=D1=8B=D0=BB=D0=BE=D0=BA=20=D0=BD=D0=B0=20=D0=BA=D1=83?= =?UTF-8?q?=D1=80=D1=81=D1=8B=20=D0=B2=20=D0=B0=D0=B4=D0=BC=D0=B8=D0=BD?= =?UTF-8?q?=D0=BA=D0=B5,=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D1=82=D1=8C=20?= =?UTF-8?q?=D0=B2=D1=81=D0=B5=20=D1=81=D1=81=D1=8B=D0=BB=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BA=D1=83=D1=80=D1=81=D1=8B=20=D0=BF=D0=BE=20?= =?UTF-8?q?slug=20(=D0=BC=D0=B5=D0=BD=D0=B5=D0=B4=D0=B6=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D1=82=20=D0=BA=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=D0=BE=D0=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 6107112a..78736651 100644 --- a/requirements.txt +++ b/requirements.txt @@ -36,3 +36,4 @@ short_url sendgrid drf_dynamic_fields flower==0.9.2 +unidecode