init
This commit is contained in:
1
course/__init__.py
Normal file
1
course/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
|
BIN
course/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
course/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
BIN
course/__pycache__/admin.cpython-313.pyc
Normal file
BIN
course/__pycache__/admin.cpython-313.pyc
Normal file
Binary file not shown.
BIN
course/__pycache__/apps.cpython-313.pyc
Normal file
BIN
course/__pycache__/apps.cpython-313.pyc
Normal file
Binary file not shown.
BIN
course/__pycache__/forms.cpython-313.pyc
Normal file
BIN
course/__pycache__/forms.cpython-313.pyc
Normal file
Binary file not shown.
BIN
course/__pycache__/models.cpython-313.pyc
Normal file
BIN
course/__pycache__/models.cpython-313.pyc
Normal file
Binary file not shown.
BIN
course/__pycache__/test_views.cpython-313.pyc
Normal file
BIN
course/__pycache__/test_views.cpython-313.pyc
Normal file
Binary file not shown.
BIN
course/__pycache__/tests.cpython-313.pyc
Normal file
BIN
course/__pycache__/tests.cpython-313.pyc
Normal file
Binary file not shown.
BIN
course/__pycache__/urls.cpython-313.pyc
Normal file
BIN
course/__pycache__/urls.cpython-313.pyc
Normal file
Binary file not shown.
BIN
course/__pycache__/views.cpython-313.pyc
Normal file
BIN
course/__pycache__/views.cpython-313.pyc
Normal file
Binary file not shown.
9
course/admin.py
Normal file
9
course/admin.py
Normal file
@ -0,0 +1,9 @@
|
||||
from django.contrib import admin
|
||||
from .models import Course
|
||||
|
||||
@admin.register(Course)
|
||||
class CourseAdmin(admin.ModelAdmin):
|
||||
list_display = ('title', 'description', 'created_at', 'updated_at')
|
||||
list_filter = ('created_at', 'updated_at')
|
||||
search_fields = ('title', 'description')
|
||||
date_hierarchy = 'created_at'
|
5
course/apps.py
Normal file
5
course/apps.py
Normal file
@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
class CourseConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'course'
|
11
course/forms.py
Normal file
11
course/forms.py
Normal file
@ -0,0 +1,11 @@
|
||||
from django import forms
|
||||
from .models import Course
|
||||
|
||||
class CourseForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Course
|
||||
fields = ['title', 'description']
|
||||
widgets = {
|
||||
'title': forms.TextInput(attrs={'class': 'form-control'}),
|
||||
'description': forms.Textarea(attrs={'class': 'form-control', 'rows': 4}),
|
||||
}
|
24
course/migrations/0001_initial.py
Normal file
24
course/migrations/0001_initial.py
Normal file
@ -0,0 +1,24 @@
|
||||
# Generated by Django 5.0.2 on 2025-06-10 19:27
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Course',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=255)),
|
||||
('description', models.TextField(blank=True)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
],
|
||||
),
|
||||
]
|
1
course/migrations/__init__.py
Normal file
1
course/migrations/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
|
BIN
course/migrations/__pycache__/0001_initial.cpython-313.pyc
Normal file
BIN
course/migrations/__pycache__/0001_initial.cpython-313.pyc
Normal file
Binary file not shown.
BIN
course/migrations/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
course/migrations/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
10
course/models.py
Normal file
10
course/models.py
Normal file
@ -0,0 +1,10 @@
|
||||
from django.db import models
|
||||
|
||||
class Course(models.Model):
|
||||
title = models.CharField(max_length=255)
|
||||
description = models.TextField(blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
10
course/tests.py
Normal file
10
course/tests.py
Normal file
@ -0,0 +1,10 @@
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
from .models import Course
|
||||
|
||||
class CourseModelTest(TestCase):
|
||||
def setUp(self):
|
||||
self.course = Course.objects.create(
|
||||
title="Тестовый курс",
|
||||
description="Описание тестового курса"
|
||||
)
|
12
course/urls.py
Normal file
12
course/urls.py
Normal file
@ -0,0 +1,12 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
app_name = 'course'
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.course_list, name='list'),
|
||||
path('create/', views.create_course, name='create'),
|
||||
path('<int:pk>/', views.course_detail, name='detail'),
|
||||
path('<int:pk>/update/', views.update_course, name='update'),
|
||||
path('<int:pk>/delete/', views.delete_course, name='delete'),
|
||||
]
|
65
course/views.py
Normal file
65
course/views.py
Normal file
@ -0,0 +1,65 @@
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib import messages
|
||||
from .models import Course
|
||||
from .forms import CourseForm
|
||||
|
||||
def course_list(request):
|
||||
courses = Course.objects.all().order_by('title')
|
||||
return render(request, 'course/course_list.html', {'courses': courses})
|
||||
|
||||
@login_required
|
||||
def course_detail(request, pk):
|
||||
course = get_object_or_404(Course, pk=pk)
|
||||
return render(request, 'course/course_detail.html', {'course': course})
|
||||
|
||||
@login_required
|
||||
def create_course(request):
|
||||
if not hasattr(request.user, 'instructor'):
|
||||
messages.error(request, 'У вас нет прав для создания курсов.')
|
||||
return redirect('course:list')
|
||||
|
||||
if request.method == 'POST':
|
||||
form = CourseForm(request.POST)
|
||||
if form.is_valid():
|
||||
course = form.save()
|
||||
messages.success(request, 'Курс успешно создан.')
|
||||
return redirect('course:list')
|
||||
else:
|
||||
form = CourseForm()
|
||||
|
||||
return render(request, 'course/course_form.html', {'form': form, 'action': 'create'})
|
||||
|
||||
@login_required
|
||||
def update_course(request, pk):
|
||||
course = get_object_or_404(Course, pk=pk)
|
||||
|
||||
if not hasattr(request.user, 'instructor'):
|
||||
messages.error(request, 'У вас нет прав для редактирования курсов.')
|
||||
return redirect('course:list')
|
||||
|
||||
if request.method == 'POST':
|
||||
form = CourseForm(request.POST, instance=course)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
messages.success(request, 'Курс успешно обновлен.')
|
||||
return redirect('course:list')
|
||||
else:
|
||||
form = CourseForm(instance=course)
|
||||
|
||||
return render(request, 'course/course_form.html', {'form': form, 'action': 'update'})
|
||||
|
||||
@login_required
|
||||
def delete_course(request, pk):
|
||||
course = get_object_or_404(Course, pk=pk)
|
||||
|
||||
if not hasattr(request.user, 'instructor'):
|
||||
messages.error(request, 'У вас нет прав для удаления курсов.')
|
||||
return redirect('course:list')
|
||||
|
||||
if request.method == 'POST':
|
||||
course.delete()
|
||||
messages.success(request, 'Курс успешно удален.')
|
||||
return redirect('course:list')
|
||||
|
||||
return render(request, 'course/course_confirm_delete.html', {'course': course})
|
Reference in New Issue
Block a user