# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from django import forms
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from .models import (User, Course, Classroom, Teacher, Students, CourseClass,
                     Hour, SchoolSchedule)


@admin.register(SchoolSchedule)
class SchoolScheduleAdmin(admin.ModelAdmin):
    list_display = ['id']


@admin.register(Hour)
class HourAdmin(admin.ModelAdmin):
    list_display = ['id', 'course_class', 'room']


@admin.register(Course)
class CourseAdmin(admin.ModelAdmin):
    list_display = ['id', 'name']


@admin.register(Teacher)
class TeacherAdmin(admin.ModelAdmin):
    list_display = ['id', 'name']


@admin.register(Students)
class StudentsAdmin(admin.ModelAdmin):
    list_display = ['id', 'group_name', 'number']


@admin.register(CourseClass)
class CourseClassAdmin(admin.ModelAdmin):
    list_display = ['id', 'course', 'students', 'teacher', 'hours']


@admin.register(Classroom)
class ClassroomAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'room_type']


class MyUserChangeForm(UserChangeForm):
    class Meta(UserChangeForm.Meta):
        model = User


class MyUserCreationForm(UserCreationForm):

    error_message = UserCreationForm.error_messages.update({
        'duplicate_username': 'This username has already been taken.'
    })

    class Meta(UserCreationForm.Meta):
        model = User

    def clean_username(self):
        username = self.cleaned_data["username"]
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError(self.error_messages['duplicate_username'])


@admin.register(User)
class MyUserAdmin(AuthUserAdmin):
    form = MyUserChangeForm
    add_form = MyUserCreationForm
    fieldsets = (
            ('User Profile', {'fields': ('name',)}),
    ) + AuthUserAdmin.fieldsets
    list_display = ('username', 'name', 'is_superuser')
    search_fields = ['name']

# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from django import forms
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from .models import (User, Course, Classroom, Teacher, Students, CourseClass,
                     Hour, SchoolSchedule)


@admin.register(SchoolSchedule)
class SchoolScheduleAdmin(admin.ModelAdmin):
    list_display = ['id']


@admin.register(Hour)
class HourAdmin(admin.ModelAdmin):
    list_display = ['id', 'course_class', 'room']


@admin.register(Course)
class CourseAdmin(admin.ModelAdmin):
    list_display = ['id', 'name']


@admin.register(Teacher)
class TeacherAdmin(admin.ModelAdmin):
    list_display = ['id', 'name']


@admin.register(Students)
class StudentsAdmin(admin.ModelAdmin):
    list_display = ['id', 'group_name', 'number']


@admin.register(CourseClass)
class CourseClassAdmin(admin.ModelAdmin):
    list_display = ['id', 'course', 'students', 'teacher', 'hours']


@admin.register(Classroom)
class ClassroomAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'room_type']


class MyUserChangeForm(UserChangeForm):
    class Meta(UserChangeForm.Meta):
        model = User


class MyUserCreationForm(UserCreationForm):

    error_message = UserCreationForm.error_messages.update({
        'duplicate_username': 'This username has already been taken.'
    })

    class Meta(UserCreationForm.Meta):
        model = User

    def clean_username(self):
        username = self.cleaned_data["username"]
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError(self.error_messages['duplicate_username'])


@admin.register(User)
class MyUserAdmin(AuthUserAdmin):
    form = MyUserChangeForm
    add_form = MyUserCreationForm
    fieldsets = (
            ('User Profile', {'fields': ('name',)}),
    ) + AuthUserAdmin.fieldsets
    list_display = ('username', 'name', 'is_superuser')
    search_fields = ['name']

# -*- coding: utf-8 -*-