def _sync(self, initial_data=None): if initial_data is None: assert self.id is not None response = self.author.server.get(self.api_path) if response.status_code == requests.codes.not_found: raise exceptions.NotFound(_('entry %s') % self.entryid) elif response.status_code != requests.codes.ok: raise exceptions.ServerException(response.status_code) reply = self.author.server.unserialize(response.content) self._content = reply['content'] self._category = reply['category'] self._contributors = [People.from_anything(x) for x in reply['contributors']] self._generator = reply['generator'] self._published = time.strptime(reply['published'], self._time_format) self._rights = reply['rights'] self._source = reply['source'] self._subtitle = reply['subtitle'] self._summary = reply['summary'] self._title = reply['title'] self._updated = time.strptime(reply['updated'], self._time_format) else: assert self.id is None initial_data['author'] = self.author.userid if 'generator' not in initial_data: initial_data['generator'] = 'Wididit python library' response = self.author.server.post(self.api_path, data=initial_data) if response.status_code == requests.codes.forbidden: raise exceptions.Forbidden(_('create an entry')) elif response.status_code != requests.codes.created: raise exceptions.ServerException(response.status_code) else: self._id = int(response.content) self._sync()
def set_biography(self, value): response = self.server.put(self.api_path, data={ 'username': self.username, 'biography': value }) if response.status_code == requests.codes.forbidden: raise exceptions.Forbidden(_('change the biography')) elif response.status_code == requests.codes.unauthorized: raise exceptions.Forbidden(_('change the biography')) elif response.status_code != requests.codes.ok: raise exceptions.ServerException(response.status_code) self._biography = value
def from_anything(data): """Return a People instance from any supported representation. Supported representation are People instances, userid strings, (username, hostname) tuples, and dictionnaries from server reply. :param data: A representation of a People object. """ if isinstance(data, People): return data elif isinstance(data, str) or isinstance(data, unicode): try: username, hostname = utils.userid2tuple(data) except ValueError: raise exceptions.PeopleNotInstanciable( _('hostname is missing.')) return People(username, hostname) elif isinstance(data, tuple) and len(data) == 2: return People(*data) elif isinstance(data, dict) and 'username' in data and \ 'server' in data and isinstance(data['server'], dict) and \ 'hostname' in data['server']: return People(data['username'], data['server']['hostname']) else: raise ValueError('Invalid representation of People object: %r' % data)
def set_password(self, value): response = self.server.put(self.api_path, data={'password': value}) if response.status_code == requests.codes.forbidden: raise exceptions.Forbidden(_('change the password')) elif response.status_code != requests.codes.ok: raise exceptions.ServerException(response.status_code) self._password = value
def _sync(self): response = self.server.get(self.api_path) if response.status_code == requests.codes.not_found: raise exceptions.NotFound(_('user %s') % self.userid) elif response.status_code != requests.codes.ok: raise exceptions.ServerException(response.status_code) response = self.server.unserialize(response.content) self._biography = response['biography']
def set_property(self, value): if assert_type is not None and not isinstance(value, assert_type): raise ValueError() state = self.as_serializable state[name] = value response = self.author.server.put(self.api_path, data=state) if response.status_code == requests.codes.ok: setattr(self, '_' + name, value) response = self.author.server.get(self.api_path) assert response.status_code == requests.codes.ok reply = self.author.server.unserialize(response.content) self._updated = time.strptime(reply['updated'], self._time_format) elif response.status_code == requests.codes.forbidden: raise exceptions.Forbidden(_('edit this entry')) else: raise exceptions.ServerException(response.status_code)
def __init__(self, action): super(Forbidden, self).__init__( _('You are not authorized to %(action)s.') % {'action': action})
def __init__(self, object_): super(NotFound, self).__init__( _('%(object)s cannot be found.') % {'object': object_})
def __init__(self, hostname): super(Unreachable, self).__init__( _('Server %(hostname)s cannot be reached.') % {'hostname': hostname})