Esempio n. 1
0
def joined_table_template(
    table_names, name='Joined Table', redirect_url='#',
    message='None', **kwargs
):
    """template for joined tables
    """

    if redirect_url != '#':
        redirect_url = url_for(redirect_url)

    tables = []
    for table_name in table_names:
        if not isinstance(table_name, str):
            tables.append(table_name)
        else:
            tables.append(config.get_table(table_name))

    joined_table = save_join(tables)
    df = joined_table.proj(
        *joined_table.heading.non_blobs
    ).fetch(format='frame').reset_index()
    data = get_jsontable(
        df, joined_table.heading.primary_key,
        **kwargs
    )

    return render_template(
        'pages/joined_table.html',
        name=name,
        url=redirect_url,
        data=data,
        toggle_off_keys=[0],
        message=message
    )
Esempio n. 2
0
    def populate_form(self, restriction, form, is_edit=False, **kwargs):

        readonly = []

        # restrict table
        entry = self.table & restriction

        formatted_dict = (entry).proj(
            *self.non_blobs).fetch1()  # proj non_blobs?

        populate_dict = self._prepare_populate_dict(readonly,
                                                    formatted_dict,
                                                    is_edit=is_edit)

        # populate part tables
        for part_name in self.table.parts():
            part_table = config.get_table(part_name)
            part_formatted_list_dict = (part_table & restriction).proj(
                *part_table.heading.non_blobs).fetch(as_dict=True)
            # proj non_blobs?
            populate_dict[_get_name(part_table)] = []
            for part_formatted_dict in part_formatted_list_dict:

                part_populate_dict = self.part_fields[_get_name(
                    part_table)]._prepare_populate_dict(readonly,
                                                        part_formatted_dict,
                                                        is_edit=is_edit)

                populate_dict[_get_name(part_table)].append(part_populate_dict)

        # update with kwargs
        populate_dict.update(kwargs)

        form.populate_form(populate_dict)
        return readonly
Esempio n. 3
0
    def get_part_fields(self):
        """get part fields attribute
        """

        part_fields = {}

        for part_name in self.table.parts():
            part_table = config.get_table(part_name)
            # TODO aliased part tables
            dynamicform = type(self)(part_table,
                                     skip=self.table.primary_key,
                                     formtype=NoCsrfForm)
            dynamicform.restriction = self.restriction
            part_fields[_get_name(part_table)] = dynamicform

        return part_fields
Esempio n. 4
0
    def get_foreign_table(self):
        """
        """
        aliased = None
        parents = self.dependencies.parents(self.table.full_table_name)
        for table_name, table_info in parents.items():
            if self.name in table_info['attr_map']:
                # deal with aliasing
                if table_info['aliased']:
                    aliased_parents = self.dependencies.parents(table_name)
                    # aliased parent should only be one
                    table_name = list(aliased_parents)[0]
                    aliased = table_info['attr_map']
                break
        else:
            return None, aliased

        return config.get_table(table_name), aliased
Esempio n. 5
0
def user_has_permission(table, user, skip_tables=None):
    """test if user is allowed to delete an entry or perform another action
    on a datajoint.Table
    """

    if user in config['administrators']:
        return True

    if table.database in config.groups_of_user(user):
        return True

    if skip_tables is None:
        skip_tables = []

    # always add table name
    skip_tables.append(table.full_table_name)

    if not table.connection.dependencies:
        table.connection.dependencies.load()

    ancestors = table.ancestors()

    if config.user_table.full_table_name in ancestors:
        if config['user_name'] in table.heading:
            user_only = table & {config['user_name']: user}
            return len(user_only) == len(table)
        else:
            for parent_name, parent_info in table.parents().items():
                if parent_name in skip_tables:
                    continue
                if parent_info['aliased']:
                    grandparents = table.connection.dependencies.parents(
                        parent_name
                    )
                    # only a single one should exist if aliased
                    parent_name = list(grandparents.keys())[0]

                # get parent table
                parent_table = config.get_table(parent_name)

                # project only necessary keys
                to_rename = {
                    ele: key
                    for key, ele in parent_info['attr_map'].items()
                }
                restricted_table = parent_table & table.proj(**to_rename)

                if not user_has_permission(
                    restricted_table, user, skip_tables
                ):
                    return False

    # checks if children have a parent table that is dependent on user table
    for child_name, child_info in table.children().items():
        if child_name in skip_tables:
            continue
        if child_info['aliased']:
            grandchildren = table.connection.dependencies.children(
                child_name
            )
            # only a single one should exist if aliased
            child_name = list(grandchildren.keys())[0]

        # get child table
        child_table = config.get_table(child_name)

        # restrict only with necessary keys
        restricted_table = child_table & table.proj(**child_info['attr_map'])

        if not user_has_permission(
            restricted_table, user, skip_tables
        ):
            return False

    return True
Esempio n. 6
0
def name_lookup(full_name):
    """ Look for a table's class name given its full name. """
    try:
        return config.get_table(full_name, as_string=True)
    except Exception:
        return full_name