Exemple #1
0
def sync_library(token=None, content_type_to_sync=None):
    """ 
    Synchronizes the ACE library with the local content.
    """
    from djax.models import ContentSyncLock

    if ContentSyncLock.objects.all().exists():
        return False

    if not token:
        token = uuid.uuid4().hex

    lock = ContentSyncLock.objects.create(token=token)

    # ensure registry loaded
    build_registry()

    if content_type_to_sync:
        log.info("Pushing %s content to ACE library." % content_type_to_sync)
        try:
            content_type = content_registry[content_type_to_sync]
            sync_library_to_content_type(content_type_to_sync)
        except KeyError:
            log.error("%s is not in the content registry." % content_type_to_sync)
    else:
        for content_type in content_registry.keys():
            log.info("Pushing %s content to ACE library." % content_type)
            sync_library_to_content_type(content_type)

    lock.delete()
    return True
Exemple #2
0
def sync_content(token=None, content_type_to_sync=None):
    """
    Synchronizes the local models with Axilent content.
    """
    from djax.models import ContentSyncLock

    if ContentSyncLock.objects.all().exists():
        return False  # already sync locked

    if not token:
        token = uuid.uuid4().hex

    lock = ContentSyncLock.objects.create(token=token)

    # ensure content registry loaded
    build_registry()

    if content_type_to_sync:
        log.info("Syncing %s." % content_type_to_sync)
        try:
            content_type = content_registry[content_type_to_sync]
            sync_content_type(content_type_to_sync)
        except KeyError:
            log.error("%s is not in the content registry." % content_type_to_sync)
    else:
        for content_type in content_registry.keys():
            sync_content_type(content_type)

    lock.delete()
    return True  # sync occured
Exemple #3
0
    def create_model(self,axilent_content_type,axilent_content_key):
        """
        Creates a new model and accompaning content record for the axilent content.
        """
        from djax.content import DefaultFieldConverter
        
        log.debug('Creating new model of content type:%s and key:%s.' % (axilent_content_type,axilent_content_key))
        
        build_registry() # ensure registry is built
        content_data = content_client.get_content(axilent_content_type,axilent_content_key)
        local_model, record = None, None
        try:
            model_class = content_registry[axilent_content_type]
        
            field_map = {}
            try:
                field_map = model_class.ACE.field_map
            except AttributeError:
                for key in content_data.data.keys():
                    field_map[key] = key
            
            # Iterate through the field map and set the local model values from the incoming Axilent content
            fields = {}
            deferred_field_converters = []
            for axilent_field, model_field in field_map.items():
                if hasattr(content_data,axilent_field):
                    try:            
                        if hasattr(model_field,'field'):
                            log.debug('%s using field converter %s.' % (axilent_field,unicode(model_field)))
                            # this is a field converter
                            # sanity check
                            if not hasattr(model_field,'to_ace') or not hasattr(model_field,'to_local_model'):
                                raise ValueError('You must define the methods to_ace and to_local_model for field converter for ace field %s.' % axilent_field)
                
                            if hasattr(model_field,'deferred') and model_field.deferred:
                                log.debug('%s uses a deferred field converter.' % axilent_field)
                                deferred_field_converters.append((axilent_field,model_field))
                            else:
                                log.debug('%s uses an immediate field converter.' % axilent_field)
                                value = model_field.to_local_model(content_data,getattr(content_data,axilent_field))
                                fields[model_field.field] = value
                        else:
                            log.debug('Using default field converter for %s.' % axilent_field)
                            # not a field converter, just a string.  Use DefaultFieldConverter
                            default_field_converter = DefaultFieldConverter(model_field)
                            fields[model_field] = default_field_converter.to_local_model(content_data,getattr(content_data,axilent_field))
                    except AttributeError:
                        log.exception('Local model has no field %s (matched to Axilent field %s).' % (model_field,axilent_field))
                else:
                    log.info('Skipping ace field %s - not in data from ace.' % axilent_field)
            
            log.debug('Creating local model with field data %s.' % unicode(fields))
            local_model = model_class.objects.create(**fields) # create the local model with the content data
            
            if deferred_field_converters:
                for deferred_axilent_field, deferred_model_field in deferred_field_converters:
                    try:
                        deferred_model_field.to_local_model(content_data,getattr(content_data,deferred_axilent_field),local_model)
                    except AttributeError:
                        log.exception('Local model has no field %s (matched to Axilent field %s).' % (deferred_model_field.field,deferred_axilent_field))
            
                local_model.save()
            else:
                log.info('No deferred field converters for %s.' % unicode(local_model))

            local_content_type = ContentType.objects.get_for_model(local_model)
            record = self.create(local_content_type=local_content_type,
                                 local_id=local_model.pk,
                                 axilent_content_type=axilent_content_type,
                                 axilent_content_key=axilent_content_key,
                                 updated=datetime.now())
        except KeyError:
            raise ValueError('ACE content type %s cannot be found in the local registry.' % axilent_content_type)
        
        return (local_model,record)
Exemple #4
0
        return self.profile

class AuthTokenManager(models.Manager):
    """
    Manager class for AuthToken.
    """
    def new_token(self,origin_domain=None):
        """
        Creates a new token.
        """
        return self.create(origin_domain=origin_domain,
                           token=uuid.uuid4().hex)

class AuthToken(models.Model):
    """
    Token for remote management of Djax install.
    """
    origin_domain = models.URLField(null=True)
    token = models.CharField(max_length=100,unique=True)
    
    objects = AuthTokenManager()
    
    def __unicode__(self):
        return self.token


# =================
# = Registry Hook =
# =================
build_registry()