def load(): file_path = os.path.join(os.path.dirname(__file__), 'test_data.json') f = open(file_path, 'r') content = f.read().decode('utf8') f.close() data = json.loads(content) users = data['users'] friends = data['friends'] for user in users: pw_md5 = hashlib.md5() pw_md5.update(user['password']) u = User(name=user['name'], password=pw_md5.hexdigest(), head_img_url=user['head_img_url'], last_login_time=datetime.now()) u.insert() for detail in user['user_details']: ud = UserDetail(user_id=u.id, field_name=detail['field_name'], field_value='%s,v' % detail['field_value'], updated_at=datetime.now()) ud.insert() ui = UserIndex(field_name=detail['field_name'], field_value=detail['field_value'], user_id=u.id) ui.insert() for friend in friends: user_id = UserIndex.get_user_id('phone', friend['user_phone']) friend_id = UserIndex.get_user_id('phone', friend['friend_phone']) f = Friend(user_id=user_id, friend_id=friend_id) f.insert() print "u_id: %d, f_id: %d" % (user_id, friend_id) f = Friend(user_id=friend_id, friend_id=user_id) f.insert() print "u_id: %d, f_id: %d" % (friend_id, user_id)
def create_friend(user, team): result = {"error_code": 0, "msg": "ok"} if not team: return {"error_code": 20501, "msg": "team not exist"} f_1 = Friend.select().where(Friend.user == user, Friend.team == team, Friend.ftype == "f").first() if f_1: if f_1.status != "normal": f_1.status = "normal" f_1.save() else: f_1 = Friend() f_1.user = user f_1.team = team f_1.ftype = "f" f_1.save() f_2 = Friend.select().where(Friend.user == user, Friend.team == team, Friend.ftype == "c").first() if f_2: if f_2.status != "normal": f_2.status = "normal" f_2.save() else: f_2 = Friend() f_2.user = user f_2.team = team f_2.ftype = "c" f_2.save() return result
def build_edit_friend_form(home_username, friend_username, review): sql_instance = SQL() result = sql_instance.get_individual_friend( home_username=home_username, friend_username=friend_username) print(result) friend = Friend(username=result[0], user_id=result[1], first_name=result[2], last_name=result[3]) web_img_paths = [] if review: friend_path = os.path.abspath('static/img/out/needs_review/') print(friend_path) image_paths = list(paths.list_images(friend_path)) for image_path in image_paths: file_name = image_path.split('/')[-1] web_path = '../static/img/out/needs_review/' + file_name web_img_paths.append(web_path) print(web_img_paths) else: friend_path = os.path.abspath('static/img/data/' + str(friend.user_id)) print(friend_path) image_paths = list(paths.list_images(friend_path)) for image_path in image_paths: file_name = image_path.split('/')[-1] web_path = '../static/img/data/' + str( friend.user_id) + '/' + file_name web_img_paths.append(web_path) print(web_img_paths) return EditFriendForm(friend=friend, image_paths=web_img_paths)
def build_model_form(username): sql_instance = SQL() results = sql_instance.get_friends(username=username) user_id = sql_instance.get_user_id(username, username) friends = [] for friend in results: friends.append( Friend(username=friend[0], user_id=friend[1], first_name=friend[2], last_name=friend[3])) model_results = sql_instance.get_models_for_user(username=username) models = [] for model in model_results: classification_results = sql_instance.get_all_classifications_for_model( model_id=model[0]) classifications = [] for c in classification_results: orig_path = c[6] print(orig_path) web_path = '../static/img/out/training/' + orig_path.split( '/')[-2] + '/' + orig_path.split('/')[-1] classifications.append( Classification(c[1], c[2], c[3], c[4], c[5], web_path)) models.append( ModelData(model_id=model[0], classifications=classifications, file_path=model[1])) x_vals, y_vals = get_training_data_distribution(username, sql_instance) return ModelsForm(user_id=user_id, friends=friends, models=models, x_vals=x_vals, y_vals=y_vals)
def build_friends_form(username): sql_instance = SQL() results = sql_instance.get_friends(username=username) friends = [] for friend in results: friends.append( Friend(username=friend[0], user_id=friend[1], first_name=friend[2], last_name=friend[3])) return FriendsForm(friends=friends)
def sign_up(): if request.method == "GET": return render_template("sign_up.html") else: form = request.form fullname = form["fullname"] username = form["username"] email = form["email"] password = form["password"] confirm = form["confirm"] bday = form["bday"] gender = form["gender"] phone = form["telephone"] existUsername = User.objects(username=username).first() existEmail = User.objects(email=email).first() error = None if fullname == "" or username == "" or email == "" or password == "" or confirm == "" or bday == "" or gender == "" or phone == "": error = "Bạn phải điền đầy đủ thông tin" return render_template("sign_up.html", error=error) elif existUsername is not None: error = "Tên đăng nhập đã tồn tại" return render_template("sign_up.html", error=error) elif len(username) < 8: error = "Tên người dùng không khả dụng" return render_template("sign_up.html", error=error) elif len(password) < 8: error = "Mật khẩu không khả dụng" return render_template("sign_up.html", error=error) elif confirm != password: error = "Rất tiếc. Xác nhận mật khẩu sai" return render_template("sign_up.html", error=error) else: m = User( fullname=fullname, username=username, email=email, password=password, birthday=bday, gender=gender, phone=phone, avt= "https://cdn1.iconfinder.com/data/icons/ninja-things-1/1772/ninja-simple-512.png" ) m.save() att = Attribute(username=username) att.save() hstr_list = All_history(user=username) hstr_list.save() Friend(username=username).save() return redirect("/sign_in")
def respond_friend_request(json_req, user): status = 0 friend_mobile = json_req['friendMobile'] action = json_req['action'] # True: Accept, False: Reject friend_user = db_session.query(User).filter_by( Mobile=friend_mobile).first() if friend_user: user_id = user.UserID friend_user_id = friend_user.UserID if not (__friendship__(user_id, friend_user_id) or user_id == friend_user_id): friend_request_received = db_session.query( FriendRequest).filter_by(UserID=friend_user_id, FriendUserID=user_id, Deleted=False).first() if friend_request_received: if action: # True: Accept new_friend = Friend(UserID=friend_user_id, FriendUserID=user_id) db_session.add(new_friend) friend_request_received.AcceptanceStatus = action friend_request_received.Deleted = True friend_request_received.RespondOn = datetime.datetime.utcnow() db_session.commit() # FCM Notification data = dict(type=notification_type.FRIEND_REQUEST_RESPOND, acceptanceStatus=action, friendUserID=user_id, friendName=user.Name, friendMobile=user.Mobile, imageURL=images.get_serving_url(user.ImageKey, secure_url=True) if user.ImageKey else None) send_data(friend_user_id, data) status = 1 message = 'success' else: message = 'No such friend request' else: message = 'You are already friend' else: message = 'User with mobile {} doesn\'t exist'.format(friend_mobile) return jsonify(status=status, message=message)
def generate_friends(session): from faker import Faker import random from models.friend import Friend faker = Faker() profile_ids_available = list(range(1, 5001)) profiles_used = [] for i in range(1, 5001): profile_id = random.choice(profile_ids_available) while profile_id in profiles_used: profile_id = random.choice(profile_ids_available) friend = Friend() friend.friend_id = i friend.profile_id = profile_id session.add(friend) profiles_used.append(profile_id) session.commit()
def new_friend(): current_user = User.get_by_id(get_jwt_identity()) approve_user = User.get_by_id(request.form.get("id")) friend = Friend(user1_id=current_user.id, user2_id=approve_user.id) if friend.save(): delete = FriendRequest.delete().where( FriendRequest.sender_id == approve_user.id, FriendRequest.recipient_id == current_user.id) delete.execute() return jsonify({ "successful": True, "message": "Friend request has been accepted!" }) else: return jsonify({"error": "Something went wrong!"})