コード例 #1
0
def add_client():

    api_resp = {
        'url': '',
        'method': '',
        'success': True,
        'resp_data': '',
        'error': ''
    }

    name = request.form['name']

    api_resp['url'] = '/dash/v1.0/add_client'
    api_resp['method'] = 'POST'

    new_client = Client(name=name)
    new_client.owner = current_user
    new_client.get_token()

    try:
        db.session.add(new_client)
        db.session.commit()
        api_resp['success'] = True
        api_resp['resp_data'] = new_client.to_dict()
    except:
        api_resp['error'] = 'Ошибка добавления клиента!'
        return jsonify(api_resp)

    api_resp['success'] = True

    return jsonify(api_resp)
コード例 #2
0
ファイル: routes.py プロジェクト: davidchak/kwork_dashboard
def add_client():

    resp_data = {}
    resp_data['api'] = '/api/v1.0/clients/add'
    resp_data['method'] = 'post'
    resp_data['data'] = []
    resp_data['error'] = ''

    data = request.json

    if data is None:
        resp_data['success'] = False
        resp_data['error'] = 'Необходимо передать имя клиента параметром name'
        return jsonify(resp_data)

    new_client = Client(name=data['name'])
    new_client.owner = g.user
    new_client.get_token()

    try:
        db.session.add(new_client)
        db.session.commit()
        resp_data['success'] = True
        resp_data['data'] = new_client.to_dict()
    except:
        resp_data['error'] = 'Ошибка добавления клиента!'
        return jsonify(resp_data)

    return jsonify(resp_data)
コード例 #3
0
def create_client():
    """Creates new user in database, also provides new api token"""
    data = json.loads(str(request.get_data().decode('utf-8')))
    if 'address' not in data or 'email' not in data or 'password' not in data:
        return bad_request('must include address, email and password fields')
    if 'name' not in data or 'surname' not in data:
        return bad_request('must include name and surname')
    if Notary.query.filter_by(address=data['address']).first():
        return bad_request('user with this address already exist')
    if Notary.query.filter_by(email=data['email']).first():
        return bad_request('please use a different email address')
    client = Client()
    client.from_dict(data, new_user=True)
    client.get_token()
    db.session.add(client)
    db.session.commit()
    response = jsonify(client.to_dict())
    response.status_code = 201
    response.headers['Location'] = url_for('get_client', client_id=client.id)
    return response