def recover_house():
    email = request.args.get('email')
    password = request.args.get('password')
    house_id = int(request.args.get('house_id'))
    Post = []
    Deleted = []
    if (control.verify(email, password)):
        user = control.getuser(email)
        user['deleted_post'].remove(str(house_id))
        try:
            user['property'].append(str(house_id))
        except:
            user['property'] = str(house_id)
        house.recover_house(house_id)
        for i in user['property']:
            Post.append(house.readhouse(i))

        for i in user['deleted_post']:
            Deleted.append(house.read_deleted_house(i))
        control.putuser(email, user['user_name'], user['birthday'], password,
                        user['phone number'], user['address'],
                        user['property'], user['deleted_post'])
    else:
        return
    birthday = '-'.join(user['birthday'].split('/')[::-1])
    return render_template('person_information_page.html',
                           email=email,
                           password=password,
                           user_name=user['user_name'],
                           birthday=birthday,
                           address=user['address'],
                           phone_number=user['phone number'],
                           Post=Post,
                           Deleted=Deleted)
def personal_info():
    email = request.args.get('email')
    password = request.args.get('password')
    # print(email, password)
    user = dict()
    Post = []
    Deleted = []
    if (control.verify(email, password)):
        user = control.getuser(email)
        post = user['property']
        for i in post:
            Post.append(house.readhouse(i))
        try:
            for i in user['deleted_post']:
                Deleted.append(house.read_deleted_house(i))
        except:
            pass
    else:
        return
    birthday = '-'.join(user['birthday'].split('/')[::-1])
    return render_template('person_information_page.html',
                           email=email,
                           password=password,
                           user_name=user['user_name'],
                           birthday=birthday,
                           address=user['address'],
                           phone_number=user['phone number'],
                           Post=Post,
                           Deleted=Deleted)
def post_house():
    email = request.form.get('email')
    password = request.form.get('password')
    if request.method == 'POST':
        title = request.form.get('title')
        price = request.form.get('price')
        guest = request.values.get('guest')
        bedroom = request.values.get('bedroom')
        bed = request.values.get('bed')
        bath = request.values.get('bath')
        content = request.values.get('content')
        policy = request.values.get('policy')
        options1 = request.values.getlist("options1")
        options2 = request.values.getlist("options2")
        picname = None
        try:
            picname = [request.files['pic']]
        except:
            pass
        max_index = control.puthouse(email, password, picname, title, guest,
                                     bedroom, bed, bath, price, options1,
                                     options2, policy, content)
        Post = []
        Deleted = []
        if (control.verify(email, password)):
            user = control.getuser(email)
            user['property'].append(str(max_index))
            try:
                control.putuser(email, user['user_name'], user['birthday'],
                                password, user['phone number'],
                                user['address'], user['property'],
                                user['deleted_post'])
            except:
                control.putuser(email, user['user_name'], user['birthday'],
                                password, user['phone number'],
                                user['address'], user['property'])
            for i in user['property']:
                Post.append(house.readhouse(i))
            print(Post)
            try:
                for i in user['deleted_post']:
                    Deleted.append(house.read_deleted_house(i))
            except:
                pass
            print(Deleted)
            birthday = '-'.join(user['birthday'].split('/')[::-1])
            return render_template('person_information_page.html',
                                   email=email,
                                   password=password,
                                   user_name=user['user_name'],
                                   birthday=birthday,
                                   address=user['address'],
                                   phone_number=user['phone number'],
                                   Post=Post,
                                   Deleted=Deleted)
    else:
        return render_template('person_information_page.html')
def remove_house():
    email = request.args.get('email')
    password = request.args.get('password')
    house_id = int(request.args.get('house_id'))
    Post = []
    Deleted = []
    Collect = []
    if (control.verify(email, password)):
        user = control.getuser(email)
        user['property'].remove(str(house_id))
        post = user['property']
        # print(post)
        house.remove_house(house_id)
        try:
            for i in post:
                Post.append(house.readhouse(i))
        except:
            pass

        try:
            user['deleted_post'].append(str(house_id))
        except:
            user['deleted_post'] = [str(house_id)]
        try:
            for i in user['deleted_post']:
                Deleted.append(house.read_deleted_house(i))
        except:
            pass

        try:
            visible_collect = set(user['collect']).difference(
                set(os.listdir('data/house_del')))
            for i in visible_collect:
                Collect.append(house.readhouse(i))
        except:
            pass
        control.putuser(email, user['user_name'], user['birthday'], password,
                        user['phone number'], user['address'],
                        user['property'], user['deleted_post'])
    else:
        return
    birthday = '-'.join(user['birthday'].split('/')[::-1])
    return render_template('person_information_page.html',
                           email=email,
                           password=password,
                           user_name=user['user_name'],
                           birthday=birthday,
                           address=user['address'],
                           phone_number=user['phone number'],
                           Post=Post,
                           Deleted=Deleted,
                           Collect=Collect)
def uncolloct():
    email = request.values.get('email')
    password = request.values.get('password')
    house_id = request.values.get('house_id')
    if (control.verify(email, password)):
        user = control.getuser(email)
        try:
            post = [house.readhouse(i) for i in user['property']]
            post_id = [i for i in user['property']]
        except:
            post = []
            post_id = []
        try:
            deleted = [
                house.read_deleted_house(i) for i in user['deleted_post']
            ]
            deleted_id = [i for i in user['deleted_post']]
        except:
            deleted = []
            deleted_id = []
        collect_id = []
        try:

            collect = []
            visible_collect = set(user['collect']).difference(
                set(os.listdir('data/house_del')))
            collect_id = [item for item in visible_collect]
            collect_id.remove(house_id)
            for i in collect_id:
                collect.append(house.readhouse(i))

        except:
            pass
        control.putuser(email, user['user_name'], user['birthday'], password,
                        user['phone number'], user['address'], post_id,
                        deleted_id, collect_id)
        user = control.getuser(email)

        birthday = '-'.join(user['birthday'].split('/')[::-1])
        return render_template('person_information_page.html',
                               email=email,
                               password=password,
                               user_name=user['user_name'],
                               birthday=birthday,
                               address=user['address'],
                               phone_number=user['phone number'],
                               Post=post,
                               Deleted=deleted,
                               Collect=collect)
    else:
        return 'fail'
def edit_house():
    email = request.values.get('email')
    password = request.values.get('password')
    house_id = int(request.values.get('house_id'))
    print(email, password, 'house_id12=', house_id)

    if request.method == 'POST':
        title = request.values.get('title')
        address = request.values.get('address')
        latitude = request.values.get('latitude')
        longitude = request.values.get('longitude')
        print('latitude=', latitude)
        print('longitude=', longitude)
        price = request.values.get('price')
        guest = request.values.get('guest')
        bedroom = request.values.get('bedroom')
        bed = request.values.get('bed')
        bath = request.values.get('bath')
        content = request.values.get('content')
        policy = request.values.get('policy')
        options1 = request.values.getlist("options1")
        options2 = request.values.getlist("options2")
        # picname = None
        # try:
        #     picname = [request.files['pic']]
        # except:
        #     pass
        # this part by fanxiaoyu

        # this is by fanxiaoyu
        picname = []
        try:
            pic = request.files.getlist('pic')
            picname.append(pic)
            print('上传文件', picname)
        except:
            pass

        control.empty_folder(str(house_id))
        policy = "\n".join(policy.split('\r\n'))
        control.edit_house_info(email, password, house_id, picname, title,
                                guest, bedroom, bed, bath, price, options1,
                                options2, policy, content, address, latitude,
                                longitude)

        Post = []
        Deleted = []
        Collect = []
        if (control.verify(email, password)):
            user = control.getuser(email)

            try:
                control.putuser(email, user['user_name'], user['birthday'],
                                password, user['phone number'],
                                user['address'], user['property'],
                                user['deleted_post'])
            except:
                control.putuser(email, user['user_name'], user['birthday'],
                                password, user['phone number'],
                                user['address'], user['property'])
            for i in user['property']:
                Post.append(house.readhouse(i))
            # print(Post)
            try:
                for i in user['deleted_post']:
                    Deleted.append(house.read_deleted_house(i))
            except:
                pass

            try:
                visible_collect = set(user['collect']).difference(
                    set(os.listdir('data/house_del')))
                for i in visible_collect:
                    Collect.append(house.readhouse(i))
            except:
                pass

            birthday = '-'.join(user['birthday'].split('/')[::-1])
            return render_template('person_information_page.html',
                                   email=email,
                                   password=password,
                                   user_name=user['user_name'],
                                   birthday=birthday,
                                   address=user['address'],
                                   phone_number=user['phone number'],
                                   Post=Post,
                                   Deleted=Deleted,
                                   Collect=Collect)
    else:
        return render_template('person_information_page.html')