def get_context_data(self, **kwargs): obj = self.crud if hasattr(self, 'crud') else self count = self.object_list.count() context = CrudListView.get_context_data(self, **kwargs) context['count'] = count parent_model = getattr( self.model, obj.parent_field).field.related_model params = {'pk': kwargs['root_pk']} if self.container_field: container = self.container_field.split('__') if len(container) > 1: params['__'.join(container[1:])] = self.request.user.pk try: parent_object = parent_model.objects.get(**params) except: raise Http404() context[ 'title'] = '%s <small>(%s)</small>' % ( context['title'], parent_object) return context
def get_urls(cls): def _add_base(view): class CrudViewWithBase(cls.BaseMixin, view): model = cls.model help_path = cls.help_path crud = cls CrudViewWithBase.__name__ = view.__name__ return CrudViewWithBase CrudListView = _add_base(cls.ListView) CrudCreateView = _add_base(cls.CreateView) CrudDetailView = _add_base(cls.DetailView) CrudUpdateView = _add_base(cls.UpdateView) CrudDeleteView = _add_base(cls.DeleteView) return [ url(regex, view.as_view(), name=view.url_name(suffix)) for regex, view, suffix in [ (CrudListView.get_url_regex(), CrudListView, base.LIST), (CrudCreateView.get_url_regex(), CrudCreateView, base.CREATE), (CrudDetailView.get_url_regex(), CrudDetailView, base.DETAIL), (CrudUpdateView.get_url_regex(), CrudUpdateView, base.UPDATE), (CrudDeleteView.get_url_regex(), CrudDeleteView, base.DELETE), ]]
def get_urls(cls): def _add_base(view): class CrudViewWithBase(cls.BaseMixin, view): model = cls.model help_path = cls.help_path crud = cls CrudViewWithBase.__name__ = view.__name__ return CrudViewWithBase CrudListView = _add_base(cls.ListView) CrudCreateView = _add_base(cls.CreateView) CrudDetailView = _add_base(cls.DetailView) CrudUpdateView = _add_base(cls.UpdateView) CrudDeleteView = _add_base(cls.DeleteView) return [ url(regex, view.as_view(), name=view.url_name(suffix)) for regex, view, suffix in [ (CrudListView.get_url_regex(), CrudListView, base.LIST), (CrudCreateView.get_url_regex(), CrudCreateView, base.CREATE), (CrudDetailView.get_url_regex(), CrudDetailView, base.DETAIL), (CrudUpdateView.get_url_regex(), CrudUpdateView, base.UPDATE), (CrudDeleteView.get_url_regex(), CrudDeleteView, base.DELETE), ] ]
def get_context_data(self, **kwargs): obj = self.crud if hasattr(self, 'crud') else self count = self.object_list.count() context = CrudListView.get_context_data(self, **kwargs) context['count'] = count parent_model = getattr(self.model, obj.parent_field).field.related_model params = {'pk': kwargs['root_pk']} if self.container_field: container = self.container_field.split('__') if len(container) > 1: params['__'.join(container[1:])] = self.request.user.pk try: parent_object = parent_model.objects.get(**params) except: raise Http404() context['title'] = '%s <small>(%s)</small>' % (context['title'], parent_object) return context
def get_queryset(self): queryset = CrudListView.get_queryset(self) # form_search_class # só pode ser usado em models que herdam de SaapSearchMixin if hasattr(self, 'form_search_class'): request = self.request if request.GET.get('q') is not None: query = normalize(str(request.GET.get('q'))) query = query.split(' ') if query: q = Q() for item in query: if not item: continue q = q & Q(search__icontains=item) if q: queryset = queryset.filter(q) list_field_names = self.list_field_names o = '1' if 'o' in self.request.GET: o = self.request.GET['o'] desc = '-' if o.startswith('-') else '' try: fields_for_ordering = list_field_names[ (abs(int(o)) - 1) % len(list_field_names)] if isinstance(fields_for_ordering, str): fields_for_ordering = [fields_for_ordering, ] ordering = () model = self.model for fo in fields_for_ordering: fm = model._meta.get_field(fo) if hasattr(fm, 'related_model') and fm.related_model: rmo = fm.related_model._meta.ordering if rmo: rmo = rmo[0] if not isinstance(rmo, str): rmo = rmo[0] fo = '%s__%s' % (fo, rmo) fo = desc + fo ordering += (fo,) model = self.model model_ordering = model._meta.ordering if model_ordering: if isinstance(model_ordering, str): model_ordering = (model_ordering,) for mo in model_ordering: if mo not in ordering: ordering = ordering + (mo, ) queryset = queryset.order_by(*ordering) except: pass if not self.request.user.is_authenticated(): return queryset if self.container_field: params = {} params[self.container_field] = self.request.user.pk queryset = queryset.filter(**params) return queryset
def get_queryset(self): queryset = CrudListView.get_queryset(self) # form_search_class # só pode ser usado em models que herdam de SaapSearchMixin if hasattr(self, 'form_search_class'): request = self.request if request.GET.get('q') is not None: query = normalize(str(request.GET.get('q'))) query = query.split(' ') if query: q = Q() for item in query: if not item: continue q = q & Q(search__icontains=item) if q: queryset = queryset.filter(q) list_field_names = self.list_field_names o = '1' if 'o' in self.request.GET: o = self.request.GET['o'] desc = '-' if o.startswith('-') else '' try: fields_for_ordering = list_field_names[(abs(int(o)) - 1) % len(list_field_names)] if isinstance(fields_for_ordering, str): fields_for_ordering = [ fields_for_ordering, ] ordering = () model = self.model for fo in fields_for_ordering: fm = model._meta.get_field(fo) if hasattr(fm, 'related_model') and fm.related_model: rmo = fm.related_model._meta.ordering if rmo: rmo = rmo[0] if not isinstance(rmo, str): rmo = rmo[0] fo = '%s__%s' % (fo, rmo) fo = desc + fo ordering += (fo, ) model = self.model model_ordering = model._meta.ordering if model_ordering: if isinstance(model_ordering, str): model_ordering = (model_ordering, ) for mo in model_ordering: if mo not in ordering: ordering = ordering + (mo, ) queryset = queryset.order_by(*ordering) except: pass if not self.request.user.is_authenticated(): return queryset if self.container_field: params = {} params[self.container_field] = self.request.user.pk queryset = queryset.filter(**params) return queryset