← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands-website/fix_check_input into lp:widelands-website

 

kaputtnik has proposed merging lp:~widelands-dev/widelands-website/fix_check_input into lp:widelands-website.

Commit message:
Respect max_length for field 'text' in model check_input

Requested reviews:
  Widelands Developers (widelands-dev)

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands-website/fix_check_input/+merge/344559

The last server errors were caused because the field 'text' in model for check_input is restricted to 200 chars, but certainly the provided text could be much longer. Those server errors didn't occur so far, because we hadn't the option  'init_command': "SET sql_mode='STRICT_TRANS_TABLES'".

This branch will fix this by truncating the text value to the value given in the model (max_length=200).

The good thing: The server errors occur, because check_input has found spam :-)
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands-website/fix_check_input into lp:widelands-website.
=== modified file 'check_input/models.py'
--- check_input/models.py	2017-11-29 22:46:16 +0000
+++ check_input/models.py	2018-04-26 21:27:37 +0000
@@ -9,14 +9,14 @@
 class SuspiciousInput(models.Model):
     """Model for collecting suspicios user input.
 
-    Call the check_input method with this attributes:
-    content_object = Model instance of a saved(!) object
-    user = user
-    text = text to check for suspicious content
+        Call the check_input method with this attributes:
+        content_object = Model instance of a saved(!) object
+        user = user
+        text = text to check for suspicious content
 
-    Example:
-    is_suspicous = SuspiciousInput.check_input(content_object=post, 
-user=post.user, text=post.body)
+        Example:
+        is_suspicous = SuspiciousInput.check_input(content_object=post,
+    user=post.user, text=post.body)
 
     """
 
@@ -34,6 +34,14 @@
     def __unicode__(self):
         return self.text
 
+    def clean(self):
+        # Cleaning fields
+        max_chars = self._meta.get_field('text').max_length
+        if len(self.text) >= max_chars:
+            # Truncate the text to fit with max_length of field
+            # otherwise a Database error is thrown
+            self.text = self.text[:max_chars]
+
     def is_suspicious(self):
         if any(x in self.text.lower() for x in settings.ANTI_SPAM_KWRDS):
             return True
@@ -46,5 +54,10 @@
         user_input = cls(*args, **kwargs)
         is_spam = user_input.is_suspicious()
         if is_spam:
-            user_input.save()
+            try:
+                user_input.clean()
+                user_input.save()
+            except:
+                pass
+
         return is_spam


Follow ups