# 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()


from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.syndication.views import Feed
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.utils.feedgenerator import Atom1Feed
from taggit.models import Tag
from .models import Language, Snippet


SITE_NAME = getattr(settings, "SITE_NAME", None)


class LatestSnippetsFeed(Feed):
    """
    Feed of the most recently published Snippets.
    """

    feed_type = Atom1Feed
    title_template = "cab/feeds/title.html"
    description_template = "cab/feeds/description.html"
    item_copyright = "Freely redistributable"
    link = "/snippets/"
    description = "Latest snippets"
    author = "Snippets submitters"

    def title(self):
        if SITE_NAME:
            return "%s: Latest snippets" % SITE_NAME
        else:
            return "Latest snippets"

    def item_author_name(self, item):
        return item.author.username

    def items(self):

# 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()


# Path: cab/feeds.py
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.syndication.views import Feed
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.utils.feedgenerator import Atom1Feed
from taggit.models import Tag
from .models import Language, Snippet



SITE_NAME = getattr(settings, "SITE_NAME", None)


class LatestSnippetsFeed(Feed):
    """
    Feed of the most recently published Snippets.
    """

    feed_type = Atom1Feed
    title_template = "cab/feeds/title.html"
    description_template = "cab/feeds/description.html"
    item_copyright = "Freely redistributable"
    link = "/snippets/"
    description = "Latest snippets"
    author = "Snippets submitters"

    def title(self):
        if SITE_NAME:
            return "%s: Latest snippets" % SITE_NAME
        else:
            return "Latest snippets"

    def item_author_name(self, item):
        return item.author.username

    def items(self):

        return Snippet.objects.all()[:15]