# 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
# 
# Path: dnsforever/models.py
# class RecordDDNS_A(Record):
#     __tablename__ = 'record_ddns_a'
# 
#     __mapper_args__ = {
#         'polymorphic_identity': 'ddns_a',
#     }
# 
#     def __init__(self, domain, name, ip, memo, ttl=300):
#         self.domain = domain
#         self.name = name
#         self.rdata = ip
#         self.ttl = ttl
#         self.cls = 0
#         self.type = u'A'
#         self.ddns_key = random_string(10)
#         self.memo = memo
# 
#     id = Column(Integer, ForeignKey('record.id'), primary_key=True)
#     ddns_key = Column(Unicode(80), nullable=False)
#     memo = Column(UnicodeText, default=u'')
# 
#     @property
#     def ip(self):
#         return self.rdata
# 
#     @ip.setter
#     def ip(self, value):
#         self.rdata = value
# 
#     def reset_key(self):
#         self.ddns_key = random_string(10)


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
from dnsforever.models import RecordDDNS_A
        return render_template('domain_ddns/new.html',
                               domain=domain,
                               form=form)

    try:
        ddns_record = RecordDDNS_A(domain=domain,
                                   name=form.name.data or None,
                                   ip=form.ip.data,
                                   memo=form.memo.data)
        with g.session.begin():
            ddns_record.update()
            g.session.add(ddns_record)
    except ValueError as e:
        form.name.errors.append(e)
        return render_template('domain_ddns/new.html',
                               domain=domain,
                               form=form)

    return redirect(url_for('domain_ddns.record_list', domain=domain.name))


@app.route('/<int:record_id>', methods=['POST'])
@login(True, '/')
def record_edit_process(domain, record_id):
    form = RecordDDNSForm(request.form)
    domain = get_domain(domain)

    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
# 
# Path: dnsforever/models.py
# class RecordDDNS_A(Record):
#     __tablename__ = 'record_ddns_a'
# 
#     __mapper_args__ = {
#         'polymorphic_identity': 'ddns_a',
#     }
# 
#     def __init__(self, domain, name, ip, memo, ttl=300):
#         self.domain = domain
#         self.name = name
#         self.rdata = ip
#         self.ttl = ttl
#         self.cls = 0
#         self.type = u'A'
#         self.ddns_key = random_string(10)
#         self.memo = memo
# 
#     id = Column(Integer, ForeignKey('record.id'), primary_key=True)
#     ddns_key = Column(Unicode(80), nullable=False)
#     memo = Column(UnicodeText, default=u'')
# 
#     @property
#     def ip(self):
#         return self.rdata
# 
#     @ip.setter
#     def ip(self, value):
#         self.rdata = value
# 
#     def reset_key(self):
#         self.ddns_key = random_string(10)


# Path: dnsforever/web/domain_ddns.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
from dnsforever.models import RecordDDNS_A

        return render_template('domain_ddns/new.html',
                               domain=domain,
                               form=form)

    try:
        ddns_record = RecordDDNS_A(domain=domain,
                                   name=form.name.data or None,
                                   ip=form.ip.data,
                                   memo=form.memo.data)
        with g.session.begin():
            ddns_record.update()
            g.session.add(ddns_record)
    except ValueError as e:
        form.name.errors.append(e)
        return render_template('domain_ddns/new.html',
                               domain=domain,
                               form=form)

    return redirect(url_for('domain_ddns.record_list', domain=domain.name))


@app.route('/<int:record_id>', methods=['POST'])
@login(True, '/')
def record_edit_process(domain, record_id):
    form = RecordDDNSForm(request.form)
    domain = get_domain(domain)

    if not domain:
        return redirect(url_for('domain.index'))


    record = g.session.query(RecordDDNS_A)\