示例#1
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
示例#2
0
def topics_rules():
    # ModelRules is a singleton that record every model rules
    rules = ModelRules()
    # Disable editing on some model
    rules.model(Country).add(is_editable=False)
    # Records "invisible" fields
    rules.model(FundraisingRound).field("personal_payer").add(is_visible=False)
    rules.model(Organization).field("adviser").add(is_visible=False)
    rules.model(Organization).field("board_member").add(is_visible=False)
    rules.model(Organization).field("company_register_link").add(is_visible=False)
    rules.model(Organization).field("litigation_against").add(is_visible=False)
    rules.model(Organization).field("monitoring_body").add(is_visible=False)
    rules.model(Organization).field("partner").add(is_visible=False)
    rules.model(Organization).field("website_url").add(is_visible=False)
    rules.model(Person).field("previous_activity_in_organization").add(is_visible=False)
    rules.model(Person).field("website_url").add(is_visible=False)
    rules.model(EnergyProduct).field("operator").add(is_visible=False)
    rules.model(EnergyProject).field("ended").add(is_visible=False)
    rules.model(EnergyProject).field("partner").add(is_visible=False)

    rules.model(Country).add(person_set=Neomatch(
        title="Persons educated or based in this country",
        target_model=Person,
        match="""
            (root)<-[r:`person_has_based_in+`|`person_has_educated_in+`]-({select})
        """
    ))

    rules.model(Person).add(organizationkey_set=Neomatch(
        title="Organizations this person has a key position in",
        target_model=Organization,
        match="""
            (root)-[:`organization_has_key_person+`]-({select})
        """
    ))

    rules.model(Person).add(organizationadviser_set=Neomatch(
        title="Organizations this person is an adviser to",
        target_model=Organization,
        match="""
            (root)-[:`organization_has_adviser+`]-({select})
        """
    ))

    rules.model(Person).add(organizationboard_set=Neomatch(
        title="Organizations this person is a board member of",
        target_model=Organization,
        match="""
            (root)-[:`organization_has_board_member+`]-({select})
        """
    ))


    rules.model(Country).add(product_set= Neomatch(
        title="Energy products distributed in this country",
        target_model=EnergyProduct,
        match="""
            (root)<--()<-[:`energy_product_has_distribution+`]-({select})
        """
    ))

    rules.model(Country).add(project_set=Neomatch(
        title="Energy projects active in this country",
        target_model=EnergyProject,
        match="""
            (root)-[:`energy_project_has_activity_in_country+`]-({select})
        """
    ))

    rules.model(EnergyProduct).add(country_set= Neomatch(
        title="Countries where this product is distributed",
        target_model=Country,
        match="""
            (root)-[:`energy_product_has_distribution+`]-()-[:`distribution_has_activity_in_country+`]-({select})
        """
    ))

    rules.model(Organization).add(energyproject_set=Neomatch(
        title="Energy projects this organization owns",
        target_model=EnergyProject,
        match="""
            (root)-[:`energy_project_has_owner+`]-({select})
        """
    ))

    rules.model(EnergyProduct).add(energyproduct_set=Neomatch(
        title="Energy project this product belongs to",
        target_model=EnergyProject,
        match="""
            (root)-[:`energy_project_has_product+`]-({select})
        """
    ))

    rules.model(Price).add(transform='{currency} {units}')
    rules.model(FundraisingRound).add(transform='{currency} {units}')

    def to_twitter_profile_url(data, field=None):
        th = data["twitter_handle"]
        if not th:
            return th
        elif th.startswith("http://") or th.startswith("https://"):
            return th
        elif th.startswith("@"):
            return "http://twitter.com/%s" % th[1:]
        else:
            return "http://twitter.com/%s" % th

    rules.model(Organization).field("twitter_handle").add(transform=to_twitter_profile_url)
    rules.model(Person).field("twitter_handle").add(transform=to_twitter_profile_url)
    rules.model(EnergyProject).field("twitter_handle").add(transform=to_twitter_profile_url)

    return rules
示例#3
0
def topics_rules():
    # ModelRules is a singleton that record every model rules
    rules = ModelRules()
    # Disable editing on some model
    rules.model(Country).add(is_editable=False)
    # Records "invisible" fields
    rules.model(FundraisingRound).field("personal_payer").add(is_visible=False)
    rules.model(Organization).field("adviser").add(is_visible=False)
    rules.model(Organization).field("board_member").add(is_visible=False)
    rules.model(Organization).field("company_register_link").add(
        is_visible=False)
    rules.model(Organization).field("litigation_against").add(is_visible=False)
    rules.model(Organization).field("monitoring_body").add(is_visible=False)
    rules.model(Organization).field("partner").add(is_visible=False)
    rules.model(Organization).field("website_url").add(is_visible=False)
    rules.model(Person).field("previous_activity_in_organization").add(
        is_visible=False)
    rules.model(Person).field("website_url").add(is_visible=False)
    rules.model(EnergyProduct).field("operator").add(is_visible=False)
    rules.model(EnergyProject).field("ended").add(is_visible=False)
    rules.model(EnergyProject).field("partner").add(is_visible=False)

    rules.model(Country).add(
        person_set=Neomatch(title="Persons educated or based in this country",
                            target_model=Person,
                            match="""
            (root)<-[r:`person_has_based_in+`|`person_has_educated_in+`]-({select})
        """))

    rules.model(Person).add(organizationkey_set=Neomatch(
        title="Organizations this person has a key position in",
        target_model=Organization,
        match="""
            (root)-[:`organization_has_key_person+`]-({select})
        """))

    rules.model(Person).add(organizationadviser_set=Neomatch(
        title="Organizations this person is an adviser to",
        target_model=Organization,
        match="""
            (root)-[:`organization_has_adviser+`]-({select})
        """))

    rules.model(Person).add(organizationboard_set=Neomatch(
        title="Organizations this person is a board member of",
        target_model=Organization,
        match="""
            (root)-[:`organization_has_board_member+`]-({select})
        """))

    rules.model(Country).add(product_set=Neomatch(
        title="Energy products distributed in this country",
        target_model=EnergyProduct,
        match="""
            (root)<--()<-[:`energy_product_has_distribution+`]-({select})
        """))

    rules.model(Country).add(
        project_set=Neomatch(title="Energy projects active in this country",
                             target_model=EnergyProject,
                             match="""
            (root)-[:`energy_project_has_activity_in_country+`]-({select})
        """))

    rules.model(EnergyProduct).add(country_set=Neomatch(
        title="Countries where this product is distributed",
        target_model=Country,
        match="""
            (root)-[:`energy_product_has_distribution+`]-()-[:`distribution_has_activity_in_country+`]-({select})
        """))

    rules.model(Organization).add(energyproject_set=Neomatch(
        title="Energy projects this organization owns",
        target_model=EnergyProject,
        match="""
            (root)-[:`energy_project_has_owner+`]-({select})
        """))

    rules.model(EnergyProduct).add(energyproduct_set=Neomatch(
        title="Energy project this product belongs to",
        target_model=EnergyProject,
        match="""
            (root)-[:`energy_project_has_product+`]-({select})
        """))

    rules.model(Price).add(transform='{currency} {units}')
    rules.model(FundraisingRound).add(transform='{currency} {units}')

    def to_twitter_profile_url(data, field=None):
        th = data["twitter_handle"]
        if not th:
            return th
        elif th.startswith("http://") or th.startswith("https://"):
            return th
        elif th.startswith("@"):
            return "http://twitter.com/%s" % th[1:]
        else:
            return "http://twitter.com/%s" % th

    rules.model(Organization).field("twitter_handle").add(
        transform=to_twitter_profile_url)
    rules.model(Person).field("twitter_handle").add(
        transform=to_twitter_profile_url)
    rules.model(EnergyProject).field("twitter_handle").add(
        transform=to_twitter_profile_url)

    return rules
示例#4
0
    def default_rules(self, topic):
        # ModelRules is a singleton that record every model rules
        rules = ModelRules()
        # We cant import this early to avoid bi-directional dependancies
        from app.detective.utils import import_class
        models = self.topic_models(topic)
        treated_models = []
        # 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)
                rules.model(model).add(is_searchable=True)
            # This model isn't searchable
            else: rules.model(model).add(is_searchable=False)
            # since we use a generator for topics models we need to convert it
            # to a list to perform a 2nd loop.
            treated_models.append(model)

        # we need to pass a first time on every models to have the proper rules
        # and a second for their RelationShip fields
        for model in treated_models:
            # Check now that each "Relationship"
            # match with a searchable model
            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"])
                    # Entering relationship are not editable yet
                    is_editable = (False if hasattr(target_model, field.name) else False) \
                                  if field.direction == 'in' and target_model is model else modelRules["is_editable"]
                    rules.model(model).field(field.name).add(is_editable=is_editable)
        return rules
示例#5
0
    def default_rules(self, topic):
        # ModelRules is a singleton that record every model rules
        rules = ModelRules()
        # We cant import this early to avoid bi-directional dependancies
        from app.detective.utils import import_class
        models = self.topic_models(topic)
        treated_models = []
        # 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)
                rules.model(model).add(is_searchable=True)
            # This model isn't searchable
            else:
                rules.model(model).add(is_searchable=False)
            # since we use a generator for topics models we need to convert it
            # to a list to perform a 2nd loop.
            treated_models.append(model)

        # we need to pass a first time on every models to have the proper rules
        # and a second for their RelationShip fields
        for model in treated_models:
            # Check now that each "Relationship"
            # match with a searchable model
            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"])
                    # Entering relationship are not editable yet
                    is_editable = (False if hasattr(target_model, field.name) else False) \
                                  if field.direction == 'in' and target_model is model else modelRules["is_editable"]
                    rules.model(model).field(
                        field.name).add(is_editable=is_editable)
        return rules