Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame^] | 1 | from plstackapi.core.models import Slice |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 2 | |
| 3 | class SliceImporter: |
| 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_slices = {} |
| 8 | self.local_slices = {} |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 9 | |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame^] | 10 | def run(self, remote_sites={}, local_sites={}): |
| 11 | if not remote_sites: |
| 12 | sites = self.api.GetSites() |
| 13 | for site in sites: |
| 14 | remote_sites[site['site_id']] = site |
| 15 | |
| 16 | if not local_sites: |
| 17 | from models import Site |
| 18 | sites = Site.objects.all() |
| 19 | for site in sites: |
| 20 | local_sites[site.login_base] = site |
| 21 | |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 22 | db_slices = Slice.objects.all() |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame^] | 23 | for db_slice in db_slices: |
| 24 | self.local_slices[db_slice.name] = db_slice |
| 25 | |
| 26 | slices = api.GetSlices() |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 27 | for slice in slices: |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame^] | 28 | self.remote_slice[slice['slice_id']] = slice |
| 29 | if slice['name'] not in self.local_slices: |
| 30 | site = local_sites[remote_sites[slice['site_id']]['login_base']] |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 31 | new_slices = Slice(name=slice['name'], |
| 32 | instantiation=slice['instantiation'], |
| 33 | omf_friendly = False, |
| 34 | description = slice['description'], |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame^] | 35 | slice_url = slice['url'], |
| 36 | site = site) |
| 37 | new_slice.save() |
| 38 | self.local_slices[new_slice.name] = new_slice |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 39 | |
| 40 | |
| 41 | |