def testFromAnything(self): people = People.from_anything('[email protected]') self.assertEqual(people.userid, '[email protected]') people = People.from_anything(people) self.assertEqual(people.userid, '[email protected]') people = People.from_anything(('tester', 'dev.progval.42')) self.assertEqual(people.userid, '[email protected]')
def testBiography(self): people = People('tester', 'test.wididit.net', 'foo', connect=True) self.assertEqual(people.biography, 'biography of user [email protected]') self.queries = [] people.biography = 'foo' self.assertEqual(self.queries.pop(), ('people', '*****@*****.**', { 'username': '******', 'biography': 'foo'})) self.assertEqual(people.biography, 'foo')
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 __init__(self, author, id=None, **initial_data): author = People.from_anything(author) self._author = author self._id = id if id is None: self._sync(initial_data) else: self._sync() missing_attr = [x for x in self._fields if not hasattr(self, x)] assert len(missing_attr) == 0, \ 'This attributes are missing: %s' % ', '.join(missing_attr)
def filterAuthor(self, author): """Only get results by this author. Using this method twice is handled as a OR clause by the server. :param author: A valid representation of a people. """ if 'author' not in self._params: self._params['author'] = [] self._params['author'].append(People.from_anything(author).userid) return self
def new_callback(new_userid, new_password): global username, hostname, password try: username, hostname = userid2tuple(new_userid) except: return False password = new_password if People(username, hostname, password, connect=True).authenticated: return callback(new_userid, new_password) else: return False
def _init_tabs(self): self._tabs = {'main': {}, 'showuser': {}} tabs = conf.get(['look', 'mainwindow', 'tabs', 'opened', 'main'], ['timeline', 'all', 'own']) for tab in tabs: self.openmaintab(tab) users = conf.get(['look', 'mainwindow', 'tabs', 'opened', 'showuser'], []) for userid in users: self.showuser(People.from_anything(userid), raises=False)
def fetch(self): """Return all entries matching this query. """ response = self._server.get(self._url, params=self._params) if response.status_code != requests.codes.ok: raise exceptions.ServerException(response.status_code) reply = self._server.unserialize(response.content) entries = [] for data in reply: entry = Entry(People.from_anything(data['author']), data['id'], initial_data=data) entries.append(entry) return entries
def showuser(self, people, raises=True): try: people = People.from_anything(people) except exceptions.PeopleNotInstanciable: if raises: dialog = QtGui.QMessageBox.critical(self, # 'Invalid user' dialog title. _('Cannot show user.'), # 'Invalid user' dialog content. _('The given userid is not valid. Try again.')) return except exceptions.Unreachable: if raises: dialog = QtGui.QMessageBox.critical(self, # 'Invalid user' dialog title. _('Cannot show user.'), # 'Invalid user' dialog content. _('The given userid does not exist. ' 'The server hosting him might be down.')) return except exceptions.NotFound: if raises: dialog = QtGui.QMessageBox.critical(self, # 'Invalid user' dialog title. _('Cannot show user.'), # 'Invalid user' dialog content. _('This user does not exist on the server. ' 'Check your input.')) return if people.userid not in self._tabs['showuser']: title = people.userid widget = PeopleWidget(self, people, with_entries=True) value = (widget, title) self._tabs['showuser'][people.userid] = value self.centralWidget().addTab(*value) self.centralWidget().setCurrentIndex( self.centralWidget().indexOf( self._tabs['showuser'][people.userid][0]))
def __new__(cls, author, id=None, *args, **kwargs): author = People.from_anything(author) return super(Entry, cls).__new__(cls, author, id)
def get_people(): global username, hostname, password if None in (username, hostname, password): return None else: return People(username, hostname, password, connect=True)