def dispatch_request(self, user_id, other_id, grading): if grading == "0": grading = str(random.randrange(0,5)) else: grading = str(random.randrange(5,10)) user = UserModel.get_by_id(int(user_id)) other = UserModel.get_by_id(int(other_id)) sim = user.cal_sim(other) features = json.loads(user.features) grade_his = json.loads(user.grades) sim_in_feature = [sim[i] for i in features] sim_str = str(sim_in_feature) if sim_str in grade_his: grade_his[sim_str] = grading else: if len(grade_his) > 15: del(grade_his[grade_his.keys()[0]]) grade_his[sim_str] = grading user.grades = json.dumps(grade_his) if len(grade_his) >= 3: import numpy as np x = [] y = [] for key, value in grade_his.items(): x.append(json.loads(key)) y.append(int(value)) xMat = np.mat(x) yMat = np.mat(y).T xTx = xMat.T * xMat if np.linalg.det(xTx) == 0.0: #user.put() return "canot do inverse" + json.dumps(user.grades) weights_learnt_matrix = xTx.I*(xMat.T*yMat) weights_learnt = [a[0] for a in weights_learnt_matrix.tolist()] weights = [0] * 7 for i in range(len(weights_learnt)): weights[features[i]] = weights_learnt[i] user.weights = json.dumps(weights) user.put() u_dict = user.to_dict() u_dict["user_id"] = user.key.id() del u_dict["timestamp"] return json.dumps(u_dict)
def dispatch_request(self, user_id, speech_percent): user = UserModel.get_by_id(int(user_id)) user.speech_percent = float(speech_percent) user.put() u_dict = user.to_dict() u_dict["user_id"] = user.key.id() del u_dict["timestamp"] return json.dumps(u_dict)
def dispatch_request(self, user_id, willing): user = UserModel.get_by_id(int(user_id)) user.willing = int(willing) user.put() u_dict = user.to_dict() u_dict["user_id"] = user.key.id() del u_dict["timestamp"] return json.dumps(u_dict)
def dispatch_request(self, user_id, other_id, grading): if grading == "0": grading = str(random.randrange(0, 5)) else: grading = str(random.randrange(5, 10)) user = UserModel.get_by_id(int(user_id)) other = UserModel.get_by_id(int(other_id)) sim = user.cal_sim(other) features = json.loads(user.features) grade_his = json.loads(user.grades) sim_in_feature = [sim[i] for i in features] sim_str = str(sim_in_feature) if sim_str in grade_his: grade_his[sim_str] = grading else: if len(grade_his) > 15: del (grade_his[grade_his.keys()[0]]) grade_his[sim_str] = grading user.grades = json.dumps(grade_his) if len(grade_his) >= 3: import numpy as np x = [] y = [] for key, value in grade_his.items(): x.append(json.loads(key)) y.append(int(value)) xMat = np.mat(x) yMat = np.mat(y).T xTx = xMat.T * xMat if np.linalg.det(xTx) == 0.0: #user.put() return "canot do inverse" + json.dumps(user.grades) weights_learnt_matrix = xTx.I * (xMat.T * yMat) weights_learnt = [a[0] for a in weights_learnt_matrix.tolist()] weights = [0] * 7 for i in range(len(weights_learnt)): weights[features[i]] = weights_learnt[i] user.weights = json.dumps(weights) user.put() u_dict = user.to_dict() u_dict["user_id"] = user.key.id() del u_dict["timestamp"] return json.dumps(u_dict)
def dispatch_request(self, user_id): user = UserModel.get_by_id(int(user_id)) if request.method == "POST": try: user.key.delete() flash(u'User %s successfully deleted.' % user_id, 'success') return redirect(url_for('admin_list_users')) except CapabilityDisabledError: flash(u'App Engine Datastore is currently in read-only mode.', 'info') return redirect(url_for('admin_list_users'))
def dispatch_request(self, user_id): user = UserModel.get_by_id(user_id) form = UserForm(obj=user) if request.method == "POST": if form.validate_on_submit(): user.name = form.data.get('name') user.put() flash(u'Example %s successfully saved.' % user_id, 'success') return redirect(url_for('list_examples')) return render_template('edit_user.html', user=user, form=form)
def dispatch_request(self, user_id): user = UserModel.get_by_id(user_id) form = UserForm(obj=user) if request.method == "POST": if form.validate_on_submit(): user.name = form.data.get('name') user.put() flash(u'Example %s successfully saved.' % user_id, 'success') return redirect(url_for('list_examples')) return render_template('edit_user.html', user=user, form=form)
def dispatch_request(self, user_id): user = UserModel.get_by_id(int(user_id)) if request.method == "POST": try: user.key.delete() flash(u'User %s successfully deleted.' % user_id, 'success') return redirect(url_for('admin_list_users')) except CapabilityDisabledError: flash(u'App Engine Datastore is currently in read-only mode.', 'info') return redirect(url_for('admin_list_users'))
def dispatch_request(self, user_id, vib, matched_with=None): user = UserModel.get_by_id(int(user_id)) user.vibrate_status = bool(int(vib)) if not user.vibrate_status: user.matched_with = None if matched_with: user.matched_with = int(matched_with) user.put() u_dict = user.to_dict() u_dict["user_id"] = user.key.id() del u_dict["timestamp"] return json.dumps(u_dict)
def dispatch_request(self, user_id, vib, matched_with=None): user = UserModel.get_by_id(int(user_id)) user.vibrate_status = bool(int(vib)) if not user.vibrate_status: user.matched_with = None if matched_with: user.matched_with = int(matched_with) user.put() u_dict = user.to_dict() u_dict["user_id"] = user.key.id() del u_dict["timestamp"] return json.dumps(u_dict)
def dispatch_request(self, user_id, gps): # Replace the API key below with a valid API key. #return "dafsdfsd" try: gmaps = googlemaps.Client( key='AIzaSyCNcSw3hYO2wes__ZHHp_emc7v8vsHkaM0') # Geocoding and address #"geocode_result = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA') # Look up an address with reverse geocoding reverse_geocode_result = gmaps.reverse_geocode(gps) types = { "food": 1, "gym": 2, "cafe": 3, "book_store": 4, "health": 5, "shopping_mall": 6, "library": 7, "park": 8, "university": 9 } places = gmaps.nearest(gps, types=types.keys()) res = {} location_type_index = 0 #unknown res["location_type"] = "unknown" #unknown if places and len(places["results"]) > 0: for t in places["results"][0]["types"]: if t in types.keys(): res["location_type"] = t location_type_index = types[t] user = UserModel.get_by_id(int(user_id)) user.where = reverse_geocode_result[0]["formatted_address"] user.gps = gps + ": " + res["location_type"] if user.locations is None: user.locations = str(location_type_index) elif len(user.locations) < 12 * 24: user.locations += str(location_type_index) else: user.locations = user.locations[1:] + str(location_type_index) user.put() except Exception as e: return json.dumps(places["results"]) + e.message res["locations"] = user.locations return json.dumps(res)
def get(self): user = getUserFromRequest(self.request) userfriendsList = [] if user: allusers = UserModel.all() currentuser = UserModel.get_by_id(user.key().id()) userfriendsList = currentuser.friends self.response.write( templater.render_friends(userSignedIn=True, allusers=allusers, user=user, currentuser=currentuser)) else: self.redirect('/signup')
def dispatch_request(self, user_id, features): user = UserModel.get_by_id(int(user_id)) user.features = "[" + features + "]" weights = [0] * 7 features_list = features.split(",") for f in features_list: weights[int(f)] = 1.0/ len(features_list) user.weights = str(weights) initial_grade = {"[5,5,5]":"5"} user.grades = json.dumps(initial_grade) user.put() u_dict = user.to_dict() u_dict["user_id"] = user.key.id() del u_dict["timestamp"] return json.dumps(u_dict)
def get(self, user_id=None): if not user_id: return {'message': 'user not found'}, 404 user = UserModel.get_by_id(user_id) if user: return { 'username': user.username, 'email': user.email, 'phone': user.phone, 'gender': user.gender, 'birthday': str(user.birthday) } else: return {'message': 'user not found'}, 404
def dispatch_request(self, user_id, features): user = UserModel.get_by_id(int(user_id)) user.features = "[" + features + "]" weights = [0] * 7 features_list = features.split(",") for f in features_list: weights[int(f)] = 1.0 / len(features_list) user.weights = str(weights) initial_grade = {"[5,5,5]": "5"} user.grades = json.dumps(initial_grade) user.put() u_dict = user.to_dict() u_dict["user_id"] = user.key.id() del u_dict["timestamp"] return json.dumps(u_dict)
def dispatch_request(self, user_id): result = {} result["is_matched"] = False result["grades"] = [] user = UserModel.get_by_id(int(user_id)) if user.vibrate_status and user.matched_with is not None: user.vibrate_status = False user.put() match = UserModel.get_by_id(int(user.matched_with)) result["is_matched"] = True result["user_id"] = match.key.id() result["name"] = match.name result["gender"] = match.gender result["photo_url"] = match.photo_url result["similarities"] = user.cal_sim(match) elif user.vibrate_status and user.matched_with is None: my_key = ndb.Key("UserModel", int(user_id)) choose_keys = UserModel.query().fetch(keys_only=True) if my_key in choose_keys: choose_keys.remove(my_key) for choose_key in choose_keys: match = choose_key.get() if match.vibrate_status and match.matched_with is None and user.where is not None and match.where == user.where and user.cal_grade(match) > 10 - user.willing: user.matched_with = match.key.id() user.vibrate_status = False user.put() match.matched_with = int(user_id) match.put() result["is_matched"] = True result["user_id"] = match.key.id() result["name"] = match.name result["gender"] = match.gender result["photo_url"] = match.photo_url result["similarities"] = user.cal_sim(match) return json.dumps(result)
class AuthService: def __init__(self): self.model = UserModel() def login(self, email, password): user = self.model.get_by_field('email', email) if user is None: raise UserNotFound if not check_password_hash(user['password'], password): raise InvalidCredentials self._authorize(user) def register(self, attributes: dict): attributes['password'] = generate_password_hash(attributes['password']) try: user_id = self.model.create(attributes) except sqlite3.IntegrityError as e: raise EmailAlreadyExist from e return self.get_user_profile(user_id) def get_user_profile(self, user_id): user = self.model.get_by_id(user_id) if user is None: raise UserNotFound user.pop("password") return user @classmethod def _authorize(cls, user): session['user_id'] = user['id'] @staticmethod def logout(): session.pop('user_id', None) @staticmethod def get_auth_user_id(): return session.get('user_id')
def post(self): user = getUserFromRequest(self.request) userfriendsList = [] allusers = UserModel.all() currentuser = UserModel.get_by_id(user.key().id()) userfriendsList = currentuser.friends addBtnAction = self.request.get('addfriend') if addBtnAction: otherPersonKey = self.request.get('addfriend') if otherPersonKey not in userfriendsList: currentuser.friends.append(long(otherPersonKey)) currentuser.sample = "really!!!" currentuser.put() time.sleep(0.1) self.response.write( templater.render_friends(userSignedIn=True, allusers=allusers, user=user, currentuser=currentuser))
def dispatch_request(self, user_id, gps): # Replace the API key below with a valid API key. #return "dafsdfsd" try: gmaps = googlemaps.Client(key='AIzaSyCNcSw3hYO2wes__ZHHp_emc7v8vsHkaM0') # Geocoding and address #"geocode_result = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA') # Look up an address with reverse geocoding reverse_geocode_result = gmaps.reverse_geocode(gps) types = {"food":1, "gym":2, "cafe":3, "book_store":4, "health":5, "shopping_mall":6, "library":7, "park":8, "university":9} places = gmaps.nearest(gps, types=types.keys()) res = {} location_type_index = 0 #unknown res["location_type"] = "unknown" #unknown if places and len(places["results"]) > 0: for t in places["results"][0]["types"]: if t in types.keys(): res["location_type"] = t location_type_index = types[t] user = UserModel.get_by_id(int(user_id)) user.where = reverse_geocode_result[0]["formatted_address"] user.gps = gps + ": " + res["location_type"] if user.locations is None: user.locations = str(location_type_index) elif len(user.locations) < 12 * 24: user.locations += str(location_type_index) else: user.locations = user.locations[1:] + str(location_type_index) user.put() except Exception as e: return json.dumps(places["results"]) + e.message res["locations"] = user.locations return json.dumps(res)
def get(self, friend_id): length = 0 friend = UserModel.get_by_id(long(friend_id)) if friend: posts = BlogModel.all() posts.filter('author = ', friend) posts.order('-modified_time') length = posts.count() user = getUserFromRequest(self.request) if user: self.response.write( templater.render_friendprofile(userSignedIn=True, posts=posts, friend_id=friend_id, user=user, length=length, friend=friend)) else: self.redirect("/") else: self.response.write("Friend entry not found")
def load_user(userid): if userid == None: return None id = int(userid) member = UserModel.get_by_id(id) return member
def identity(payload): user_id = payload["identity"] return UserModel.get_by_id(user_id) #userid_mapping.get(user_id, None)
def load_user(userid): if userid == None: return None id = int(userid) member = UserModel.get_by_id(id) return member