Commits

hirunatan  committed 9daa063

Auto generate matches when creating a new sync

  • Participants
  • Parent commits 8a1522b

Comments (0)

Files changed (1)

File src/macadjan_sync/models.py

 
     class Meta:
         app_label = 'macadjan_sync'
-        ordering = ['other_subcategory_slug']
+        ordering = ['local_subcategory__category__name', 'other_subcategory_slug']
         verbose_name = _(u'vínculo subcategoría')
         verbose_name_plural = _(u'vínculos subcategorías')
 
 	verbose_name = _(u'conexión otra BD')
 	verbose_name_plural = _(u'conexiones otras BD')
 
+    def save(self, *args, **kwargs):
+        new_link = (not self.id)
+        super(DBLink, self).save(*args, **kwargs)
+        if new_link:
+            self.generate_type_matches()
+            self.generate_subcategory_matches()
+
+    def generate_type_matches(self):
+        '''
+        Generate an initial set of type matches, assuming that types in the other db
+        are the same as in the local db (only true matches are created).
+        '''
+        for local_entity_type in models_base.EntityType.objects.order_by('name').all():
+            try:
+
+                other_entity_type = \
+                    models_base.EntityType.objects.using(self.other_db_name).get(name = local_entity_type.name)
+                self.entity_type_matches.create(local_entity_type = local_entity_type,
+                                                other_entity_type_id = other_entity_type.id)
+
+            except models_base.EntityType.DoesNotExist:
+                pass
+
+    def generate_subcategory_matches(self):
+        '''
+        Generate an initial set of subcategory matches, assuming that subcategories in the other db
+        are the same as in the local db (only true matches are created).
+        '''
+        for local_subcategory in models_base.SubCategory.objects.order_by('slug').all():
+            try:
+
+                other_subcategory = \
+                    models_base.SubCategory.objects.using(self.other_db_name).get(slug = local_subcategory.slug)
+                self.subcategory_matches.create(local_subcategory = local_subcategory,
+                                                other_subcategory_slug = other_subcategory.slug)
+
+            except models_base.SubCategory.DoesNotExist:
+                pass
+
+
     def get_current_sync(self):
         '''
         Get the DBLinkSync with finished_date = NULL (i.e. the current sync). If it