Exemplo n.º 1
0
 def get_order_by(self, order_by_args):
     order_by_enum = []
     for to_enum in order_by_args:
         # for some reason enums doesn't get converted so do it manually
         order_by_enum.append({
             'field':
             self.order_by_enum(to_enum['field']),
             'direction':
             OrderingDirectionEnum(to_enum['direction']),
             'modifiers':
             [OrderingModifierEnum(m) for m in to_enum['modifiers']],
         })
     order_by = []
     for o in order_by_enum:
         desc = o['direction'] == OrderingDirectionEnum.DESC
         field = to_snake_case(o['field'].name)
         order_field = F(field)
         for modifier in o['modifiers']:
             if modifier == OrderingModifierEnum.CASE_INSENSITIVE:
                 order_field = Lower(order_field)
             else:
                 raise Exception(
                     f"Ordering modifier `{modifier}` does not exist")
         order_by.append(order_field.desc() if desc else order_field.asc())
     return order_by
Exemplo n.º 2
0
    def get_queryset(self, *args, **kwargs):
        if 'pr' in self.request.query_params:
            if int(self.request.query_params.get('pr', 0)):
                qs = models.ProspectLead.objects.filter(
                    is_hot_transfer=False).select_related('lead')
            else:
                qs = models.ProspectLead.objects.filter(
                    is_hot_transfer=True).select_related('lead')
        else:
            qs = self.queryset
        qs = qs.select_related('lead')
        sortBy = self.request.query_params.get('sortBy')
        sortOrder = self.request.query_params.get('sortOrder')
        if sortBy:
            fields = {
                field.name: type(field)
                for field in models.ProspectLead._meta.fields
            }
            if fields[sortBy] == 'django.db.models.fields.CharField':
                sortAttr = Lower(self.request.query_params.get('sortBy'))
                sortAttr = getattr(sortAttr, sortOrder)() if hasattr(
                    sortAttr, sortOrder) else sortAttr.asc()
            else:
                sortAttr = sortBy if sortOrder == 'asc' else "-" + sortBy
                qs = qs.order_by(sortAttr)
        else:
            self.queryset = self.queryset.order_by('-id')

        if self.request.user.groups.filter(name='admin').exists():
            return qs
        elif self.request.user.groups.filter(name='company-head').exists():
            return qs.filter(submitted_by=self.request.user) | qs.filter(
                submitted_by__parent=self.request.user) | qs.filter(
                    submitted_by__parent__parent=self.request.user)
        elif self.request.user.groups.filter(name='team-manager').exists():
            return qs.filter(submitted_by=self.request.user) | qs.filter(
                submitted_by__parent=self.request.user)
        elif self.request.user.groups.filter(
                name__in=['sales-person', 'stage-1']).exists():
            return qs.filter(submitted_by=self.request.user)
        elif self.request.user.groups.filter(
                name__in=['quality-analyst', 'quality_manager']).exists():
            return qs
        else:
            return []
Exemplo n.º 3
0
    def sort(self: QuerySet, sort_column: str, sort_dir: str) -> QuerySet:
        """Sort the specified queryset first by archived status (unarchived theses first),
        then by the specified column in the specified direction,
        or by newest first if not specified
        """
        db_column = ""
        if sort_column == "advisor":
            db_column = "_advisor_name"
        elif sort_column == "title":
            db_column = "title"

        resulting_ordering = "-modified"
        if db_column:
            orderer = Lower(db_column)
            resulting_ordering = orderer.desc() if sort_dir == "desc" else orderer.asc()

        # We want to first order by archived
        return self.order_by("_is_archived", resulting_ordering)
Exemplo n.º 4
0
 def get_queryset(self):
     sortBy = self.request.query_params.get('sortBy')
     sortOrder = self.request.query_params.get('sortOrder')
     filter_q = Q()
     if sortBy:
         fields = {
             field.name: type(field)
             for field in models.Lead._meta.fields
         }
         fields.update({
             "callbacks__" + field.name: type(field)
             for field in models.Callback._meta.fields
         })
         if fields[sortBy] == 'django.db.models.fields.CharField':
             sortAttr = Lower(self.request.query_params.get('sortBy'))
             sortAttr = getattr(sortAttr, sortOrder)() if hasattr(
                 sortAttr, sortOrder) else sortAttr.asc()
         else:
             sortAttr = sortBy if sortOrder == 'asc' else "-" + sortBy
             self.queryset = self.queryset.order_by(sortAttr)
     else:
         self.queryset = self.queryset.order_by('-id')
     if self.request.user.groups.filter(name='sales-person').exists():
         filter_q = Q(assigned_to=self.request.user)
     elif self.request.user.groups.filter(name='stage-1').exists():
         filter_q = Q(assigned_to=self.request.user)
     elif self.request.user.groups.filter(name='quality-analyst').exists():
         filter_q = Q(assigned_to=self.request.user) | Q(
             prospect__isnull=False)
     elif self.request.user.groups.filter(name='team-manager').exists():
         filter_q = Q(assigned_to=self.request.user) | Q(
             assigned_to__parent=self.request.user)
     elif self.request.user.groups.filter(name='company-head').exists():
         filter_q = Q(assigned_to=self.request.user) | Q(
             assigned_to__parent=self.request.user) | Q(
                 assigned_to__parent__parent=self.request.user)
     if self.request.query_params.get(
             'include_raw_leads') and self.request.user.view_raw_leads:
         filter_q = filter_q | Q(submission_status='raw')
     if self.request.user.groups.filter(name='admin').exists():
         return self.queryset
     return self.queryset.filter(filter_q)
Exemplo n.º 5
0
    def filter_queryset(self, request, queryset, view):
        ordering = self.get_ordering(request, queryset, view)

        if ordering:
            case_insensitive_ordering = []
            for field in ordering:
                asc = not field.startswith('-')
                field_name = field if asc else field[1:]
                if field not in self.STRING_ORDERING_FIELDS:
                    order_by_expr = F(field_name)
                else:
                    order_by_expr = Lower(field_name)
                if asc:
                    order_by_expr = order_by_expr.asc(nulls_last=True)
                else:
                    order_by_expr = order_by_expr.desc(nulls_last=True)
                case_insensitive_ordering.append(order_by_expr)
            queryset = queryset.order_by(*case_insensitive_ordering)

        return queryset