<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask.ext.restful import Resource
from flask.ext.restful import reqparse
from flask import request
from ..models import *
from ..lib.util import *
from .. import URI
from ..login.views import auth


class friend(Resource):
    # decorators = [auth.login_required]

    def get(self):
        user_id = request.args['user_id']
        friend = friends.query.filter_by(user_id=user_id).all()
        if len(friend) == 0:
            return {'friends': []}, 200
        names = []
        for person in friend:
            friend_infor = {}
            friend_infor['id'] = person.friend_id
            friend_infor['name'] = User.query.filter_by(
                id=person.friend_id).first().name
            friend_infor['username'] = User.query.filter_by(
                id=person.friend_id).first().username
            names.append(friend_infor)
        temp = {}
        temp['friends'] = names
        return {'status': 'success', 'data': temp}

    def post(self):
        paser = reqparse.RequestParser()
        paser.add_argument('id', type=str)
        paser.add_argument('friendname', type=str)
        args = paser.parse_args(strict=True)
        friend = User.query.filter_by(username=args['friendname']).first()
        if friend is None:
            return {'status': 'fail', 'mesg': '该好友还未注册!'}
        exit = friends.query.filter(friends.user_id == args['id'],
                                    friends.friend_id == friend.id).first()
        if exit is not None:
            return {'status': 'fail', 'mesg': '已经是朋友!'}
        if args['id'] == str(friend.id):
            return {'status': 'fail', 'mesg': '不能和自己成为好友!'}
        record = friends(args['id'], friend.id)
        try:
            db.session.add(record)
            db.session.commit()
            return {'status': 'success', 'mesg': '成功添加好友!'}
        except:
            return {'status': 'fail', 'mesg': '添加好友失败!'}

    def delete(self):
        paser = reqparse.RequestParser()
        paser.add_argument('id', type=str)
        paser.add_argument('friendname', type=str)
        args = paser.parse_args(strict=True)
        friend = User.query.filter_by(username=args['friendname']).first()
        if friend.id is None:
            return {'status': 'fail', 'mesg': '该好友还未注册!'}
        friendShip = friends.query.filter(
            friends.user_id == args['id'],
            friends.friend_id == friend.id).first()
        try:
            db.session.delete(friendShip)
            db.session.commit()
            return {'status': 'success', 'mesg': '成功删除好友!'}
        except:
            return {'status': 'fail', 'mesg': '删除好友失败!'}

    def put(self):
        pass


class friendInfor(Resource):
    # decorators = [auth.login_required]

    def get(self):
        user_id = request.args['user_id']
        friend = friends.query.filter_by(user_id=user_id).all()
        if len(friend) == 0:
            return {'friends': []}, 200
        names = []
        for person in friend:
            friend_infor = {}
            friend_infor['id'] = person.friend_id
            friend_infor['img'] = URI + str(person.friend_id)
            friend_infor['name'] = User.query.filter_by(
                id=person.friend_id).first().name
            friend_infor['province'] = User.query.filter_by(
                id=person.friend_id).first().province
            names.append(friend_infor)
        temp = {}
        temp['friends'] = names
        return {'status': 'success', 'data': temp}


class searchFriend(Resource):
    # decorators = [auth.login_required]

    def get(self):
        name = request.args['username']
        users = User.query.filter(
            User.username.ilike("%" + name + "%")).all()
        result = []
        if len(users) == 0:
            return {'status': 'success', 'data': result}
        else:
            for user in users:
                username = user.username
                result.append(username)
            return {'status': 'success', 'data': result}


class searchFrid(Resource):
    decorators = [auth.login_required]

    def get(self):
        name = request.args['username']
        id = str(request.args['id'])
        users = User.query.filter(
            User.username.ilike("%" + name + "%")).all()
        result = []
        if len(users) == 0:
            return {'status': 'success', 'data': result}, 200
        else:
            for user in users:
                buf = {}
                buf['name'] = user.name
                buf['img'] = URI + str(user.id)
                buf['area'] = user.province + user.district
                buf['friend_id'] = str(user.id)
                beFri = friends.query.filter(
                    friends.user_id == id,
                    friends.friend_id == str(user.id)).first()
                if beFri is not None:
                    continue
                else:
                    record = application.query.filter(
                        application.friend_id == str(user.id),
                        application.user_id == id).first()
                    if record is None:
                        record = application.query.filter(
                            application.user_id == str(user.id),
                            application.friend_id == id).first()
                        if record is None:
                            # 添加状态
                            buf['append'] = '1'
                        else:
                            continue
                    else:
                            # 待确认状态
                        buf['append'] = '0'
                result.append(buf)
            return {'status': 'success', 'data': result}, 200


class usrApply(Resource):
    decorators = [auth.login_required]

    def post(self):
        paser = reqparse.RequestParser()
        paser.add_argument('id', type=str)
        paser.add_argument('friend_id', type=str)
        paser.add_argument('text')
        args = paser.parse_args(strict=True)
        record = application.query.filter(
            application.user_id == args['id'],
            application.friend_id == args['friend_id']).first()
        if record is None:
            record = application(
                args['friend_id'], args['id'], args['text'], '0', '1')<commit_msg>fix bug<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask.ext.restful import Resource
from flask.ext.restful import reqparse
from flask import request
from ..models import *
from ..lib.util import *
from .. import URI
from ..login.views import auth


class friend(Resource):
    # decorators = [auth.login_required]

    def get(self):
        user_id = request.args['user_id']
        friend = friends.query.filter_by(user_id=user_id).all()
        if len(friend) == 0:
            return {'friends': []}, 200
        names = []
        for person in friend:
            friend_infor = {}
            friend_infor['id'] = person.friend_id
            friend_infor['name'] = User.query.filter_by(
                id=person.friend_id).first().name
            friend_infor['username'] = User.query.filter_by(
                id=person.friend_id).first().username
            names.append(friend_infor)
        temp = {}
        temp['friends'] = names
        return {'status': 'success', 'data': temp}

    def post(self):
        paser = reqparse.RequestParser()
        paser.add_argument('id', type=str)
        paser.add_argument('friendname', type=str)
        args = paser.parse_args(strict=True)
        friend = User.query.filter_by(username=args['friendname']).first()
        if friend is None:
            return {'status': 'fail', 'mesg': '该好友还未注册!'}
        exit = friends.query.filter(friends.user_id == args['id'],
                                    friends.friend_id == friend.id).first()
        if exit is not None:
            return {'status': 'fail', 'mesg': '已经是朋友!'}
        if args['id'] == str(friend.id):
            return {'status': 'fail', 'mesg': '不能和自己成为好友!'}
        record = friends(args['id'], friend.id)
        try:
            db.session.add(record)
            db.session.commit()
            return {'status': 'success', 'mesg': '成功添加好友!'}
        except:
            return {'status': 'fail', 'mesg': '添加好友失败!'}

    def delete(self):
        paser = reqparse.RequestParser()
        paser.add_argument('id', type=str)
        paser.add_argument('friendname', type=str)
        args = paser.parse_args(strict=True)
        friend = User.query.filter_by(username=args['friendname']).first()
        if friend.id is None:
            return {'status': 'fail', 'mesg': '该好友还未注册!'}
        friendShip = friends.query.filter(
            friends.user_id == args['id'],
            friends.friend_id == friend.id).first()
        try:
            db.session.delete(friendShip)
            db.session.commit()
            return {'status': 'success', 'mesg': '成功删除好友!'}
        except:
            return {'status': 'fail', 'mesg': '删除好友失败!'}

    def put(self):
        pass


class friendInfor(Resource):
    # decorators = [auth.login_required]

    def get(self):
        user_id = request.args['user_id']
        friend = friends.query.filter_by(user_id=user_id).all()
        if len(friend) == 0:
            return {'friends': []}, 200
        names = []
        for person in friend:
            friend_infor = {}
            friend_infor['id'] = person.friend_id
            friend_infor['img'] = URI + str(person.friend_id)
            friend_infor['name'] = User.query.filter_by(
                id=person.friend_id).first().name
            friend_infor['province'] = User.query.filter_by(
                id=person.friend_id).first().province
            names.append(friend_infor)
        temp = {}
        temp['friends'] = names
        return {'status': 'success', 'data': temp}


class searchFriend(Resource):
    # decorators = [auth.login_required]

    def get(self):
        name = request.args['username']
        users = User.query.filter(
            User.username.ilike("%" + name + "%")).all()
        result = []
        if len(users) == 0:
            return {'status': 'success', 'data': result}
        else:
            for user in users:
                username = user.username
                result.append(username)
            return {'status': 'success', 'data': result}


class searchFrid(Resource):
    decorators = [auth.login_required]

    def get(self):
        name = request.args['username']
        id = str(request.args['id'])
        users = User.query.filter(
            User.username.ilike("%" + name + "%")).all()
        result = []
        if len(users) == 0:
            return {'status': 'success', 'data': result}, 200
        else:
            for user in users:
                if str(user.id) == str(id):
                    continue
                buf = {}
                buf['name'] = user.name
                buf['img'] = URI + str(user.id)
                buf['area'] = user.province + user.district
                buf['friend_id'] = str(user.id)
                beFri = friends.query.filter(
                    friends.user_id == id,
                    friends.friend_id == str(user.id)).first()
                if beFri is not None:
                    continue
                else:
                    record = application.query.filter(
                        application.friend_id == str(user.id),
                        application.user_id == id).first()
                    if record is None:
                        record = application.query.filter(
                            application.user_id == str(user.id),
                            application.friend_id == id).first()
                        if record is None:
                            # 添加状态
                            buf['append'] = '1'
                        else:
                            continue
                    else:
                            # 待确认状态
                        buf['append'] = '0'
                result.append(buf)
            return {'status': 'success', 'data': result}, 200


class usrApply(Resource):
    # decorators = [auth.login_required]

    def post(self):
        paser = reqparse.RequestParser()
        paser.add_argument('id', type=str)
        paser.add_argument('friend_id', type=str)
        paser.add_argument('text')
        args = paser.parse_args(strict=True)
        record = application.query.filter(
            application.user_id == args['id'],
            application.friend_id == args['friend_id']).first()
        a = friends.query.filter_by(user_id=args['id']).first()
        b = friends.query.filter_by(user_id=args['friend_id']).first()
        if a or b:
            return {'status': 'success', 'mesg': 'fial'}, 200
        if record is None:
            record = application(
                args['friend_id'], args['id'], args['text'], '0', '1')
{'fraction_non_alphanumeric': 0.13916132519885704, 'fraction_numerical': 0.0034751718279403816, 'mean_word_length': 1.7349524815205914, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2600135', 'n_tokens_mistral': 3794, 'n_tokens_neox': 3716, 'n_words': 762}