# Path: mainapp/models.py
# class Author(models.Model):
#     name = models.CharField(max_length=64)
#     surname = models.CharField(max_length=64)
#     optional_email = models.EmailField(unique=True, null=True, blank=True)
#     author = models.ForeignKey(User, null=True, blank=True, on_delete=models.DO_NOTHING)
#     additional_info = models.TextField(max_length=256)
#     image_link = models.CharField(max_length=256, null=True, blank=True)
#     blog_link = models.CharField(max_length=256, null=True, blank=True)
#     linkedin_link = models.CharField(max_length=256, null=True, blank=True)
#     external_link = models.CharField(max_length=256, null=True, blank=True)
#     is_public_mentor = models.BooleanField(default=False)
#     #created_at = models.DateField(default=django.utils.timezone.now)
#     #updated_at = AutoDateTimeField(default=django.utils.timezone.now)
# 
#     def __str__(self):
#         return "{} {}".format(self.name, self.surname)
# 
# class Module(models.Model):
#     contents = models.ManyToManyField(Chapter)
#     title = models.CharField(max_length=128)
#     comment = models.CharField(max_length=1024, blank=True)
#     sort_order = models.PositiveIntegerField(default=0, editable=False, db_index=True)
#     author = models.ForeignKey(Author, null=True, blank=True, on_delete=models.DO_NOTHING)
#     curiosities = models.ManyToManyField(Curiosity)
#     achievements_desc = models.CharField(max_length=256, blank=True)
#     is_enabled = models.BooleanField(default=False)
#     views_count = models.PositiveIntegerField(default=0, editable=False)
#     finished_count = models.PositiveIntegerField(default=0, editable=False)
#     chapters = []
#     contents_list = []
#     icons_list = []
#     #created_at = models.DateField(default=django.utils.timezone.now)
#     #updated_at = AutoDateTimeField(default=django.utils.timezone.now)
# 
#     def __str__(self):
#         return self.title
# 
#     class Meta:
#         ordering = ('title',)
# 
#     def add_chapters(self, chapters):
#         self.chapters = chapters
# 
#     def add_chapter(self, chapter):
#         self.chapters.append(chapter)
# 
#     def add_content(self, content):
#         self.contents_list = content
# 
#     def add_contents(self, contents):
#         self.contents_list.append(contents)
# 
#     def prepare_icons_list_from_comment(self):
#         self.icons_list = self.comment.split(',')
# 
# class Chapter(models.Model):
#     contents = models.ManyToManyField(Content)
#     name = models.CharField(max_length=128, blank=True)
#     sort_order = models.PositiveIntegerField(default=0, editable=False, db_index=True)
#     level = enum.EnumField(ChapterLevelType)
#     note = models.TextField(max_length=256)
#     author = models.ForeignKey(Author, null=True, blank=True, on_delete=models.DO_NOTHING)
#     #created_at = models.DateField(default=django.utils.timezone.now)
#     #updated_at = AutoDateTimeField(default=django.utils.timezone.now)
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         ordering = ('name',)
# 
# class Content(models.Model):
#     status = enum.EnumField(ContentType)
#     value = models.TextField(max_length=4000)
#     additional_text = models.CharField(max_length=512)
#     author = models.ForeignKey(Author, null=True, blank=True, on_delete=models.DO_NOTHING)
#     #created_at = models.DateField(default=django.utils.timezone.now)
#     #updated_at = AutoDateTimeField(default=django.utils.timezone.now)
# 
#     def __str__(self):
#         return self.additional_text
# 
#     class Meta:
#         ordering = ('additional_text',)
# 
# class Curiosity(models.Model):
#     title = models.CharField(max_length=128)
#     contents = models.ManyToManyField(Content)
#     author = models.ForeignKey(Author, null=True, blank=True, on_delete=models.DO_NOTHING)
# 
#     def __str__(self):
#         return self.title
# 
#     class Meta:
#         ordering = ('title',)
# 
# class ContentStatus(models.Model):
#     user = models.ForeignKey(User, null=False, blank=False, on_delete=models.DO_NOTHING)
#     content = models.ForeignKey(Content, null=False, blank=False, on_delete=models.DO_NOTHING)
#     status = models.CharField(max_length=4,
#                               choices=CONTENT_STATUS_TYPE_CHOICES,
#                               default=ContentStatusType.NEW)
#     created_at = models.DateTimeField(default=django.utils.timezone.now)
#     updated_at = AutoDateTimeField(default=django.utils.timezone.now)
# 
#     def __str__(self):
#         return self.status
# 
#     @staticmethod
#     def get_opinions_by_user_id(user_id):
#         q = '''SELECT id, status, COUNT(status) AS status_count 
#                FROM mainapp_contentstatus
#                WHERE user_id=%s
#                GROUP BY status'''
#         return ContentStatus.objects.raw(q, [user_id])


from django.contrib import admin
from mainapp.models import Author, Module, Chapter, Content, Curiosity, ContentStatus


class AuthorAdmin(admin.ModelAdmin):
    list_display = ('name', 'surname', 'author')
    fieldsets = [
        (None, {'fields': [('name', 'surname')]}),
    ]

    def save_model(self, request, obj, form, change):
        if getattr(obj, 'author', None) is None:
            obj.author = request.user
            # obj.author = request.user.username
            # obj.save()
        obj.save()


class ChapterAdmin(admin.StackedInline):
    model = Chapter
    list_display = ('name', 'author')

    # def save_model(self, request, obj, form, change):
    #     if getattr(obj, 'author', None) is None:
    #         obj.author = request.user
    #     obj.save()


class ModuleAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'is_enabled',)

# Path: mainapp/models.py
# class Author(models.Model):
#     name = models.CharField(max_length=64)
#     surname = models.CharField(max_length=64)
#     optional_email = models.EmailField(unique=True, null=True, blank=True)
#     author = models.ForeignKey(User, null=True, blank=True, on_delete=models.DO_NOTHING)
#     additional_info = models.TextField(max_length=256)
#     image_link = models.CharField(max_length=256, null=True, blank=True)
#     blog_link = models.CharField(max_length=256, null=True, blank=True)
#     linkedin_link = models.CharField(max_length=256, null=True, blank=True)
#     external_link = models.CharField(max_length=256, null=True, blank=True)
#     is_public_mentor = models.BooleanField(default=False)
#     #created_at = models.DateField(default=django.utils.timezone.now)
#     #updated_at = AutoDateTimeField(default=django.utils.timezone.now)
# 
#     def __str__(self):
#         return "{} {}".format(self.name, self.surname)
# 
# class Module(models.Model):
#     contents = models.ManyToManyField(Chapter)
#     title = models.CharField(max_length=128)
#     comment = models.CharField(max_length=1024, blank=True)
#     sort_order = models.PositiveIntegerField(default=0, editable=False, db_index=True)
#     author = models.ForeignKey(Author, null=True, blank=True, on_delete=models.DO_NOTHING)
#     curiosities = models.ManyToManyField(Curiosity)
#     achievements_desc = models.CharField(max_length=256, blank=True)
#     is_enabled = models.BooleanField(default=False)
#     views_count = models.PositiveIntegerField(default=0, editable=False)
#     finished_count = models.PositiveIntegerField(default=0, editable=False)
#     chapters = []
#     contents_list = []
#     icons_list = []
#     #created_at = models.DateField(default=django.utils.timezone.now)
#     #updated_at = AutoDateTimeField(default=django.utils.timezone.now)
# 
#     def __str__(self):
#         return self.title
# 
#     class Meta:
#         ordering = ('title',)
# 
#     def add_chapters(self, chapters):
#         self.chapters = chapters
# 
#     def add_chapter(self, chapter):
#         self.chapters.append(chapter)
# 
#     def add_content(self, content):
#         self.contents_list = content
# 
#     def add_contents(self, contents):
#         self.contents_list.append(contents)
# 
#     def prepare_icons_list_from_comment(self):
#         self.icons_list = self.comment.split(',')
# 
# class Chapter(models.Model):
#     contents = models.ManyToManyField(Content)
#     name = models.CharField(max_length=128, blank=True)
#     sort_order = models.PositiveIntegerField(default=0, editable=False, db_index=True)
#     level = enum.EnumField(ChapterLevelType)
#     note = models.TextField(max_length=256)
#     author = models.ForeignKey(Author, null=True, blank=True, on_delete=models.DO_NOTHING)
#     #created_at = models.DateField(default=django.utils.timezone.now)
#     #updated_at = AutoDateTimeField(default=django.utils.timezone.now)
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         ordering = ('name',)
# 
# class Content(models.Model):
#     status = enum.EnumField(ContentType)
#     value = models.TextField(max_length=4000)
#     additional_text = models.CharField(max_length=512)
#     author = models.ForeignKey(Author, null=True, blank=True, on_delete=models.DO_NOTHING)
#     #created_at = models.DateField(default=django.utils.timezone.now)
#     #updated_at = AutoDateTimeField(default=django.utils.timezone.now)
# 
#     def __str__(self):
#         return self.additional_text
# 
#     class Meta:
#         ordering = ('additional_text',)
# 
# class Curiosity(models.Model):
#     title = models.CharField(max_length=128)
#     contents = models.ManyToManyField(Content)
#     author = models.ForeignKey(Author, null=True, blank=True, on_delete=models.DO_NOTHING)
# 
#     def __str__(self):
#         return self.title
# 
#     class Meta:
#         ordering = ('title',)
# 
# class ContentStatus(models.Model):
#     user = models.ForeignKey(User, null=False, blank=False, on_delete=models.DO_NOTHING)
#     content = models.ForeignKey(Content, null=False, blank=False, on_delete=models.DO_NOTHING)
#     status = models.CharField(max_length=4,
#                               choices=CONTENT_STATUS_TYPE_CHOICES,
#                               default=ContentStatusType.NEW)
#     created_at = models.DateTimeField(default=django.utils.timezone.now)
#     updated_at = AutoDateTimeField(default=django.utils.timezone.now)
# 
#     def __str__(self):
#         return self.status
# 
#     @staticmethod
#     def get_opinions_by_user_id(user_id):
#         q = '''SELECT id, status, COUNT(status) AS status_count 
#                FROM mainapp_contentstatus
#                WHERE user_id=%s
#                GROUP BY status'''
#         return ContentStatus.objects.raw(q, [user_id])


# Path: static/admin.py
from django.contrib import admin
from mainapp.models import Author, Module, Chapter, Content, Curiosity, ContentStatus



class AuthorAdmin(admin.ModelAdmin):
    list_display = ('name', 'surname', 'author')
    fieldsets = [
        (None, {'fields': [('name', 'surname')]}),
    ]

    def save_model(self, request, obj, form, change):
        if getattr(obj, 'author', None) is None:
            obj.author = request.user
            # obj.author = request.user.username
            # obj.save()
        obj.save()


class ChapterAdmin(admin.StackedInline):
    model = Chapter
    list_display = ('name', 'author')

    # def save_model(self, request, obj, form, change):
    #     if getattr(obj, 'author', None) is None:
    #         obj.author = request.user
    #     obj.save()


class ModuleAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'is_enabled',)

    inlines = [ChapterAdmin]