Tony Mack | 0e17ee2 | 2013-04-06 20:04:02 -0400 | [diff] [blame] | 1 | from plstackapi.planetstack.models import Site |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 2 | |
| 3 | class SiteImporter: |
| 4 | |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 5 | def __init__(self, api): |
| 6 | self.api = api |
| 7 | self.remote_sites = {} |
| 8 | self.local_sites = {} |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 9 | |
| 10 | def run(self): |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 11 | db_sites = Site.objects.all() |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 12 | for db_site in db_sites: |
| 13 | self.local_sites[db_site.login_base] = db_site |
Tony Mack | dd12c68 | 2013-04-02 10:15:27 -0400 | [diff] [blame] | 14 | print "%s local sites" % len(db_sites) |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 15 | |
Tony Mack | 172f888 | 2013-04-02 11:36:34 -0400 | [diff] [blame] | 16 | sites = self.api.GetSites({'peer_id': None}) |
Tony Mack | d9f8d2e | 2013-04-02 10:18:48 -0400 | [diff] [blame] | 17 | print "%s remote sites" % len(sites) |
Tony Mack | f1afe39 | 2013-04-02 07:40:38 -0400 | [diff] [blame] | 18 | count = 0 |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 19 | for site in sites: |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 20 | self.remote_sites[site['site_id']] = site |
| 21 | if site['login_base'] not in self.local_sites: |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 22 | new_site = Site(name=site['name'], |
Tony Mack | d8c390f | 2013-04-02 10:36:19 -0400 | [diff] [blame] | 23 | login_base=site['login_base'], |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 24 | 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 Mack | f1afe39 | 2013-04-02 07:40:38 -0400 | [diff] [blame] | 31 | count += 1 |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 32 | self.local_sites[new_site.login_base] = new_site |
Tony Mack | f1afe39 | 2013-04-02 07:40:38 -0400 | [diff] [blame] | 33 | print "imported %s sites" % count |