Skip to content

Commit

Permalink
Merge pull request #174 from weaverse-techtide/164-curriculumcourse-개선
Browse files Browse the repository at this point in the history
164 curriculumcourse 개선
  • Loading branch information
AlbertImKr authored Oct 13, 2024
2 parents 3964013 + 28416ae commit 4e0586c
Show file tree
Hide file tree
Showing 27 changed files with 719 additions and 311 deletions.
5 changes: 5 additions & 0 deletions accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,8 @@ class CustomUser(AbstractUser):

def __str__(self):
return self.email

def get_image_url(self):
if getattr(self, "image", None):
return self.image.image_url
return "https://paullab.co.kr/images/weniv-licat.png"
2 changes: 2 additions & 0 deletions courses/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .models import (
Assignment,
Course,
Curriculum,
Lecture,
MultipleChoiceQuestion,
MultipleChoiceQuestionChoice,
Expand All @@ -17,3 +18,4 @@
admin.site.register(Assignment)
admin.site.register(MultipleChoiceQuestion)
admin.site.register(MultipleChoiceQuestionChoice)
admin.site.register(Curriculum)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 5.1.1 on 2024-10-10 02:02

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('courses', '0007_alter_course_category'),
]

operations = [
migrations.RenameField(
model_name='course',
old_name='course_level',
new_name='skill_level',
),
migrations.AddField(
model_name='curriculum',
name='category',
field=models.CharField(choices=[('JavaScript', 'JavaScript'), ('Python', 'Python'), ('Django', 'Django'), ('React', 'React'), ('Vue', 'Vue'), ('Node', 'Node'), ('AWS', 'AWS'), ('Docker', 'Docker'), ('DB', 'DB')], default='JavaScript', max_length=255, verbose_name='카테고리'),
),
migrations.AddField(
model_name='curriculum',
name='skill_level',
field=models.CharField(choices=[('beginner', '초급'), ('intermediate', '중급'), ('advanced', '고급')], default='beginner', max_length=255, verbose_name='난이도'),
),
]
28 changes: 28 additions & 0 deletions courses/migrations/0009_course_author_curriculum_author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 5.1.1 on 2024-10-10 02:31

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('courses', '0008_rename_course_level_course_skill_level_and_more'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AddField(
model_name='course',
name='author',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='courses', to=settings.AUTH_USER_MODEL, verbose_name='작성자'),
preserve_default=False,
),
migrations.AddField(
model_name='curriculum',
name='author',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='curriculums', to=settings.AUTH_USER_MODEL, verbose_name='작성자'),
preserve_default=False,
),
]
17 changes: 17 additions & 0 deletions courses/migrations/0010_remove_topic_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.1.1 on 2024-10-13 08:53

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('courses', '0009_course_author_curriculum_author'),
]

operations = [
migrations.RemoveField(
model_name='topic',
name='description',
),
]
24 changes: 17 additions & 7 deletions courses/mixins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.db import transaction

from materials.models import Image, Video

from .models import (
Assignment,
Course,
Expand All @@ -16,12 +18,14 @@ class CourseMixin:
"""

@transaction.atomic
def create_course_with_lectures_and_topics(self, course_data, lectures_data):
def create_course_with_lectures_and_topics(
self, course_data, lectures_data, author
):
"""
course 및 하위 모델 lecture, topic, assignment, quiz 등을 함께 생성합니다.
"""

course = self._create_course(course_data)
course = self._create_course(course_data, author)
for lecture_data in lectures_data:
lecture = self._create_lecture(lecture_data, course)
for topic_data in lecture_data.get("topics", []):
Expand All @@ -46,19 +50,24 @@ def update_course_with_lectures_and_topics(
topic = self._create_topic(topic_data, lecture)
self._handle_topic_type(topic, topic_data)

def _create_course(self, course_data):
def _create_course(self, course_data, author):
"""
course 인스턴스를 생성합니다.
"""

return Course.objects.create(
course = Course.objects.create(
title=course_data.get("title"),
short_description=course_data.get("short_description"),
description=course_data.get("description"),
category=course_data.get("category"),
course_level=course_data.get("course_level"),
skill_level=course_data.get("skill_level"),
price=course_data.get("price"),
author=author,
)
Image.objects.filter(id=course_data.get("thumbnail_id")).update(course=course)
Video.objects.filter(id=course_data.get("video_id")).update(course=course)

return course

def _create_lecture(self, lecture_data, course):
"""
Expand All @@ -76,14 +85,15 @@ def _create_topic(self, topic_data, lecture):
topic 인스턴스를 생성합니다.
"""

return Topic.objects.create(
topic = Topic.objects.create(
lecture=lecture,
title=topic_data.get("title"),
type=topic_data.get("type"),
description=topic_data.get("description"),
order=topic_data.get("order"),
is_premium=topic_data.get("is_premium"),
)
Video.objects.filter(id=topic_data.get("video_id")).update(topic=topic)
return topic

def _handle_topic_type(self, topic, topic_data):
"""
Expand Down
57 changes: 49 additions & 8 deletions courses/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.db import models


Expand All @@ -6,9 +7,44 @@ class Curriculum(models.Model):
커리큘럼 모델입니다.
"""

category_choices = [
("JavaScript", "JavaScript"),
("Python", "Python"),
("Django", "Django"),
("React", "React"),
("Vue", "Vue"),
("Node", "Node"),
("AWS", "AWS"),
("Docker", "Docker"),
("DB", "DB"),
]
skill_level_choices = [
("beginner", "초급"),
("intermediate", "중급"),
("advanced", "고급"),
]

author = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="curriculums",
verbose_name="작성자",
)
name = models.CharField(max_length=255, verbose_name="커리큘럼 이름")
description = models.TextField(verbose_name="설명")
price = models.PositiveIntegerField(verbose_name="가격")
category = models.CharField(
max_length=255,
verbose_name="카테고리",
choices=category_choices,
default="JavaScript",
)
skill_level = models.CharField(
max_length=255,
verbose_name="난이도",
choices=skill_level_choices,
default="beginner",
)
created_at = models.DateTimeField(auto_now_add=True, verbose_name="생성일")
updated_at = models.DateTimeField(auto_now=True, verbose_name="수정일")

Expand Down Expand Up @@ -37,7 +73,7 @@ class Course(models.Model):
("Docker", "Docker"),
("DB", "DB"),
]
course_level_choices = [
skill_level_choices = [
("beginner", "초급"),
("intermediate", "중급"),
("advanced", "고급"),
Expand All @@ -51,6 +87,12 @@ class Course(models.Model):
related_name="courses",
verbose_name="커리큘럼",
)
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="courses",
verbose_name="작성자",
)
title = models.CharField(max_length=255, verbose_name="코스 제목")
short_description = models.TextField(verbose_name="간단한 설명")
description = models.JSONField(verbose_name="설명")
Expand All @@ -60,10 +102,10 @@ class Course(models.Model):
choices=category_choices,
default="JavaScript",
)
course_level = models.CharField(
skill_level = models.CharField(
max_length=255,
verbose_name="난이도",
choices=course_level_choices,
choices=skill_level_choices,
default="beginner",
)
price = models.PositiveIntegerField(verbose_name="가격")
Expand All @@ -83,7 +125,7 @@ def update(self, **kwargs):
- short_description: 간단한 설명
- description: 설명
- category: 카테고리
- course_level: 난이도
- skill_level: 난이도
- price: 가격
"""
for key, value in kwargs.items():
Expand All @@ -92,7 +134,7 @@ def update(self, **kwargs):
"short_description",
"description",
"category",
"course_level",
"skill_level",
"price",
]:
continue
Expand Down Expand Up @@ -122,7 +164,7 @@ class Lecture(models.Model):
updated_at = models.DateTimeField(auto_now=True, verbose_name="수정일")

def __str__(self):
return f"{self.course.title} - {self.title}"
return f"{self.id} - {self.title}"

class Meta:
ordering = ["order"]
Expand Down Expand Up @@ -152,14 +194,13 @@ class Topic(models.Model):
choices=topic_type_choices,
default="video",
)
description = models.TextField(verbose_name="설명")
order = models.PositiveIntegerField(verbose_name="순서")
is_premium = models.BooleanField(verbose_name="프리미엄 여부", default=False)
created_at = models.DateTimeField(auto_now_add=True, verbose_name="생성일")
updated_at = models.DateTimeField(auto_now=True, verbose_name="수정일")

def __str__(self):
return f"{self.lecture.title} - {self.title}"
return f"{self.id} - {self.title}"

class Meta:
ordering = ["order"]
Expand Down
Loading

0 comments on commit 4e0586c

Please sign in to comment.