def test_sort_oneorder_01(self): "Ordering: natural ordering key" field_name1 = 'name' field_name2 = 'email' self.assertEqual((field_name1,), FakeOrganisation._meta.ordering) build_cell = partial(EntityCellRegularField.build, model=FakeOrganisation) cells = [build_cell(name=field_name2), build_cell(name=field_name1), ] lvs = ListViewState(url=FakeOrganisation.get_lv_absolute_url()) key = cells[1].key ordering = lvs.set_sort(model=FakeOrganisation, cells=cells, cell_key=key, order='') self.assertEqual((field_name1, 'cremeentity_ptr_id'), ordering) self.assertEqual(key, lvs.sort_field) self.assertEqual('', lvs.sort_order) # Fast mode self.assertEqual((field_name1, 'cremeentity_ptr_id'), lvs.set_sort(model=FakeOrganisation, cells=cells, cell_key=key, order='', fast_mode=True, ) ) # DESC ------------------- ordering = lvs.set_sort(model=FakeOrganisation, cells=cells, cell_key=key, order='-') self.assertEqual(('-' + field_name1, '-cremeentity_ptr_id'), ordering) self.assertEqual(key, lvs.sort_field) self.assertEqual('-', lvs.sort_order)
def test_sort_descorder_03(self): "Natural ordering is DESC + another field" field_name1 = 'start' field_name2 = 'title' build_cell = partial(EntityCellRegularField.build, model=FakeActivity) cells = [build_cell(name=field_name1), build_cell(name=field_name2), ] lvs = ListViewState() key = cells[1].key ordering = lvs.set_sort(model=FakeActivity, cells=cells, cell_key=key, order='') self.assertEqual((field_name2, '-' + field_name1, 'cremeentity_ptr_id'), ordering) self.assertEqual(key, lvs.sort_field) self.assertEqual('', lvs.sort_order) # DESC ------------------------------ ordering = lvs.set_sort(model=FakeActivity, cells=cells, cell_key=key, order='-') self.assertEqual(('-' + field_name2, '-' + field_name1, '-cremeentity_ptr_id'), ordering) self.assertEqual(key, lvs.sort_field) self.assertEqual('-', lvs.sort_order) # FAST MODE self.assertEqual((field_name2, 'cremeentity_ptr_id'), lvs.set_sort(model=FakeActivity, cells=cells, cell_key=key, order='', fast_mode=True) )
def test_get_state02(self): request = self._build_request() url = self.url lvs1 = ListViewState(url=url) lvs1.register_in_session(request) self.assertIsInstance(request.session.get(url), dict) lvs2 = ListViewState.get_state(request) self._assertLVSEmpty(lvs2) self.assertEqual(url, lvs2.url)
def test_sort_twoorders_04(self): "set_sort(): natural ordering fields not in cells" build_cell = partial(EntityCellRegularField.build, model=FakeContact) cells = [build_cell(name='phone'), build_cell(name='email'), ] lvs = ListViewState() ordering = lvs.set_sort(model=FakeContact, cells=cells, cell_key=None, order=None) self.assertEqual(('cremeentity_ptr_id',), ordering) self.assertIsNone(lvs.sort_field) self.assertEqual('', lvs.sort_order)
def test_sort_oneorder_04(self): "set_sort(): invalid cell name" field_name1 = 'name' build_cell = partial(EntityCellRegularField.build, model=FakeOrganisation) cells = [build_cell(name=field_name1), build_cell(name='email'), ] lvs = ListViewState() ordering = lvs.set_sort(model=FakeOrganisation, cells=cells, cell_key='invalid', order='') self.assertEqual(cells[0].key, lvs.sort_field) # Fallback on natural model ordering self.assertEqual('', lvs.sort_order) self.assertEqual((field_name1, 'cremeentity_ptr_id'), ordering)
def test_sort_oneorder_03(self): "set_sort(): empty cell name" field_name1 = 'name' build_cell = partial(EntityCellRegularField.build, model=FakeOrganisation) cells = [build_cell(name='email'), build_cell(name=field_name1), ] lvs = ListViewState(url=FakeOrganisation.get_lv_absolute_url()) ordering = lvs.set_sort(model=FakeOrganisation, cells=cells, cell_key=None, order=None) self.assertEqual(cells[1].key, lvs.sort_field) # Fallback on natural model ordering self.assertEqual('', lvs.sort_order) self.assertEqual((field_name1, 'cremeentity_ptr_id'), ordering)
def test_sort_twoorders_03(self): "Add invalid order" field_name1 = 'first_name' field_name2 = 'last_name' build_cell = partial(EntityCellRegularField.build, model=FakeContact) cells = [build_cell(name=field_name1), build_cell(name=field_name2), ] lvs = ListViewState() ordering = lvs.set_sort(model=FakeContact, cells=cells, cell_key='invalid', order='') self.assertEqual(cells[1].key, lvs.sort_field) # Fallback to (first) natural ordering field self.assertEqual('', lvs.sort_order) self.assertEqual((field_name2, field_name1, 'cremeentity_ptr_id'), ordering)
def test_sort_twoorders_06(self): "set_sort(): one natural ordering field not in cells, but an smart index exists." field_name1 = 'last_name' build_cell = partial(EntityCellRegularField.build, model=FakeContact) cells = [build_cell(name='email'), build_cell(name=field_name1), ] lvs = ListViewState() ordering = lvs.set_sort(model=FakeContact, cells=cells, cell_key=None, order=None) self.assertEqual((field_name1, 'first_name', 'cremeentity_ptr_id'), ordering) key = cells[1].key # First natural order self.assertEqual(key, lvs.sort_field) self.assertEqual('', lvs.sort_order)
def test_sort_descorder_02(self): "Natural ordering is DESC => Empty GET/POST => DESC" field_name1 = 'start' field_name2 = 'title' build_cell = partial(EntityCellRegularField.build, model=FakeActivity) cells = [build_cell(name=field_name1), build_cell(name=field_name2), ] lvs = ListViewState() key = cells[0].key ordering = lvs.set_sort(model=FakeActivity, cells=cells, cell_key=None, order=None) self.assertEqual(('-' + field_name1, '-cremeentity_ptr_id'), ordering) self.assertEqual(key, lvs.sort_field) self.assertEqual('-', lvs.sort_order)
def test_sort_oneorder_02(self): "Ordering: add a not natural ordering key" field_name1 = 'name' field_name2 = 'email' build_cell = partial(EntityCellRegularField.build, model=FakeOrganisation) cells = [build_cell(name=field_name2), build_cell(name=field_name1), ] lvs = ListViewState() key = cells[0].key ordering = lvs.set_sort(model=FakeOrganisation, cells=cells, cell_key=key, order='') self.assertEqual((field_name2, field_name1, 'cremeentity_ptr_id'), ordering) self.assertEqual(key, lvs.sort_field) self.assertEqual('', lvs.sort_order)
def test_sort_oneorder_07(self): "Ordering: add a not natural ordering key (FK to CremeModel)" self.assertEqual(('order',), FakeSector._meta.ordering) field_name1 = 'name' field_name2 = 'sector' build_cell = partial(EntityCellRegularField.build, model=FakeOrganisation) cells = [build_cell(name=field_name1), build_cell(name=field_name2), ] lvs = ListViewState() key = cells[1].key ordering = lvs.set_sort(model=FakeOrganisation, cells=cells, cell_key=key, order='') self.assertEqual((field_name2 + '__order', field_name1, 'cremeentity_ptr_id'), ordering) self.assertEqual(key, lvs.sort_field) self.assertEqual('', lvs.sort_order)
def test_sort_oneorder_08(self): "set_sort(): natural ordering field not in cells" build_cell = partial(EntityCellRegularField.build, model=FakeOrganisation) cells = [build_cell(name='phone'), build_cell(name='email'), ] lvs = ListViewState() key = cells[0].key ordering = lvs.set_sort(model=FakeOrganisation, cells=cells, cell_key=key, order='') self.assertEqual(('phone', 'cremeentity_ptr_id'), ordering) self.assertEqual(cells[0].key, lvs.sort_field) self.assertEqual('', lvs.sort_order) # Initial ordering = lvs.set_sort(model=FakeOrganisation, cells=cells, cell_key=None, order=None) self.assertEqual(('cremeentity_ptr_id',), ordering) self.assertIsNone(lvs.sort_field) self.assertEqual('', lvs.sort_order)
def test_sort_oneorder_06(self): "Ordering: add a not natural ordering key (FK to CremeEntity)" field_name1 = 'title' field_name2 = 'linked_folder' self.assertEqual((field_name1,), FakeDocument._meta.ordering) build_cell = partial(EntityCellRegularField.build, model=FakeDocument) cells = [build_cell(name=field_name1), build_cell(name=field_name2), ] lvs = ListViewState() key = cells[1].key ordering = lvs.set_sort(model=FakeDocument, cells=cells, cell_key=key, order='') self.assertEqual((field_name2 + '__header_filter_search_field', field_name1, 'cremeentity_ptr_id'), ordering ) self.assertEqual(key, lvs.sort_field) self.assertEqual('', lvs.sort_order)
def test_sort_twoorders_05(self): "set_sort(): one natural ordering field not in cells" self.assertEqual(('name', '-expiration_date'), FakeInvoice._meta.ordering ) self.assertIsNone(get_indexed_ordering(FakeInvoice, ['name', '-expiration_date'])) field_name1 = 'name' build_cell = partial(EntityCellRegularField.build, model=FakeInvoice) cells = [build_cell(name='number'), build_cell(name=field_name1), # Not expiration_date ] lvs = ListViewState() ordering = lvs.set_sort(model=FakeInvoice, cells=cells, cell_key=None, order=None) self.assertEqual((field_name1, 'cremeentity_ptr_id'), ordering) key = cells[1].key # First natural order self.assertEqual(key, lvs.sort_field) self.assertEqual('', lvs.sort_order)
def test_sort_twoorders_01(self): "meta.ordering: 2 fields" field_name1 = 'first_name' field_name2 = 'last_name' self.assertEqual((field_name2, field_name1), FakeContact._meta.ordering) build_cell = partial(EntityCellRegularField.build, model=FakeContact) cells = [build_cell(name=field_name1), build_cell(name=field_name2), ] lvs = ListViewState() key = cells[0].key ordering = lvs.set_sort(model=FakeContact, cells=cells, cell_key=key, order='') self.assertEqual((field_name1, field_name2, 'cremeentity_ptr_id'), ordering) self.assertEqual(key, lvs.sort_field) self.assertEqual('', lvs.sort_order) # DESC ----------------------------- ordering = lvs.set_sort(model=FakeContact, cells=cells, cell_key=key, order='-') self.assertEqual(('-' + field_name1, '-' + field_name2, '-cremeentity_ptr_id'), ordering) self.assertEqual(key, lvs.sort_field) self.assertEqual('-', lvs.sort_order)
def test_sort_twoordersdesc_01(self): "meta.ordering: 2 fields (one is DESC)" field_name1 = 'name' field_name2 = 'expiration_date' self.assertEqual((field_name1, '-' + field_name2), FakeInvoice._meta.ordering) build_cell = partial(EntityCellRegularField.build, model=FakeInvoice) cells = [build_cell(name=field_name1), build_cell(name=field_name2), ] lvs = ListViewState() key = cells[0].key ordering = lvs.set_sort(model=FakeInvoice, cells=cells, cell_key=key, order='') self.assertEqual((field_name1, '-' + field_name2, 'cremeentity_ptr_id'), ordering) self.assertEqual(key, lvs.sort_field) self.assertEqual('', lvs.sort_order) # DESC ----------------------------- ordering = lvs.set_sort(model=FakeInvoice, cells=cells, cell_key=key, order='-') self.assertEqual(('-' + field_name1, field_name2, '-cremeentity_ptr_id'), ordering) self.assertEqual(key, lvs.sort_field) self.assertEqual('-', lvs.sort_order)
def test_sort_oneorder_05(self): "set_sort(): cell name is not displayed" field_name1 = 'name' field_name2 = 'phone' build_cell = partial(EntityCellRegularField.build, model=FakeOrganisation) cells = [build_cell(name=field_name2), build_cell(name=field_name1), ] lvs = ListViewState() ordering = lvs.set_sort(model=FakeOrganisation, cells=cells, cell_key='email', order='') self.assertEqual(cells[1].key, lvs.sort_field) # Fallback on natural model ordering self.assertEqual('', lvs.sort_order) self.assertEqual((field_name1, 'cremeentity_ptr_id'), ordering) # Natural ordering not displayed --------------- cells = [build_cell(name=field_name2), build_cell(name='sector'), ] ordering = lvs.set_sort(model=FakeOrganisation, cells=cells, cell_key='email', order='') self.assertIsNone(lvs.sort_field) # TODO: Fallback on first column ? self.assertEqual('', lvs.sort_order) self.assertEqual(('cremeentity_ptr_id',), ordering)
def test_sort_twoorders_02(self): "Add not natural ordering" field_name1 = 'first_name' field_name2 = 'last_name' field_name3 = 'phone' build_cell = partial(EntityCellRegularField.build, model=FakeContact) cells = [build_cell(name=field_name1), build_cell(name=field_name2), build_cell(name=field_name3), ] lvs = ListViewState() key = cells[2].key ordering = lvs.set_sort(model=FakeContact, cells=cells, cell_key=key, order='') self.assertEqual((field_name3, field_name2, field_name1, 'cremeentity_ptr_id'), ordering) self.assertEqual(key, lvs.sort_field) self.assertEqual('', lvs.sort_order) # DESC ------------------ ordering = lvs.set_sort(model=FakeContact, cells=cells, cell_key=key, order='-') self.assertEqual(('-' + field_name3, field_name2, field_name1, '-cremeentity_ptr_id'), ordering) self.assertEqual(key, lvs.sort_field) self.assertEqual('-', lvs.sort_order)
def test_get_state01(self): request = self._build_request() lvs = ListViewState.get_state(request) self.assertIsNone(lvs)
def test_init(self): lvs = ListViewState() self._assertLVSEmpty(lvs) self.assertIsNone(lvs.url)
def test_build_from_request(self): request = self._build_request() lvs = ListViewState.build_from_request(request.GET, request.path) self.assertIsInstance(lvs, ListViewState) self.assertEqual(self.url, lvs.url) self._assertLVSEmpty(lvs)
def list_view_content( request, model, hf_pk='', extra_dict=None, template='creme_core/generics/list_entities.html', show_actions=True, extra_q=None, mode=MODE_MULTIPLE_SELECTION, post_process=None, content_template='creme_core/frags/list_view_content.html', lv_state_id=None): """ Generic list_view wrapper / generator Accepts only CremeEntity model and subclasses. @param post_process: Function that takes the template context and the request as parameters (so you can modify the context). """ assert issubclass( model, CremeEntity), '{} is not a subclass of CremeEntity'.format(model) PAGE_SIZES = settings.PAGE_SIZES is_GET = request.method == 'GET' arguments = request.GET if is_GET else request.POST lv_state_id = lv_state_id or request.path current_lvs = ListViewState.get_or_create_state(request, url=lv_state_id) rows = _build_rowscount(arguments, current_lvs) transient = is_GET or (arguments.get('transient') in {'1', 'true'}) ct = ContentType.objects.get_for_model(model) user = request.user header_filters = HeaderFilterList(ct, user) hf = current_lvs.set_headerfilter(header_filters, arguments.get('hfilter', -1), hf_pk) cells = hf.cells if show_actions: # cells.insert(0, EntityCellActions(model=model)) cells.insert( 0, EntityCellActions(model=model, actions_registry=actions_registry)) if arguments.get('search', '') == 'clear': current_lvs.clear_research() else: current_lvs.handle_research(arguments, cells, merge=transient) entity_filters = EntityFilterList(ct, user) efilter = _select_entityfilter(arguments, entity_filters, current_lvs.entity_filter_id) current_lvs.entity_filter_id = efilter.id if efilter else None json_q_filter, extra_filter = _build_extrafilter(arguments, extra_q) entities, count = _build_entity_queryset(user, model, current_lvs, extra_filter, efilter, hf) fast_mode = (count >= settings.FAST_QUERY_MODE_THRESHOLD) ordering = current_lvs.set_sort( model, cells, cell_key=arguments.get('sort_field', current_lvs.sort_field), order=arguments.get('sort_order', current_lvs.sort_order), fast_mode=fast_mode, ) entities_page = _build_entities_page( arguments, current_lvs, entities.order_by(*ordering), size=rows, count=count, ordering=ordering, fast_mode=fast_mode, ) if not transient: current_lvs.register_in_session(request) template_dict = { 'model': model, 'list_title': _('List of {models}').format(models=model._meta.verbose_name_plural), 'sub_title': '', 'header_filters': header_filters, 'entity_filters': entity_filters, 'entities': entities_page, 'list_view_state': current_lvs, 'content_type': ct, 'content_type_id': ct.id, 'search': len(current_lvs.research) > 0, 'content_template': content_template, 'page_sizes': PAGE_SIZES, 'o2m': (mode == MODE_SINGLE_SELECTION), 'add_url': model.get_create_absolute_url(), 'extra_bt_templates': None, # TODO: () instead ???, 'show_actions': show_actions, 'extra_filter': QSerializer().dumps(extra_filter), 'q_filter': json_q_filter, 'research_cellkeys': {cell_key for cell_key, _value in current_lvs.research}, 'is_popup_view': False, } if extra_dict: template_dict.update(extra_dict) if request.is_ajax(): template = template_dict['content_template'] if post_process: post_process(template_dict, request) # Optimisation time !! hf.populate_entities(entities_page.object_list, user) return template, template_dict