<commit_before>"""
urls.py

URL dispatch route mappings and error handlers

"""
from flask import render_template

from google.appengine.api import users
from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError

from application import app
from application import views

from models import ExampleModel

from flask_restful import reqparse, abort, Api, Resource
api = Api(app)

## URL dispatch rules
# App Engine warm up handler
# See http://code.google.com/appengine/docs/python/config/appconfig.html#Warming_Requests
app.add_url_rule('/api/_ah/warmup', 'warmup', view_func=views.warmup)

# Login page
app.add_url_rule('/login', 'login', view_func=views.login)

# Home page
app.add_url_rule('/api/', 'home', view_func=views.home)

# Test page
app.add_url_rule('/api/test', 'test', view_func=views.test)


TODOS = {
    'todo1': {'id': 1, 'title': 'build an API'},
    'todo2': {'id': 2, 'title': '?????'},
    'todo3': {'id': 3, 'title': 'profit!'},
}


def abort_if_todo_doesnt_exist(todo_id):
    if todo_id not in TODOS:
        abort(404, message="Todo {} doesn't exist".format(todo_id))

parser = reqparse.RequestParser()
parser.add_argument('title')
parser.add_argument('description')

def formatExample(example):
    return {
        'id': example.key.id(),
        'title': example.example_name,
        'description': example.example_description,
        'added_by': {
            'email': example.added_by.email(),
            'user_id': example.added_by.user_id()
        }
    }


# Todo
# shows a single todo item and lets you delete a todo item
class Todo(Resource):
    def get(self, todo_id):
        abort_if_todo_doesnt_exist(todo_id)
        return TODOS[todo_id]

    def delete(self, todo_id):
        abort_if_todo_doesnt_exist(todo_id)
        del TODOS[todo_id]
        return '', 204

    def put(self, todo_id):
        args = parser.parse_args()
        task = {'task': args['task']}
        TODOS[todo_id] = task
        return task, 201


# TodoList
# shows a list of all todos, and lets you POST to add new tasks
class TodoList(Resource):
    def get(self):
        examples = ExampleModel.query()
        out = []
        for example in examples:
            out.append(formatExample(example))
        return out<commit_msg>Add timestamps to api response<commit_after>"""
urls.py

URL dispatch route mappings and error handlers

"""

from flask import render_template

from google.appengine.api import users
from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError

from application import app
from application import views

from models import ExampleModel

from flask_restful import reqparse, abort, Api, Resource
api = Api(app)

## URL dispatch rules
# App Engine warm up handler
# See http://code.google.com/appengine/docs/python/config/appconfig.html#Warming_Requests
app.add_url_rule('/api/_ah/warmup', 'warmup', view_func=views.warmup)

# Login page
app.add_url_rule('/login', 'login', view_func=views.login)

# Home page
app.add_url_rule('/api/', 'home', view_func=views.home)

# Test page
app.add_url_rule('/api/test', 'test', view_func=views.test)


TODOS = {
    'todo1': {'id': 1, 'title': 'build an API'},
    'todo2': {'id': 2, 'title': '?????'},
    'todo3': {'id': 3, 'title': 'profit!'},
}


def abort_if_todo_doesnt_exist(todo_id):
    if todo_id not in TODOS:
        abort(404, message="Todo {} doesn't exist".format(todo_id))

parser = reqparse.RequestParser()
parser.add_argument('title')
parser.add_argument('description')

def formatExample(example):
    return {
        'id': example.key.id(),
        'title': example.example_name,
        'description': example.example_description,
        'added_by': {
            'email': example.added_by.email(),
            'user_id': example.added_by.user_id()
        },
        'timestamp': example.timestamp.isoformat()
    }


# Todo
# shows a single todo item and lets you delete a todo item
class Todo(Resource):
    def get(self, todo_id):
        abort_if_todo_doesnt_exist(todo_id)
        return TODOS[todo_id]

    def delete(self, todo_id):
        abort_if_todo_doesnt_exist(todo_id)
        del TODOS[todo_id]
        return '', 204

    def put(self, todo_id):
        args = parser.parse_args()
        task = {'task': args['task']}
        TODOS[todo_id] = task
        return task, 201


# TodoList
# shows a list of all todos, and lets you POST to add new tasks
class TodoList(Resource):
    def get(self):
        examples = ExampleModel.query()
        out = []
        for example in examples:
            out.append(formatExample(example))
        return out
{'fraction_non_alphanumeric': 0.12597402597402596, 'fraction_numerical': 0.006493506493506494, 'mean_word_length': 3.4347408829174664, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6394337', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1494, 'n_words': 379}