def active_coupons(self, start_date, end_date): """ Number of coupons that have been active, i.e. possible to use, during period from start_date to end_date (both inclusive). """ start_dt, end_dt = full_day_datetimes(start_date, end_date) return self.filter( status='500', # actually printed expiration_date__gte=start_date, generating_purchase__time_of_purchase__lte=end_dt ).count()
def find(self, **kwargs): start_dt, end_dt = full_day_datetimes(kwargs.pop('start_date', None), kwargs.pop('end_date', None)) location = kwargs.pop('location', None) products = kwargs.pop('products', None) filters = [] if isinstance(location, query.QuerySet): location_model = getattr(location, 'model', None) if location_model == Chain: filters.append(Q(purchase__pos__store__chain__in=location)) elif location_model == Store: filters.append(Q(purchase__pos__store__in=location)) elif location_model == PointOfSale: filters.append(Q(purchase__pos__in=location)) elif location_model == Seller: filters.append(Q(purchase__seller__in=location)) elif location: location_model = location.__class__ if location_model == Chain: filters.append(Q(purchase__pos__store__chain=location)) elif location_model == Store: filters.append(Q(purchase__pos__store=location)) elif location_model == PointOfSale: filters.append(Q(purchase__pos=location)) elif location_model == Seller: filters.append(Q(purchase__seller=location)) if isinstance(products, query.QuerySet): filters.append(Q(product__in=products)) elif products: filters.append(Q(product=products)) if start_dt: filters.append(Q(purchase__time_of_purchase__gte=start_dt)) if end_dt: filters.append(Q(purchase__time_of_purchase__lte=end_dt)) if filters: return self.get_query_set().filter(*filters) return self.get_query_set()