def __eq__(self, other): """ Compare the userid for equality with `other`. `other` can be anything plausibly on the RHS of a comparison, which can include other SQL clause elements or expressions, as in User.userid == sa.tuple_(User.username, Group.authority) or literals, as in User.userid == 'acct:[email protected]' We treat the literal case specially, and split the string into username and authority ourselves. If the string is not a well-formed userid, the comparison will always return False. """ if isinstance(other, string_types): try: val = split_user(other) except ValueError: # The value being compared isn't a valid userid return False else: other = sa.tuple_(_normalise_username(val["username"]), val["domain"]) return self.__clause_element__() == other
def in_(self, userids): others = [] for userid in userids: try: val = split_user(userid) except ValueError: continue other = sa.tuple_(_normalise_username(val["username"]), val["domain"]) others.append(other) if not others: return False return self.__clause_element__().in_(others)
class AnnoQueryFactory(DbQueryFactory): convert = ( uuid_to_urlsafe, lambda d: datetime.isoformat(d) + '+00:00', # FIXME hack WARNING MAKE SURE ALL TIMESTAMPS THAT GO IN lambda d: datetime.isoformat( d) + '+00:00', # FIXME hack ARE DERIVED FROM datetime.utcnow() None, lambda userid: split_user(userid)['username'], None, lambda lst: [uuid_to_urlsafe(uuid) for uuid in lst], ) query = ( 'SELECT id, created, updated, target_uri, userid, tags, a.references ' 'FROM annotation AS a')
def authority(self): """ Return the authority of the user and group this annotation belongs to. For example, returns "hypothes.is" for Hypothesis first-party annotations, or "elifesciences.org" for eLife third-party annotations. If this annotation doesn't have a userid (which is possible for annotations that haven't been saved to the DB yet) then return None. :raises ValueError: if the annotation's userid is invalid """ if self.userid is None: return None return split_user(self.userid)["domain"]
def test_split_user(): parts = user_util.split_user("acct:[email protected]") assert parts == {"username": "******", "domain": "hypothes.is"}
def test_split_user_no_match(): with pytest.raises(ValueError): user_util.split_user("donkeys")