# Path: dnsforever/models.py
# class User(Base):
#     __tablename__ = 'user'
# 
#     EMAIL_PATTERN = re.compile('[^@]+@[^@]+\.[^@]+')
# 
#     id = Column(Integer, primary_key=True)
# 
#     name = Column(Unicode(100), nullable=False)
# 
#     email = Column(Unicode(100), nullable=False, index=True, unique=True)
#     password = Column(Unicode(100), nullable=True)
# 
#     created_at = Column(DateTime(timezone=True), nullable=False,
#                         default=functions.now())
#     type = Column(Integer, nullable=False, default=2)
# 
#     @validates('email')
#     def email_validates(self, key, email):
#         if email is None:
#             return None
#         email = email.strip()
#         if self.EMAIL_PATTERN.match(email):
#             return email
#         raise ValueError
# 
# class Domain(Base):
#     __tablename__ = 'domain'
# 
#     DOMAIN_PATTERN = re.compile('^([a-z0-9\-]+\.)+([a-z0-9\-]+)$')
# 
#     id = Column(Integer, primary_key=True)
#     name = Column(String(255), nullable=False, index=True)
#     created_at = Column(DateTime(timezone=True), nullable=False,
#                         default=functions.now())
#     updated_at = Column(DateTime(timezone=True), nullable=False,
#                         default=functions.now())
#     update_serial = Column(Integer, nullable=False, default=1)
# 
#     parent_id = Column(Integer, ForeignKey('domain.id'), nullable=True,
#                        default=None)
#     parent = relationship('Domain')
# 
#     @validates('domain')
#     def domain_validates(self, key, domain):
#         if not isinstance(domain, str):
#             raise ValueError
#         if not self.DOMAIN_PATTERN.match(domain):
#             raise ValueError
# 
#         check_domain(domain)
#         return domain
# 
#     def update(self):
#         self.updated_at = functions.now()
#         self.update_serial = self.update_serial + 1
# 
# class DomainOwnership(Base):
#     __tablename__ = 'domain_ownership'
# 
#     id = Column(Integer, primary_key=True)
# 
#     user_id = Column(Integer, ForeignKey('user.id'), nullable=False)
#     user = relationship(User, backref='ownership')
# 
#     domain_id = Column(Integer, ForeignKey('domain.id'), nullable=False)
#     domain = relationship(Domain, backref='ownership')
# 
#     master = Column(Boolean, nullable=False, default=False)


from flask import session, redirect, g
from functools import wraps
from dnsforever.models import User, Domain, DomainOwnership



def set_user(user):
    if user is None:
        del session['user_id']
        return
    if not isinstance(user, User):
        raise TypeError
    session['user_id'] = user.id


def get_user():
    if 'user_id' not in session:
        return None
    if hasattr(g, 'user'):
        return g.user

# Path: dnsforever/models.py
# class User(Base):
#     __tablename__ = 'user'
# 
#     EMAIL_PATTERN = re.compile('[^@]+@[^@]+\.[^@]+')
# 
#     id = Column(Integer, primary_key=True)
# 
#     name = Column(Unicode(100), nullable=False)
# 
#     email = Column(Unicode(100), nullable=False, index=True, unique=True)
#     password = Column(Unicode(100), nullable=True)
# 
#     created_at = Column(DateTime(timezone=True), nullable=False,
#                         default=functions.now())
#     type = Column(Integer, nullable=False, default=2)
# 
#     @validates('email')
#     def email_validates(self, key, email):
#         if email is None:
#             return None
#         email = email.strip()
#         if self.EMAIL_PATTERN.match(email):
#             return email
#         raise ValueError
# 
# class Domain(Base):
#     __tablename__ = 'domain'
# 
#     DOMAIN_PATTERN = re.compile('^([a-z0-9\-]+\.)+([a-z0-9\-]+)$')
# 
#     id = Column(Integer, primary_key=True)
#     name = Column(String(255), nullable=False, index=True)
#     created_at = Column(DateTime(timezone=True), nullable=False,
#                         default=functions.now())
#     updated_at = Column(DateTime(timezone=True), nullable=False,
#                         default=functions.now())
#     update_serial = Column(Integer, nullable=False, default=1)
# 
#     parent_id = Column(Integer, ForeignKey('domain.id'), nullable=True,
#                        default=None)
#     parent = relationship('Domain')
# 
#     @validates('domain')
#     def domain_validates(self, key, domain):
#         if not isinstance(domain, str):
#             raise ValueError
#         if not self.DOMAIN_PATTERN.match(domain):
#             raise ValueError
# 
#         check_domain(domain)
#         return domain
# 
#     def update(self):
#         self.updated_at = functions.now()
#         self.update_serial = self.update_serial + 1
# 
# class DomainOwnership(Base):
#     __tablename__ = 'domain_ownership'
# 
#     id = Column(Integer, primary_key=True)
# 
#     user_id = Column(Integer, ForeignKey('user.id'), nullable=False)
#     user = relationship(User, backref='ownership')
# 
#     domain_id = Column(Integer, ForeignKey('domain.id'), nullable=False)
#     domain = relationship(Domain, backref='ownership')
# 
#     master = Column(Boolean, nullable=False, default=False)


# Path: dnsforever/web/tools/session.py
from flask import session, redirect, g
from functools import wraps
from dnsforever.models import User, Domain, DomainOwnership




def set_user(user):
    if user is None:
        del session['user_id']
        return
    if not isinstance(user, User):
        raise TypeError
    session['user_id'] = user.id


def get_user():
    if 'user_id' not in session:
        return None
    if hasattr(g, 'user'):
        return g.user

    g.user = g.session.query(User).filter_by(id=session['user_id']).first()