# Path: vilya/core.py
# class VilyaError(Exception):
# class VilyaFormError(Exception):
# class Service(object):
#     def __init__(self, msg):
#     def __init__(self, errors=None):
#     def _isinstance(self, model, raise_error=True):
#     def _preprocess_params(self, kwargs):
#     def save(self, model):
#     def all(self):
#     def get(self, id):
#     def get_all(self, *ids):
#     def find(self, **kwargs):
#     def first(self, **kwargs):
#     def get_or_404(self, id):
#     def new(self, **kwargs):
#     def create(self, **kwargs):
#     def update(self, model, **kwargs):
#     def delete(self, model):
#     def transaction(cls, func):
#         def transaction_block(*args, **kwargs):
# 
# Path: vilya/helpers.py
# class JsonSerializer(object):
#     """A mixin that can be used to mark a SQLAlchemy model class which
#     implements a :func:`to_json` method. The :func:`to_json` method is used
#     in conjuction with the custom :class:`JSONEncoder` class. By default this
#     mixin will assume all properties of the SQLAlchemy model are to be visible
#     in the JSON output. Extend this class to customize which properties are
#     public, hidden or modified before being being passed to the JSON serializer.
#     """
# 
#     __json_public__ = None
#     __json_hidden__ = None
#     __json_modifiers__ = None
# 
#     def get_field_names(self):
#         for p in self.__mapper__.iterate_properties:
#             yield p.key
# 
#     def to_json(self):
#         field_names = self.get_field_names()
# 
#         public = self.__json_public__ or field_names
#         hidden = self.__json_hidden__ or []
#         modifiers = self.__json_modifiers__ or dict()
# 
#         rv = dict()
#         for key in public:
#             rv[key] = getattr(self, key)
#         for key, modifier in modifiers.items():
#             value = getattr(self, key)
#             rv[key] = modifier(value, self)
#         for key in hidden:
#             rv.pop(key, None)
#         return rv
# 
# Path: vilya/libs/avatar.py
# def get_gravatar_url(email, size=140):
#     normalized_email = email.encode('utf8').lower()
#     url = "https://secure.gravatar.com/avatar/%s?%s" % (
#         hashlib.md5(normalized_email).hexdigest(),
#         urllib.urlencode({'d': DEFAULT_USER,
#                           's': str(size),
#                           'r': 'x'}))
#     return url


from datetime import datetime
from flask_security import UserMixin, RoleMixin
from ..core import db
from ..helpers import JsonSerializer
from ..libs.avatar import get_gravatar_url




roles_users = db.Table(
    'roles_users',
    db.Column('user_id', db.Integer(), db.ForeignKey('users.id')),
    db.Column('role_id', db.Integer(), db.ForeignKey('roles.id')))


class Role(RoleMixin, db.Model):
    __tablename__ = 'roles'

    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

    def __eq__(self, other):
        return (self.name == other or
                self.name == getattr(other, 'name', None))

    def __ne__(self, other):
        return (self.name != other and
                self.name != getattr(other, 'name', None))


class UserJsonSerializer(JsonSerializer):
    __json_public__ = ['id', 'email']



# Path: vilya/core.py
# class VilyaError(Exception):
# class VilyaFormError(Exception):
# class Service(object):
#     def __init__(self, msg):
#     def __init__(self, errors=None):
#     def _isinstance(self, model, raise_error=True):
#     def _preprocess_params(self, kwargs):
#     def save(self, model):
#     def all(self):
#     def get(self, id):
#     def get_all(self, *ids):
#     def find(self, **kwargs):
#     def first(self, **kwargs):
#     def get_or_404(self, id):
#     def new(self, **kwargs):
#     def create(self, **kwargs):
#     def update(self, model, **kwargs):
#     def delete(self, model):
#     def transaction(cls, func):
#         def transaction_block(*args, **kwargs):
# 
# Path: vilya/helpers.py
# class JsonSerializer(object):
#     """A mixin that can be used to mark a SQLAlchemy model class which
#     implements a :func:`to_json` method. The :func:`to_json` method is used
#     in conjuction with the custom :class:`JSONEncoder` class. By default this
#     mixin will assume all properties of the SQLAlchemy model are to be visible
#     in the JSON output. Extend this class to customize which properties are
#     public, hidden or modified before being being passed to the JSON serializer.
#     """
# 
#     __json_public__ = None
#     __json_hidden__ = None
#     __json_modifiers__ = None
# 
#     def get_field_names(self):
#         for p in self.__mapper__.iterate_properties:
#             yield p.key
# 
#     def to_json(self):
#         field_names = self.get_field_names()
# 
#         public = self.__json_public__ or field_names
#         hidden = self.__json_hidden__ or []
#         modifiers = self.__json_modifiers__ or dict()
# 
#         rv = dict()
#         for key in public:
#             rv[key] = getattr(self, key)
#         for key, modifier in modifiers.items():
#             value = getattr(self, key)
#             rv[key] = modifier(value, self)
#         for key in hidden:
#             rv.pop(key, None)
#         return rv
# 
# Path: vilya/libs/avatar.py
# def get_gravatar_url(email, size=140):
#     normalized_email = email.encode('utf8').lower()
#     url = "https://secure.gravatar.com/avatar/%s?%s" % (
#         hashlib.md5(normalized_email).hexdigest(),
#         urllib.urlencode({'d': DEFAULT_USER,
#                           's': str(size),
#                           'r': 'x'}))
#     return url


# Path: vilya/users/models.py
from datetime import datetime
from flask_security import UserMixin, RoleMixin
from ..core import db
from ..helpers import JsonSerializer
from ..libs.avatar import get_gravatar_url





roles_users = db.Table(
    'roles_users',
    db.Column('user_id', db.Integer(), db.ForeignKey('users.id')),
    db.Column('role_id', db.Integer(), db.ForeignKey('roles.id')))


class Role(RoleMixin, db.Model):
    __tablename__ = 'roles'

    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

    def __eq__(self, other):
        return (self.name == other or
                self.name == getattr(other, 'name', None))

    def __ne__(self, other):
        return (self.name != other and
                self.name != getattr(other, 'name', None))


class UserJsonSerializer(JsonSerializer):
    __json_public__ = ['id', 'email']



class User(UserJsonSerializer, UserMixin, db.Model):