def crud_history_fields(obj, fields=None, auditTable=None): """ Display object fields in table rows:: <table> {% crud_fields object 'id, %} </table> * ``fields`` fields to include If fields is ``None`` all fields will be displayed. If fields is ``string`` comma separated field names will be displayed. if field is dictionary, key should be field name and value field verbose name. """ if fields is None: fields = utils.get_fields(type(obj)) elif isinstance(fields, six.string_types): field_names = [f.strip() for f in fields.split(',')] fields = utils.get_fields(type(obj), include=field_names) return { 'object': obj, 'fields': fields, 'audit_table': auditTable, }
def get_urls_and_fields(self, context): include = None if hasattr(self, 'display_fields') and self.view_type == 'detail': include = getattr(self, 'display_fields') if hasattr(self, 'list_fields') and self.view_type == 'list': include = getattr(self, 'list_fields') context['fields'] = utils.get_fields(self.model, include=include) if hasattr(self, 'object') and self.object: for action in utils.INSTANCE_ACTIONS: try: nurl = utils.crud_url_name(self.model, action) if self.namespace: nurl = self.namespace + ':' + nurl url = reverse(nurl, kwargs={'pk': self.object.pk}) except NoReverseMatch: url = None context['url_%s' % action] = url for action in utils.LIST_ACTIONS: try: nurl = utils.crud_url_name(self.model, action) if self.namespace: nurl = self.namespace + ':' + nurl url = reverse(nurl) except NoReverseMatch: url = None context['url_%s' % action] = url
def get_fields(model, fields=None): """ Assigns fields for model. """ include = [f.strip() for f in fields.split(',')] if fields else None return utils.get_fields( model, include )
def test_get_listview_fields(self): self.client.login(username='******', password='******') self.type = LIST url = get_action_url(self, self.type) response = self.client.get(url) self.assertEqual(response.status_code, 200) fields = response.context[ 'fields'] # columns fields to build table html if self.view.template_name_base != crud_views.CRUDView.template_name_base: # test if used custom template tmp = self.app_testing + '/' + self.model + '/' + self.view.template_name_base + '/list.html' self.assertTemplateUsed(response, tmp) # loaded the correct template model_fields = utils.get_fields(self.view.model) if self.view.list_fields and self.view.fields != '__all__': # test if fields exist on response for field in self.view.list_fields: self.assertIn(field, model_fields) # fields exist on model self.assertIn( field, fields) # fields exist on listview setting list_fields html = '<th class="th-field-%s th-fieldtype-' % (field) self.assertContains(response, "%s" % html) # column field exist on table html else: # '__all__' for field in model_fields: if not field == 'id': self.assertIn( field, fields) # field exist on listview setting list_fields html = '<th class="th-field-%s th-fieldtype-' % (field) self.assertContains( response, "%s" % html) # column field exist on table html self.client.logout()
def test_get_fields_ordering(): """ get_fields should return fields in appropiate order. """ res = get_fields(Author, ('birthday', 'name')) assert [r for r in res] == ['birthday', 'name']
def get_context_data(self, **kwargs): """ Adds available urls and names. """ context = super(CRUDMixin, self).get_context_data(**kwargs) context.update({ 'model_verbose_name': self.model._meta.verbose_name, 'model_verbose_name_plural': self.model._meta.verbose_name_plural, 'namespace': self.namespace }) include = None if hasattr(self, 'display_fields') and self.view_type == 'detail': include = getattr(self, 'display_fields') if hasattr(self, 'list_fields') and self.view_type == 'list': include = getattr(self, 'list_fields') context['fields'] = utils.get_fields(self.model, include=include) if hasattr(self, 'object') and self.object: for action in utils.INSTANCE_ACTIONS: try: nurl = utils.crud_url_name(self.model, action) if self.namespace: nurl = self.namespace + ':' + nurl url = reverse(nurl, kwargs={'pk': self.object.pk}) except NoReverseMatch: url = None context['url_%s' % action] = url for action in utils.LIST_ACTIONS: try: nurl = utils.crud_url_name(self.model, action) if self.namespace: nurl = self.namespace + ':' + nurl url = reverse(nurl) except NoReverseMatch: url = None context['url_%s' % action] = url try: context['search'] = self.search_fields except AttributeError: context['search'] = False if self.view_type == 'list' and 'q' in self.request.GET: context['q'] = self.request.GET.get('q', '') if self.view_type in ['update', 'detail']: context['inlines'] = self.inlines if 'object' not in context: context['object'] = self.model context['views_available'] = self.views_available user = self.request.user available_perms = {} for perm in self.all_perms: if self.check_perms: if perm in self.views_available: available_perms[perm] = all( [user.has_perm(x) for x in self.all_perms[perm]]) else: available_perms[perm] = False else: available_perms[perm] = True context['crud_perms'] = available_perms context['template_father'] = self.template_father context.update(self.context_rel) context['getparams'] = self.getparams return context
def test_get_editView_content(self): self.client.login(username='******', password='******') firstobject = self.view.model.objects.first() self.assertTrue(isinstance( firstobject, self.view.model)) # check if return something self.type = UPDATE if (self.type in self.view.views_available): url = get_action_url(self, self.type, firstobject.pk) response = self.client.get(url) self.assertEqual(response.status_code, 200) if self.view.template_name_base != crud_views.CRUDView.template_name_base: # test if used custom template tmp = self.app_testing + '/' + self.model + '/' + self.view.template_name_base + '/update.html' self.assertTemplateUsed(response, tmp) # loaded the correct template if (DELETE in self.view.views_available): url_delete = get_action_url(self, DELETE, firstobject.pk) # when cruds_url exist in view, that check the url with namespace if hasattr( self.view, 'cruds_url' ): # cruds_url is in url actions and that changes the url # If cruds_url exist on view, the DELETE action should have 'namespace' url_delete = url_delete.replace(self.view.cruds_url, 'namespace') html = '%s' % (url_delete ) # When it have a A.href or Form.action self.assertContains(response, html) fields = response.context[ 'fields'] # columns fields to build table html model_fields = utils.get_fields(self.view.model) if self.view.fields and not self.view.fields == '__all__': # test if fields exist on response for field in self.view.fields: self.assertIn(field, model_fields) # fields exist on model self.assertIn( field, fields) # fields exist on update view setting fields html = '<div id="div_id_%s" ' % (field) self.assertContains( response, "%s" % html) # column field exist on table html else: # '__all__' for field in model_fields: if not field == 'id': self.assertIn( field, fields ) # field exist on update view setting fields html = '<div id="div_id_%s" ' % (field) self.assertContains( response, "%s" % html) # column field exist on table html if self.view.inlines: for inline in self.view.inlines: inline_model_fields = utils.get_fields(inline.model) html = '<div data-refresh-url="/inline/testapp/%s/%i/list" id="%s' % ( inline.name, firstobject.pk, inline.name) self.assertContains(response, html) self.assertEqual(self.view.views_available, response.context['views_available']) else: try: url = get_action_url(self, self.type, firstobject.pk) response = self.client.get(url) self.assertEqual(response.status_code, 404) except NoReverseMatch: pass self.client.logout()
def test_get_fields_order(self): res = get_fields(Author, ('birthday', 'name')) self.assertEqual(list(res.keys())[0], 'birthday')
def test_get_fields_order(self): res = get_fields(Autor, ('name', )) self.assertEqual(list(res.keys())[0], 'name')