# Path: odpw/web_rest/rest/odpw_restapi.py
# def default_error_handler(error):
# def database_not_found_error_handler(e):
# 
# Path: odpw/web_rest/rest/helpers.py
# def toJSON(func):
#     @wraps(func)
#     def decorated_function(*args, **kwargs):
#         results= func(*args, **kwargs)
#         return Response(json.dumps(results),  mimetype='application/json')
# 
#     return decorated_function
# 
# def toCSV(func):
#     @wraps(func)
#     def decorated_function(*args, **kwargs):
#         results= func(*args, **kwargs)
# 
#         keys = results[0].keys()
# 
#         si = StringIO.StringIO()
#         cw = csv.DictWriter(si,keys)
#         cw.writeheader()
#         cw.writerows(results)
# 
#         output = make_response(si.getvalue())
#         #output.headers["Content-Disposition"] = "attachment; filename=portals.csv"
#         output.headers["Content-type"] = "text/csv"
#         return output
# 
#     return decorated_function
# 
# Path: odpw/core/db.py
# def row2dict(r):
#     if hasattr(r, '_fields'):
#         d = {}
#         for field in r._fields:
#             rf = r.__getattribute__(field)
#             if isinstance(rf, Base):
#                 d.update(_row2dict(rf))
#             else:
#                 d[field] = rf
#         return d
#     if isinstance(r, Base):
#         return _row2dict(r)
# 
# Path: odpw/core/model.py
# class Portal(Base):
# class PortalSnapshot(Base):
# class Serializable(object):
# class PortalSnapshotDynamicity(Base,Serializable):
# class PortalSnapshotQuality(Base):
# class Dataset(Base):
# class DatasetData(Base):
# class DatasetQuality(Base):
# class MetaResource(Base):
# class ResourceInfo(Base):
# class ResourceCrawlLog(Base):
# class ResourceHistory(Base):
# class ResourceFreshness(Base):
# class FormatDist(Base):
#     def snapshot_count(self):
#     def snapshot_count(cls):
#     def first_snapshot(self):
#     def first_snapshot(cls):
#     def last_snapshot(self):
#     def last_snapshot(cls):
#     def datasetcount(self):
#     def datasetcount(cls):
#     def resourcecount(self):
#     def resourcecount(cls):
#     def __repr__(self):
#     def fetchtime(self):
#     def __repr__(self):
#     def to_dict(self):
#     def dyratio(self):
#     def adddelratio(self):
#     def addRatio(self):
#     def delRatio(self):
#     def updatedRatio(self):
#     def staticRatio(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def info(cls):
#     def __repr__(self):
# 
# Path: odpw/web_rest/cache.py



import json
import logging
from flask import current_app, Response, jsonify
from odpw.web_rest.rest.odpw_restapi import api
from odpw.web_rest.rest.helpers import toJSON, toCSV
from odpw.core.db import row2dict
from odpw.core.model import Portal, PortalSnapshotQuality, PortalSnapshot, ResourceInfo, Base
from odpw.web_rest.cache import cache
from flask_restplus import Resource
from flask import request
from flask_restplus import Resource
from flask_restplus import reqparse
from flask_restplus import fields
    @ns.doc('get_PortalsQuality')
    def get(self, snapshot):
        """
       get list of portals with their quality assessment metrics for the specified snapshot
        """
        #args = pagination_arguments.parse_args(request)
        #page = args.get('page', 1)
        #per_page = args.get('per_page', 10)
        session=current_app.config['dbsession']
        data=[row2dict(r) for r in session.query(Portal, Portal.datasetcount, Portal.resourcecount).join(PortalSnapshotQuality).filter(PortalSnapshotQuality.snapshot==snapshot).add_entity(PortalSnapshotQuality)]

        return Response(json.dumps(data),
                        mimetype='application/json')

@ns.route('/quality')
class PortalsCurQuality(Resource):

    #@ns.expect(pagination_arguments)
    #@api.marshal_with(portalsquality, as_list=True)

    @ns.doc('get_PortalsCurQuality')
    def get(self):
        """
        get list of portals with their current quality assessment metrics.
        """
        #args = pagination_arguments.parse_args(request)
        #page = args.get('page', 1)
        #per_page = args.get('per_page', 10)
        session=current_app.config['dbsession']
        #snapshot=getPreviousWeek(getSnapshotfromTime(datetime.datetime.now()))

# Path: odpw/web_rest/rest/odpw_restapi.py
# def default_error_handler(error):
# def database_not_found_error_handler(e):
# 
# Path: odpw/web_rest/rest/helpers.py
# def toJSON(func):
#     @wraps(func)
#     def decorated_function(*args, **kwargs):
#         results= func(*args, **kwargs)
#         return Response(json.dumps(results),  mimetype='application/json')
# 
#     return decorated_function
# 
# def toCSV(func):
#     @wraps(func)
#     def decorated_function(*args, **kwargs):
#         results= func(*args, **kwargs)
# 
#         keys = results[0].keys()
# 
#         si = StringIO.StringIO()
#         cw = csv.DictWriter(si,keys)
#         cw.writeheader()
#         cw.writerows(results)
# 
#         output = make_response(si.getvalue())
#         #output.headers["Content-Disposition"] = "attachment; filename=portals.csv"
#         output.headers["Content-type"] = "text/csv"
#         return output
# 
#     return decorated_function
# 
# Path: odpw/core/db.py
# def row2dict(r):
#     if hasattr(r, '_fields'):
#         d = {}
#         for field in r._fields:
#             rf = r.__getattribute__(field)
#             if isinstance(rf, Base):
#                 d.update(_row2dict(rf))
#             else:
#                 d[field] = rf
#         return d
#     if isinstance(r, Base):
#         return _row2dict(r)
# 
# Path: odpw/core/model.py
# class Portal(Base):
# class PortalSnapshot(Base):
# class Serializable(object):
# class PortalSnapshotDynamicity(Base,Serializable):
# class PortalSnapshotQuality(Base):
# class Dataset(Base):
# class DatasetData(Base):
# class DatasetQuality(Base):
# class MetaResource(Base):
# class ResourceInfo(Base):
# class ResourceCrawlLog(Base):
# class ResourceHistory(Base):
# class ResourceFreshness(Base):
# class FormatDist(Base):
#     def snapshot_count(self):
#     def snapshot_count(cls):
#     def first_snapshot(self):
#     def first_snapshot(cls):
#     def last_snapshot(self):
#     def last_snapshot(cls):
#     def datasetcount(self):
#     def datasetcount(cls):
#     def resourcecount(self):
#     def resourcecount(cls):
#     def __repr__(self):
#     def fetchtime(self):
#     def __repr__(self):
#     def to_dict(self):
#     def dyratio(self):
#     def adddelratio(self):
#     def addRatio(self):
#     def delRatio(self):
#     def updatedRatio(self):
#     def staticRatio(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def info(cls):
#     def __repr__(self):
# 
# Path: odpw/web_rest/cache.py



# Path: odpw/web_rest/rest/portals_namespace.py
import json
import logging
from flask import current_app, Response, jsonify
from odpw.web_rest.rest.odpw_restapi import api
from odpw.web_rest.rest.helpers import toJSON, toCSV
from odpw.core.db import row2dict
from odpw.core.model import Portal, PortalSnapshotQuality, PortalSnapshot, ResourceInfo, Base
from odpw.web_rest.cache import cache
from flask_restplus import Resource
from flask import request
from flask_restplus import Resource
from flask_restplus import reqparse
from flask_restplus import fields

    @ns.doc('get_PortalsQuality')
    def get(self, snapshot):
        """
       get list of portals with their quality assessment metrics for the specified snapshot
        """
        #args = pagination_arguments.parse_args(request)
        #page = args.get('page', 1)
        #per_page = args.get('per_page', 10)
        session=current_app.config['dbsession']
        data=[row2dict(r) for r in session.query(Portal, Portal.datasetcount, Portal.resourcecount).join(PortalSnapshotQuality).filter(PortalSnapshotQuality.snapshot==snapshot).add_entity(PortalSnapshotQuality)]

        return Response(json.dumps(data),
                        mimetype='application/json')

@ns.route('/quality')
class PortalsCurQuality(Resource):

    #@ns.expect(pagination_arguments)
    #@api.marshal_with(portalsquality, as_list=True)

    @ns.doc('get_PortalsCurQuality')
    def get(self):
        """
        get list of portals with their current quality assessment metrics.
        """
        #args = pagination_arguments.parse_args(request)
        #page = args.get('page', 1)
        #per_page = args.get('per_page', 10)
        session=current_app.config['dbsession']
        #snapshot=getPreviousWeek(getSnapshotfromTime(datetime.datetime.now()))

        data=[row2dict(r) for r in session.query(Portal,  Portal.datasetcount, Portal.resourcecount).join(PortalSnapshotQuality).add_entity(PortalSnapshotQuality)]