<commit_before>import json,logging, urlparse
from urllib import urlencode
from flask import jsonify, Response, abort, Blueprint, request, url_for
from dougrain import Builder
from podserve.model import Dataset, User, Organization, Schema

__author__ = 'dwcaraway'
__credits__ = ['<NAME>']

log = logging.getLogger(__name__)

#TODO refactor this function out of code
# def load_rels():
#     pwd = os.path.abspath(os.path.dirname(__file__))
#     file_path = os.path.join(pwd, 'rels.json')
#
#     with open(file_path, 'r') as f:
#         return json.loads(f.read())
#     return {}
#
# relations = load_rels()

api = Blueprint('api', __name__)

@api.route('/', methods=['GET'])
def index():
    """
    Handle API home, the starting point, which lists endpoints to navigate to
    """
    b = Builder(request.url).add_curie('ep', '/rel/{rel}')\
        .add_link('ep:user', '/users')\
        .add_link('ep:dataset', '/datasets')\
        .add_link('ep:organization', '/organizations')\
        .add_link('ep:schema', '/schema')
    o = b.as_object()

    print "request.path = %s" % request.path
    print "request.full_path = %s" % request.full_path
    print "request.url = %s" % request.url

    return Response(json.dumps(o), mimetype='application/hal+json')

@api.route('/datasets', methods=['GET'])
def list_all_datasets():
    """
    Lists all Datasets
    """
    page = int(request.args.get('page', '1'))

    # log.debug("reconstructed_url=%s", urlparse.urljoin(request.endpoint, '?'+urlencode(request.args)))

    pagination = Dataset.objects.paginate(page=page, per_page=10)
    b = Builder(request.url).add_link('home', '/')

    for dataset in pagination.items:
        b.add_link('/rel/dataset', '/datasets/%s' % dataset.id)

    if pagination.has_prev:
        b.add_link('prev', '/datasets?page=%d'%pagination.prev_num)

    if pagination.has_next:
        b.add_link('next', '/datasets?page=%d'%pagination.next_num)

    o = b.as_object()

    return Response(json.dumps(o), mimetype='application/hal+json')

@api.route('/datasets', methods=['POST'])
def create_dataset():
    """
    Creates a Dataset
    """

    print("Request JSON = %s" % request.get_json())

    dataset = Dataset()
    dataset.save()

    b = Builder(request.url).add_link('/rel/dataset', '/datasets/%d' % dataset.id)

    #TODO implement
    return abort(501)

@api.route('/datasets/{id}', methods=['GET, PUT, DELETE'])
def handle_datasets():
    """
    Retrieve a Dataset
    """

    #TODO implement
    return abort(501)

@api.route('/users', methods=['GET'])
def list_all_users(page=1, per_page=10):
    """
    Lists all Users
    """
    pagination = User.objects.paginate(page=page, per_page=per_page)
    b = Builder(request.url)
    for dataset in pagination.iter_pages():
        b.add_link('/rel/user', '/users/%d' % dataset.id)

    o = b.as_object()

    return Response(json.dumps(o), mimetype='application/hal+json')

@api.route('/users', methods=['POST'])
def create_user():
    """
    Creates a user
    """
    dataset = User()
    b = Builder(request.url).add_link('/rel/user', '/users/%d' % dataset.id)

    #TODO implement
    return abort(501)

@api.route('/users/{id}', methods=['GET, PUT, DELETE'])
def handle_users():
    """
    Retrieve a User
    """

    #TODO implement
    return abort(501)

@api.route('/organizations', methods=['GET'])
def list_all_orgs(page=1, per_page=10):
    """
    Lists all Organizations
    """
    pagination = Organization.objects.paginate(page=int(page), per_page=per_page)

    b = Builder(request.url)
    for org in pagination.items:
        b.add_link('/rel/organization', '/organizations/%s' % org.id)

    o = b.as_object()

    return Response(json.dumps(o), mimetype='application/hal+json')

@api.route('/organizations', methods=['POST'])
def create_org():
    """
    Creates an organization
    """
    dataset = Organization()
    b = Builder(request.url).add_link('/rel/organization', '/organizations/%d' % dataset.id)

    #TODO implement
    return abort(501)

@api.route('/organizations/{id}', methods=['GET, PUT, DELETE'])
def handle_orgs():
    """
    Retrieve an organization
    """

    #TODO implement
    return abort(501)

@api.route('/schema', methods=['GET'])
def list_all_schema(page=1):
    """
    Handle routing of JSON schema (see json-schema.org)
    """
    ret = Schema.objects.paginate(page=page, per_page=10)
    return jsonify(ret)
<commit_msg>Have many of the tests working again. Converted much of urls to use url_for() method<commit_after>import json,logging, urlparse
from urllib import urlencode
from flask import jsonify, Response, abort, Blueprint, request, url_for
from dougrain import Builder
from podserve.model import Dataset, User, Organization, Schema

__author__ = 'dwcaraway'
__credits__ = ['<NAME>']

log = logging.getLogger(__name__)

#TODO refactor this function out of code
# def load_rels():
#     pwd = os.path.abspath(os.path.dirname(__file__))
#     file_path = os.path.join(pwd, 'rels.json')
#
#     with open(file_path, 'r') as f:
#         return json.loads(f.read())
#     return {}
#
# relations = load_rels()

api = Blueprint('api', __name__)

@api.route('/', methods=['GET'])
def index():
    """
    Handle API home, the starting point, which lists endpoints to navigate to
    """
    b = Builder(request.url).add_curie('ep', '/rel/{rel}')\
        .add_link('ep:user', url_for('.list_all_users'))\
        .add_link('ep:dataset', url_for('.list_datasets'))\
        .add_link('ep:organization', url_for('.list_all_orgs'))\
        .add_link('ep:schema', url_for('.list_all_schema'))
    o = b.as_object()

    return Response(json.dumps(o), mimetype='application/hal+json')

@api.route('/datasets', methods=['GET'])
def list_datasets():
    """
    Lists all Datasets
    """
    page = int(request.args.get('page', '1'))

    pagination = Dataset.objects.paginate(page=page, per_page=10)
    b = Builder(request.url).add_link('home', url_for('.index'))

    for dataset in pagination.items:
        b.add_link('/rel/dataset', url_for('.get_dataset', id=dataset.id))

    if pagination.has_prev:
        b.add_link('prev', url_for('.list_datasets', page=pagination.prev_num))

    if pagination.has_next:
        b.add_link('next', url_for('.list_datasets', page=pagination.next_num))

    o = b.as_object()

    return Response(json.dumps(o), mimetype='application/hal+json')

@api.route('/datasets', methods=['POST'])
def create_dataset():
    """
    Creates a Dataset
    """

    print("Request JSON = %s" % request.get_json())

    dataset = Dataset()
    dataset.save()

    b = Builder(request.url).add_link('/rel/dataset', '/datasets/%d' % dataset.id)

    #TODO implement
    return abort(501)

@api.route('/datasets/<id>', methods=['GET, PUT, DELETE'])
def get_dataset(id=None):
    """
    Retrieve a Dataset
    """

    #TODO implement
    return abort(501)

@api.route('/users', methods=['GET'])
def list_all_users(page=1, per_page=10):
    """
    Lists all Users
    """
    pagination = User.objects.paginate(page=page, per_page=per_page)

    b = Builder(request.url)
    for dataset in pagination.iter_pages():
        b.add_link('/rel/user', '/users/%d' % dataset.id)

    o = b.as_object()

    return Response(json.dumps(o), mimetype='application/hal+json')

@api.route('/users', methods=['POST'])
def create_user():
    """
    Creates a user
    """
    dataset = User()
    b = Builder(request.url).add_link('/rel/user', '/users/%d' % dataset.id)

    #TODO implement
    return abort(501)

@api.route('/users/{id}', methods=['GET, PUT, DELETE'])
def get_users():
    """
    Retrieve a User
    """

    #TODO implement
    return abort(501)

@api.route('/organizations', methods=['GET'])
def list_all_orgs(page=1, per_page=10):
    """
    Lists all Organizations
    """
    pagination = Organization.objects.paginate(page=int(page), per_page=per_page)

    b = Builder(request.url)
    for org in pagination.items:
        b.add_link('/rel/organization', '/organizations/%s' % org.id)

    o = b.as_object()

    return Response(json.dumps(o), mimetype='application/hal+json')

@api.route('/organizations', methods=['POST'])
def create_org():
    """
    Creates an organization
    """
    dataset = Organization()
    b = Builder(request.url).add_link('/rel/organization', '/organizations/%d' % dataset.id)

    #TODO implement
    return abort(501)

@api.route('/organizations/{id}', methods=['GET, PUT, DELETE'])
def get_org():
    """
    Retrieve an organization
    """

    #TODO implement
    return abort(501)

@api.route('/schema', methods=['GET'])
def list_all_schema(page=1):
    """
    Handle routing of JSON schema (see json-schema.org)
    """
    ret = Schema.objects.paginate(page=page, per_page=10)
    return jsonify(ret)

{'fraction_non_alphanumeric': 0.17075664621676892, 'fraction_numerical': 0.006816632583503749, 'mean_word_length': 3.5968668407310704, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2173023', 'n_tokens_mistral': 3040, 'n_tokens_neox': 2943, 'n_words': 608}