Add slug field

remotes/origin/hasaccess
Ivlev Denis 8 years ago
parent 34c22321f9
commit 52481c4bbb
  1. 10
      api/v1/serializers/course.py
  2. 19
      apps/course/migrations/0028_course_slug.py
  3. 19
      apps/course/migrations/0029_auto_20180209_0911.py
  4. 37
      apps/course/models.py

@ -65,13 +65,15 @@ class CategorySerializer(serializers.ModelSerializer):
class CourseCreateSerializer(serializers.ModelSerializer): class CourseCreateSerializer(serializers.ModelSerializer):
content = serializers.ListSerializer(child=ContentCreateSerializer()) slug = serializers.SlugField(allow_unicode=True, required=False)
materials = MaterialSerializer(many=True) content = serializers.ListSerializer(child=ContentCreateSerializer(), required=False)
materials = MaterialSerializer(many=True, required=False)
class Meta: class Meta:
model = Course model = Course
fields = ( fields = (
'id', 'id',
'slug',
'author', 'author',
'title', 'title',
'short_description', 'short_description',
@ -101,8 +103,8 @@ class CourseCreateSerializer(serializers.ModelSerializer):
) )
def create(self, validated_data): def create(self, validated_data):
materials = validated_data.pop('materials') materials = validated_data.pop('materials', [])
content = validated_data.pop('content') content = validated_data.pop('content', [])
course = super().create(validated_data) course = super().create(validated_data)

@ -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,
),
]

@ -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),
),
]

@ -1,6 +1,8 @@
import arrow import arrow
from uuid import uuid4
from django.db import models from django.db import models
from django.utils import timezone from django.utils import timezone
from django.utils.text import slugify
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.urls import reverse_lazy from django.urls import reverse_lazy
from polymorphic_tree.models import PolymorphicMPTTModel, PolymorphicTreeForeignKey from polymorphic_tree.models import PolymorphicMPTTModel, PolymorphicTreeForeignKey
@ -19,6 +21,10 @@ class Like(models.Model):
update_at = models.DateTimeField(auto_now=True) update_at = models.DateTimeField(auto_now=True)
def default_slug():
return str(uuid4())
class Course(models.Model): class Course(models.Model):
PENDING = 0 PENDING = 0
PUBLISHED = 1 PUBLISHED = 1
@ -28,10 +34,17 @@ class Course(models.Model):
(PUBLISHED, 'Published'), (PUBLISHED, 'Published'),
(ARCHIVED, 'Archived'), (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) title = models.CharField('Название курса', max_length=100, db_index=True)
short_description = models.TextField('Краткое описание курса', db_index=True) short_description = models.TextField(
from_author = models.TextField('От автора', default='', null=True, blank=True) 'Краткое описание курса', db_index=True)
from_author = models.TextField(
'От автора', default='', null=True, blank=True)
cover = models.ForeignKey( cover = models.ForeignKey(
ImageObject, related_name='course_covers', ImageObject, related_name='course_covers',
verbose_name='Обложка курса', on_delete=models.CASCADE, verbose_name='Обложка курса', on_delete=models.CASCADE,
@ -49,7 +62,8 @@ class Course(models.Model):
category = models.ForeignKey('Category', on_delete=models.PROTECT) category = models.ForeignKey('Category', on_delete=models.PROTECT)
duration = models.IntegerField('Продолжительность курса', default=0) duration = models.IntegerField('Продолжительность курса', default=0)
is_featured = models.BooleanField(default=False) 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) likes = models.ManyToManyField(Like, blank=True)
materials = models.ManyToManyField('Material', blank=True) materials = models.ManyToManyField('Material', blank=True)
gallery = models.ForeignKey( gallery = models.ForeignKey(
@ -60,6 +74,12 @@ class Course(models.Model):
created_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True)
update_at = models.DateTimeField(auto_now=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 @property
def url(self): def url(self):
return self.get_absolute_url() return self.get_absolute_url()
@ -114,7 +134,8 @@ class Category(models.Model):
class Lesson(models.Model): class Lesson(models.Model):
title = models.CharField('Название урока', max_length=100) title = models.CharField('Название урока', max_length=100)
short_description = models.TextField('Краткое описание урока') 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( cover = models.ForeignKey(
ImageObject, related_name='lesson_covers', ImageObject, related_name='lesson_covers',
verbose_name='Обложка урока', on_delete=models.CASCADE, verbose_name='Обложка урока', on_delete=models.CASCADE,
@ -179,7 +200,8 @@ class Comment(PolymorphicMPTTModel):
class CourseComment(Comment): 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): class Meta(Comment.Meta):
verbose_name = 'Комментарий курса' verbose_name = 'Комментарий курса'
@ -187,7 +209,8 @@ class CourseComment(Comment):
class LessonComment(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): class Meta(Comment.Meta):
verbose_name = 'Комментарий урока' verbose_name = 'Комментарий урока'

Loading…
Cancel
Save