From 556f2745cf5bb26734cf203b8d735ade62b6a1d7 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Thu, 12 Sep 2013 16:02:20 +0200 Subject: [PATCH] Changed fetching languages in diaspy/settings.py, setLanguage() now raises exception instead of returning status code --- Changelog.markdown | 2 ++ diaspy/settings.py | 20 ++++++-------------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/Changelog.markdown b/Changelog.markdown index e581132..942ee5e 100644 --- a/Changelog.markdown +++ b/Changelog.markdown @@ -45,6 +45,8 @@ And the test suite was updated. Yay! * __upd__: `diaspy.settings.Account.setEmail()` can now raise `SettingsError` when request fails, +* __upd__: `diaspy.settings.Account.getEmail()` will now return empty string instead of raising an exception if cannot fetch mail, +* __upd__: improved language fetching in `diaspy.settings.Account.getLanguages()`, **`0.4.1-rc.3` (2013-09-08):** diff --git a/diaspy/settings.py b/diaspy/settings.py index da6f183..ded2258 100644 --- a/diaspy/settings.py +++ b/diaspy/settings.py @@ -15,6 +15,7 @@ class Account(): """Provides interface to account settings. """ email_regexp = re.compile('(.*?)') def __init__(self, connection): self._connection = connection @@ -83,8 +84,8 @@ class Account(): """ data = self._connection.get('user/edit') email = self.email_regexp.search(data.text) - if email is None: raise errors.DiaspyError('cannot fetch email') - email = email.group(1) + if email is None: email = '' + else: email = email.group(1) return email def setLanguage(self, lang): @@ -94,24 +95,15 @@ class Account(): """ data = {'_method': 'put', 'utf8': '✓', 'user[language]': lang, 'authenticity_token': repr(self._connection)} request = self._connection.post('user', data=data, allow_redirects=False) - return request.status_code + if request.status_code != 302: + raise errors.SettingsError('setting language failed: {0}'.format(request.status_code)) def getLanguages(self): """Returns a list of tuples containing ('Language name', 'identifier'). One of the Black Magic(tm) methods. """ - selection_start = '' - languages = [] request = self._connection.get('user/edit') - data = request.text[request.text.find(selection_start)+len(selection_start):] - data = data[:data.find(selection_end)].split('\n') - for item in data: - name = item[item.find('>')+1:item.rfind('<')] - identifier = item[item.find('"')+1:] - identifier = identifier[:identifier.find('"')] - languages.append((name, identifier)) - return languages + return self.language_option_regexp.findall(request.text) class Privacy(): -- 2.25.1