# 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 Record(Base):
#     __tablename__ = 'record'
# 
#     id = Column(Integer, primary_key=True)
#     service = Column(UnicodeText, nullable=False)
# 
#     domain_id = Column(Integer, ForeignKey('domain.id'), nullable=False)
#     domain = relationship(Domain, backref='records')
# 
#     name = Column(String(255), nullable=True)
#     type = Column(String(32), nullable=False)
#     cls = Column(Integer, nullable=False)
#     ttl = Column(Integer, nullable=False)
#     rdata = Column(String(255), nullable=False)
# 
#     __mapper_args__ = {
#         'polymorphic_identity': None,
#         'polymorphic_on': service,
#     }
# 
#     def update(self):
#         self.domain.update()
# 
# 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)
# 
# class NameServer(Base):
#     __tablename__ = 'nameserver'
# 
#     id = Column(Integer, primary_key=True)
# 
#     domain = Column(String(255), nullable=False)
#     ip = Column(String(64), nullable=False)


import string
import re
from datetime import datetime, tzinfo, timedelta
from flask import Blueprint, g, request
from flask.ext import restful
from flask.ext.restful import reqparse
from dnsforever.models import Domain, Record, RecordDDNS_A, NameServer
        return "UTC"

    def dst(self, dt):
        return timedelta(0)


def totimestamp(dt, epoch=datetime(1970, 1, 1)):
    td = dt - epoch
    return int(td.total_seconds())


def todatetime(ts, epoch=datetime(1970, 1, 1)):
    return datetime.fromtimestamp(ts, UTC())


class ServerUpdate(restful.Resource):
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('last_update', type=int, default=None)
        args = parser.parse_args()

        ns = g.session.query(NameServer)\
                      .filter(NameServer.ip == request.remote_addr).first()

        if not ns and not g.debug:
            return 'ERROR', 403

        update_query = g.session.query(Domain)
        if args.last_update:
            last_update = todatetime(args.last_update)

# 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 Record(Base):
#     __tablename__ = 'record'
# 
#     id = Column(Integer, primary_key=True)
#     service = Column(UnicodeText, nullable=False)
# 
#     domain_id = Column(Integer, ForeignKey('domain.id'), nullable=False)
#     domain = relationship(Domain, backref='records')
# 
#     name = Column(String(255), nullable=True)
#     type = Column(String(32), nullable=False)
#     cls = Column(Integer, nullable=False)
#     ttl = Column(Integer, nullable=False)
#     rdata = Column(String(255), nullable=False)
# 
#     __mapper_args__ = {
#         'polymorphic_identity': None,
#         'polymorphic_on': service,
#     }
# 
#     def update(self):
#         self.domain.update()
# 
# 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)
# 
# class NameServer(Base):
#     __tablename__ = 'nameserver'
# 
#     id = Column(Integer, primary_key=True)
# 
#     domain = Column(String(255), nullable=False)
#     ip = Column(String(64), nullable=False)


# Path: dnsforever/web/apis.py
import string
import re
from datetime import datetime, tzinfo, timedelta
from flask import Blueprint, g, request
from flask.ext import restful
from flask.ext.restful import reqparse
from dnsforever.models import Domain, Record, RecordDDNS_A, NameServer

        return "UTC"

    def dst(self, dt):
        return timedelta(0)


def totimestamp(dt, epoch=datetime(1970, 1, 1)):
    td = dt - epoch
    return int(td.total_seconds())


def todatetime(ts, epoch=datetime(1970, 1, 1)):
    return datetime.fromtimestamp(ts, UTC())


class ServerUpdate(restful.Resource):
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('last_update', type=int, default=None)
        args = parser.parse_args()

        ns = g.session.query(NameServer)\
                      .filter(NameServer.ip == request.remote_addr).first()

        if not ns and not g.debug:
            return 'ERROR', 403

        update_query = g.session.query(Domain)
        if args.last_update:
            last_update = todatetime(args.last_update)

            update_query = update_query.filter(Domain.updated_at > last_update)