CapPaste: Support expiration for posting, as a max_age parameter

This commit is contained in:
Laurent Bachelier 2011-04-30 00:26:23 +02:00
commit f63180c187
10 changed files with 71 additions and 27 deletions

View file

@ -20,7 +20,7 @@
from __future__ import with_statement
from weboob.capabilities.paste import ICapPaste
from weboob.tools.capabilities.paste import BasePasteBackend
from weboob.tools.backend import BaseBackend
from weboob.capabilities.base import NotLoaded
@ -31,7 +31,7 @@ from .paste import PastealaconPaste
__all__ = ['PastealaconBackend']
class PastealaconBackend(BaseBackend, ICapPaste):
class PastealaconBackend(BaseBackend, BasePasteBackend):
NAME = 'pastealacon'
MAINTAINER = 'Laurent Bachelier'
EMAIL = 'laurent@bachelier.name'
@ -40,12 +40,21 @@ class PastealaconBackend(BaseBackend, ICapPaste):
LICENSE = 'AGPLv3+'
BROWSER = PastealaconBrowser
EXPIRATIONS = {
24*3600: 'd',
24*3600*30: 'm',
False: 'f',
}
def new_paste(self, *args, **kwargs):
return PastealaconPaste(*args, **kwargs)
def can_post(self, public=None):
def can_post(self, public=None, max_age=None):
if public is False:
return 0
if max_age is not None:
if self.get_closest_expiration(max_age) is None:
return 0
return 1
def get_paste(self, _id):
@ -65,8 +74,12 @@ class PastealaconBackend(BaseBackend, ICapPaste):
self.browser.fill_paste(paste)
return paste
def post_paste(self, paste):
def post_paste(self, paste, max_age = None):
if max_age is not None:
expiration = self.get_closest_expiration(max_age)
else:
expiration = None
with self.browser:
self.browser.post_paste(paste)
self.browser.post_paste(paste, expiration=self.EXPIRATIONS.get(expiration))
OBJECTS = {PastealaconPaste: fill_paste}

View file

@ -66,9 +66,9 @@ class PastealaconBrowser(BaseBrowser):
except BrowserHTTPNotFound:
raise PasteNotFound()
def post_paste(self, paste):
def post_paste(self, paste, expiration=None):
self.home()
self.page.post(paste)
self.page.post(paste, expiration=expiration)
if self.is_on_page(CaptchaPage):
raise BrowserUnavailable("Detected as spam and unable to handle the captcha")
paste.id = self.page.get_id()

View file

@ -47,11 +47,12 @@ class PastePage(BasePage):
return self.group_dict['id']
class PostPage(BasePage):
def post(self, paste):
def post(self, paste, expiration=None):
self.browser.select_form(name='editor')
self.browser['code2'] = paste.contents.encode(self.browser.ENCODING)
self.browser['poster'] = paste.title.encode(self.browser.ENCODING)
self.browser['expiry'] = ['m']
if expiration:
self.browser['expiry'] = [expiration]
self.browser.submit()
class CaptchaPage(BasePage):

View file

@ -46,7 +46,7 @@ class PastealaconTest(BackendTest):
def test_post(self):
p = self.backend.new_paste(None, title='ouiboube', contents=u'Weboob Test héhéhé')
self.backend.post_paste(p)
self.backend.post_paste(p, max_age=3600*24)
assert p.id
self.backend.fill_paste(p, ['title'])
assert p.title == 'ouiboube'
@ -83,3 +83,9 @@ class PastealaconTest(BackendTest):
def test_can_post(self):
assert 0 == self.backend.can_post(public=False)
assert 1 == self.backend.can_post(public=True)
assert 0 == self.backend.can_post(public=True, max_age=600)
assert 1 == self.backend.can_post(public=True, max_age=3600*24)
assert 1 == self.backend.can_post(public=True, max_age=3600*24*3)
assert 1 == self.backend.can_post(public=True, max_age=False)
assert 1 == self.backend.can_post(public=None, max_age=False)
assert 1 == self.backend.can_post(public=True, max_age=3600*24*40)