Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Merge 7fae79c into 61eaec0
Browse files Browse the repository at this point in the history
  • Loading branch information
jbenito3 committed Aug 28, 2015
2 parents 61eaec0 + 7fae79c commit b01f6c3
Show file tree
Hide file tree
Showing 18 changed files with 636 additions and 238 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ FROM python:3.4
# Install some prerequisites ahead of `setup.py` in order to profit
# from the docker build cache:
RUN pip install flask \
flask-restful \
flask-sqlalchemy \
isodate \
jsonschema \
Expand Down
20 changes: 12 additions & 8 deletions claimstore/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@

"""Flask app creation."""

from flask import Flask, jsonify, render_template
from flask import Flask, jsonify, render_template, request
from flask.ext.sqlalchemy import SQLAlchemy

from claimstore.core.exception import InvalidUsage
from claimstore.core.exception import RestApiException

# Define the database object which is imported
# by modules and controllers
db = SQLAlchemy()


def handle_invalid_usage(error):
def handle_restful_exceptions(error):
"""Handle invalid usage exception."""
response = jsonify(error.to_dict())
response.status_code = error.status_code
Expand All @@ -45,11 +45,11 @@ def create_app(db_create_all=False):
app.config.from_object('config')

# Blueprints
from claimstore.modules.claims.restful import claims_restful
from claimstore.modules.claims.views import claims_views
from claimstore.modules.claims.restful import blueprint as restful_bp
from claimstore.modules.claims.views import blueprint as views_bp

app.register_blueprint(claims_views)
app.register_blueprint(claims_restful)
app.register_blueprint(views_bp)
app.register_blueprint(restful_bp)

# Init databse
db.init_app(app)
Expand All @@ -75,10 +75,14 @@ def create_app(db_create_all=False):
db.session.commit()

# Register exceptions
app.register_error_handler(InvalidUsage, handle_invalid_usage)
app.register_error_handler(RestApiException, handle_restful_exceptions)

@app.errorhandler(404)
def not_found(error):
if 'application/json' in request.headers['Content-Type']:
return jsonify({
'status': 'error',
'message': 'Resource not found.'}), 404
return render_template('404.html'), 404

return app
9 changes: 9 additions & 0 deletions claimstore/core/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@
def now_utc():
"""Return UTC datetime with tzinfo."""
return pytz.utc.localize(datetime.utcnow())


def loc_date_utc(date):
"""Localise a naive date in UTC.
:param date: naive date
:returns: date with UTC tzinfo
"""
return pytz.utc.localize(date)
38 changes: 0 additions & 38 deletions claimstore/core/decorators.py

This file was deleted.

36 changes: 26 additions & 10 deletions claimstore/core/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@
"""Definition of specific exceptions."""


class InvalidUsage(Exception):
class RestApiException(Exception):

"""Invalid request.
Used to identify that a request is not correct (e.g. it does not follow the
proper JSON schema).
"""
"""Generic Rest API exception."""

status_code = 400

Expand All @@ -37,11 +33,31 @@ def __init__(self, message, status_code=None, payload=None, details=None):
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload
self.details = details

def to_dict(self):
"""Return exception as a dictionary."""
rv = dict(self.payload or ())
rv['status'] = 'error'
rv['message'] = self.message
rv = {
'status': 'error',
'message': self.message
}
if self.details:
rv['details'] = self.details
return rv


class InvalidJSONData(RestApiException):

"""Invalid JSON Data.
Used to identify that JSON data that do not follow the appropiate schema.
"""

pass


class InvalidRequest(RestApiException):

"""The REST request could not be fulfilled."""

pass
35 changes: 24 additions & 11 deletions claimstore/modules/claims/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Claim(db.Model):

"""Represents a Claim."""

uid = db.Column(
id = db.Column(
db.Integer,
primary_key=True
)
Expand All @@ -54,12 +54,12 @@ class Claim(db.Model):
)
claimant_id = db.Column(
db.Integer,
db.ForeignKey('claimant.uid'),
db.ForeignKey('claimant.id'),
nullable=False
)
subject_type_id = db.Column(
db.Integer,
db.ForeignKey('identifier_type.uid'),
db.ForeignKey('identifier_type.id'),
nullable=False
)
subject_value = db.Column(
Expand All @@ -68,19 +68,19 @@ class Claim(db.Model):
)
predicate_id = db.Column(
db.Integer,
db.ForeignKey('predicate.uid'),
db.ForeignKey('predicate.id'),
nullable=False
)
certainty = db.Column(
db.Integer,
db.Float,
nullable=False
)
human = db.Column(db.Boolean)
human = db.Column(db.Integer)
actor = db.Column(db.String)
role = db.Column(db.String)
object_type_id = db.Column(
db.Integer,
db.ForeignKey('identifier_type.uid'),
db.ForeignKey('identifier_type.id'),
nullable=False
)
object_value = db.Column(
Expand All @@ -98,7 +98,7 @@ class Claimant(db.Model):

"""Represents a Claimant."""

uid = db.Column(
id = db.Column(
db.Integer,
primary_key=True,
)
Expand All @@ -120,6 +120,13 @@ class Claimant(db.Model):
)
url = db.Column(db.String)

claim = db.relationship(
'Claim',
backref='claimant',
cascade="all, delete-orphan",
lazy='dynamic'
)

def __repr__(self):
"""Printable version of the Claimant object."""
return '<Claimant {}>'.format(self.uuid)
Expand All @@ -129,7 +136,7 @@ class IdentifierType(db.Model):

"""Represents an identifier type."""

uid = db.Column(db.Integer, primary_key=True)
id = db.Column(db.Integer, primary_key=True)
name = db.Column(
db.String,
nullable=False,
Expand All @@ -154,7 +161,7 @@ class IdentifierType(db.Model):
)
claimant_id = db.Column(
db.Integer,
db.ForeignKey('claimant.uid')
db.ForeignKey('claimant.id')
)

def __repr__(self):
Expand All @@ -170,13 +177,19 @@ class Predicate(db.Model):
is_same_as.
"""

uid = db.Column(db.Integer, primary_key=True)
id = db.Column(db.Integer, primary_key=True)
name = db.Column(
db.String,
nullable=False,
unique=True,
index=True
)
claim = db.relationship(
'Claim',
backref='predicate',
cascade="all, delete-orphan",
lazy='dynamic'
)

def __repr__(self):
"""Printable version of the Predicate object."""
Expand Down
Loading

0 comments on commit b01f6c3

Please sign in to comment.