<commit_before># -*- coding: utf-8 -*-
from flask.ext.security import  SQLAlchemyUserDatastore
from ..extensions import db
from flask import current_app
from ..models.security import User, Role
from sqlalchemy.orm import joinedload, scoped_session
from flask import g, current_app

class CacheUserDatastore(SQLAlchemyUserDatastore):

    def __init__(self, db = None, user_model=None, role_model=None):
        self.user_model = user_model
        self.role_model = role_model
        self.db = db

    def init_app(self, db, user_model, role_model):
        self.user_model = user_model
        self.role_model = role_model
        self.db = db

    def get_user(self, identifier):
        try:
           filter_ = {"id": int(identifier)}
        except ValueError:
           filter_ = {"email": identifier}
        return self.find_user(**filter_)

    def find_user(self, **kwargs):
        if kwargs.keys()[0] == 'id':
            return User.cache.get(kwargs['id'])
        try:
            return User.cache.filter(**kwargs).next()
        except StopIteration:
            return None

    def find_role(self, role):
        try:
            return Role.cache.filter(name=role).next()
        except StopIteration:
            return None<commit_msg>Retrieve a CachedUser when requesting with an apikey.<commit_after># -*- coding: utf-8 -*-
from flask.ext.security import  SQLAlchemyUserDatastore
from ..extensions import db, regions
from flask import current_app
from ..models.security import User, Role, UserMixin
from sqlalchemy.orm import joinedload, scoped_session
from flask import g, current_app
from sqlalchemy import inspect

class CachedValue(object):
    def __init__(self, v):
        for i in inspect(v).attrs:
            if isinstance(i.value, list):
                setattr(self, i.key, [])
                for i2 in i.value:
                    getattr(self, i.key).append(CachedValue(i2))
            else:
                setattr(self, i.key, i.value)

    @classmethod
    def create(cls, **kwargs):
        def creator():
            v = cls.base_class.query.filter_by(**kwargs).first()
            if v:
                return cls(v)
            return None
        return creator

    @classmethod
    def get_key(cls, **kwargs):
        return '{}.{}[{}]'.format(cls.base_class.__tablename__,
                kwargs.keys()[0], kwargs.values()[0])

    @classmethod
    def get(cls, **kwargs):
        return regions[cls.base_class.cache_label].get_or_create(
                cls.get_key(**kwargs),
                cls.create(**kwargs)
        )


class CachedUser(CachedValue, UserMixin):
    base_class = User


class CacheUserDatastore(SQLAlchemyUserDatastore):

    def __init__(self, db = None, user_model=None, role_model=None):
        self.user_model = user_model
        self.role_model = role_model
        self.db = db

    def init_app(self, db, user_model, role_model):
        self.user_model = user_model
        self.role_model = role_model
        self.db = db

    def get_user(self, identifier):
        try:
           filter_ = {"id": int(identifier)}
        except ValueError:
           filter_ = {"email": identifier}
        return self.find_user(**filter_)

    def find_user(self, **kwargs):
        if kwargs.keys()[0] == 'id':
            return User.query.get(kwargs['id'])
        elif kwargs.keys()[0] == 'apikey':
            return CachedUser.get(**kwargs)
        try:
            return User.query.filter_by(**kwargs).first()
        except StopIteration:
            return None

    def find_role(self, role):
        try:
            return Role.query.filter_by(name=role).first()
        except StopIteration:
            return None
{'fraction_non_alphanumeric': 0.1084727468969239, 'fraction_numerical': 0.0024284943335132216, 'mean_word_length': 2.3098214285714285, 'pattern_counts': {'":': 4, '<': 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': '1440818', 'n_tokens_mistral': 1108, 'n_tokens_neox': 1070, 'n_words': 266}