Merge remote-tracking branch 'zawadzki/new-design'
[wolnelektury.git] / src / suggest / models.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 import re
5 from datetime import timedelta
6
7 from django.db import models
8 from django.contrib.auth.models import User
9 from django.utils.translation import ugettext_lazy as _
10
11
12 class Suggestion(models.Model):
13     contact = models.CharField(_('contact'), blank=True, max_length=120)
14     description = models.TextField(_('description'), blank=True)
15     created_at = models.DateTimeField(_('creation date'), auto_now=True)
16     ip = models.GenericIPAddressField(_('IP address'))
17     user = models.ForeignKey(User, models.SET_NULL, blank=True, null=True)
18
19     class Meta:
20         ordering = ('-created_at',)
21         verbose_name = _('suggestion')
22         verbose_name_plural = _('suggestions')
23
24     def __str__(self):
25         return str(self.created_at)
26
27
28 class PublishingSuggestion(models.Model):
29     contact = models.CharField(_('contact'), blank=True, max_length=120)
30     books = models.TextField(_('books'), null=True, blank=True)
31     audiobooks = models.TextField(_('audiobooks'), null=True, blank=True)
32     created_at = models.DateTimeField(_('creation date'), auto_now_add=True)
33     ip = models.GenericIPAddressField(_('IP address'))
34     user = models.ForeignKey(User, models.SET_NULL, blank=True, null=True)
35
36     class Meta:
37         ordering = ('-created_at',)
38         verbose_name = _('publishing suggestion')
39         verbose_name_plural = _('publishing suggestions')
40
41     def is_spam(self):
42         suggestion_text = (self.books or self.audiobooks).strip(' \r\n,')
43         # similar = PublishingSuggestion.objects.filter(
44         #     books__in=('', suggestion_text), audiobooks__in=('', suggestion_text))
45         similar = PublishingSuggestion.objects.filter(books=self.books, audiobooks=self.audiobooks).exclude(pk=self.pk)
46         http = 'http' in suggestion_text
47         spam = False
48         if re.search(r'([^\W\d_])\1\1\1', suggestion_text):
49             # same letter repetition outside URL
50             spam = True
51         elif re.search(r'[^\W\d_]\d|\d[^\W\d_]', suggestion_text) and not http:
52             # string of letters and digits outside URL
53             spam = True
54         elif re.search(r'[^\W\d_]{17}', suggestion_text):
55             # long string of letters (usually gibberish)
56             spam = True
57         elif ' ' not in suggestion_text:
58             # single word - usually spam
59             spam = True
60         elif len(suggestion_text) < 11:
61             # too short
62             spam = True
63         elif similar.filter(created_at__range=(self.created_at - timedelta(1), self.created_at)):
64             # the same suggestion within 24h
65             spam = True
66         elif similar.filter(ip=self.ip):
67             # the same suggestion from the same IP
68             spam = True
69         return spam
70
71     def __str__(self):
72         return str(self.created_at)