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 |
Tony Mack | dd12c68 | 2013-04-02 10:15:27 -0400 | [diff] [blame] | 15 | |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 16 | |
| 17 | if not local_sites: |
| 18 | from models import Site |
| 19 | sites = Site.objects.all() |
| 20 | for site in sites: |
| 21 | local_sites[site.login_base] = site |
| 22 | |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 23 | db_slices = Slice.objects.all() |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 24 | for db_slice in db_slices: |
| 25 | self.local_slices[db_slice.name] = db_slice |
Tony Mack | dd12c68 | 2013-04-02 10:15:27 -0400 | [diff] [blame] | 26 | print "%s local slices" % len(db_slices) |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 27 | |
Tony Mack | a3a40cf | 2013-04-02 11:21:21 -0400 | [diff] [blame] | 28 | slices = self.api.GetSlices() |
Tony Mack | 787f46e | 2013-04-02 11:24:45 -0400 | [diff] [blame^] | 29 | print "%s remote slices" % len(slices) |
Tony Mack | f1afe39 | 2013-04-02 07:40:38 -0400 | [diff] [blame] | 30 | count = 0 |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 31 | for slice in slices: |
Tony Mack | a3a40cf | 2013-04-02 11:21:21 -0400 | [diff] [blame] | 32 | self.remote_slices[slice['slice_id']] = slice |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 33 | if slice['name'] not in self.local_slices: |
| 34 | site = local_sites[remote_sites[slice['site_id']]['login_base']] |
Tony Mack | a3a40cf | 2013-04-02 11:21:21 -0400 | [diff] [blame] | 35 | new_slice = Slice(name=slice['name'], |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 36 | instantiation=slice['instantiation'], |
| 37 | omf_friendly = False, |
| 38 | description = slice['description'], |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 39 | slice_url = slice['url'], |
| 40 | site = site) |
| 41 | new_slice.save() |
Tony Mack | f1afe39 | 2013-04-02 07:40:38 -0400 | [diff] [blame] | 42 | count += 1 |
Tony Mack | 743ddf2 | 2013-04-02 07:36:25 -0400 | [diff] [blame] | 43 | self.local_slices[new_slice.name] = new_slice |
Tony Mack | f1afe39 | 2013-04-02 07:40:38 -0400 | [diff] [blame] | 44 | print "Imported %s slices" % count |
Tony Mack | f593dba | 2013-03-24 13:17:43 -0400 | [diff] [blame] | 45 | |
| 46 | |
| 47 | |