# Path: dnsforever/web/tools/session.py
# def login(state=True, redirect_url='/'):
#     def dco_func(func):
#         @wraps(func)
#         def wrapper(*args, **xargs):
#             if is_login() == state:
#                 return func(*args, **xargs)
#             else:
#                 return redirect(redirect_url)
#         return wrapper
#     return dco_func
# 
# def get_domain(domain, master=False):
#     if not get_user():
#         return None
# 
#     query = g.session.query(Domain, DomainOwnership)
#     query = query.join(DomainOwnership.domain)
#     query = query.filter(Domain.name.like(domain))
#     query = query.filter(DomainOwnership.user_id == get_user().id)
#     if master:
#         query = query.filter(DomainOwnership.master)
# 
#     if query.count() == 0:
#         return None
# 
#     domain, ownership = query.first()
# 
#     domain.master = ownership.master
# 
#     return domain
# 
# 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()
#     return g.user
# 
# Path: dnsforever/models.py
# 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 SubdomainSharing(Base):
#     __tablename__ = 'subdomain_sharing'
# 
#     def __init__(self, domain, name, email):
#         self.domain = domain
#         self.name = name
#         self.email = email
#         self.token = random_string()
# 
#     id = Column(Integer, primary_key=True)
# 
#     name = Column(String(255), nullable=True)
#     domain_id = Column(Integer, ForeignKey('domain.id'), nullable=False)
#     domain = relationship(Domain)
# 
#     email = Column(Unicode(100), nullable=False, index=True, unique=True)
# 
#     created_at = Column(DateTime(timezone=True), nullable=False,
#                         default=functions.now())
# 
#     token = Column(Unicode(127), nullable=False)


from flask import Blueprint, g, render_template, request, url_for, redirect
from wtforms import Form, StringField, validators
from dnsforever.web.tools.session import login, get_domain, get_user
from dnsforever.models import Domain, SubdomainSharing
    domain = get_domain(domain, True)
    if not domain:
        return redirect(url_for('domain.index'))

    ticket = g.session.query(SubdomainSharing)\
                      .filter(SubdomainSharing.id == ticket_id)\
                      .filter(SubdomainSharing.domain == domain)\
                      .first()

    if not ticket:
        return redirect(url_for('domain_subdomain.subdomain_info',
                                domain=domain.name))

    if request.method == 'POST':
        with g.session.begin():
            g.session.delete(ticket)
        return redirect(url_for('domain_subdomain.subdomain_info',
                                domain=domain.name))

    return render_template('domain_subdomain/del.html',
                           domain=domain, ticket=ticket)


@app.route('/<int:subdomain_id>/del', methods=['GET', 'POST'])
@login(True, '/')
def subdomain_delete(domain, subdomain_id):
    domain = get_domain(domain, True)
    if not domain:
        return redirect(url_for('domain.index'))


# Path: dnsforever/web/tools/session.py
# def login(state=True, redirect_url='/'):
#     def dco_func(func):
#         @wraps(func)
#         def wrapper(*args, **xargs):
#             if is_login() == state:
#                 return func(*args, **xargs)
#             else:
#                 return redirect(redirect_url)
#         return wrapper
#     return dco_func
# 
# def get_domain(domain, master=False):
#     if not get_user():
#         return None
# 
#     query = g.session.query(Domain, DomainOwnership)
#     query = query.join(DomainOwnership.domain)
#     query = query.filter(Domain.name.like(domain))
#     query = query.filter(DomainOwnership.user_id == get_user().id)
#     if master:
#         query = query.filter(DomainOwnership.master)
# 
#     if query.count() == 0:
#         return None
# 
#     domain, ownership = query.first()
# 
#     domain.master = ownership.master
# 
#     return domain
# 
# 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()
#     return g.user
# 
# Path: dnsforever/models.py
# 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 SubdomainSharing(Base):
#     __tablename__ = 'subdomain_sharing'
# 
#     def __init__(self, domain, name, email):
#         self.domain = domain
#         self.name = name
#         self.email = email
#         self.token = random_string()
# 
#     id = Column(Integer, primary_key=True)
# 
#     name = Column(String(255), nullable=True)
#     domain_id = Column(Integer, ForeignKey('domain.id'), nullable=False)
#     domain = relationship(Domain)
# 
#     email = Column(Unicode(100), nullable=False, index=True, unique=True)
# 
#     created_at = Column(DateTime(timezone=True), nullable=False,
#                         default=functions.now())
# 
#     token = Column(Unicode(127), nullable=False)


# Path: dnsforever/web/domain_subdomain.py
from flask import Blueprint, g, render_template, request, url_for, redirect
from wtforms import Form, StringField, validators
from dnsforever.web.tools.session import login, get_domain, get_user
from dnsforever.models import Domain, SubdomainSharing

    domain = get_domain(domain, True)
    if not domain:
        return redirect(url_for('domain.index'))

    ticket = g.session.query(SubdomainSharing)\
                      .filter(SubdomainSharing.id == ticket_id)\
                      .filter(SubdomainSharing.domain == domain)\
                      .first()

    if not ticket:
        return redirect(url_for('domain_subdomain.subdomain_info',
                                domain=domain.name))

    if request.method == 'POST':
        with g.session.begin():
            g.session.delete(ticket)
        return redirect(url_for('domain_subdomain.subdomain_info',
                                domain=domain.name))

    return render_template('domain_subdomain/del.html',
                           domain=domain, ticket=ticket)


@app.route('/<int:subdomain_id>/del', methods=['GET', 'POST'])
@login(True, '/')
def subdomain_delete(domain, subdomain_id):
    domain = get_domain(domain, True)
    if not domain:
        return redirect(url_for('domain.index'))


    subdomain = g.session.query(Domain)\