Пример #1
0
 def create_update_view(self, **query):
     config_data = query.get('config', None)
     pk = config_data.get('id', None)
     if config_data:
         config_data['key'] = "{}.{}".format(
             self.config_key, config_data['value']['view'].get('name'))
         config_data['value']['view']['model'] = self.get_model_pkg()
         exist_conf = TableConfig.objects(
             is_deleted=False, key=config_data['key'],
             id__ne=pk) if pk else TableConfig.objects(
                 is_deleted=False, key=config_data['key'])
         if not exist_conf:
             return self.save_config(config_data)
     else:
         return None
Пример #2
0
    def get_config(self, data, **kwargs):
        from implement_table.table.serializer import TableSerializer
        mine = data.pop('mine', False)

        if mine:
            owner = self._get_owner(kwargs['subscriber'].user)
        else:
            owner = None

        return TableSerializer(
            TableConfig.get_by_key(self.config_key, owner).value).data
Пример #3
0
    def update_columns(self, data, **kwargs):
        from implement_table.table.serializer import TableSerializer
        owner = None

        _d = []
        for value in data:
            _d.append(TableColumns(**value))

        config = TableConfig.get_by_key(self.config_key, owner)
        config.value.columns = _d
        config.save()
        return TableSerializer(config.value).data
Пример #4
0
def insert_tables_config(name, path=json_path):
    with open(os.path.join(path, '{}-columns.json'.format(kebab_case(name))),
              encoding='utf-8') as data_file:
        data = ujson.load(data_file)
    table = Table(columns=list(), classes=list())
    columns = data.get('columns', [])
    classes = data.get('classes', [])
    view = data.get('view', [])
    for item in columns:
        config = TableColumns(**item)
        table.columns.append(config)
    for item in classes:
        config = TableClasses(**item)
        table.classes.append(config)
    table.filtered_by_owner = data.get('filtered_by_owner', False)

    table_config = TableConfig.get_by_key("{}{}".format(name, '.default'))
    if table_config is None:
        table.view = TableView(**view)
        table_config = TableConfig(key="{}{}".format(name, '.default'),
                                   value=table)
    else:
        table.pagination = table_config.value.pagination
        table_config.value = table
    table_config.save()
Пример #5
0
    def create(self, request, *args, **kwargs):
        config_key = request.data.pop('configKey', "")
        config = request.data.pop('config', "")
        columns = list()
        # **{'name': 'default', 'page_size': 5, 'page_options': [5, 10, 20, 30]}))
        for item in config['columns']:
            column = TableColumns(**item)
            columns.append(column)

        table_config = TableConfig.get_by_key(config_key)
        table = Table(columns=columns)

        if table_config is None:
            # table.pagination.append(
            #     PaginationNamespace(**{'name': 'default', 'page_size': 5, 'page_options': [5, 10, 20, 30]}))
            table_config = TableConfig(key=config_key, value=table)
        else:
            table.pagination = table_config.value.pagination
            table_config.value = table
        table_config.save(validate=False)

        return Response(TableConfigSerializer(table_config).data)
Пример #6
0
 def _filtered_by_owner(self):
     return TableConfig.get_by_key(self.config_key,
                                   None).value.filtered_by_owner
Пример #7
0
 def _get_columns(self):
     return TableConfig.get_by_key(self.config_key, None).value.columns
Пример #8
0
    def request_list(self, data, order, configKey):
        dictionary = {}
        query = {"$and": []}

        data_order = order.pop('sort_by', 'id')
        order_direction = order.pop('direction',
                                    '-').replace('asc',
                                                 '+').replace('desc', '-')
        search_filter = data.pop('filter', {})
        config_value = TableConfig.get_by_key(configKey, None).value

        pagination = find(config_value.pagination,
                          lambda item: item.name == data['namespace']) or find(
                              config_value.pagination,
                              lambda item: item.name == 'default')
        search_keys = []
        if "multi_search" in search_filter and len(
                search_filter["multi_search"]) > 0:
            print('test---------------', search_filter["multi_search"])

            columns = self._get_columns(configKey)
            for col in search_filter["multi_search"]:
                column = list(
                    filter(lambda x: x.order_by == col['column'], columns))
                if len(column) > 0:
                    column = column[0]
                    if not column.is_ref and self._generate_query(
                            column, col['value']) is not None:
                        search_keys.append(
                            self._generate_query(column, col['value']))
                    elif column.is_ref and self._generate_reference_query(
                            column, col['value']) is not None:
                        search_keys.append(
                            self._generate_reference_query(
                                column, col['value']))
                else:
                    search_keys.append({col['column']['name']: col['value']})

        if "search_all" in search_filter and search_filter["search_all"] != "":
            search_keys = []
            columns = list(
                filter(lambda x: x.is_global_searchable,
                       self._get_columns(configKey)))
            search_values = search_filter["search_all"].split(' ')
            for column in columns:
                for k in search_values:
                    if not column.is_ref and self._generate_query(
                            column, k) is not None:
                        search_keys.append(self._generate_query(column, k))
                    elif column.is_ref and self._generate_reference_query(
                            column, k) is not None:
                        search_keys.append(
                            self._generate_reference_query(column, k))

        search_keys, p_fields = self.split_properties_fields(search_keys)

        if search_keys:
            query["$and"].append({'$or': search_keys})
        # query["$and"] += self._handle_access(kwargs)
        page = self._model.objects(
            __raw__=query) if len(query['$and']) > 0 else self._model.objects
        offset = (data["page"]) * pagination['page_size']
        dictionary["length"] = page.count()
        if dictionary["length"] < offset:
            offset = 0
        page = page.skip(offset).limit(pagination['page_size']).order_by(
            '{}{}'.format(order_direction, data_order))
        if p_fields:
            page = self.filter_by_properties(p_fields, page)
        dictionary["list"] = self._model_serializer(page, many=True).data
        return dictionary
Пример #9
0
 def get_table_views(self, **query):
     model_name = query.get('model')
     return TableConfigSerializer(TableConfig.objects(
         is_deleted=False, value__view__model=model_name),
                                  many=True).data
Пример #10
0
 def save_config(self, config_data):
     config = TableConfig(**config_data)
     config.save()
     return TableConfigSerializer(config).data
Пример #11
0
 def get_default_view(self, **query):
     obj = TableConfig.objects(is_deleted=False,
                               value__view__model=self.get_model_pkg(),
                               value__view__is_default=True)
     return TableSerializer(obj[0].value).data if obj else []