コード例 #1
0
    def __init__(self,
                 stock_selection=None,
                 facet_selection=None,
                 index=None,
                 request=None):
        """
        Values are extracted from the request.

        This will raise an IndexError if there is no object at the given index.
        """
        if index is None:
            if request:
                self.index = int(request.GET.get("index", 0))
            else:
                self.index = 0
        else:
            self.index = index
        self.stock_selection = stock_selection
        self.facet_selection = facet_selection
        if facet_selection:
            self.stock_facet_qs = facet_selection.stock_facet_qs(
                self.stock_selection.stock)
        else:
            self.stock_facet_qs = StockFacetQuerySet(
                stock=self.stock_selection.stock)
        try:
            self.object_at_index = self.stock_facet_qs[self.index]
        except IndexError:
            self.object_at_index = None
コード例 #2
0
ファイル: views.py プロジェクト: Jyggafey/django-stockandflow
    def __init__(self, stock_selection=None, facet_selection=None, index=None, request=None):
        """
        Values are extracted from the request.

        This will raise an IndexError if there is no object at the given index.
        """
        if index is None:
            if request:
                self.index = int(request.GET.get("index", 0))
            else:
                self.index = 0
        else:
            self.index = index
        self.stock_selection = stock_selection
        self.facet_selection = facet_selection
        if facet_selection:
            self.stock_facet_qs = facet_selection.stock_facet_qs(self.stock_selection.stock)
        else:
            self.stock_facet_qs = StockFacetQuerySet(stock=self.stock_selection.stock)
        try:
            self.object_at_index = self.stock_facet_qs[self.index]
        except IndexError:
            self.object_at_index = None
コード例 #3
0
ファイル: views.py プロジェクト: realmbit/django-stockandflow
 def stock_facet_qs(self, stock):
     return StockFacetQuerySet(stock=stock, facet_slug=self.slug, facet_value=self.value)
コード例 #4
0
ファイル: views.py プロジェクト: realmbit/django-stockandflow
class StockSequencer(object):
    """
    This class is used to create a view that iterates through a faceted stock
    based.  The iterating depends on information that is placed in the query
    string of a GET request.
    """

    #Movement constants
    (NEXT, PREVIOUS, FIRST, LAST, TO_INDEX) = range(5)

    def __init__(self, stock_selection=None, facet_selection=None, index=None, request=None):
        """
        Values are extracted from the request.

        This will raise an IndexError if there is no object at the given index.
        """
        if index is None:
            if request:
                self.index = int(request.GET.get("index", 0))
            else:
                self.index = 0
        else:
            self.index = index
        self.stock_selection = stock_selection
        self.facet_selection = facet_selection
        if facet_selection:
            self.stock_facet_qs = facet_selection.stock_facet_qs(self.stock_selection.stock)
        else:
            self.stock_facet_qs = StockFacetQuerySet(stock=self.stock_selection.stock)
        try:
            self.object_at_index = self.stock_facet_qs[self.index]
        except IndexError:
            self.object_at_index = None

    @property
    def stock(self):
        return self.stock_selection.stock

    def next(self, current_object_id=None, current_slug=None, slug_field=None):
        return self._step(1, current_object_id, current_slug, slug_field)

    def previous(self, current_object_id=None, current_slug=None, slug_field=None):
        return self._step(-1, current_object_id, current_slug, slug_field)

    def first(self):
        if self.count() == 0:
            raise StopIteration
        return StockSequencer(self.stock_selection, self.facet_selection, 0)

    def last(self ):
        if self.count() == 0:
            raise StopIteration
        return StockSequencer(self.stock_selection, self.facet_selection, self.count() - 1)

    def to_index(self, to_index):
        if self.count() == 0:
            raise StopIteration
        return StockSequencer(self.stock_selection, self.facet_selection, to_index)


    def _step(self, step_amount, current_object_id, current_slug, slug_field):
        """
        Return the next or previous in the sequence based on the step_amount.
        If cur_object_id and slug are None then the previous object is index +
        step.

        If current_object or slug is not None and if the object at index is not
        equal to current object's id or slug then previous will have the same
        index, so simply return self.

        Raises StopIteration if the next index is invalid.
        """
        if not current_object_id:
            self.index = 0
            rv = self
        elif not self.object_at_index:
            raise StopIteration
        elif current_object_id != self.object_at_index.id:
            rv = self
        elif current_slug is not None:
            if slug_field is None:
                raise ValueError("There must be a slug_field give if current_slug is given.")
            if current_slug != self.get_object()[slug_field]:
                rv = self
        else:
            stepped_index = self.index + step_amount
            if stepped_index < 0:
                raise StopIteration
            rv = StockSequencer(self.stock_selection, self.facet_selection, stepped_index)
        if not rv.object_at_index:
            raise StopIteration
        return rv

    def update_query_dict(self, query_dict):
        """
        Set the values relevant to this object in the query dict.
        """
        query_dict = self.stock_selection.update_query_dict(query_dict)
        if self.facet_selection:
            query_dict = self.facet_selection.update_query_dict(query_dict)
        query_dict["index"] = str(self.index)
        return query_dict

    def query_str(self):
        """
        Update the request's GET query string with the values in this object
        and return the resulting query url encoded string.
        """
        qd = QueryDict("", mutable=True)
        qd = self.update_query_dict(qd)
        return qd.urlencode()

    def count(self):
        """
        Return a count that takes into account the facet.
        """
        return self.stock_facet_qs.count()
コード例 #5
0
ファイル: views.py プロジェクト: Jyggafey/django-stockandflow
class StockSequencer(object):
    """
    This class is used to create a view that iterates through a faceted stock
    based.  The iterating depends on information that is placed in the query
    string of a GET request.
    """

    #Movement constants
    (NEXT, PREVIOUS, FIRST, LAST, TO_INDEX) = range(5)

    def __init__(self, stock_selection=None, facet_selection=None, index=None, request=None):
        """
        Values are extracted from the request.

        This will raise an IndexError if there is no object at the given index.
        """
        if index is None:
            if request:
                self.index = int(request.GET.get("index", 0))
            else:
                self.index = 0
        else:
            self.index = index
        self.stock_selection = stock_selection
        self.facet_selection = facet_selection
        if facet_selection:
            self.stock_facet_qs = facet_selection.stock_facet_qs(self.stock_selection.stock)
        else:
            self.stock_facet_qs = StockFacetQuerySet(stock=self.stock_selection.stock)
        try:
            self.object_at_index = self.stock_facet_qs[self.index]
        except IndexError:
            self.object_at_index = None

    @property
    def stock(self):
        return self.stock_selection.stock

    def next(self, current_object_id=None, current_slug=None, slug_field=None):
        return self._step(1, current_object_id, current_slug, slug_field)

    def previous(self, current_object_id=None, current_slug=None, slug_field=None):
        return self._step(-1, current_object_id, current_slug, slug_field)

    def first(self):
        if self.count() == 0:
            raise StopIteration
        return StockSequencer(self.stock_selection, self.facet_selection, 0)

    def last(self ):
        if self.count() == 0:
            raise StopIteration
        return StockSequencer(self.stock_selection, self.facet_selection, self.count() - 1)

    def to_index(self, to_index):
        if self.count() == 0:
            raise StopIteration
        return StockSequencer(self.stock_selection, self.facet_selection, to_index)


    def _step(self, step_amount, current_object_id, current_slug, slug_field):
        """
        Return the next or previous in the sequence based on the step_amount.
        If cur_object_id and slug are None then the previous object is index +
        step.

        If current_object or slug is not None and if the object at index is not
        equal to current object's id or slug then previous will have the same
        index, so simply return self.

        Raises StopIteration if the next index is invalid.
        """
        if not current_object_id:
            self.index = 0
            rv = self
        elif not self.object_at_index:
            raise StopIteration
        elif current_object_id != self.object_at_index.id:
            rv = self
        elif current_slug is not None:
            if slug_field is None:
                raise ValueError("There must be a slug_field give if current_slug is given.")
            if current_slug != self.get_object()[slug_field]:
                rv = self
        else:
            stepped_index = self.index + step_amount
            if stepped_index < 0:
                raise StopIteration
            rv = StockSequencer(self.stock_selection, self.facet_selection, stepped_index)
        if not rv.object_at_index:
            raise StopIteration
        return rv

    def update_query_dict(self, query_dict):
        """
        Set the values relevant to this object in the query dict.
        """
        query_dict = self.stock_selection.update_query_dict(query_dict)
        if self.facet_selection:
            query_dict = self.facet_selection.update_query_dict(query_dict)
        query_dict["index"] = self.index
        return query_dict

    def query_str(self):
        """
        Update the request's GET query string with the values in this object
        and return the resulting query url encoded string.
        """
        qd = QueryDict("", mutable=True)
        qd = self.update_query_dict(qd)
        return qd.urlencode()

    def count(self):
        """
        Return a count that takes into account the facet.
        """
        return self.stock_facet_qs.count()