Merge pull request #49 from CaptainQwark/master

Optimize domain sync
This commit is contained in:
Khanh Ngo 2016-06-16 10:30:19 +07:00 committed by GitHub
commit d59166107c

View File

@ -426,6 +426,7 @@ class Domain(db.Model):
""" """
db_domain = Domain.query.all() db_domain = Domain.query.all()
list_db_domain = [d.name for d in db_domain] list_db_domain = [d.name for d in db_domain]
dict_db_domain = dict((x.name,x) for x in db_domain)
headers = {} headers = {}
headers['X-API-Key'] = PDNS_API_KEY headers['X-API-Key'] = PDNS_API_KEY
@ -454,15 +455,25 @@ class Domain(db.Model):
# update/add new domain # update/add new domain
for data in jdata: for data in jdata:
d = Domain.query.filter(Domain.name == data['name']).first() d = dict_db_domain.get(data['name'], None)
changed = False
if d: if d:
#update exist domain # existing domain, only update if something actually has changed
if ( d.master != str(data['masters'])
or d.type != data['kind']
or d.serial != data['serial']
or d.notified_serial != data['notified_serial']
or d.last_check != ( 1 if data['last_check'] else 0 )
or d.dnssec != data['dnssec'] ):
d.master = str(data['masters']) d.master = str(data['masters'])
d.type = data['kind'] d.type = data['kind']
d.serial = data['serial'] d.serial = data['serial']
d.notified_serial = data['notified_serial'] d.notified_serial = data['notified_serial']
d.last_check = 1 if data['last_check'] else 0 d.last_check = 1 if data['last_check'] else 0
d.dnssec = data['dnssec'] d.dnssec = data['dnssec']
changed = True
else: else:
# add new domain # add new domain
d = Domain() d = Domain()
@ -474,6 +485,8 @@ class Domain(db.Model):
d.last_check = data['last_check'] d.last_check = data['last_check']
d.dnssec = 1 if data['dnssec'] else 0 d.dnssec = 1 if data['dnssec'] else 0
db.session.add(d) db.session.add(d)
changed = True
if changed:
try: try:
db.session.commit() db.session.commit()
except: except: