# Path: codebox/utils/models.py
# class Model(object):
#     __metaclass__ = ModelDescriptor
# 
#     DoesNotExist = DoesNotExist
#     DuplicateKeyError = DuplicateKeyError
# 
#     def __init__(self, pk, **kwargs):
#         self._storage = RedisHashMap(g.redis, '%s:items:%s' % (self._meta.db_name, pk))
#         self.pk = pk
#         for attname, field in self._meta.fields.iteritems():
#             try:
#                 val = field.to_python(kwargs.pop(attname))
#             except KeyError:
#                 # This is done with an exception rather than the
#                 # default argument on pop because we don't want
#                 # get_default() to be evaluated, and then not used.
#                 # Refs #12057.
#                 val = field.get_default()
#             if val:
#                 setattr(self, attname, val)
#         if kwargs:
#             raise ValueError('%s are not part of the schema for %s' % (', '.join(kwargs.keys()), self.__class__.__name__))
# 
#     def __getattribute__(self, key):
#         if key in object.__getattribute__(self, '_meta').fields:
#             return self[key]
#         return object.__getattribute__(self, key)
# 
#     def __setattr__(self, key, value):
#         if key in object.__getattribute__(self, '_meta').fields:
#             self[key] = value
#         return object.__setattr__(self, key, value)
# 
#     def __getitem__(self, key):
#         return self._storage[key]
# 
#     def __setitem__(self, key, value):
#         field = self._meta.fields.get(key)
#         if field:
#             value = field.to_python(value)
#         self._storage[key] = value
#         # Store additional predefined index
#         if (key,) in self._meta.index or (key,) in self._meta.unique:
#             self.objects.add_to_index(self.pk, **{key: value})
# 
#     def __repr__(self):
#         return u'<%s: %s>' % (self.__class__.__name__, unicode(self))
# 
#     def __eq__(self, other):
#         if type(self) != type(other):
#             return False
#         return self.pk == other.pk
# 
#     def __unicode__(self):
#         return self.pk
# 
#     def __contains__(self, key):
#         return key in self._storage
# 
#     def update(self, **kwargs):
#         for k, v in kwargs.iteritems():
#             self[k] = v
#         if len(kwargs) > 1:
#             # Store composite indexes
#             for fields in itertools.chain(self._meta.index, self._meta.unique):
#                 if all([f in kwargs for f in fields]):
#                     idx_kwargs = dict((f, getattr(self, f)) for f in fields)
#                     self.objects.add_to_index(self.pk, **idx_kwargs)
# 
#     def delete(self):
#         # Clear all indexes first
#         #  Default index
#         g.redis.zrem(self.objects._get_default_index_key(), self.pk)
#         g.redis.decr(self.objects._get_default_count_key())
# 
#         #  Store additional predefined index
#         for fields in itertools.chain(self._meta.index, self._meta.unique):
#             idx_kwargs = dict((f, getattr(self, f)) for f in fields)
#             self.objects.remove_from_index(self.pk, **idx_kwargs)
# 
#         # Clear out the hash table for object
#         self._storage.clear()
# 
#     def post_create(self):
#         pass
# 
# class String(Field):
#     def to_python(self, value=None):
#         if value:
#             value = unicode(value)
#         return value
# 
# class Float(Field):
#     def to_python(self, value=None):
#         if value:
#             value = float(value)
#         else:
#             value = 0.0
#         return value
# 
# class Boolean(Field):
#     def to_db(self, value=None):
#         if not value:
#             value = False
#         return int(bool(value))
#     
#     def to_python(self, value=None):
#         if not value:
#             value = False
#         return bool(int(value))


import time
from codebox.utils.models import Model, String, Float, Boolean
        from codebox.apps.organizations.models import OrganizationMember, Organization
    created_at = Float(default=time.time)
    api_token = String(required=False)

    class Meta:
        unique = (('email',), ('api_token',))

    def get_all_organizations(self, admin=False):

        memberships = list(OrganizationMember.objects.filter(user=self.pk))
        orgs = Organization.objects.get_many([m.org for m in memberships])
        if admin:
            orgs = filter(lambda x: x.owned_by == self.pk, orgs)
        return orgs

    def can_admin_org(self, org):
        return org.owned_by == self.pk

    def get_relation(self, relation):
        return list(relation.objects.filter(user=self.pk))

class Email(Model):
    user = String()
    email = String()
    verified = Boolean(default=False)
    
    class Meta:
        index = (('user',),)
        unique = (('email',),)

class Profile(Model):

# Path: codebox/utils/models.py
# class Model(object):
#     __metaclass__ = ModelDescriptor
# 
#     DoesNotExist = DoesNotExist
#     DuplicateKeyError = DuplicateKeyError
# 
#     def __init__(self, pk, **kwargs):
#         self._storage = RedisHashMap(g.redis, '%s:items:%s' % (self._meta.db_name, pk))
#         self.pk = pk
#         for attname, field in self._meta.fields.iteritems():
#             try:
#                 val = field.to_python(kwargs.pop(attname))
#             except KeyError:
#                 # This is done with an exception rather than the
#                 # default argument on pop because we don't want
#                 # get_default() to be evaluated, and then not used.
#                 # Refs #12057.
#                 val = field.get_default()
#             if val:
#                 setattr(self, attname, val)
#         if kwargs:
#             raise ValueError('%s are not part of the schema for %s' % (', '.join(kwargs.keys()), self.__class__.__name__))
# 
#     def __getattribute__(self, key):
#         if key in object.__getattribute__(self, '_meta').fields:
#             return self[key]
#         return object.__getattribute__(self, key)
# 
#     def __setattr__(self, key, value):
#         if key in object.__getattribute__(self, '_meta').fields:
#             self[key] = value
#         return object.__setattr__(self, key, value)
# 
#     def __getitem__(self, key):
#         return self._storage[key]
# 
#     def __setitem__(self, key, value):
#         field = self._meta.fields.get(key)
#         if field:
#             value = field.to_python(value)
#         self._storage[key] = value
#         # Store additional predefined index
#         if (key,) in self._meta.index or (key,) in self._meta.unique:
#             self.objects.add_to_index(self.pk, **{key: value})
# 
#     def __repr__(self):
#         return u'<%s: %s>' % (self.__class__.__name__, unicode(self))
# 
#     def __eq__(self, other):
#         if type(self) != type(other):
#             return False
#         return self.pk == other.pk
# 
#     def __unicode__(self):
#         return self.pk
# 
#     def __contains__(self, key):
#         return key in self._storage
# 
#     def update(self, **kwargs):
#         for k, v in kwargs.iteritems():
#             self[k] = v
#         if len(kwargs) > 1:
#             # Store composite indexes
#             for fields in itertools.chain(self._meta.index, self._meta.unique):
#                 if all([f in kwargs for f in fields]):
#                     idx_kwargs = dict((f, getattr(self, f)) for f in fields)
#                     self.objects.add_to_index(self.pk, **idx_kwargs)
# 
#     def delete(self):
#         # Clear all indexes first
#         #  Default index
#         g.redis.zrem(self.objects._get_default_index_key(), self.pk)
#         g.redis.decr(self.objects._get_default_count_key())
# 
#         #  Store additional predefined index
#         for fields in itertools.chain(self._meta.index, self._meta.unique):
#             idx_kwargs = dict((f, getattr(self, f)) for f in fields)
#             self.objects.remove_from_index(self.pk, **idx_kwargs)
# 
#         # Clear out the hash table for object
#         self._storage.clear()
# 
#     def post_create(self):
#         pass
# 
# class String(Field):
#     def to_python(self, value=None):
#         if value:
#             value = unicode(value)
#         return value
# 
# class Float(Field):
#     def to_python(self, value=None):
#         if value:
#             value = float(value)
#         else:
#             value = 0.0
#         return value
# 
# class Boolean(Field):
#     def to_db(self, value=None):
#         if not value:
#             value = False
#         return int(bool(value))
#     
#     def to_python(self, value=None):
#         if not value:
#             value = False
#         return bool(int(value))


# Path: codebox/apps/auth/models.py
import time
from codebox.utils.models import Model, String, Float, Boolean
        from codebox.apps.organizations.models import OrganizationMember, Organization

    created_at = Float(default=time.time)
    api_token = String(required=False)

    class Meta:
        unique = (('email',), ('api_token',))

    def get_all_organizations(self, admin=False):

        memberships = list(OrganizationMember.objects.filter(user=self.pk))
        orgs = Organization.objects.get_many([m.org for m in memberships])
        if admin:
            orgs = filter(lambda x: x.owned_by == self.pk, orgs)
        return orgs

    def can_admin_org(self, org):
        return org.owned_by == self.pk

    def get_relation(self, relation):
        return list(relation.objects.filter(user=self.pk))

class Email(Model):
    user = String()
    email = String()
    verified = Boolean(default=False)
    
    class Meta:
        index = (('user',),)
        unique = (('email',),)

class Profile(Model):

    user = String()