Exemplo n.º 1
0
def _import_modules():
    data = request.json
    items = data[config.ITEMS]

    def modules_convert(item):
        if item.get('image'):
            item['image'] = item['image'].split('/')[-1]

        allowed_key = ('name', 'image')
        item = filter(lambda v: v[0] in allowed_key, item.items())
        item = dict(item)
        item = filter(lambda v: v[1] is not None, item.items())
        item = dict(item)

        return item
    items = map(modules_convert, items)
    items = list(items)

    status_all = True
    resource = 'modules'

    domain_ori = deepcopy(app.config['DOMAIN'])
    app.config['DOMAIN'][resource]['schema']['image']['type'] = 'string'
    app.config['DOMAIN'][resource]['_media'] = []
    try:
        _ = delete(resource)
    except NotFound:
        pass

    response = post_internal(resource, items)
    pprint(response)
    status_all = status_all and response[3] == 201

    app.config['DOMAIN'] = domain_ori
    return jsonify({'status': status_all})
Exemplo n.º 2
0
def item_endpoint(**lookup):
    """ Item endpoint handler

    :param url: the url that led here
    :param lookup: the query

    .. versionchanged:: 0.1.1
       Relying on request.path for determining the current endpoint url.

    .. versionchanged:: 0.1.0
       Support for PUT method.

    .. versionchanged:: 0.0.7
       Using 'utils.request_method' helper function now.

    .. versionchanged:: 0.0.6
       Support for HEAD requests
    """
    k = request.path.rstrip('/').rfind('/')
    resource = config.RESOURCES[request.path[:k]]
    response = None
    method = request_method()
    if method in ('GET', 'HEAD'):
        response = getitem(resource, **lookup)
    elif method == 'PATCH':
        response = patch(resource, **lookup)
    elif method == 'PUT':
        response = put(resource, **lookup)
    elif method == 'DELETE':
        response = delete(resource, **lookup)
    elif method == 'OPTIONS':
        send_response(resource, response)
    else:
        abort(405)
    return send_response(resource, response)
Exemplo n.º 3
0
def item_endpoint(url, **lookup):
    """ Item endpoint handler

    :param url: the url that led here
    :param lookup: the query

    .. versionchanged:: 0.1.0
       Support for PUT method.

    .. versionchanged:: 0.0.7
       Using 'utils.request_method' helper function now.

    .. versionchanged:: 0.0.6
       Support for HEAD requests
    """
    resource = config.RESOURCES[url]
    response = None
    method = request_method()
    if method in ('GET', 'HEAD'):
        response = getitem(resource, **lookup)
    elif method == 'PATCH':
        response = patch(resource, **lookup)
    elif method == 'PUT':
        response = put(resource, **lookup)
    elif method == 'DELETE':
        response = delete(resource, **lookup)
    elif method == 'OPTIONS':
        send_response(resource, response)
    else:
        abort(405)
    return send_response(resource, response)
Exemplo n.º 4
0
def _import_branches():
    data = request.json
    items = data[config.ITEMS]

    def branch_convert(item):
        allowed_key = ('name', 'address')
        item = filter(lambda v: v[0] in allowed_key, item.items())
        item = dict(item)
        item = filter(lambda v: v[1] is not None, item.items())
        item = dict(item)
        return item
    items = map(branch_convert, items)
    items = list(items)

    status_all = True
    resource = 'branches'
    try:
        _ = delete(resource)
    except NotFound:
        pass

    response = post_internal(resource, items)
    pprint(response)
    status_all = status_all and response[3] == 201

    return jsonify({'status': status_all})
Exemplo n.º 5
0
def item_endpoint(**lookup):
    """ Item endpoint handler

    :param url: the url that led here
    :param lookup: the query

    .. versionchanged:: 0.1.1
       Relying on request.path for determining the current endpoint url.

    .. versionchanged:: 0.1.0
       Support for PUT method.

    .. versionchanged:: 0.0.7
       Using 'utils.request_method' helper function now.

    .. versionchanged:: 0.0.6
       Support for HEAD requests
    """
    k = request.path.rstrip('/').rfind('/')
    resource = config.RESOURCES[request.path[:k]]
    response = None
    method = request_method()
    if method in ('GET', 'HEAD'):
        response = getitem(resource, **lookup)
    elif method == 'PATCH':
        response = patch(resource, **lookup)
    elif method == 'PUT':
        response = put(resource, **lookup)
    elif method == 'DELETE':
        response = delete(resource, **lookup)
    elif method == 'OPTIONS':
        send_response(resource, response)
    else:
        abort(405)
    return send_response(resource, response)
Exemplo n.º 6
0
def item_endpoint(url, **lookup):
    """ Item endpoint handler

    :param url: the url that led here
    :param lookup: the query

    .. versionchanged:: 0.0.7
       Using 'utils.request_method' helper function now.

    .. versionchanged:: 0.0.6
       Support for HEAD requests
    """
    resource = config.RESOURCES[url]
    response = None
    method = request_method()
    if method in ('GET', 'HEAD'):
        response = getitem(resource, **lookup)
    elif method == 'PATCH':
        response = patch(resource, **lookup)
    elif method == 'DELETE':
        response = delete(resource, **lookup)
    elif method == 'OPTIONS':
        send_response(resource, response)
    else:
        abort(405)
    return send_response(resource, response)
Exemplo n.º 7
0
def on_update(resource_name, updates, original):
    if resource_name == 'users':
        if original['role'] == 'student':
            if updates.get('guardians'):
                _ = delete('student_guardians', **{'student': original['id']})
                _guardians = json.loads(updates.get('guardians'))
                update_student_guardians(_guardians, original)
                del updates['guardians']
Exemplo n.º 8
0
def _import_users():
    data = request.json
    items = data[config.ITEMS]

    def users_convert(item):

        # if item.get('photo'):
        #     item['photo'] = item['photo'].split('/')[-1]
        if item.get('pass_'):
            item['password'] = item['pass_']
        if item.get('is_deleted'):
            item[config.DELETED] = item['is_deleted']
        else:
            item[config.DELETED] = False
        if item.get('contact_no'):
            item['contact'] = item['contact_no']

        allowed_key = (config.DELETED,
                       'password', 'username', 'email', 'role', 'password', 'name', 'dob', 'address', 'school', 'grade', 'contact')
        item = filter(lambda v: v[0] in allowed_key, item.items())
        item = dict(item)
        item = filter(lambda v: v[1] is not None, item.items())
        item = dict(item)

        return item
    items = map(users_convert, items)
    items = list(items)

    status_all = True
    resource = 'users'
    domain_ori = deepcopy(app.config['DOMAIN'])
    app.config['DOMAIN'][resource]['soft_delete'] = False
    try:
        _ = delete(resource)
    except NotFound:
        pass

    response = post_internal(resource, items, skip_validation=True)
    pprint(response)
    status_all = status_all and response[3] == 201

    app.config['DOMAIN'] = domain_ori
    return jsonify({'status': status_all})
Exemplo n.º 9
0
def collections_endpoint(**lookup):
    """ Resource endpoint handler

    :param url: the url that led here

    .. versionchanged:: 0.3
       Pass lookup query down to delete_resource, so it can properly process
       sub-resources.

    .. versionchanged:: 0.2
       Relying on request.endpoint to retrieve the resource being consumed.

    .. versionchanged:: 0.1.1
       Relying on request.path for determining the current endpoint url.

    .. versionchanged:: 0.0.7
       Using 'utils.request_method' helper function now.

    .. versionchanged:: 0.0.6
       Support for HEAD requests

    .. versionchanged:: 0.0.2
        Support for DELETE resource method.
    """

    resource = _resource()
    response = None
    method = request.method
    if method in ('GET', 'HEAD'):
        response = get(resource, lookup)
    elif method == 'POST':
        print "inside endpoints.py now************"
        print resource
        print "resource end here****************"
        response = post(resource)
    elif method == 'DELETE':
        response = delete(resource, lookup)
    elif method == 'OPTIONS':
        send_response(resource, response)
    else:
        abort(405)
    return send_response(resource, response)
Exemplo n.º 10
0
def collections_endpoint(**lookup):
    """ Resource endpoint handler

    :param url: the url that led here

    .. versionchanged:: 0.3
       Pass lookup query down to delete_resource, so it can properly process
       sub-resources.

    .. versionchanged:: 0.2
       Relying on request.endpoint to retrieve the resource being consumed.

    .. versionchanged:: 0.1.1
       Relying on request.path for determining the current endpoint url.

    .. versionchanged:: 0.0.7
       Using 'utils.request_method' helper function now.

    .. versionchanged:: 0.0.6
       Support for HEAD requests

    .. versionchanged:: 0.0.2
        Support for DELETE resource method.
    """

    resource = _resource()
    response = None
    method = request.method
    if method in ("GET", "HEAD"):
        response = get(resource, lookup)
    elif method == "POST":
        response = post(resource)
    elif method == "DELETE":
        response = delete(resource, lookup)
    elif method == "OPTIONS":
        send_response(resource, response)
    else:
        abort(405)
    return send_response(resource, response)
Exemplo n.º 11
0
def item_endpoint(**lookup):
    """ Item endpoint handler

    :param url: the url that led here
    :param lookup: sub resource query

    .. versionchanged:: 0.2
       Support for sub-resources.
       Relying on request.endpoint to retrieve the resource being consumed.

    .. versionchanged:: 0.1.1
       Relying on request.path for determining the current endpoint url.

    .. versionchanged:: 0.1.0
       Support for PUT method.

    .. versionchanged:: 0.0.7
       Using 'utils.request_method' helper function now.

    .. versionchanged:: 0.0.6
       Support for HEAD requests
    """
    resource = _resource()
    response = None
    method = request_method()
    if method in ('GET', 'HEAD'):
        response = getitem(resource, **lookup)
    elif method == 'PATCH':
        response = patch(resource, **lookup)
    elif method == 'PUT':
        response = put(resource, **lookup)
    elif method == 'DELETE':
        response = delete(resource, **lookup)
    elif method == 'OPTIONS':
        send_response(resource, response)
    else:
        abort(405)
    return send_response(resource, response)
Exemplo n.º 12
0
def item_endpoint(**lookup):
    """ Item endpoint handler

    :param url: the url that led here
    :param lookup: sub resource query

    .. versionchanged:: 0.2
       Support for sub-resources.
       Relying on request.endpoint to retrieve the resource being consumed.

    .. versionchanged:: 0.1.1
       Relying on request.path for determining the current endpoint url.

    .. versionchanged:: 0.1.0
       Support for PUT method.

    .. versionchanged:: 0.0.7
       Using 'utils.request_method' helper function now.

    .. versionchanged:: 0.0.6
       Support for HEAD requests
    """
    resource = _resource()
    response = None
    method = request_method()
    if method in ('GET', 'HEAD'):
        response = getitem(resource, **lookup)
    elif method == 'PATCH':
        response = patch(resource, **lookup)
    elif method == 'PUT':
        response = put(resource, **lookup)
    elif method == 'DELETE':
        response = delete(resource, **lookup)
    elif method == 'OPTIONS':
        send_response(resource, response)
    else:
        abort(405)
    return send_response(resource, response)
Exemplo n.º 13
0
def _import_classes():
    data = request.json
    items = data[config.ITEMS]

    domain_ori = deepcopy(app.config['DOMAIN'])

    def classes_convert(item):
        if item.get('start_at'):
            item['startAt'] = item['start_at']
        if item.get('finish_at'):
            item['finishAt'] = item['finish_at']
        if item.get('module'):
            lookup = {
                'name': item['module']['name']
            }
            module, *_ = getitem_internal('modules', **lookup)
            item['module'] = module[config.ID_FIELD]
        if item.get('branch'):
            lookup = {
                'name': item['branch']['name']
            }
            branch, *_ = getitem_internal('branches', **lookup)
            item['branch'] = branch[config.ID_FIELD]
        if item.get('tutor'):
            resource = 'users'
            app.config['DOMAIN'][resource]['soft_delete'] = False
            lookup = {
                'username': item['tutor']['username']
            }
            try:
                tutor, *_ = getitem_internal(resource, **lookup)
                item['tutor'] = tutor[config.ID_FIELD]
            except NotFound:
                item['tutor'] = None

        if item.get('students'):
            resource = 'users'
            for v in item['students']:
                lookup = {
                    'username': v['student']['username']
                }
                try:
                    student, *_ = getitem_internal(resource, **lookup)
                    v['student'] = student[config.ID_FIELD]
                except NotFound:
                    v['student'] = None

        allowed_key = ('day', 'startAt', 'finishAt',
                       'module', 'branch', 'tutor', 'students')
        item = filter(lambda v: v[0] in allowed_key, item.items())
        item = dict(item)
        item = filter(lambda v: v[1] is not None, item.items())
        item = dict(item)

        return item
    items = map(classes_convert, items)
    items = list(items)

    status_all = True
    resource = 'classes'
    resource_child = 'classes_students'

    try:
        _ = delete(resource)
        _ = delete(resource_child)
    except NotFound:
        pass

    for v in items:
        students = v.pop('students', None)
        class_, *response = post_internal(
            resource, v, skip_validation=True)
        status_all = status_all and response[2] == 201

        payload = [{'class': class_[config.ID_FIELD], 'student': v2['student']}
                   for v2 in students]
        response = post_internal(resource_child, payload)
        pprint(response)
        status_all = status_all and response[3] == 201

    app.config['DOMAIN'] = domain_ori
    return jsonify({'status': status_all})