# 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 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 import template
from django.contrib.postgres.search import SearchVector
from ..models import Bookmark, Snippet, SnippetFlag


register = template.Library()


@register.filter
def is_bookmarked(snippet, user):
    """
    {% if snippet|is_bookmarked:request.user %}
        already bookmarked
    {% else %}
        not bookmarked yet
    {% endif %}
    """
    if not user.is_authenticated:
        return False
    return Bookmark.objects.filter(snippet=snippet, user=user).exists()


@register.filter
def has_flagged(user, snippet):
    if not user.is_authenticated:
        return False
    return SnippetFlag.objects.filter(snippet=snippet, user=user).exists()


@register.filter
def more_like_this(snippet, limit=None):
    try:

# 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 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/templatetags/cab_tags.py
from django import template
from django.contrib.postgres.search import SearchVector
from ..models import Bookmark, Snippet, SnippetFlag



register = template.Library()


@register.filter
def is_bookmarked(snippet, user):
    """
    {% if snippet|is_bookmarked:request.user %}
        already bookmarked
    {% else %}
        not bookmarked yet
    {% endif %}
    """
    if not user.is_authenticated:
        return False
    return Bookmark.objects.filter(snippet=snippet, user=user).exists()


@register.filter
def has_flagged(user, snippet):
    if not user.is_authenticated:
        return False
    return SnippetFlag.objects.filter(snippet=snippet, user=user).exists()


@register.filter
def more_like_this(snippet, limit=None):
    try:

        sqs = Snippet.objects.annotate(