def test_adding_user_twice_doesnt_cause_dupes(self): user = User("Joe Bloggs", "*****@*****.**", location_func=lambda: ((0, 0), 0)) users = Users() users.add_user(user) users.add_user( User("Joe Bloggs", "*****@*****.**", location_func=lambda: ((90, 90), 0))) self.assertEqual(len(users.users), 1)
def test_supplying_better_location_function_overrides(self): user = User("Joe Bloggs", "*****@*****.**", location_func=lambda: ((0, 0), 0)) users = Users() users.add_user(user) users.add_user( User("Joe Bloggs", "*****@*****.**", location_func=lambda: ((90, 90), 1))) self.assertEqual( users.get_user_by_name(u"Joe Bloggs").location, (90, 90))
def add_sender(self, message): def email_location(): recieved = message.get_all('Original-Received') ips = [IP.findall(h) for h in recieved] ips = [ ip[0] for ip in ips if ip and not ip[0].startswith("10.") and not ip[0].startswith("192.168") ] likely = ips[-1] try: logger.info("geocoder: Getting location for %s" % (likely)) url = "http://freegeoip.net/json/%s" % likely logger.debug("geocoder: Fetching %s" % (url)) loc = json.loads(urllib2.urlopen(url).read()) ll = float(loc['latitude']), float(loc['longitude']) if any(ll): return ll, 0 except: pass users = getUtility(IUserDatabase) from_ = list(email.utils.parseaddr(message.get("From"))) # Remove quoted printable from_[0] = decode_header(from_[0])[0] encoding = from_[0][1] if encoding is None: encoding = "utf-8" from_[0] = from_[0][0].decode(encoding) users.add_user(User(from_[0], from_[1], location_func=email_location))
def test_get_location(self): user = User("Joe Bloggs", "*****@*****.**", location_func=lambda: ((0, 0), 0)) users = Users() users.add_user(user) self.assertEqual( users.get_user_by_name(u"Joe Bloggs").location, (0, 0))
def _get_github_accounts(self, accounts, token): users = getUtility(IUserDatabase) i = 0.0 for account in accounts: i+=1 if account in ALREADY_FOUND: continue url = "https://api.github.com/users/%s?access_token=%s" % (account,token) logger.debug("github: getting %s" % url) gh_api = urllib2.urlopen(url) user = json.loads(gh_api.read()) ALREADY_FOUND.add(account) users.add_user(User(user.get('name', user['login']), user.get('email', None), username=user['login'], location_func=lazy_location(user)))
def get_github_accounts(accounts, token): geocoder = ggeocoder.Geocoder() users = getUtility(IUserDatabase) i = 0.0 for account in accounts: i += 1 print "%2d%% complete" % ((i / len(accounts)) * 100.0) gh_api = urllib2.urlopen( "https://api.github.com/users/%s?access_token=%s" % (account, token)) user = json.loads(gh_api.read()) try: location = geocoder.geocode(user['location'])[0].coordinates except: location = None users.add_user( User(user.get('name', user['login']), user.get('email', None), location))
def test_search_by_name(self): user = User("Joe Bloggs", "*****@*****.**") users = Users() users.add_user(user) self.assertEqual(users.get_user_by_name("Joe Bloggs"), user)
def test_search_by_name_matches_with_missing_accents(self): user = User("Joe Bloggs", "*****@*****.**") users = Users() users.add_user(user) self.assertEqual(users.get_user_by_name(u"Joé Bloggs"), user)