blob: 2ee81571e55cd91539a5589787acb0f3440b2ddf [file] [log] [blame]
Siobhan Tully30fd4292013-05-10 08:59:56 -04001from core.models import Site
Tony Mackf593dba2013-03-24 13:17:43 -04002
3class SiteImporter:
4
Tony Mack743ddf22013-04-02 07:36:25 -04005 def __init__(self, api):
6 self.api = api
7 self.remote_sites = {}
8 self.local_sites = {}
Tony Mackf593dba2013-03-24 13:17:43 -04009
10 def run(self):
Tony Mackf593dba2013-03-24 13:17:43 -040011 db_sites = Site.objects.all()
Tony Mack743ddf22013-04-02 07:36:25 -040012 for db_site in db_sites:
13 self.local_sites[db_site.login_base] = db_site
Tony Mackdd12c682013-04-02 10:15:27 -040014 print "%s local sites" % len(db_sites)
Tony Mack743ddf22013-04-02 07:36:25 -040015
Tony Mack172f8882013-04-02 11:36:34 -040016 sites = self.api.GetSites({'peer_id': None})
Tony Mackd9f8d2e2013-04-02 10:18:48 -040017 print "%s remote sites" % len(sites)
Tony Mackf1afe392013-04-02 07:40:38 -040018 count = 0
Tony Mackf593dba2013-03-24 13:17:43 -040019 for site in sites:
Tony Mack743ddf22013-04-02 07:36:25 -040020 self.remote_sites[site['site_id']] = site
21 if site['login_base'] not in self.local_sites:
Tony Mackf593dba2013-03-24 13:17:43 -040022 new_site = Site(name=site['name'],
Tony Mackd8c390f2013-04-02 10:36:19 -040023 login_base=site['login_base'],
Tony Mackf593dba2013-03-24 13:17:43 -040024 site_url=site['url'],
25 enabled=site['enabled'],
26 longitude=site['longitude'],
27 latitude=site['latitude'],
28 is_public=site['is_public'],
29 abbreviated_name=site['abbreviated_name'])
30 new_site.save()
Tony Mackf1afe392013-04-02 07:40:38 -040031 count += 1
Tony Mack743ddf22013-04-02 07:36:25 -040032 self.local_sites[new_site.login_base] = new_site
Tony Mackf1afe392013-04-02 07:40:38 -040033 print "imported %s sites" % count