You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
3.1 KiB
108 lines
3.1 KiB
|
|
import os
|
|
import hashlib
|
|
|
|
from django.contrib import admin
|
|
from django import forms
|
|
from ajaxdatatable.admin import AjaxDatatable
|
|
|
|
from utils.json_editor_field import JsonEditorWidget
|
|
from apps.course.models import Course, Glossary, Attachment, CourseCategory
|
|
from utils.schema import get_weekly_timing_schema, get_course_feature_schema
|
|
|
|
|
|
|
|
@admin.register(CourseCategory)
|
|
class CourseCategoryAdmin(admin.ModelAdmin):
|
|
list_display = ('name', 'slug')
|
|
search_fields = ('name',)
|
|
exclude = ('slug', )
|
|
|
|
|
|
|
|
class CourseForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Course
|
|
fields = '__all__'
|
|
# exclude = ('slug',)
|
|
widgets = {
|
|
'timing': JsonEditorWidget(attrs={'schema': get_weekly_timing_schema}),
|
|
'features': JsonEditorWidget(attrs={'schema': get_course_feature_schema}),
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Course)
|
|
class CourseAdmin(AjaxDatatable):
|
|
form = CourseForm
|
|
list_display = ('title', 'category', 'level', 'status', 'final_price', 'is_online')
|
|
list_filter = ('status', 'level', 'is_online', 'is_free', 'category')
|
|
search_fields = ('title', 'description')
|
|
exclude = ('slug', )
|
|
|
|
|
|
# @admin.display(description='Add Student')
|
|
# def _add_student(self, obj):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Glossary)
|
|
class GlossaryAdmin(admin.ModelAdmin):
|
|
list_display = ('title', 'course', 'description')
|
|
list_filter = ('course',)
|
|
search_fields = ('title', 'description', 'course__title')
|
|
ordering = ('-id',)
|
|
|
|
|
|
|
|
|
|
class AttachmentAdminForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Attachment
|
|
fields = '__all__'
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if 'file' in self.data or 'file' in self.files:
|
|
file = self.files.get('file')
|
|
if file:
|
|
file.name = self._shorten_file_name(file.name)
|
|
|
|
|
|
def _shorten_file_name(self, file_name):
|
|
max_length = 100
|
|
if len(file_name) > max_length:
|
|
base_name, ext = os.path.splitext(file_name) # جدا کردن نام و پسوند
|
|
allowed_length = max_length - len(ext) # طول مجاز نام بدون پسوند
|
|
|
|
# 80٪ از نام اصلی و 20٪ هش
|
|
base_length = int(allowed_length * 0.8) # 80٪ از طول مجاز
|
|
hash_length = allowed_length - base_length # 20٪ از طول مجاز
|
|
|
|
base_part = base_name[:base_length] # 80٪ اول نام اصلی
|
|
hash_part = hashlib.sha256(base_name.encode('utf-8')).hexdigest()[:hash_length] # 20٪ هش
|
|
|
|
return f"{base_part}{hash_part}{ext}" # ترکیب بخش اصلی و هش با پسوند
|
|
|
|
return file_name
|
|
|
|
|
|
@admin.register(Attachment)
|
|
class AttachmentAdmin(admin.ModelAdmin):
|
|
form = AttachmentAdminForm
|
|
list_display = ('title', 'course', 'file', 'file_size')
|
|
list_filter = ('course',)
|
|
search_fields = ('title', 'file', 'course__title')
|
|
|
|
def save_model(self, request, obj, form, change):
|
|
if obj.file:
|
|
obj.file_size = obj.file.size
|
|
super().save_model(request, obj, form, change)
|
|
|
|
|