# Path: cab/models.py
# class Bookmark(models.Model):
#     snippet = models.ForeignKey(Snippet, related_name="bookmarks", on_delete=models.CASCADE)
#     user = models.ForeignKey(User, related_name="cab_bookmarks", on_delete=models.CASCADE)
#     date = models.DateTimeField(auto_now_add=True)
# 
#     class Meta:
#         ordering = ("-date",)
# 
#     def __str__(self):
#         return "%s bookmarked by %s" % (self.snippet, self.user)
# 
#     def save(self, *args, **kwargs):
#         super(Bookmark, self).save(*args, **kwargs)
#         self.snippet.update_bookmark_count()
# 
#     def delete(self, *args, **kwargs):
#         super(Bookmark, self).delete(*args, **kwargs)
#         self.snippet.update_bookmark_count()
# 
# 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/templatetags/markup.py
# @register.filter
# def safe_markdown(value):
#     """
#     Strip raw HTML and run Markdown over a given value.
#     """
#     return sanitize_markdown(value)


from django.contrib.auth.models import AnonymousUser, User
from django.template import Context, Template
from django.test import SimpleTestCase, TestCase
from django.urls import reverse
from rest_framework import status
from ..api.serializers import SnippetSerializer
from ..models import Bookmark, Language, Snippet
from ..templatetags.markup import safe_markdown
        c = Context({"snippet": self.snippet1, "user": self.user_a})
        rendered = t.render(c)

        self.assertEqual(rendered, "Y")

        Bookmark.objects.filter(user=self.user_a, snippet=self.snippet1).delete()

        rendered = t.render(c)
        self.assertEqual(rendered, "N")

        c = Context({"snippet": self.snippet1, "user": AnonymousUser()})
        rendered = t.render(c)
        self.assertEqual(rendered, "N")

    def test_core_tags(self):
        t = Template("""{% load core_tags %}{% for s in "cab.snippet"|latest:2 %}{{ s.title }}|{% endfor %}""")
        rendered = t.render(Context({}))
        self.assertEqual(rendered, "%s|%s|" % (self.snippet3.title, self.snippet2.title))

        t = Template(
            '{% load core_tags %}{% for t in "cab.snippet"|call_manager:"top_tags"|slice:":2" %}'
            "{{ t.name }}|{% endfor %}"
        )
        rendered = t.render(Context({}))
        self.assertEqual(rendered, "world|goodbye|")


class MarkupTests(SimpleTestCase):
    def test_safe_markdown(self):
        self.assertEqual(safe_markdown("<p>foo</p>"), "<p>foo</p>")

# Path: cab/models.py
# class Bookmark(models.Model):
#     snippet = models.ForeignKey(Snippet, related_name="bookmarks", on_delete=models.CASCADE)
#     user = models.ForeignKey(User, related_name="cab_bookmarks", on_delete=models.CASCADE)
#     date = models.DateTimeField(auto_now_add=True)
# 
#     class Meta:
#         ordering = ("-date",)
# 
#     def __str__(self):
#         return "%s bookmarked by %s" % (self.snippet, self.user)
# 
#     def save(self, *args, **kwargs):
#         super(Bookmark, self).save(*args, **kwargs)
#         self.snippet.update_bookmark_count()
# 
#     def delete(self, *args, **kwargs):
#         super(Bookmark, self).delete(*args, **kwargs)
#         self.snippet.update_bookmark_count()
# 
# 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/templatetags/markup.py
# @register.filter
# def safe_markdown(value):
#     """
#     Strip raw HTML and run Markdown over a given value.
#     """
#     return sanitize_markdown(value)


# Path: cab/tests/tests.py
from django.contrib.auth.models import AnonymousUser, User
from django.template import Context, Template
from django.test import SimpleTestCase, TestCase
from django.urls import reverse
from rest_framework import status
from ..api.serializers import SnippetSerializer
from ..models import Bookmark, Language, Snippet
from ..templatetags.markup import safe_markdown

        c = Context({"snippet": self.snippet1, "user": self.user_a})
        rendered = t.render(c)

        self.assertEqual(rendered, "Y")

        Bookmark.objects.filter(user=self.user_a, snippet=self.snippet1).delete()

        rendered = t.render(c)
        self.assertEqual(rendered, "N")

        c = Context({"snippet": self.snippet1, "user": AnonymousUser()})
        rendered = t.render(c)
        self.assertEqual(rendered, "N")

    def test_core_tags(self):
        t = Template("""{% load core_tags %}{% for s in "cab.snippet"|latest:2 %}{{ s.title }}|{% endfor %}""")
        rendered = t.render(Context({}))
        self.assertEqual(rendered, "%s|%s|" % (self.snippet3.title, self.snippet2.title))

        t = Template(
            '{% load core_tags %}{% for t in "cab.snippet"|call_manager:"top_tags"|slice:":2" %}'
            "{{ t.name }}|{% endfor %}"
        )
        rendered = t.render(Context({}))
        self.assertEqual(rendered, "world|goodbye|")


class MarkupTests(SimpleTestCase):
    def test_safe_markdown(self):
        self.assertEqual(safe_markdown("<p>foo</p>"), "<p>foo</p>")

        self.assertEqual(safe_markdown("<pre>foo</pre>"), "<pre>foo</pre>")