diff --git a/api/v1/serializers/course.py b/api/v1/serializers/course.py index a91c3032..f04b81ac 100644 --- a/api/v1/serializers/course.py +++ b/api/v1/serializers/course.py @@ -65,13 +65,15 @@ class CategorySerializer(serializers.ModelSerializer): class CourseCreateSerializer(serializers.ModelSerializer): - content = serializers.ListSerializer(child=ContentCreateSerializer()) - materials = MaterialSerializer(many=True) + slug = serializers.SlugField(allow_unicode=True, required=False) + content = serializers.ListSerializer(child=ContentCreateSerializer(), required=False) + materials = MaterialSerializer(many=True, required=False) class Meta: model = Course fields = ( 'id', + 'slug', 'author', 'title', 'short_description', @@ -101,8 +103,8 @@ class CourseCreateSerializer(serializers.ModelSerializer): ) def create(self, validated_data): - materials = validated_data.pop('materials') - content = validated_data.pop('content') + materials = validated_data.pop('materials', []) + content = validated_data.pop('content', []) course = super().create(validated_data) diff --git a/apps/course/migrations/0028_course_slug.py b/apps/course/migrations/0028_course_slug.py new file mode 100644 index 00000000..f9e1a3ce --- /dev/null +++ b/apps/course/migrations/0028_course_slug.py @@ -0,0 +1,19 @@ +# Generated by Django 2.0.2 on 2018-02-09 08:59 +from uuid import uuid4 +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('course', '0027_remove_course_url'), + ] + + operations = [ + migrations.AddField( + model_name='course', + name='slug', + field=models.SlugField(allow_unicode=True, default=str(uuid4()), max_length=100, unique=True), + preserve_default=False, + ), + ] diff --git a/apps/course/migrations/0029_auto_20180209_0911.py b/apps/course/migrations/0029_auto_20180209_0911.py new file mode 100644 index 00000000..daff2a0c --- /dev/null +++ b/apps/course/migrations/0029_auto_20180209_0911.py @@ -0,0 +1,19 @@ +# Generated by Django 2.0.2 on 2018-02-09 09:11 + +import apps.course.models +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('course', '0028_course_slug'), + ] + + operations = [ + migrations.AlterField( + model_name='course', + name='slug', + field=models.SlugField(allow_unicode=True, default=apps.course.models.default_slug, max_length=100, unique=True), + ), + ] diff --git a/apps/course/models.py b/apps/course/models.py index 10295cd3..b03092b6 100644 --- a/apps/course/models.py +++ b/apps/course/models.py @@ -1,6 +1,8 @@ import arrow +from uuid import uuid4 from django.db import models from django.utils import timezone +from django.utils.text import slugify from django.contrib.auth import get_user_model from django.urls import reverse_lazy from polymorphic_tree.models import PolymorphicMPTTModel, PolymorphicTreeForeignKey @@ -19,6 +21,10 @@ class Like(models.Model): update_at = models.DateTimeField(auto_now=True) +def default_slug(): + return str(uuid4()) + + class Course(models.Model): PENDING = 0 PUBLISHED = 1 @@ -28,10 +34,17 @@ class Course(models.Model): (PUBLISHED, 'Published'), (ARCHIVED, 'Archived'), ) - author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True) + slug = models.SlugField( + allow_unicode=True, + max_length=100, unique=True, db_index=True, + ) + author = models.ForeignKey( + User, on_delete=models.SET_NULL, null=True, blank=True) title = models.CharField('Название курса', max_length=100, db_index=True) - short_description = models.TextField('Краткое описание курса', db_index=True) - from_author = models.TextField('От автора', default='', null=True, blank=True) + short_description = models.TextField( + 'Краткое описание курса', db_index=True) + from_author = models.TextField( + 'От автора', default='', null=True, blank=True) cover = models.ForeignKey( ImageObject, related_name='course_covers', verbose_name='Обложка курса', on_delete=models.CASCADE, @@ -49,7 +62,8 @@ class Course(models.Model): category = models.ForeignKey('Category', on_delete=models.PROTECT) duration = models.IntegerField('Продолжительность курса', default=0) is_featured = models.BooleanField(default=False) - status = models.PositiveSmallIntegerField('Статус', default=0, choices=STATUS_CHOICES) + status = models.PositiveSmallIntegerField( + 'Статус', default=0, choices=STATUS_CHOICES) likes = models.ManyToManyField(Like, blank=True) materials = models.ManyToManyField('Material', blank=True) gallery = models.ForeignKey( @@ -60,6 +74,12 @@ class Course(models.Model): created_at = models.DateTimeField(auto_now_add=True) update_at = models.DateTimeField(auto_now=True) + def save(self, *args, **kwargs): + print(self.title) + self.slug = slugify(self.title[:100] + '_' + str(uuid4())[:6], allow_unicode=True) + print(self.slug) + return super().save() + @property def url(self): return self.get_absolute_url() @@ -114,7 +134,8 @@ class Category(models.Model): class Lesson(models.Model): title = models.CharField('Название урока', max_length=100) short_description = models.TextField('Краткое описание урока') - course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='lessons') + course = models.ForeignKey( + Course, on_delete=models.CASCADE, related_name='lessons') cover = models.ForeignKey( ImageObject, related_name='lesson_covers', verbose_name='Обложка урока', on_delete=models.CASCADE, @@ -179,7 +200,8 @@ class Comment(PolymorphicMPTTModel): class CourseComment(Comment): - course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='comments') + course = models.ForeignKey( + Course, on_delete=models.CASCADE, related_name='comments') class Meta(Comment.Meta): verbose_name = 'Комментарий курса' @@ -187,7 +209,8 @@ class CourseComment(Comment): class LessonComment(Comment): - lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE, related_name='comments') + lesson = models.ForeignKey( + Lesson, on_delete=models.CASCADE, related_name='comments') class Meta(Comment.Meta): verbose_name = 'Комментарий урока'