Exemple #1
0
    def _migration_sync_to_sql(self, sql_object):
        from corehq.apps.commtrack.management.commands.populate_commtrackconfig import Command
        for field_name in Command.attrs_to_sync():
            value = getattr(self, field_name)
            setattr(sql_object, field_name, value)

        submodels = []
        for spec in Command.one_to_one_submodels():
            couch_submodel = getattr(self, spec['couch_attr'], {})
            sql_submodel = spec['sql_class'](**{
                field: getattr(couch_submodel, field)
                for field in spec['fields']
            })
            setattr(sql_object, spec['sql_class'].__name__.lower(),
                    sql_submodel)
            submodels.append(sql_submodel)

        # Make sure model has id so that submodels can be saved
        if not sql_object.id:
            sql_object.save(sync_to_couch=False)

        for sql_submodel in submodels:
            setattr(sql_submodel, 'commtrack_config', sql_object)
            sql_submodel.save()

        sql_object.set_actions([
            SQLActionConfig(
                action=action.action,
                subaction=action.subaction,
                _keyword=action._keyword,
                caption=action.caption,
            ) for action in self.actions
        ])
        sql_object.save(sync_to_couch=False)
Exemple #2
0
 def _migration_sync_to_couch(self, couch_object):
     from corehq.apps.commtrack.management.commands.populate_commtrackconfig import Command
     for field_name in Command.attrs_to_sync():
         value = getattr(self, field_name)
         couch_object[field_name] = value
     couch_object.actions = [
         CommtrackActionConfig(
             action=action.action,
             subaction=action.subaction,
             _keyword=action._keyword,
             caption=action.caption,
         ) for action in self.all_actions
     ]
     for spec in Command.one_to_one_submodels():
         sql_submodel = getattr(self, spec['sql_class'].__name__.lower())
         couch_object[spec['couch_attr']] = spec['couch_class']({
             field: getattr(sql_submodel, field)
             for field in spec['fields']
         })
         sql_submodel.save()
     couch_object.save(sync_to_sql=False)