changeset 7:a80770f82311 5.2 tip

Use list of tokens
author Cédric Krier <ced@b2ck.com>
date Tue, 12 Nov 2019 11:17:47 +0100
parents e7e6f3ae5555
children
files marketing.py
diffstat 1 files changed, 12 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/marketing.py	Wed Oct 30 11:22:38 2019 +0100
+++ b/marketing.py	Tue Nov 12 11:17:47 2019 +0100
@@ -159,21 +159,21 @@
     @classmethod
     def subscribe_url(cls, url):
         parts = urlsplit(url)
-        tokens = [
-            _f for _f in parse_qs(parts.query).get('token', [None]) if _f]
-        for token in tokens:
-            cls.subscribe(token)
+        tokens = filter(
+            None, parse_qs(parts.query).get('token', [None]))
+        return cls.subscribe(list(tokens))
 
     @classmethod
-    def subscribe(cls, token):
+    def subscribe(cls, tokens):
         # Make it slow to prevent brute force attacks
         delay = config.getint('marketing', 'subscribe_delay', default=1)
         Transaction().atexit(time.sleep, delay)
         with Transaction().set_context(active_test=False):
             records = cls.search([
-                    ('email_token', '=', token),
+                    ('email_token', 'in', tokens),
                     ])
         cls.write(records, {'active': True})
+        return bool(records)
 
     def get_email_unsubscribe(self, report_name='marketing.email.unsubscribe'):
         pool = Pool()
@@ -191,20 +191,20 @@
     @classmethod
     def unsubscribe_url(cls, url):
         parts = urlsplit(url)
-        tokens = [
-            _f for _f in parse_qs(parts.query).get('token', [None]) if _f]
-        for token in tokens:
-            cls.unsubscribe(token)
+        tokens = filter(
+            None, parse_qs(parts.query).get('token', [None]))
+        return cls.unsubscribe(list(tokens))
 
     @classmethod
-    def unsubscribe(cls, token):
+    def unsubscribe(cls, tokens):
         # Make it slow to prevent brute force attacks
         delay = config.getint('marketing', 'subscribe_delay', default=1)
         Transaction().atexit(time.sleep, delay)
         records = cls.search([
-                ('email_token', '=', token),
+                ('email_token', 'in', tokens),
                 ])
         cls.write(records, {'active': False})
+        return bool(records)
 
 
 class EmailSubscribe(Report):