|
|
@ -23,6 +23,7 @@ from apps.course.models import Participant |
|
|
# Import Admin Sites from utils |
|
|
# Import Admin Sites from utils |
|
|
from utils.admin import project_admin_site, dovoodi_admin_site , is_dovoodi_panel |
|
|
from utils.admin import project_admin_site, dovoodi_admin_site , is_dovoodi_panel |
|
|
from apps.account.admin.location import LocationHistoryInline |
|
|
from apps.account.admin.location import LocationHistoryInline |
|
|
|
|
|
from unfold.widgets import UnfoldAdminSelectWidget |
|
|
|
|
|
|
|
|
# ========================================================= |
|
|
# ========================================================= |
|
|
# 1. Base User Admin (Logic Shared by all User types) |
|
|
# 1. Base User Admin (Logic Shared by all User types) |
|
|
@ -330,16 +331,81 @@ class CourseTableSection(TableSection): |
|
|
) |
|
|
) |
|
|
edit_link.short_description = _("Edit") |
|
|
edit_link.short_description = _("Edit") |
|
|
|
|
|
|
|
|
|
|
|
class ProfessorUpgradeForm(forms.ModelForm): |
|
|
|
|
|
existing_user = forms.ModelChoiceField( |
|
|
|
|
|
queryset=User.objects.exclude(groups__name="Professor Group"), |
|
|
|
|
|
required=True, |
|
|
|
|
|
label=_("Select Existing User"), |
|
|
|
|
|
help_text=_("Choose an existing user to upgrade to Professor."), |
|
|
|
|
|
widget=UnfoldAdminSelectWidget, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
class Meta: |
|
|
|
|
|
model = ProfessorUser |
|
|
|
|
|
fields = ("existing_user", "is_active", "is_staff", "is_superuser", "groups") |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
|
|
super().__init__(*args, **kwargs) |
|
|
|
|
|
if 'groups' in self.fields: |
|
|
|
|
|
self.fields['groups'].required = False |
|
|
|
|
|
|
|
|
|
|
|
def _post_clean(self): |
|
|
|
|
|
# جلوگیری از اعتبارسنجی مدل خالی برای جلوگیری از ارور فیلدهای اجباری |
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
def save(self, commit=True): |
|
|
|
|
|
# کاربر موجود (که هنوز پروفسور نیست) را میگیریم |
|
|
|
|
|
user = self.cleaned_data.get('existing_user') |
|
|
|
|
|
|
|
|
|
|
|
# ابتدا user_type را تغییر میدهیم تا با Manager پروفسور سازگار شود |
|
|
|
|
|
user.user_type = User.UserType.PROFESSOR |
|
|
|
|
|
user.is_active = self.cleaned_data.get('is_active', user.is_active) |
|
|
|
|
|
user.is_staff = self.cleaned_data.get('is_staff', user.is_staff) |
|
|
|
|
|
user.is_superuser = self.cleaned_data.get('is_superuser', user.is_superuser) |
|
|
|
|
|
user.save() # ذخیره با مدل User |
|
|
|
|
|
|
|
|
|
|
|
# حالا که user_type آپدیت شد، میتوانیم آن را به عنوان ProfessorUser واکشی کنیم |
|
|
|
|
|
prof_user = ProfessorUser.objects.get(pk=user.pk) |
|
|
|
|
|
|
|
|
|
|
|
# برای ذخیرهسازی ManyToMany (مثل groups)، باید instance فرم ست شود |
|
|
|
|
|
self.instance = prof_user |
|
|
|
|
|
|
|
|
|
|
|
def save_m2m(): |
|
|
|
|
|
groups = self.cleaned_data.get('groups') |
|
|
|
|
|
if groups is not None: |
|
|
|
|
|
self.instance.groups.set(groups) |
|
|
|
|
|
# اضافهکردن کاربر به گروه پروفسورها و ساخت اسلاگ (در صورت نیاز) |
|
|
|
|
|
self.instance.ensure_professor_profile(commit=True) |
|
|
|
|
|
|
|
|
|
|
|
self.save_m2m = save_m2m |
|
|
|
|
|
|
|
|
|
|
|
if commit: |
|
|
|
|
|
self.save_m2m() |
|
|
|
|
|
|
|
|
|
|
|
return prof_user |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProfessorUserAdmin(UserAdmin): |
|
|
class ProfessorUserAdmin(UserAdmin): |
|
|
form = UserAdminChangeForm |
|
|
form = UserAdminChangeForm |
|
|
add_form = UserAdminCreationForm |
|
|
|
|
|
|
|
|
add_form = ProfessorUpgradeForm # <--- آپدیت شد به فرم ارتقا |
|
|
list_display = ('display_header', 'email', 'courses_count') |
|
|
list_display = ('display_header', 'email', 'courses_count') |
|
|
list_sections = [CourseTableSection] |
|
|
list_sections = [CourseTableSection] |
|
|
save_as = True |
|
|
save_as = True |
|
|
|
|
|
|
|
|
|
|
|
# بازنویسی کامل فیلدستهای صفحه Add (ساخت) |
|
|
|
|
|
add_fieldsets = ( |
|
|
|
|
|
(None, { |
|
|
|
|
|
'classes': ('wide',), |
|
|
|
|
|
'fields': ('existing_user',), |
|
|
|
|
|
}), |
|
|
|
|
|
(_('Permissions'), { |
|
|
|
|
|
'fields': ('is_active', 'is_staff', 'is_superuser', 'groups'), |
|
|
|
|
|
'classes': ('wide',), |
|
|
|
|
|
}), |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
@display(description=_("Professor"), header=True) |
|
|
@display(description=_("Professor"), header=True) |
|
|
def display_header(self, instance: StudentUser): |
|
|
|
|
|
|
|
|
def display_header(self, instance: ProfessorUser): |
|
|
avatar_path = instance.avatar.url if instance.avatar else static("images/reading(1).png") |
|
|
avatar_path = instance.avatar.url if instance.avatar else static("images/reading(1).png") |
|
|
return [ |
|
|
return [ |
|
|
instance.fullname, |
|
|
instance.fullname, |
|
|
|