Exemplo n.º 1
0
    def read(self):
        # Initialize the query to the model.
        queryset = self.session.query(self.meta.model)

        query = None
        if self.slug is not None:
            # This is an item-access (eg. GET /<name>/:slug); ignore the
            # query string and generate a query-object based on the slug.
            query = Query(segments=[QuerySegment(
                path=self.meta.slug.path.split('.'),
                operator=constants.OPERATOR_EQUAL[0],
                values=[self.slug])])

        elif self.request.query:
            # This is a list-access; use the query string and construct
            # a query object from it.
            query = parser.parse(self.request.query)

        # Determine if we need to filter the queryset in some way; and if so,
        # filter it.
        clause = None
        if query is not None:
            clause = build_clause(query, self.attributes, self.meta.model)
            queryset = self.filter(clause, queryset)

        # Filter the queryset by asserting authorization.
        queryset = self.meta.authorization.filter(
            self.request.user, 'read', self, queryset)

        # Return the queryset.
        return queryset.all() if self.slug is None else queryset.first()
Exemplo n.º 2
0
    def read(self):
        # Initialize the queryset to the model manager.
        queryset = self.meta.model.objects

        query = None
        if self.slug is not None:
            # This is an item-access (eg. GET /<name>/:slug); ignore the
            # query string and generate a query-object based on the slug.
            query = Query(segments=[QuerySegment(
                path=self.meta.slug.path.split('.'),
                operator=constants.OPERATOR_EQUAL[0],
                values=[self.slug])])

        elif self.request.query:
            # This is a list-access; use the query string and construct
            # a query object from it.
            query = parser.parse(self.request.query)

        # Determine if we need to filter the queryset in some way; and if so,
        # filter it.
        if query is not None:
            clause = build_clause(query, self.attributes)
            queryset = self.filter(clause, queryset)

        # Filter the queryset by asserting authorization.
        queryset = self.meta.authorization.filter(
            self.request.user, 'read', self, queryset)

        if self.slug is not None:
            # Attempt to return just the single result we should have.
            result = queryset.all()[:1]
            return result[0] if result else None

        # Return the entire queryset.
        return list(queryset.all())
Exemplo n.º 3
0
    def read(self):
        # Initialize the query to the model.
        queryset = self.session.query(self.meta.model)

        query = None
        if self.slug is not None:
            # This is an item-access (eg. GET //:slug); ignore the
            # query string and generate a query-object based on the slug.
            query = Query(
                original=None,
                parsed=QuerySegment(
                    path=self.meta.slug.path.split('.'),
                    operator=constants.OPERATOR_MAP[constants.OPERATOR_EQUAL],
                    values=[self.slug]
                )
            )

        elif self.request.query:
            # This is a list-access; use the query string and construct
            # a query object from it.
            query = parser.parse(self.request.query)

        # Determine if we need to filter the queryset in some way; and if so,
        # filter it.
        clause = None
        if query is not None:
            clause = build_clause(
                self, query, self.attributes,
                self.cleaners, self.meta.model)

            queryset = self.filter(clause, queryset)

        if self.slug is None:
            # Filter the queryset by asserting authorization.
            queryset = self.meta.authorization.filter(
                self.request.user, 'read', self, queryset)

            # Return the queryset.
            return queryset

        else:
            # Get the item in question.
            item = queryset.first()

            # Sanity check to make sure some item was found.
            if item is None:
                return None

            # Ensure the user is authorized to perform this action.
            authz = self.meta.authorization
            if not authz.is_authorized(self.request.user, 'read', self, item):
                authz.unauthorized()

            # We're good, return the item.
            return item
Exemplo n.º 4
0
    def read(self):
        # Initialize the queryset to the model manager.
        queryset = self.meta.model.objects

        query = None
        if self.slug is not None:
            # This is an item-access (eg. GET /<name>/:slug); ignore the
            # query string and generate a query-object based on the slug.
            query = Query(
                original=None,
                parsed=QuerySegment(
                    path=self.meta.slug.path.split('.'),
                    operator=constants.OPERATOR_MAP[constants.OPERATOR_EQUAL],
                    values=[self.slug]))

        elif self.request.query:
            # This is a list-access; use the query string and construct
            # a query object from it.
            query = parser.parse(self.request.query)

        # Determine if we need to filter the queryset in some way; and if so,
        # filter it.
        if query is not None:
            clause = build_clause(query, self.attributes)
            queryset = self.filter(clause, queryset)

        # Filter the queryset by asserting authorization.
        queryset = self.meta.authorization.filter(self.request.user, 'read',
                                                  self, queryset)

        if self.slug is not None:
            # Attempt to return just the single result we should have.
            result = queryset.all()[:1]
            return result[0] if result else None

        # Return the entire queryset.
        return queryset.all()
Exemplo n.º 5
0
 def parse(self, text):
     """Simple convenience function to unwrap the array of parameters."""
     return parser.parse(text).parsed
Exemplo n.º 6
0
 def parse(self, text):
     """Simple convenience function to unwrap the array of parameters."""
     return parser.parse(text)