class ReservationsAdminList(PicotableListView): model = Reservation columns = [ Column("id", _("ID"), sort_field="id", display="id", filter_config=TextFilter()), Column("name", _("Name"), sort_field="reservable__product__translations__name", display="reservable__product__name", filter_config=TextFilter( filter_field="reservable__product__translations__name")), Column("order", _("From Order"), sort_field="order_line__order", display="order_line__order", filter_config=TextFilter(filter_field="order_line__order__id")), Column("start_time", _("Sign In Time"), sort_field="start_time", display="format_start_time", filter_config=DateRangeFilter(filter_field="start_time")), Column("end_time", _("Sign Out Time"), sort_field="end_time", display="format_end_time", filter_config=DateRangeFilter(filter_field="end_time")), Column("persons", _("Persons"), display="persons"), ] def format_start_time(self, instance, *args, **kwargs): return format_datetime(localtime(instance.start_time), locale=get_current_babel_locale()) def format_end_time(self, instance, *args, **kwargs): return format_datetime(localtime(instance.end_time), locale=get_current_babel_locale()) def get_toolbar(self): toolbar = super(ReservationsAdminList, self).get_toolbar() toolbar.append( URLActionButton( text=_("New Reservation"), icon="fa fa-calendar", url=reverse("shoop_admin:reservations.new"), )) return toolbar def get_object_url(self, instance): return reverse("shoop_admin:reservations.edit", kwargs={"pk": instance.id})
def get_pico(rf, model=None, columns=None): model = model or get_user_model() columns = columns or [ Column("id", "Id", filter_config=Filter(), display=instance_id), Column("username", "Username", sortable=False, filter_config=MultiFieldTextFilter( filter_fields=("username", "email"), operator="iregex")), Column("email", "Email", sortable=False, filter_config=TextFilter()), Column("is_superuser", "Is Superuser", display="superuser_display", filter_config=ChoicesFilter(choices=false_and_true())), Column("is_active", "Is Active", filter_config=ChoicesFilter( choices=false_and_true)), # `choices` callable Column("date_joined", "Date Joined", filter_config=DateRangeFilter()) ] return Picotable(request=rf.get("/"), columns=columns, queryset=model.objects.all(), context=PicoContext())
def test_choice_filter_with_default(rf, admin_user, regular_user): columns = [ Column("id", "Id", filter_config=Filter(), display=instance_id), Column("username", "Username", sortable=False, filter_config=MultiFieldTextFilter(filter_fields=("username", "email"), operator="iregex")), Column("email", "Email", sortable=False, filter_config=TextFilter()), Column("is_superuser", "Is Superuser", display="superuser_display", filter_config=ChoicesFilter(choices=false_and_true())), Column("date_joined", "Date Joined", filter_config=DateRangeFilter()) ] is_active = [ Column("is_active", "Is Active", filter_config=ChoicesFilter(choices=false_and_true)) ] is_active_with_default = [ Column("is_active", "Is Active", filter_config=ChoicesFilter(choices=false_and_true, default=True)) ] query = {"perPage": 100, "page": 1, "sort": "+id"} pico_no_defaults = get_pico(rf, columns=(columns + is_active)) data = pico_no_defaults.get_data(query) user_data = data["items"][0] user = get_user_model().objects.get(id=user_data["id"]) assert user.is_active pico_with_defaults = get_pico(rf, columns=(columns + is_active_with_default)) data = pico_with_defaults.get_data(query) user_data = data["items"][0] user_with_defaults = get_user_model().objects.get(id=user_data["id"]) assert user_with_defaults == user user.is_active = False user.save() data = pico_no_defaults.get_data(query) user_data = data["items"][0] new_user = get_user_model().objects.get(id=user_data["id"]) assert new_user == user data = pico_with_defaults.get_data(query) user_data = data["items"][0] new_user_with_defaults = get_user_model().objects.get(id=user_data["id"]) assert new_user_with_defaults != user_with_defaults
class OrderListView(PicotableListView): model = Order columns = [ Column("identifier", _(u"Order"), linked=True, filter_config=TextFilter(operator="startswith")), Column("order_date", _(u"Order Date"), display="format_order_date", filter_config=DateRangeFilter()), Column("customer", _(u"Customer"), filter_config=MultiFieldTextFilter( filter_fields=("customer__email", "customer__name"))), Column("status", _(u"Status"), filter_config=ChoicesFilter(choices=OrderStatus.objects.all())), Column("payment_status", _(u"Payment Status"), filter_config=ChoicesFilter(choices=PaymentStatus.choices)), Column("shipping_status", _(u"Shipping Status"), filter_config=ChoicesFilter(choices=ShippingStatus.choices)), Column("taxful_total_price", _(u"Total"), sort_field="taxful_total_price_value", display="format_taxful_total_price", class_name="text-right", filter_config=RangeFilter( field_type="number", filter_field="taxful_total_price_value")), ] def get_queryset(self): return super(OrderListView, self).get_queryset().exclude(deleted=True) def format_order_date(self, instance, *args, **kwargs): return format_datetime(localtime(instance.order_date), locale=get_current_babel_locale()) def format_taxful_total_price(self, instance, *args, **kwargs): return escape(format_money(instance.taxful_total_price)) def get_object_abstract(self, instance, item): return [{ "text": "%s" % instance, "class": "header" }, { "title": _(u"Total"), "text": item["taxful_total_price"] }, { "title": _(u"Status"), "text": item["status"] }]
class CartListView(PicotableListView): model = StoredBasket columns = [ Column("created_on", _(u"Created on"), display="format_created_date", filter_config=DateRangeFilter()), Column("updated_on", _(u"Last updated on"), display="format_updated_date", filter_config=DateRangeFilter()), Column("finished", _("Abandoned"), display="format_abandoned_status", filter_config=ChoicesFilter([(False, _("yes")), (True, _("no"))])), Column("shop", _("Shop"), filter_config=ChoicesFilter(choices=Shop.objects.all())), Column("product_count", _("Product count"), filter_config=RangeFilter()), Column("customer", _(u"Customer"), filter_config=MultiFieldTextFilter( filter_fields=("customer__email", "customer__name"))), Column("orderer", _(u"Orderer"), filter_config=MultiFieldTextFilter( filter_fields=("orderer__email", "orderer__name"))), Column("taxful_total_price", _(u"Total"), sort_field="taxful_total_price_value", display="format_taxful_total_price", class_name="text-right", filter_config=RangeFilter( field_type="number", filter_field="taxful_total_price_value")), ] def get_queryset(self): """ Ignore potentially active carts, displaying only those not updated for at least 2 hours. """ cutoff = now() - datetime.timedelta(hours=2) filters = {"updated_on__lt": cutoff, "product_count__gte": 0} return super(CartListView, self).get_queryset().filter(**filters) def format_abandoned_status(self, instance, *args, **kwargs): return "yes" if not instance.finished else "no" def format_created_date(self, instance, *args, **kwargs): return get_locally_formatted_datetime(instance.created_on) def format_updated_date(self, instance, *args, **kwargs): return get_locally_formatted_datetime(instance.updated_on) def format_taxful_total_price(self, instance, *args, **kwargs): return escape(format_money(instance.taxful_total_price)) def get_context_data(self, **kwargs): context = super(CartListView, self).get_context_data(**kwargs) context["title"] = _("Carts") return context def get_object_abstract(self, instance, item): return [ { "text": "%s" % instance, "class": "header" }, { "title": _(u"Created on"), "text": item["created_on"] }, { "title": _(u"Last updated on"), "text": item["updated_on"] }, { "title": _(u"Ordered"), "text": item["finished"] }, { "title": _(u"Total"), "text": item["taxful_total_price"] }, ]