# Path: hkust-gmission/gmission/rest/base.py
# class ReSTBase(object):
#     @classmethod
#     def universal_before_post(cls, data):
#         # print 'Universal before_post'
#         data.pop('id', None)
# 
#     @classmethod
#     def universal_after_get_many(cls, result=None, search_params=None, **kwargs):
#         if request.method == 'HEAD':
#             if result is not None:
#                 result.pop('objects', [])
#         pass
# 
#     @classmethod
#     @jwt_auth()
#     def check_user_token(cls, **kw):
#         return True
# 
#     @classmethod
#     def processor_name_mapping(cls, prefix):
#         exclude_list = []
#         processors = {}
#         processors_fields = ['GET_SINGLE', 'GET_MANY', 'PATCH_SINGLE', 'PATCH_MANY', 'PUT_SINGLE', 'PUT_MANY', 'POST',
#                              'DELETE']
#         for raw_method in inspect.getmembers(cls, predicate=inspect.ismethod):
#             name, method = raw_method
#             if name.startswith(prefix):
#                 processors[name[len(prefix) + 1:].upper()] = [method.__get__(cls), ]
# 
#         # if cls.__name__ not in exclude_list and prefix == 'before':
#         #     for key in processors_fields:
#         #         preprocessor = processors.get(key, [])
#         #         preprocessor.insert(0, cls.check_user_token)
#         #         processors[key] = preprocessor
#         return processors
# 
#     @classmethod
#     def universal_preprocessors(cls):
#         prefix = 'universal_before'
#         return ReSTBase.processor_name_mapping(prefix)
# 
#     @classmethod
#     def universal_postprocessors(cls):
#         prefix = 'universal_after'
#         return ReSTBase.processor_name_mapping(prefix)
# 
#     @classmethod
#     def rest_preprocessors(cls):
#         prefix = 'before'
#         return cls.processor_name_mapping(prefix)
# 
#     @classmethod
#     def rest_postprocessors(cls):
#         prefix = 'after'
#         return cls.processor_name_mapping(prefix)
# 
#     @classmethod
#     def rest_exclude_columns(cls):
#         # r = [cln for cln in cls.__mapper__.columns if isinstance(cln, db.RelationshipProperty)]
#         return [str(r).split('.')[1] for r in cls.__mapper__.relationships]


import inspect
from flask.ext import restless
from gmission.models import *
from .base import ReSTBase
from gmission.flask_app import db, app
__author__ = 'chenzhao'


REST_PREFIX = '/rest'


class ReSTManager(object):
    rest_models = []

    @classmethod
    def rest_url_get_single(cls, model_obj):
        return '%s/%s/%d' % (REST_PREFIX, model_obj.urlname(), model_obj.id)

    @classmethod
    def init_rest(cls, app):
        for model_cls in globals().values():
            if inspect.isclass(model_cls) and issubclass(model_cls, db.Model):
                # print model_cls
                if model_cls not in ReSTBase.__subclasses__():
                    ReSTManager.rest_models.append(type('ReST' + model_cls.__name__, (model_cls, ReSTBase), {}))

        cls.manager = restless.APIManager(app, flask_sqlalchemy_db=db,
                                          preprocessors=ReSTBase.universal_preprocessors(),
                                          postprocessors=ReSTBase.universal_postprocessors())

# Path: hkust-gmission/gmission/rest/base.py
# class ReSTBase(object):
#     @classmethod
#     def universal_before_post(cls, data):
#         # print 'Universal before_post'
#         data.pop('id', None)
# 
#     @classmethod
#     def universal_after_get_many(cls, result=None, search_params=None, **kwargs):
#         if request.method == 'HEAD':
#             if result is not None:
#                 result.pop('objects', [])
#         pass
# 
#     @classmethod
#     @jwt_auth()
#     def check_user_token(cls, **kw):
#         return True
# 
#     @classmethod
#     def processor_name_mapping(cls, prefix):
#         exclude_list = []
#         processors = {}
#         processors_fields = ['GET_SINGLE', 'GET_MANY', 'PATCH_SINGLE', 'PATCH_MANY', 'PUT_SINGLE', 'PUT_MANY', 'POST',
#                              'DELETE']
#         for raw_method in inspect.getmembers(cls, predicate=inspect.ismethod):
#             name, method = raw_method
#             if name.startswith(prefix):
#                 processors[name[len(prefix) + 1:].upper()] = [method.__get__(cls), ]
# 
#         # if cls.__name__ not in exclude_list and prefix == 'before':
#         #     for key in processors_fields:
#         #         preprocessor = processors.get(key, [])
#         #         preprocessor.insert(0, cls.check_user_token)
#         #         processors[key] = preprocessor
#         return processors
# 
#     @classmethod
#     def universal_preprocessors(cls):
#         prefix = 'universal_before'
#         return ReSTBase.processor_name_mapping(prefix)
# 
#     @classmethod
#     def universal_postprocessors(cls):
#         prefix = 'universal_after'
#         return ReSTBase.processor_name_mapping(prefix)
# 
#     @classmethod
#     def rest_preprocessors(cls):
#         prefix = 'before'
#         return cls.processor_name_mapping(prefix)
# 
#     @classmethod
#     def rest_postprocessors(cls):
#         prefix = 'after'
#         return cls.processor_name_mapping(prefix)
# 
#     @classmethod
#     def rest_exclude_columns(cls):
#         # r = [cln for cln in cls.__mapper__.columns if isinstance(cln, db.RelationshipProperty)]
#         return [str(r).split('.')[1] for r in cls.__mapper__.relationships]


# Path: hkust-gmission/gmission/rest/manager.py
import inspect
from flask.ext import restless
from gmission.models import *
from .base import ReSTBase
from gmission.flask_app import db, app

__author__ = 'chenzhao'


REST_PREFIX = '/rest'


class ReSTManager(object):
    rest_models = []

    @classmethod
    def rest_url_get_single(cls, model_obj):
        return '%s/%s/%d' % (REST_PREFIX, model_obj.urlname(), model_obj.id)

    @classmethod
    def init_rest(cls, app):
        for model_cls in globals().values():
            if inspect.isclass(model_cls) and issubclass(model_cls, db.Model):
                # print model_cls
                if model_cls not in ReSTBase.__subclasses__():
                    ReSTManager.rest_models.append(type('ReST' + model_cls.__name__, (model_cls, ReSTBase), {}))

        cls.manager = restless.APIManager(app, flask_sqlalchemy_db=db,
                                          preprocessors=ReSTBase.universal_preprocessors(),
                                          postprocessors=ReSTBase.universal_postprocessors())

        for rest_class in ReSTBase.__subclasses__():