Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/spam snippet filtering #272

Merged
merged 3 commits into from
Jun 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cab/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@


class SnippetList(generics.ListCreateAPIView):
queryset = Snippet.objects.all()
queryset = Snippet.objects.active_snippet()
serializer_class = SnippetSerializer


class SnippetDetail(generics.RetrieveUpdateAPIView):
queryset = Snippet.objects.all()
queryset = Snippet.objects.active_snippet()
serializer_class = SnippetSerializer
60 changes: 35 additions & 25 deletions cab/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
from .listeners import start_listening
from .utils import sanitize_markdown

VERSIONS = getattr(settings, 'CAB_VERSIONS', ())
VERSIONS = getattr(settings, "CAB_VERSIONS", ())


class LanguageManager(models.Manager):
def top_languages(self):
return self.annotate(score=Count('snippet')).order_by('-score')
return self.annotate(score=Count("snippet")).order_by("-score")


class Language(models.Model):
Expand All @@ -31,39 +31,43 @@ class Language(models.Model):
objects = LanguageManager()

class Meta:
ordering = ('name',)
ordering = ("name",)

def __str__(self):
return self.name

def get_absolute_url(self):
return reverse('cab_language_detail', kwargs={'slug': self.slug})
return reverse("cab_language_detail", kwargs={"slug": self.slug})

def get_lexer(self):
return lexers.get_lexer_by_name(self.language_code)


class SnippetManager(models.Manager):
def top_authors(self):
return User.objects.annotate(
score=Count('snippet')).order_by('-score', 'username')
return User.objects.annotate(score=Count("snippet")).order_by(
"-score", "username"
)

def top_tags(self):
return self.model.tags.most_common().order_by('-num_times', 'name')
return self.model.tags.most_common().order_by("-num_times", "name")

def top_rated(self):
# this is slow
# return self.annotate(score=Sum('ratings__score')).order_by('-score')
return self.all().order_by('-rating_score', '-pub_date')
return self.all().order_by("-rating_score", "-pub_date")

def most_bookmarked(self):
# this is slow
# self.annotate(score=Count('bookmarks')).order_by('-score')
return self.all().order_by('-bookmark_count', '-pub_date')
return self.all().order_by("-bookmark_count", "-pub_date")

def matches_tag(self, tag):
return self.filter(tags__in=[tag])

def active_snippet(self):
return self.exclude(flags__flag=SnippetFlag.FLAG_SPAM)


class Snippet(models.Model):
title = models.CharField(max_length=255)
Expand All @@ -73,7 +77,7 @@ class Snippet(models.Model):
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')
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
Expand All @@ -85,7 +89,7 @@ class Snippet(models.Model):
objects = SnippetManager()

class Meta:
ordering = ('-pub_date',)
ordering = ("-pub_date",)

def __str__(self):
return self.title
Expand All @@ -96,15 +100,15 @@ def save(self, *args, **kwargs):
super(Snippet, self).save(*args, **kwargs)

def get_absolute_url(self):
return reverse('cab_snippet_detail', kwargs={'snippet_id': self.id})
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))
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()])
return ", ".join([t.name for t in self.tags.order_by("name").all()])

def get_version(self):
return dict(VERSIONS)[self.version]
Expand All @@ -119,28 +123,30 @@ def update_bookmark_count(self):

def mark_as_inappropiate(self):
snippet_flag = SnippetFlag(
snippet=self, user=self.author, flag=SnippetFlag.FLAG_INAPPROPRIATE)
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=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'),
(FLAG_SPAM, "Spam"),
(FLAG_INAPPROPRIATE, "Inappropriate"),
)
snippet = models.ForeignKey(Snippet, related_name='flags', on_delete=models.CASCADE)
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' % (
return "%s flagged as %s by %s" % (
self.snippet.title,
self.get_flag_display(),
self.user.username,
Expand All @@ -155,12 +161,16 @@ def remove_and_ban(self):


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)
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',)
ordering = ("-date",)

def __str__(self):
return "%s bookmarked by %s" % (self.snippet, self.user)
Expand Down
Loading