Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 1 | from plstackapi.core.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 |
| 14 | |
| 15 | sites = self.api.GetSites() |
Tony Mack | f1afe39 | 2013-04-02 07:40:38 -0400 | [diff] [blame] | 16 | count = 0 |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 17 | for site in sites: |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 18 | self.remote_sites[site['site_id']] = site |
| 19 | if site['login_base'] not in self.local_sites: |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 20 | new_site = Site(name=site['name'], |
| 21 | site_url=site['url'], |
| 22 | enabled=site['enabled'], |
| 23 | longitude=site['longitude'], |
| 24 | latitude=site['latitude'], |
| 25 | is_public=site['is_public'], |
| 26 | abbreviated_name=site['abbreviated_name']) |
| 27 | new_site.save() |
Tony Mack | f1afe39 | 2013-04-02 07:40:38 -0400 | [diff] [blame] | 28 | count += 1 |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 29 | self.local_sites[new_site.login_base] = new_site |
Tony Mack | f1afe39 | 2013-04-02 07:40:38 -0400 | [diff] [blame] | 30 | print "imported %s sites" % count |