Beispiel #1
0
    def dehydrate(self, bundle):
        from app.detective import register
        # Get the model's rules manager
        rulesManager = register.topics_rules()
        # Get all registered models
        models = get_registered_models()
        # Filter model to the one under app.detective.topics
        bundle.data["models"] = []
        # Create a thumbnail for this topic
        try:
            thumbnailer = get_thumbnailer(bundle.obj.background)
            thumbnailSmall = thumbnailer.get_thumbnail({'size': (60, 60), 'crop': True})
            thumbnailMedium = thumbnailer.get_thumbnail({'size': (300, 200), 'crop': True})
            bundle.data['thumbnail'] = {
                'small' : thumbnailSmall.url,
                'medium': thumbnailMedium.url
            }
        # No image available
        except InvalidImageFormatError:
            bundle.data['thumbnail'] = None

        for m in bundle.obj.get_models():
            model = {
                'name': m.__name__,
                'verbose_name': m._meta.verbose_name,
                'verbose_name_plural': m._meta.verbose_name_plural,
                'is_searchable': rulesManager.model(m).all().get("is_searchable", False)
            }
            bundle.data["models"].append(model)
        return bundle
Beispiel #2
0
 def dehydrate(self, bundle):
     # Get all registered models
     models = get_registered_models()
     in_topic = lambda m: m.__module__.startswith("app.detective.topics.%s." % bundle.obj.module)
     # Filter model to the one under app.detective.topics
     bundle.data["models"] = [ m.__name__ for m in models if in_topic(m) ]
     # Every topic has a single permalink
     bundle.data['link']   = bundle.obj.get_absolute_path()
     return bundle
Beispiel #3
0
    def dehydrate(self, bundle):
        if bundle.obj.background:
            # Create a thumbnail for this topic
            try:
                thumbnailer = get_thumbnailer(bundle.obj.background)
                bundle.data['thumbnail'] = {
                    key : thumbnailer.get_thumbnail({
                        'size' : size,
                        'crop' : True
                    }).url
                    for key, size in settings.THUMBNAIL_SIZES.items()
                }
            # No image available
            except InvalidImageFormatError:
                bundle.data['thumbnail'] = None
        else:
            bundle.data['thumbnail'] = None

        if 'models' not in self._meta.excludes:
            # Get the model's rules manager
            rulesManager = bundle.request.current_topic.get_rules()
            # Get all registered models
            models = get_registered_models()
            # return json for ontology_as_json field
            if bundle.obj.ontology_as_json:
                bundle.data["ontology_as_json"] = bundle.obj.ontology_as_json
            # Filter model to the one under app.detective.topics
            bundle.data["models"] = []
            bundle.data["ontology_models"] = []
            for m in bundle.obj.get_models():
                try:
                    idx = m.__idx__
                except AttributeError:
                    idx = 0
                model = {
                    'name': m.__name__,
                    'verbose_name': m._meta.verbose_name,
                    'verbose_name_plural': m._meta.verbose_name_plural,
                    'is_searchable': rulesManager.model(m).all().get("is_searchable", False),
                    'index': idx
                }
                bundle.data["models"].append(model)
                # Save searchable models for this ontology
                if model["is_searchable"]:
                    bundle.data["ontology_models"].append(model["verbose_name"])
        _is_uploading = cache.get("{0}_is_uploading".format(bundle.data["ontology_as_mod"]))
        if _is_uploading != None and _is_uploading:
            bundle.data["is_uploading"] = True
        return bundle
Beispiel #4
0
    def summary_forms(self, bundle):
        available_resources = {}
        # Get the model's rules manager
        rulesManager = register_model_rules()
        # Fetch every registered model
        # to print out its rules
        for model in get_registered_models():
            # Do this ressource has a model?
            # Do this ressource is a part of apps?
            if model != None and model.__module__.startswith("app.detective.apps"):
                name                = model.__name__.lower()
                rules               = rulesManager.model(model).all()
                fields              = get_model_fields(model)
                verbose_name        = getattr(model._meta, "verbose_name", name).title()
                verbose_name_plural = getattr(model._meta, "verbose_name_plural", verbose_name + "s").title()
                # Extract the model parent to find its scope
                scope               = model.__module__.split(".")[-2]

                for key in rules:
                    # Filter rules to keep only Neomatch
                    if isinstance(rules[key], Neomatch):
                        fields.append({
                            "name"         : key,
                            "type"         : "ExtendedRelationship",
                            "verbose_name" : rules[key].title,
                            "rules"        : {},
                            "related_model": rules[key].target_model.__name__
                        })

                available_resources[name] = {
                    'description'         : getattr(model, "_description", None),
                    'scope'               : getattr(model, "_scope", scope),
                    'model'               : getattr(model, "__name_", ""),
                    'verbose_name'        : verbose_name,
                    'verbose_name_plural' : verbose_name_plural,
                    'name'                : name,
                    'fields'              : fields,
                    'rules'               : rules
                }

        return available_resources
Beispiel #5
0
def register_model_rules():
    # ModelRules is a singleton that record every model rules
    rules = ModelRules()
    # Disable editing on some model
    rules.model(Country).add(is_editable=False)
    # We can import this early to avoid bi-directional dependancies
    from app.detective.utils import get_registered_models, import_class
    # Get all registered models
    models = get_registered_models()
    # Them filter the list to the detective's apps
    models = [m for m in models if m.__module__.startswith("app.detective.apps")]    
    # Set "is_searchable" to true on every model with a name
    for model in models:
        # If the current model has a name
        if "name" in rules.model(model).field_names:
            field_names = rules.model(model).field_names
            # Count the fields len
            fields_len = len(field_names)
            # Put the highest priority to that name
            rules.model(model).field('name').add(priority=fields_len)
        # This model isn't searchable
        else: rules.model(model).add(is_searchable=False)

    # Check now that each "Relationship"
    # match with a searchable model
    for model in models:
        for field in model._meta.fields:  
            # Find related model for relation
            if hasattr(field, "target_model"):                       
                target_model  = field.target_model         
                # Load class path
                if type(target_model) is str: target_model = import_class(target_model)
                # It's a searchable field !
                modelRules = rules.model(target_model).all()
                # Set it into the rules
                rules.model(model).field(field.name).add(is_searchable=modelRules["is_searchable"])
                rules.model(model).field(field.name).add(is_editable=modelRules["is_editable"])                            

    return rules
Beispiel #6
0
    def dehydrate(self, bundle):
        if bundle.obj.background:
            # Create a thumbnail for this topic
            try:
                thumbnailer = get_thumbnailer(bundle.obj.background)
                thumbnailSmall = thumbnailer.get_thumbnail({'size': (60, 60), 'crop': True})
                thumbnailMedium = thumbnailer.get_thumbnail({'size': (300, 200), 'crop': True})
                bundle.data['thumbnail'] = {
                    'small' : thumbnailSmall.url,
                    'medium': thumbnailMedium.url
                }
            # No image available
            except InvalidImageFormatError:
                bundle.data['thumbnail'] = None
        else:
            bundle.data['thumbnail'] = None

        if 'models' not in self._meta.excludes:
            # Get the model's rules manager
            rulesManager = bundle.request.current_topic.get_rules()
            # Get all registered models
            models = get_registered_models()
            # Filter model to the one under app.detective.topics
            bundle.data["models"] = []
            for m in bundle.obj.get_models():
                try:
                    idx = m.__idx__
                except AttributeError:
                    idx = 0
                model = {
                    'name': m.__name__,
                    'verbose_name': m._meta.verbose_name,
                    'verbose_name_plural': m._meta.verbose_name_plural,
                    'is_searchable': rulesManager.model(m).all().get("is_searchable", False),
                    'index': idx
                }
                bundle.data["models"].append(model)
        return bundle
Beispiel #7
0
 def get_models_output(self):
     # Select only some atribute
     output = lambda m: {'name': get_model_scope(m) + ":" + m.__name__, 'label': m._meta.verbose_name.title()}
     return [ output(m) for m in get_registered_models() if m.__module__.startswith("app.detective.apps") ]