def test_insert_ignores(self): user1 = db_utils.create_user() user2 = db_utils.create_user() # user1 ignores user2 db_utils.create_ignoreuser(user1, user2) # attempts to follow in either direction throw a WeasylError self.assertRaises(WeasylError, followuser.insert, user1, user2) self.assertRaises(WeasylError, followuser.insert, user2, user1)
def test_insert_ignores(self): user1 = db_utils.create_user() user2 = db_utils.create_user() # user1 ignores user2 db_utils.create_ignoreuser(user1, user2) # attempts to follow in either direction throw a WeasylError self.assertRaises(WeasylError, followuser.insert, user1, user2) self.assertRaises(WeasylError, followuser.insert, user2, user1) self.assertFalse(profile.select_relation(user1, user2)["follow"]) self.assertFalse(profile.select_relation(user2, user1)["follower"])
def test_insert_s_only(self, followuser_insert, followuser_remove, get_config): user1 = db_utils.create_user() user2 = db_utils.create_user() get_config.side_effect = lambda userid: "s" # user1 watches user2 followuser.insert(user1, user2) self.assertEqual(1, d.sessionmaker().query(orm.Follow).count()) followuser_remove.assert_called_once_with(user1, user2) followuser_insert.assert_called_once_with(user1, user2) self.assertEqual("s", d.sessionmaker().query(orm.Follow).first().settings)
def test_mutual_follow(self): user1 = db_utils.create_user() user2 = db_utils.create_user() # user1 watches user2 followuser.insert(user1, user2) # user2 watches user1 followuser.insert(user2, user1) self.assertTrue(profile.select_relation(user1, user2)["follow"]) self.assertTrue(profile.select_relation(user2, user1)["follower"]) self.assertTrue(profile.select_relation(user2, user1)["follow"]) self.assertTrue(profile.select_relation(user1, user2)["follower"])
def test_insert(self, followuser_insert, followuser_remove, get_config): user1 = db_utils.create_user() user2 = db_utils.create_user() get_config.side_effect = lambda userid: "scftj" # user1 watches user2 followuser.insert(user1, user2) self.assertEqual(1, d.sessionmaker().query(orm.Follow).count()) followuser_remove.assert_called_once_with(user1, user2) followuser_insert.assert_called_once_with(user1, user2) self.assertEqual("cfjst", d.sessionmaker().query(orm.Follow).first().settings)
def test_insert_s_only(self, followuser_insert, followuser_remove, get_config): user1 = db_utils.create_user() user2 = db_utils.create_user() get_config.side_effect = lambda userid: "s" # user1 watches user2 followuser.insert(user1, user2) self.assertTrue(profile.select_relation(user1, user2)["follow"]) self.assertTrue(profile.select_relation(user2, user1)["follower"]) self.assertEqual(1, d.sessionmaker().query(orm.Follow).count()) followuser_remove.assert_called_once_with(user1, user2) followuser_insert.assert_called_once_with(user1, user2) self.assertEqual("s", d.sessionmaker().query(orm.Follow).first().settings)
def test_remove(self, followuser_remove): user1 = db_utils.create_user() user2 = db_utils.create_user() # user1 watches user2 followuser.insert(user1, user2) self.assertEqual(1, d.sessionmaker().query(orm.Follow).count()) followuser_remove.reset_mock() # user1 changed their mind followuser.remove(user1, user2) self.assertEqual(0, d.sessionmaker().query(orm.Follow).count()) followuser_remove.assert_called_once_with(user1, user2)
def test_update(self): user1 = db_utils.create_user() user2 = db_utils.create_user() # user1 watches user2 followuser.insert(user1, user2) # user1 updates watch settings followuser.update(user1, user2, followuser.WatchSettings.from_code("cf")) self.assertEqual("cf", d.sessionmaker().query(orm.Follow).first().settings) # again followuser.update(user1, user2, followuser.WatchSettings.from_code("st")) self.assertEqual("st", d.sessionmaker().query(orm.Follow).first().settings)
def test_update(self): user1 = db_utils.create_user() user2 = db_utils.create_user() # user1 watches user2 followuser.insert(user1, user2) # user1 updates watch settings followuser.update(user1, user2, followuser.WatchSettings.from_code("cf")) self.assertEqual("cf", d.sessionmaker().query(orm.Follow).first().settings) # again followuser.update(user1, user2, followuser.WatchSettings.from_code("st")) self.assertTrue(profile.select_relation(user1, user2)["follow"]) self.assertTrue(profile.select_relation(user2, user1)["follower"]) self.assertEqual("st", d.sessionmaker().query(orm.Follow).first().settings)
def test_insert(self, followuser_insert, followuser_remove, get_config): user1 = db_utils.create_user() user2 = db_utils.create_user() get_config.side_effect = lambda userid: "scftj" self.assertFalse(profile.select_relation(user1, user2)["follow"]) # user1 watches user2 followuser.insert(user1, user2) self.assertEqual(1, d.sessionmaker().query(orm.Follow).count()) # profile.select_relation should return u1 follows u2, u2 is follower of u1, u1 is not # follower of u2 self.assertTrue(profile.select_relation(user1, user2)["follow"]) self.assertTrue(profile.select_relation(user2, user1)["follower"]) self.assertFalse(profile.select_relation(user1, user2)["follower"]) followuser_remove.assert_called_once_with(user1, user2) followuser_insert.assert_called_once_with(user1, user2) self.assertEqual("cfjst", d.sessionmaker().query(orm.Follow).first().settings)
def get_userid_by_username(username): """Get a user's user_id given a username""" if db.session.query(User).filter( User.username == username).first() is None: params_find_by_username = base_params() params_find_by_username['method'] = "flickr.people.findByUsername" params_find_by_username['username'] = username response_username = requests.get( API_URL, params=params_find_by_username).json() if response_username['stat'].encode('utf-8') == 'fail': return response_username['stat'] else: user_id = response_username['user']['nsid'].encode('utf-8') user = User(user_id=user_id, username=username) db_utils.create_user(user) else: user = db.session.query(User).filter(User.username == username).one() return user
async def create_user(user_in: UserIn) -> Dict[str, Any]: user_in.password = bcrypt.hashpw(user_in.password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8") user_out = db_utils.create_user(user_in=user_in) if user_out: return { "success": True, "message": "user created successfully", "data": UserOut(pk=str(user_out.id), first_name=user_out.first_name, last_name=user_out.last_name, user_name=user_out.user_name, email=user_out.email) } return {"success": False, "message": "could not create user", "data": {}}
def recommendation_by_geo(lat, lon, per_page=36): """ Get most relavent photos based on given text info(tags, title and description) tags: a comma-delimited list of tags text: list of words Add the photos into db if it's not in it, and return a list of photo_id. """ params = base_params() params['method'] = "flickr.photos.search" params['lat'] = lat params['lon'] = lon params['sort'] = 'interestingness-desc' params['content_type'] = 1 params['accuracy'] = 3 params['extras'] = ','.join( ['description', 'date_taken', 'owner_name', 'geo', 'tags', 'url_q']) params['per_page'] = per_page response = requests.get(API_URL, params=params).json() photos = response['photos']['photo'] #a list of photos photo_ids = [] for p in photos: photo_id = p['id'].encode('utf-8') photo_ids.append(photo_id) if db.session.query(Photo).get(photo_id) is None: user_id = p['owner'].encode('utf-8') username = p['ownername'].encode('utf-8') if db.session.query(User).get(user_id) is None: user = User(user_id=user_id, username=username) db_utils.create_user(user) description = p['description']['_content'].encode('utf-8') tags = p['tags'].encode('utf-8') title = p['title'].encode('utf-8') url = p['url_q'].encode('utf-8') date_taken = p['datetaken'].encode('utf-8') lat = p['latitude'] lon = p['longitude'] if lat == 0 and lon == 0: country_code = None else: country_code = rg.search((lat, lon))[0]['cc'] photo = Photo(photo_id=photo_id, user_id=user_id, username=username, description=description, tags=tags, title=title, url=url, date_taken=date_taken, lat=lat, lon=lon, country_code=country_code) db_utils.add_photo(photo) else: pass return photo_ids
def start(bot, update): chat_id = update.message.chat_id db_utils.create_user(chat_id=chat_id) update.message.reply_text("Hi, I'm microchip. What should I call you?") return 1
def gconnect(): """ Connect to the site via Google :return: Login success/failure message """ if request.args.get("state") != session["state"]: return make_json_response("Invalid state parameter", 401) code = request.data try: # Upgrade the authorization code into a credentials object oauth_flow = flow_from_clientsecrets(CLIENT_SECRETS_PATH, scope="") oauth_flow.redirect_uri = "postmessage" credentials = oauth_flow.step2_exchange(code) except FlowExchangeError: return make_json_response("Failed to upgrade the authorization code", 401) # Check that the access token is valid. access_token = credentials.access_token url = "https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=%s" % access_token h = httplib2.Http() result = json.loads(h.request(url, "GET")[1]) # If there was an error in the access token info, abort. if result.get("error") is not None: return make_json_response(result.get("error"), 500) # Verify that the access token is used for the intended user. gplus_id = credentials.id_token["sub"] if result[SESSION_USER_ID] != gplus_id: return make_json_response("Token user ID doesn't match given user ID", 401) # Verify that the access token is valid for this app. if result["issued_to"] != CLIENT_ID: return make_json_response("Token's client ID does not match app's", 401) # Check to see if user is already logged in stored_credentials = session.get(SESSION_CREDENTIALS) stored_gplus_id = session.get(SESSION_GPLUS_ID) if stored_credentials is not None and gplus_id == stored_gplus_id: return make_json_response("Current user is already connected", 200) # Store the access token in the session for later use. session[SESSION_PROVIDER] = "google" session[SESSION_CREDENTIALS] = access_token session[SESSION_GPLUS_ID] = gplus_id # Get user info userinfo_url = "https://www.googleapis.com/oauth2/v1/userinfo" params = {"access_token": credentials.access_token, "alt": "json"} answer = requests.get(userinfo_url, params=params) data = answer.json() session[SESSION_USERNAME] = data["name"] session[SESSION_PICTURE] = data["picture"] session[SESSION_EMAIL] = data["email"] # See if user exists. If it doesn't, make a new one. user_id = get_user_id(session[SESSION_EMAIL]) if not user_id: user_id = create_user(session) session[SESSION_USER_ID] = user_id output = "" output += "<p>Welcome, %s!</p>" % session[SESSION_USERNAME] output += "<img src=\"%s\" class=\"avatar\">" % session[SESSION_PICTURE] flash("You are now logged in as %s" % session[SESSION_USERNAME], "success") return output