Esempio n. 1
0
def acceptFriend():
    #writeDirtyLog('acceptFriend: ' + request.form['data'])
    result = dict(
        type=ProtocolTypes.AcceptFriend,
        result=ResultCodes.Success)

    if request.method == 'POST' and request.form['data']:
        got_data = json.loads(request.form['data'])

        from_keys = ['session_id', 'accept_friend']
        if checkContainKeys(from_keys, got_data):
            if (got_data['accept_friend'] is None) or (got_data['accept_friend'] == ''):
                result['result'] = ResultCodes.InputParamError
            else:
                result['result'], got_user = checkSessionId(got_data['session_id'])

                if got_user:
                    find_friend = Friend.query.filter_by(
                        user_id=got_data['accept_friend'], friend_id=got_user.id, requested=True, accepted=False).first()
                    if find_friend:
                        find_friend.accepted = True
                        db_session.add(find_friend)
                        my_friend = Friend.query.filter_by(
                            user_id=got_user.id, friend_id=got_data['accept_friend']).first()
                        if my_friend:
                            my_friend.request = True
                            my_friend.accepted = True
                            db_session.add(my_friend)
                        else:
                            regist_friend = Friend(got_user.id, got_data['accept_friend'])
                            regist_friend.request = True
                            regist_friend.accepted = True
                            db_session.add(regist_friend)
                        result['result'] = commitData()
                    else:
                        result['result'] = ResultCodes.NoData
        else:
            result['result'] = ResultCodes.InputParamError
    else:
        result['result'] = ResultCodes.AccessError

    return str(json.dumps(result))
Esempio n. 2
0
def acceptFriendWithTara(user_nickname):
    got_user = User.query.filter_by(nickname=user_nickname).first()
    if got_user:
        regist_friend = Friend(got_user.id, 1)
        regist_friend.request = True
        regist_friend.accepted = True
        db_session.add(regist_friend)

        tara_friend = Friend(1, got_user.id)
        tara_friend.request = True
        tara_friend.accepted = True
        db_session.add(tara_friend)

        db_session.commit()
def hotspots_handler():
    if request.method == 'GET':
        friends = Friend.query.\
            all()
        return make_json_response([f.to_json() for f in friends], 200)

    if request.method == 'POST':
        data = request.json
        f = Friend()
        f.name = data.get('name')
        f.birthday = data.get('birthday')
        f.height = data.get('height')
        f.image_url = data.get('image_url')
        f.notes = data.get('notes')
        f.is_special = data.get('is_special')
        db.session.add(f)
        try:
            db.session.commit()
        except:
            db.session.rollback()
            return make_json_response({'error': 'failed'}, 400)
        return make_json_response(f.to_json(), 200)
    def __construct_friend_list(self):
        name_counter = Counter()
        inbox_messages_path = paths.get_message_inbox_path(self.root_path)
        friends: List[Friend] = []
        self.__name_inference_counter = Counter()
        for inbox in os.scandir(inbox_messages_path):
            if not inbox.is_dir():
                continue
            msg_one_json = utils.read_json(os.path.join(inbox, "message_1.json"))

            thread_type: str = msg_one_json["thread_type"]
            # only dealing with 1-on-1 messages, not groups yet.
            if thread_type != "Regular":
                continue

            printable_name: str = msg_one_json["title"].strip()
            formatted_name: str = printable_name.replace(" ", "").lower()

            # to only load messages from people that you are friend with
            if formatted_name not in self._friend_names:
                continue

            # to deal with duplicate names (both first and last)
            if formatted_name in name_counter:
                formatted_name += "_" + str(name_counter[formatted_name])
            name_counter[formatted_name] += 1

            messages, oldest_message = self.__read_messages(inbox.path)

            friends.append(
                Friend(
                    name=formatted_name,
                    printable_name=printable_name,
                    timestamp=oldest_message,
                    messages=messages
                )
            )
        self.sender_name = self.__name_inference_counter.most_common(1)[0][0].lower().replace(" ", "")
        return friends
Esempio n. 5
0
def add_friend(secure_channel, request):
    ''' 添加好友 '''
    username = secure_channel_to_username[secure_channel]
    to_username = request.strip()

    session = Session()
    from_user = session.query(User).\
        filter(User.username == username).\
        first()
    user = session.query(User).\
        filter(User.username == to_username).first()

    result = {'success': False, 'message': ''}
    if not user:
        result['message'] = '用户不存在!'
        secure_channel.send(Command.ADD_FRIEND_RESULT, result)
        return

    if user.username == username:
        result['message'] = "无法添加自己为好友!"
        secure_channel.send(Command.ADD_FRIEND_RESULT, result)
        return

    applyed = session.query(Friend).\
        filter(Friend.from_user == username,
               Friend.to_user == user.username,
               Friend.accepted == False).count()
    if applyed:
        result['message'] = '已经申请过!'
        secure_channel.send(Command.ADD_FRIEND_RESULT, result)
        return

    if _is_friend(session, username, user.username):
        result['message'] = '好友已存在!'
        secure_channel.send(Command.ADD_FRIEND_RESULT, result)
        return

    requested = session.query(Friend).\
        filter(Friend.from_user == user.username,
               Friend.to_user == username,
               Friend.accepted == False).\
        first()
    if requested:
        requested.accepted = True
        session.add(requested)
        session.commit()
        contact_info = user.username
        secure_channel.send(Command.NEW_CONTACT, contact_info)
        if user.username in username_to_secure_channel:
            contact_info = from_user.username
            username_to_secure_channel[user.username].\
                send(Command.NEW_CONTACT, contact_info)
        return

    friend = Friend(from_user=from_user.username, to_user=user.username)
    session.add(friend)
    session.commit()
    result['success'] = True
    secure_channel.send(Command.ADD_FRIEND_RESULT, result)

    if user.username in username_to_secure_channel:
        data = from_user.username
        username_to_secure_channel[user.username].\
            send(Command.INCOMMING_FRIEND_REQUEST, data)
Esempio n. 6
0
def save_friends(user_id, friend_jarray):
    for name in friend_jarray:
        friend = Friend()
        friend.user_id = user_id
        friend.friends = name
        friend.save()
Esempio n. 7
0
def immed():
    if request.method == 'GET':
        # is_login = request.cookies.get('name')
        is_login = session.get('name')
        if not is_login:
            print('未登录')
            return redirect('/login/')
        else:
            return render_template('immed.html', is_login=is_login)
    else:
        print(request.form)
        # id = request.cookies.get('id')
        id = session.get('name')
        user = User.query.filter_by(iphone=id).first()
        nickname = request.form.get('nickname')
        sex = request.form.get('sex')
        birth = request.form.get('birth')
        age = request.values.get("age")
        high = request.form.get('high')
        image1 = request.files.get('image')
        filename = secure_filename(image1.filename)
        img = filename
        image1.save(os.path.join(BASE_DIR + '/static/home/media', filename))
        education = request.form.get('education')
        profession = request.form.get('profession')
        property = request.form.get('property')
        income = request.form.get('income')
        residence = request.form.get('residence')
        register = request.form.get('register')
        qq = request.form.get('qq')
        wechat = request.form.get('wechat')
        email = request.form.get('email')
        f_age = request.form.get('f_age')
        f_high = request.form.get('f_high')
        f_income = request.form.get('f_income')
        f_education = request.form.get('f_education')
        f_register = request.form.get('f_register')
        f_residence = request.form.get('f_residence')
        try:
            user_info = User_info(user_id=user.id,
                                  nickname=nickname,
                                  image=img,
                                  sex=sex,
                                  birth=birth,
                                  age=age,
                                  high=high,
                                  education=education,
                                  profession=profession,
                                  income=income,
                                  property=property,
                                  residence=residence,
                                  register=register,
                                  qq=qq,
                                  wechat=wechat,
                                  email=email)
            friend = Friend(
                user_id=user.id,
                f_age=f_age,
                f_high=f_high,
                f_education=f_education,
                f_income=f_income,
                f_register=f_register,
                f_residence=f_residence,
            )
            db.session.add(user_info)
            db.session.add(friend)
            db.session.commit()
        except Exception as e:
            print(e)
            return Response('用户信息提交失败')
        return redirect('/')