1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 
   2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 
   5 from datetime import timedelta
 
   7 from django.db import models
 
   8 from django.contrib.auth.models import User
 
   9 from django.utils.translation import ugettext_lazy as _
 
  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)
 
  20         ordering = ('-created_at',)
 
  21         verbose_name = _('suggestion')
 
  22         verbose_name_plural = _('suggestions')
 
  25         return str(self.created_at)
 
  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)
 
  37         ordering = ('-created_at',)
 
  38         verbose_name = _('publishing suggestion')
 
  39         verbose_name_plural = _('publishing suggestions')
 
  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
 
  48         if re.search(r'([^\W\d_])\1\1\1', suggestion_text):
 
  49             # same letter repetition outside URL
 
  51         elif re.search(r'[^\W\d_]\d|\d[^\W\d_]', suggestion_text) and not http:
 
  52             # string of letters and digits outside URL
 
  54         elif re.search(r'[^\W\d_]{17}', suggestion_text):
 
  55             # long string of letters (usually gibberish)
 
  57         elif ' ' not in suggestion_text:
 
  58             # single word - usually spam
 
  60         elif len(suggestion_text) < 11:
 
  63         elif similar.filter(created_at__range=(self.created_at - timedelta(1), self.created_at)):
 
  64             # the same suggestion within 24h
 
  66         elif similar.filter(ip=self.ip):
 
  67             # the same suggestion from the same IP
 
  72         return str(self.created_at)