From dd6864c2c4336c0c7b30fd8097eca126143dc8f2 Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Thu, 5 May 2011 00:18:45 +0200 Subject: [PATCH] CapPaste: Also check the title in can_post Because unsurprisingly pastealacon has silly restrictions. --- weboob/backends/pastealacon/backend.py | 7 ++++++- weboob/backends/pastealacon/test.py | 14 +++++++------- weboob/backends/pastebin/backend.py | 4 +++- weboob/capabilities/paste.py | 3 ++- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/weboob/backends/pastealacon/backend.py b/weboob/backends/pastealacon/backend.py index a8d08549..7fd79d7c 100644 --- a/weboob/backends/pastealacon/backend.py +++ b/weboob/backends/pastealacon/backend.py @@ -20,6 +20,8 @@ from __future__ import with_statement +import re + from weboob.tools.capabilities.paste import BasePasteBackend from weboob.tools.backend import BaseBackend from weboob.capabilities.base import NotLoaded @@ -49,7 +51,7 @@ class PastealaconBackend(BaseBackend, BasePasteBackend): def new_paste(self, *args, **kwargs): return PastealaconPaste(*args, **kwargs) - def can_post(self, contents, public=None, max_age=None): + def can_post(self, contents, title=None, public=None, max_age=None): try: contents.encode(self.browser.ENCODING) except UnicodeEncodeError: @@ -59,6 +61,9 @@ class PastealaconBackend(BaseBackend, BasePasteBackend): if max_age is not None: if self.get_closest_expiration(max_age) is None: return 0 + # the "title" is filtered (does not even accepts dots) + if not title or re.match('^\w+$', title) and len(title) <= 24: + return 2 return 1 def get_paste(self, _id): diff --git a/weboob/backends/pastealacon/test.py b/weboob/backends/pastealacon/test.py index e49109ec..a99e5a48 100644 --- a/weboob/backends/pastealacon/test.py +++ b/weboob/backends/pastealacon/test.py @@ -82,12 +82,12 @@ class PastealaconTest(BackendTest): def test_can_post(self): assert 0 == self.backend.can_post('hello', public=False) - assert 1 == self.backend.can_post('hello', public=True) + assert 1 <= self.backend.can_post('hello', public=True) assert 0 == self.backend.can_post('hello', public=True, max_age=600) - assert 1 == self.backend.can_post('hello', public=True, max_age=3600*24) - assert 1 == self.backend.can_post('hello', public=True, max_age=3600*24*3) - assert 1 == self.backend.can_post('hello', public=True, max_age=False) - assert 1 == self.backend.can_post('hello', public=None, max_age=False) - assert 1 == self.backend.can_post('hello', public=True, max_age=3600*24*40) - assert 1 == self.backend.can_post(u'héhé', public=True) + assert 1 <= self.backend.can_post('hello', public=True, max_age=3600*24) + assert 1 <= self.backend.can_post('hello', public=True, max_age=3600*24*3) + assert 1 <= self.backend.can_post('hello', public=True, max_age=False) + assert 1 <= self.backend.can_post('hello', public=None, max_age=False) + assert 1 <= self.backend.can_post('hello', public=True, max_age=3600*24*40) + assert 1 <= self.backend.can_post(u'héhé', public=True) assert 0 == self.backend.can_post(u'hello ♥', public=True) diff --git a/weboob/backends/pastebin/backend.py b/weboob/backends/pastebin/backend.py index 2917a916..d2170da0 100644 --- a/weboob/backends/pastebin/backend.py +++ b/weboob/backends/pastebin/backend.py @@ -63,10 +63,12 @@ class PastebinBackend(BaseBackend, BasePasteBackend): def new_paste(self, *args, **kwargs): return PastebinPaste(*args, **kwargs) - def can_post(self, contents, public=None, max_age=None): + def can_post(self, contents, title=None, public=None, max_age=None): if max_age is not None: if self.get_closest_expiration(max_age) is None: return 0 + if not title or len(title) <= 60: + return 2 return 1 def get_paste(self, _id): diff --git a/weboob/capabilities/paste.py b/weboob/capabilities/paste.py index e3aba399..5e555652 100644 --- a/weboob/capabilities/paste.py +++ b/weboob/capabilities/paste.py @@ -64,13 +64,14 @@ class ICapPaste(IBaseCap): """ raise NotImplementedError() - def can_post(self, contents, public=None, max_age=None): + def can_post(self, contents, title=None, public=None, max_age=None): """ Checks if the paste can be pasted by this backend. Some properties are considered required (public/private, max_age) while others are just bonuses (language). contents: Can be used to check encodability, maximum length, etc. + title: Can be used to check length, allowed characters. Should not be required. public: True must be public, False must be private, None do not care. max_age: Maximum time to live in seconds.