# Path: cab/models.py
# class Language(models.Model):
#     name = models.CharField(max_length=100)
#     slug = models.SlugField(unique=True)
#     language_code = models.CharField(max_length=50)
#     mime_type = models.CharField(max_length=100)
#     file_extension = models.CharField(max_length=10)
# 
#     objects = LanguageManager()
# 
#     class Meta:
#         ordering = ("name",)
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse("cab_language_detail", kwargs={"slug": self.slug})
# 
#     def get_lexer(self):
#         return lexers.get_lexer_by_name(self.language_code)
# 
# class Snippet(models.Model):
#     title = models.CharField(max_length=255)
#     language = models.ForeignKey(Language, on_delete=models.CASCADE)
#     author = models.ForeignKey(User, on_delete=models.CASCADE)
#     description = models.TextField()
#     description_html = models.TextField(editable=False)
#     code = models.TextField()
#     highlighted_code = models.TextField(editable=False)
#     version = models.CharField(max_length=5, choices=VERSIONS, default="0.0")
#     pub_date = models.DateTimeField(auto_now_add=True)
#     updated_date = models.DateTimeField(auto_now=True)
#     bookmark_count = models.IntegerField(default=0)  # denormalized count
#     rating_score = models.IntegerField(default=0)  # denormalized score
# 
#     ratings = Ratings()
#     tags = TaggableManager(blank=True)
# 
#     objects = SnippetManager()
# 
#     class Meta:
#         ordering = ("-pub_date",)
# 
#     def __str__(self):
#         return self.title
# 
#     def save(self, *args, **kwargs):
#         self.description_html = sanitize_markdown(self.description)
#         self.highlighted_code = self.highlight()
#         super(Snippet, self).save(*args, **kwargs)
# 
#     def get_absolute_url(self):
#         return reverse("cab_snippet_detail", kwargs={"snippet_id": self.id})
# 
#     def highlight(self):
#         return highlight(self.code, self.language.get_lexer(), formatters.HtmlFormatter(linenos=True))
# 
#     def get_tagstring(self):
#         return ", ".join([t.name for t in self.tags.order_by("name").all()])
# 
#     def get_version(self):
#         return dict(VERSIONS)[self.version]
# 
#     def update_rating(self):
#         self.rating_score = self.ratings.cumulative_score() or 0
#         self.save()
# 
#     def update_bookmark_count(self):
#         self.bookmark_count = self.bookmarks.count() or 0
#         self.save()
# 
#     def mark_as_inappropiate(self):
#         snippet_flag = SnippetFlag(snippet=self, user=self.author, flag=SnippetFlag.FLAG_INAPPROPRIATE)
#         snippet_flag.save()
# 
#     def mark_as_spam(self):
#         snippet_flag = SnippetFlag(snippet=self, user=self.author, flag=SnippetFlag.FLAG_SPAM)
#         snippet_flag.save()
# 
# class SnippetFlag(models.Model):
#     FLAG_SPAM = 1
#     FLAG_INAPPROPRIATE = 2
#     FLAG_CHOICES = (
#         (FLAG_SPAM, "Spam"),
#         (FLAG_INAPPROPRIATE, "Inappropriate"),
#     )
#     snippet = models.ForeignKey(Snippet, related_name="flags", on_delete=models.CASCADE)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     flag = models.IntegerField(choices=FLAG_CHOICES)
# 
#     def __str__(self):
#         return "%s flagged as %s by %s" % (
#             self.snippet.title,
#             self.get_flag_display(),
#             self.user.username,
#         )
# 
#     def remove_and_ban(self):
#         user = self.snippet.author
#         user.set_unusable_password()
#         user.is_active = False
#         user.save()
#         self.snippet.delete()


from django.contrib import admin
from .models import Language, Snippet, SnippetFlag
    list_display = ("id", "title", "author", "rating_score", "pub_date")
    list_filter = ("language",)
    date_hierarchy = "pub_date"
    search_fields = (
        "author__username",
        "title",
        "description",
        "code",
    )
    raw_id_fields = ("author",)
    actions = ["mark_as_inappropiate", "mark_as_spam"]

    def mark_as_inappropiate(self, request, queryset):
        for obj in queryset:
            obj.mark_as_inappropiate()
        self.message_user(request, "Snippets marked as inappropiate successfully")

    mark_as_inappropiate.short_description = "Mark snippets as inappropiate"

    def mark_as_spam(self, request, queryset):
        for obj in queryset:
            obj.mark_as_spam()
        self.message_user(request, "Snippets marked as spam successfully")

    mark_as_spam.short_description = "Mark snippets as spam"


class SnippetFlagAdmin(admin.ModelAdmin):
    list_display = ("snippet", "flag")
    list_filter = ("flag",)

# Path: cab/models.py
# class Language(models.Model):
#     name = models.CharField(max_length=100)
#     slug = models.SlugField(unique=True)
#     language_code = models.CharField(max_length=50)
#     mime_type = models.CharField(max_length=100)
#     file_extension = models.CharField(max_length=10)
# 
#     objects = LanguageManager()
# 
#     class Meta:
#         ordering = ("name",)
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse("cab_language_detail", kwargs={"slug": self.slug})
# 
#     def get_lexer(self):
#         return lexers.get_lexer_by_name(self.language_code)
# 
# class Snippet(models.Model):
#     title = models.CharField(max_length=255)
#     language = models.ForeignKey(Language, on_delete=models.CASCADE)
#     author = models.ForeignKey(User, on_delete=models.CASCADE)
#     description = models.TextField()
#     description_html = models.TextField(editable=False)
#     code = models.TextField()
#     highlighted_code = models.TextField(editable=False)
#     version = models.CharField(max_length=5, choices=VERSIONS, default="0.0")
#     pub_date = models.DateTimeField(auto_now_add=True)
#     updated_date = models.DateTimeField(auto_now=True)
#     bookmark_count = models.IntegerField(default=0)  # denormalized count
#     rating_score = models.IntegerField(default=0)  # denormalized score
# 
#     ratings = Ratings()
#     tags = TaggableManager(blank=True)
# 
#     objects = SnippetManager()
# 
#     class Meta:
#         ordering = ("-pub_date",)
# 
#     def __str__(self):
#         return self.title
# 
#     def save(self, *args, **kwargs):
#         self.description_html = sanitize_markdown(self.description)
#         self.highlighted_code = self.highlight()
#         super(Snippet, self).save(*args, **kwargs)
# 
#     def get_absolute_url(self):
#         return reverse("cab_snippet_detail", kwargs={"snippet_id": self.id})
# 
#     def highlight(self):
#         return highlight(self.code, self.language.get_lexer(), formatters.HtmlFormatter(linenos=True))
# 
#     def get_tagstring(self):
#         return ", ".join([t.name for t in self.tags.order_by("name").all()])
# 
#     def get_version(self):
#         return dict(VERSIONS)[self.version]
# 
#     def update_rating(self):
#         self.rating_score = self.ratings.cumulative_score() or 0
#         self.save()
# 
#     def update_bookmark_count(self):
#         self.bookmark_count = self.bookmarks.count() or 0
#         self.save()
# 
#     def mark_as_inappropiate(self):
#         snippet_flag = SnippetFlag(snippet=self, user=self.author, flag=SnippetFlag.FLAG_INAPPROPRIATE)
#         snippet_flag.save()
# 
#     def mark_as_spam(self):
#         snippet_flag = SnippetFlag(snippet=self, user=self.author, flag=SnippetFlag.FLAG_SPAM)
#         snippet_flag.save()
# 
# class SnippetFlag(models.Model):
#     FLAG_SPAM = 1
#     FLAG_INAPPROPRIATE = 2
#     FLAG_CHOICES = (
#         (FLAG_SPAM, "Spam"),
#         (FLAG_INAPPROPRIATE, "Inappropriate"),
#     )
#     snippet = models.ForeignKey(Snippet, related_name="flags", on_delete=models.CASCADE)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     flag = models.IntegerField(choices=FLAG_CHOICES)
# 
#     def __str__(self):
#         return "%s flagged as %s by %s" % (
#             self.snippet.title,
#             self.get_flag_display(),
#             self.user.username,
#         )
# 
#     def remove_and_ban(self):
#         user = self.snippet.author
#         user.set_unusable_password()
#         user.is_active = False
#         user.save()
#         self.snippet.delete()


# Path: cab/admin.py
from django.contrib import admin
from .models import Language, Snippet, SnippetFlag

    list_display = ("id", "title", "author", "rating_score", "pub_date")
    list_filter = ("language",)
    date_hierarchy = "pub_date"
    search_fields = (
        "author__username",
        "title",
        "description",
        "code",
    )
    raw_id_fields = ("author",)
    actions = ["mark_as_inappropiate", "mark_as_spam"]

    def mark_as_inappropiate(self, request, queryset):
        for obj in queryset:
            obj.mark_as_inappropiate()
        self.message_user(request, "Snippets marked as inappropiate successfully")

    mark_as_inappropiate.short_description = "Mark snippets as inappropiate"

    def mark_as_spam(self, request, queryset):
        for obj in queryset:
            obj.mark_as_spam()
        self.message_user(request, "Snippets marked as spam successfully")

    mark_as_spam.short_description = "Mark snippets as spam"


class SnippetFlagAdmin(admin.ModelAdmin):
    list_display = ("snippet", "flag")
    list_filter = ("flag",)

    actions = ["remove_and_ban"]