Example #1
0
    def __init__(self):
        super(PicotableListView, self).__init__()
        if self.mass_actions:
            self.default_columns = [
                Column("select",
                       "",
                       display="",
                       sortable=False,
                       linked=False,
                       class_name="text-center"),
            ] + self.default_columns

        self.settings = ViewSettings(self.model,
                                     self.default_columns,
                                     view_context=self)

        if self.mass_actions:
            if self.settings.columns:
                # settings.columns never have selects
                self.columns = [
                    Column("select",
                           "",
                           display="",
                           sortable=False,
                           linked=False,
                           class_name="text-center"),
                ] + self.settings.columns
            else:
                self.columns = self.default_columns
        else:
            self.columns = (self.settings.columns or self.default_columns)
Example #2
0
class CurrencyListView(PicotableListView):
    model = Currency

    default_columns = [
        Column("name",
               _("Name"),
               display="get_currency_display",
               sortable=False),
        Column("code",
               _(u"Code"),
               sort_field="code",
               filter_config=TextFilter(
                   filter_field="code",
                   placeholder=_("Filter by code"),
               )),
        Column(
            "decimal_places",
            _("Decimal places"),
            display="format_decimal_places",
        )
    ]

    def format_decimal_places(self, instance):
        return "{0}".format(instance.decimal_places)

    def get_currency_display(self, instance):
        locale = get_current_babel_locale()
        return locale.currencies.get(instance.code, instance.code)
Example #3
0
class ShopListView(PicotableListView):
    model = Shop
    default_columns = [
        Column("logo",
               _(u"Logo"),
               display="logo",
               class_name="text-center",
               raw=True,
               ordering=1,
               sortable=False),
        Column("name",
               _(u"Name"),
               sort_field="translations__name",
               display="name",
               filter_config=TextFilter(filter_field="translations__name",
                                        placeholder=_("Filter by name..."))),
        Column("domain", _(u"Domain")),
        Column("identifier", _(u"Identifier")),
        Column("status",
               _(u"Status"),
               filter_config=ChoicesFilter(choices=ShopStatus.choices)),
    ]

    def get_queryset(self):
        if self.request.user.is_superuser:
            return super(ShopListView, self).get_queryset()
        else:
            return Shop.objects.get_for_user(self.request.user)

    def get_toolbar(self):
        if WshopSettings.get_setting("WSHOP_ENABLE_MULTIPLE_SHOPS"):
            return super(ShopListView, self).get_toolbar()
        else:
            return Toolbar([])
Example #4
0
class ContactGroupListView(PicotableListView):
    model = ContactGroup
    default_columns = [
        Column("name",
               _(u"Name"),
               sort_field="translations__name",
               display="name",
               filter_config=TextFilter(filter_field="translations__name",
                                        placeholder=_("Filter by name..."))),
        Column("n_members", _(u"Number of Members")),
    ]

    def get_queryset(self):
        return ContactGroup.objects.all().annotate(n_members=Count("members"))

    def get_context_data(self, **kwargs):
        context = super(ContactGroupListView, self).get_context_data(**kwargs)
        if self.request.user.is_superuser:
            settings_button = SettingsActionButton.for_model(
                ContactGroup, return_url="contact_group")
        else:
            settings_button = None
        context["toolbar"] = Toolbar([
            NewActionButton("wshop_admin:contact_group.new"), settings_button
        ])
        return context
Example #5
0
class SupplierListView(PicotableListView):
    model = Supplier
    default_columns = [
        Column("name",
               _(u"Name"),
               sort_field="name",
               display="name",
               filter_config=TextFilter(filter_field="name",
                                        placeholder=_("Filter by name..."))),
        Column("type", _(u"Type")),
        Column("module_identifier",
               _(u"Module"),
               display="get_module_display",
               sortable=True)
    ]

    def get_module_display(self, instance):
        return instance.module.name or _(
            "No %s module") % self.model._meta.verbose_name

    def get_toolbar(self):
        if settings.WSHOP_ENABLE_MULTIPLE_SUPPLIERS:
            return super(SupplierListView, self).get_toolbar()
        else:
            return Toolbar([])
Example #6
0
class PageListView(PicotableListView):
    url_identifier = "simple_cms.page"
    model = Page
    default_columns = [
        Column("title",
               _(u"Title"),
               sort_field="translations__title",
               display="title",
               linked=True,
               filter_config=TextFilter(operator="startswith")),
        Column("available_from", _(u"Available from")),
        Column("available_to", _(u"Available to")),
        Column("created_by", _(u"Created by")),
        Column("created_on", _(u"Date created")),
    ]

    def get_object_abstract(self, instance, item):
        return [{
            "text": "%s" % (instance or _("Page")),
            "class": "header"
        }, {
            "title": _(u"Available from"),
            "text": item.get("available_from")
        }, {
            "title": _(u"Available to"),
            "text": item.get("available_to")
        } if instance.available_to else None]

    def get_queryset(self):
        return super(PageListView,
                     self).get_queryset().for_shop(get_shop(self.request))
Example #7
0
def test_picotable_correctly_sorts_translated_fields(rf, admin_user,
                                                     regular_user):
    """
    Make sure that translated fields, such as product names, are correctly sorted
    """
    populate_if_required()

    columns = [
        Column("id", "Id", filter_config=Filter(), display=instance_id),
        Column("name",
               "Name",
               sort_field="translations__name",
               filter_config=TextFilter(filter_field="translations__name")),
    ]

    pico = get_pico(rf, model=Product, columns=columns)

    # Verify ascending sort
    sorted_products = pico.get_data({
        "perPage": 100,
        "page": 1,
        "sort": "+name"
    })
    sorted_names = [p["name"] for p in sorted_products["items"]]
    assert sorted_names == sorted(sorted_names)

    # Verify descending sort
    sorted_products = pico.get_data({
        "perPage": 100,
        "page": 1,
        "sort": "-name"
    })
    sorted_names = [p["name"] for p in sorted_products["items"]]
    assert sorted_names == sorted(sorted_names, reverse=True)
Example #8
0
class UnitListView(PicotableListView):
    default_columns = [
        Column("name", _(u"Name"), sort_field="translations__name", display="name", filter_config=TextFilter(
            filter_field="translations__name",
            placeholder=_("Filter by name...")
        )),
        Column("symbol", _(u"Symbol"), sort_field="translations__symbol", display="symbol"),
        Column("decimals", _(u"Allowed decimals")),
    ]

    def get_queryset(self):
        return self.model.objects.all()
Example #9
0
class ScriptListView(PicotableListView):
    template_name = "notify/admin/list_script.jinja"

    model = Script
    default_columns = [
        Column("name",
               _("Name"),
               linked=True,
               filter_config=TextFilter(operator="startswith")),
        Column("event_identifier",
               _("Event"),
               display="get_event_identifier_text"),
        Column("enabled", _("Enabled"), filter_config=true_or_false_filter),
    ]

    def get_object_url(self, instance):
        return reverse("wshop_admin:notify.script.edit",
                       kwargs={"pk": instance.pk})

    def get_event_identifier_text(self, instance):
        if not hasattr(self, "_event_identifier_names"):
            self._event_identifier_names = dict(get_name_map("notify_event"))
        return self._event_identifier_names.get(instance.event_identifier,
                                                instance.event_identifier)

    def get_toolbar(self):
        return Toolbar([
            URLActionButton(text=_("New Script"),
                            icon="fa fa-plus",
                            extra_css_class="btn-success",
                            url=reverse("wshop_admin:notify.script.new")),
            JavaScriptActionButton(text=_("New From Template"),
                                   icon="fa fa-book",
                                   extra_css_class="btn-info",
                                   onclick="showScriptTemplates()")
        ])

    def get_object_abstract(self, instance, item):
        return [{
            "text": "%s" % instance,
            "class": "header"
        }, {
            "title": _("Event"),
            "text": item.get("event_identifier")
        }, {
            "title": _("Enabled"),
            "text": item.get("enabled")
        }]

    def get_queryset(self):
        return super(ScriptListView,
                     self).get_queryset().filter(shop=get_shop(self.request))
Example #10
0
class ProductTypeListView(PicotableListView):
    model = ProductType
    default_columns = [
        Column("name",
               _(u"Name"),
               sort_field="translations__name",
               display="name",
               filter_config=TextFilter(filter_field="translations__name",
                                        placeholder=_("Filter by name..."))),
        Column("n_attributes", _(u"Number of Attributes")),
    ]

    def get_queryset(self):
        return ProductType.objects.all().annotate(
            n_attributes=Count("attributes"))
Example #11
0
class CategoryListView(PicotableListView):
    model = Category
    default_columns = [
        Column("image", _("Image"), sortable=False, linked=True, raw=True),
        Column("name",
               _(u"Name"),
               sortable=False,
               display="format_name",
               linked=True,
               filter_config=MPTTFilter(choices="get_name_filter_choices",
                                        filter_field="id")),
        Column("status",
               _(u"Status"),
               filter_config=ChoicesFilter(
                   choices=CategoryStatus.choices,
                   default=CategoryStatus.VISIBLE.value)),
        Column(
            "visibility",
            _(u"Visibility"),
            filter_config=ChoicesFilter(choices=CategoryVisibility.choices)),
    ]

    def get_name_filter_choices(self):
        choices = []
        shop = self.request.shop
        for c in Category.objects.all_except_deleted(shop=shop):
            name = self.format_name(c)
            choices.append((c.pk, name))
        return choices

    def get_queryset(self):
        return Category.objects.all_except_deleted(shop=self.request.shop)

    def format_name(self, instance, *args, **kwargs):
        level = getattr(instance, instance._mptt_meta.level_attr)
        return ('---' * level) + ' ' + instance.name

    def get_object_abstract(self, instance, item):
        return [
            {
                "text": "%s" % instance,
                "class": "header"
            },
            {
                "title": _(u"Status"),
                "text": item.get("status")
            },
        ]
Example #12
0
    def _get_translated_column(self, model, field, known_names, identifier):
        field_name = field.verbose_name.title()
        if field_name in known_names:
            field_name = "%s %s" % (model.__name__, field_name)

        # take the first extension, usually we should not have more then one
        translation_rel_name = model._parler_meta._extensions[0].rel_name

        if model != self.model:
            filter_field = "%s__%s__%s" % (
                identifier, translation_rel_name,
                field.name) if identifier else field.name
        else:
            filter_field = "%s__%s" % (translation_rel_name, field.name)

        display = "%s__%s" % (identifier,
                              field.name) if identifier else field.name

        column = Column("%s_%s" % (model.__name__.lower(), field.name),
                        field_name,
                        sort_field=display,
                        display=display,
                        filter_config=TextFilter(filter_field=filter_field,
                                                 placeholder=field_name))
        return self.handle_special_column(field, column)[0]
Example #13
0
class TaxRuleListView(PicotableListView):
    url_identifier = "default_tax.tax_rule"
    model = TaxRule

    default_columns = [
        Column("id", _("Tax Rule")),
        Column("tax", _("Tax")),
        Column("tax_classes", _("Tax Classes")),
        Column("customer_tax_groups", _("Customer Tax Groups")),
        Column("country_codes_pattern", _("Countries")),
        Column("region_codes_pattern", _("Regions")),
        Column("postal_codes_pattern", _("Postal Codes")),
        Column("priority", _(u"Priority")),
        Column("override_group", _(u"Override Group")),
        Column("enabled", _(u"Enabled")),
    ]
Example #14
0
def test_mptt_filter(rf):
    parent_category = Category.objects.create(name="parent")
    child_category = Category.objects.create(name="child")
    parent_category.children.add(child_category)
    columns = [
        Column("name",
               "name",
               filter_config=MPTTFilter(choices=Category.objects.all(),
                                        filter_field="id"))
    ]
    pico = get_pico(rf, model=Category, columns=columns)
    data = pico.get_data({
        "perPage": 100,
        "page": 1,
        "filters": {
            "id": parent_category.id
        }
    })
    assert len(data["items"]) == 2

    data = pico.get_data({
        "perPage": 100,
        "page": 1,
        "filters": {
            "name": child_category.id
        }
    })
    assert len(data["items"]) == 1
Example #15
0
class CouponListView(PicotableListView):
    model = Coupon
    default_columns = [
        Column("code",
               _(u"Code"),
               sort_field="code",
               display="code",
               linked=True,
               filter_config=TextFilter(operator="startswith")),
        Column("usages", _("Usages"), display="get_usages"),
        Column("usage_limit_customer", _("Usages Limit per contact")),
        Column("usage_limit", _("Usage Limit")),
        Column("active", _("Active")),
        Column("created_by", _(u"Created by")),
        Column("created_on", _(u"Date created")),
    ]

    def get_usages(self, instance, *args, **kwargs):
        return instance.usages.count()

    def get_context_data(self, **kwargs):
        context = super(CouponListView, self).get_context_data(**kwargs)
        if self.request.user.is_superuser:
            settings_button = SettingsActionButton.for_model(
                self.model, return_url="coupon")
        else:
            settings_button = None
        context["toolbar"] = Toolbar([
            NewActionButton("wshop_admin:coupon.new",
                            text=_("Create new Coupon")), settings_button
        ])
        return context
Example #16
0
class TaxListView(PicotableListView):
    model = Tax

    default_columns = [
        Column(
            "name",
            _("Name"),
            sort_field="translations__name",
            filter_config=TextFilter(
                filter_field="translations__name",
                placeholder=_("Filter by name..."),
            ),
        ),
        Column("code", _(u"Code")),
        Column("rate", _("Rate"), display=_format_rate),
        # Column("amount", _(u"Amount")),
        Column("enabled", _(u"Enabled"), filter_config=true_or_false_filter),
    ]
Example #17
0
class CampaignListView(PicotableListView):
    default_columns = [
        Column("name",
               _(u"Title"),
               sort_field="name",
               display="name",
               linked=True,
               filter_config=TextFilter(operator="startswith")),
        Column("start_datetime", _("Starts")),
        Column("end_datetime", _("Ends")),
        Column("active",
               _("Active"),
               filter_config=ChoicesFilter(choices=[(0,
                                                     _("No")), (1,
                                                                _("Yes"))])),
    ]

    def get_queryset(self):
        shop = self.request.shop
        return self.model.objects.filter(shop=shop)

    def start_datetime(self, instance, *args, **kwargs):
        if not instance.start_datetime:
            return ""
        return self._formatted_datetime(instance.start_datetime)

    def end_datetime(self, instance, *args, **kwargs):
        if not instance.end_datetime:
            return ""
        return self._formatted_datetime(instance.end_datetime)

    def _formatted_datetime(self, dt):
        return format_datetime(localtime(dt),
                               locale=get_current_babel_locale())

    def get_object_abstract(self, instance, item):
        return [
            {
                "text": "%s" % (instance or _("CatalogCampaign")),
                "class": "header"
            },
        ]
Example #18
0
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
Example #19
0
class OrderStatusListView(PicotableListView):
    model = OrderStatus
    default_columns = [
        Column("identifier",
               _("Identifier"),
               linked=True,
               filter_config=TextFilter(operator="startswith")),
        Column("name",
               _("Name"),
               linked=True,
               filter_config=TextFilter(operator="startswith")),
        Column("public_name",
               _("Public Name"),
               linked=False,
               filter_config=TextFilter(operator="startswith")),
        Column("role",
               _("Role"),
               linked=False,
               filter_config=ChoicesFilter(choices=OrderStatusRole.choices)),
        Column("default",
               _("Default"),
               linked=False,
               filter_config=ChoicesFilter([(False, _("yes")),
                                            (True, _("no"))])),
        Column("is_active",
               _("Active"),
               linked=False,
               filter_config=ChoicesFilter([(False, _("yes")),
                                            (True, _("no"))])),
    ]
Example #20
0
def get_pico(rf, model=None, columns=None):
    get_default_shop()
    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())
    ]

    request = apply_request_middleware(rf.get("/"))
    return Picotable(request=request,
                     columns=columns,
                     mass_actions=[],
                     queryset=model.objects.all(),
                     context=PicoContext(request))
Example #21
0
class CarouselListView(PicotableListView):
    model = Carousel
    default_columns = [
        Column("name",
               _("Name"),
               sort_field="name",
               display="name",
               filter_config=TextFilter(filter_field="name",
                                        placeholder=_("Filter by name..."))),
    ]

    def get_queryset(self):
        return super(CarouselListView,
                     self).get_queryset().filter(shops=get_shop(self.request))
Example #22
0
class TaxClassListView(PicotableListView):
    model = TaxClass

    default_columns = [
        Column(
            "name",
            _("Name"),
            sort_field="translations__name",
            filter_config=TextFilter(
                filter_field="translations__name",
                placeholder=_("Filter by name..."),
            ),
        ),
    ]
Example #23
0
class ServiceProviderListView(PicotableListView):
    model = ServiceProvider
    default_columns = [
        Column("name",
               _("Name"),
               sort_field="base_translations__name",
               filter_config=TextFilter(filter_field="base_translations__name",
                                        placeholder=_("Filter by name..."))),
        Column("type", _(u"Type"), display="get_type_display", sortable=False),
    ]

    def get_type_display(self, instance):
        return instance._meta.verbose_name.capitalize()

    def get_object_abstract(self, instance, item):
        return [
            {
                "text": "%s" % instance,
                "class": "header"
            },
            {
                "text": self.get_type_display(instance)
            },
        ]
Example #24
0
class ServiceListView(PicotableListView):
    model = None  # Override in subclass
    columns = []
    base_columns = [
        Column("name",
               _("Name"),
               sort_field="translations__name",
               filter_config=TextFilter(filter_field="translations__name",
                                        placeholder=_("Filter by name..."))),
        Column(
            "choice_identifier",
            _(u"Service choice"),
            display="format_service_choice",
            sortable=False,
        ),
        Column("enabled", _(u"Enabled"), filter_config=true_or_false_filter),
        Column("shop", _(u"Shop"))
    ]

    def get_object_abstract(self, instance, item):
        return [
            {
                "text": "%s" % instance,
                "class": "header"
            },
        ]

    def format_service_choice(self, instance, *args, **kwargs):
        if instance.provider:
            for choice in instance.provider.get_service_choices():
                if choice.identifier == instance.choice_identifier:
                    return force_text(choice.name)

    def get_queryset(self):
        return super(ServiceListView,
                     self).get_queryset().filter(shop=self.request.shop)
Example #25
0
    def _get_column(self, model, field, known_names, identifier):
        if not self._valid_field(field.name):
            return None

        field_name = field.verbose_name.title()
        if field_name in known_names:
            field_name = "%s %s" % (model.__name__, field_name)

        display = "%s__%s" % (identifier,
                              field.name) if identifier else field.name

        column = Column("%s_%s" % (model.__name__.lower(), field.name),
                        field_name,
                        display=display)

        column, is_special = self.handle_special_column(field, column)
        if not is_special:
            if isinstance(field, CharField):
                column.filter_config = TextFilter(placeholder=field_name)
            if isinstance(field, EnumIntegerField):
                column.filter_config = ChoicesFilter(field.choices)
            if isinstance(field, BooleanField):
                column.filter_config = true_or_false_filter
        return column
Example #26
0
class UserListView(PicotableListView):
    model = settings.AUTH_USER_MODEL
    default_columns = [
        Column("username", _(u"Username"), filter_config=TextFilter()),
        Column("email", _(u"Email"), filter_config=TextFilter()),
        Column("first_name", _(u"First Name"), filter_config=TextFilter()),
        Column("last_name", _(u"Last Name"), filter_config=TextFilter()),
        Column("is_active",
               _(u"Active"),
               filter_config=ChoicesFilter([(False, _("no")),
                                            (True, _("yes"))],
                                           default=True)),
        Column("is_staff", _(u"Staff"), filter_config=true_or_false_filter),
        Column("is_superuser",
               _(u"Superuser"),
               filter_config=true_or_false_filter),
    ]

    def get_model(self):
        return get_user_model()

    def get_queryset(self):
        model = self.get_model()
        qs = self.get_model().objects.all()
        if "date_joined" in model._meta.get_all_field_names():
            qs = qs.order_by("-date_joined")
        return qs

    def get_context_data(self, **kwargs):
        context = super(UserListView, self).get_context_data(**kwargs)
        context["title"] = force_text(
            self.get_model()._meta.verbose_name_plural).title()
        return context

    def get_object_abstract(self, instance, item):
        bits = filter(None, [
            _("First Name: %s") %
            (getattr(instance, 'first_name', None) or "\u2014"),
            _("Last Name: %s") %
            (getattr(instance, 'last_name', None) or "\u2014"),
            _("Active") if instance.is_active else _(u"Inactive"),
            _("Email: %s") % (getattr(instance, 'email', None) or "\u2014"),
            _("Staff") if getattr(instance, 'is_staff', None) else None,
            _("Superuser") if getattr(instance, 'is_superuser', None) else None
        ])
        return [{
            "text": instance.get_username() or _("User"),
            "class": "header"
        }, {
            "text": ", ".join([force_text(bit) for bit in bits])
        }]
Example #27
0
class PermissionGroupListView(PicotableListView):
    model = PermissionGroup
    default_columns = [
        Column(
            "name",
            _(u"Name"),
            sort_field="name",
            display="name",
            filter_config=TextFilter(filter_field="name", placeholder=_("Filter by name..."))
        ),
    ]

    def get_context_data(self, **kwargs):
        context = super(PermissionGroupListView, self).get_context_data(**kwargs)
        context["title"] = _("Permission Groups")
        if self.request.user.is_superuser:
            settings_button = SettingsActionButton.for_model(self.model, return_url="permission_group")
        else:
            settings_button = None
        context["toolbar"] = Toolbar([
            NewActionButton("wshop_admin:permission_group.new", text=_("Create new Permission Group")),
            settings_button
        ])
        return context
Example #28
0
class CartListView(PicotableListView):
    model = StoredBasket
    default_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, "persistent": False}
        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):
        if not instance.taxful_total_price:
            return ""
        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.get("created_on")},
            {"title": _(u"Last updated on"), "text": item.get("updated_on")},
            {"title": _(u"Ordered"), "text": item.get("finished")},
            {"title": _(u"Total"), "text": item.get("taxful_total_price")},
        ]
Example #29
0
class StocksListView(PicotableListView):
    template_name = "wshop/simple_supplier/admin/base_picotable.jinja"
    model = Product
    default_columns = [
        Column("sku",
               _("SKU"),
               sort_field="product__sku",
               display="product__sku",
               linked=True,
               filter_config=TextFilter(filter_field="product__sku",
                                        placeholder=_("Filter by SKU..."))),
        Column("name",
               _("Name"),
               sort_field="product__translations__name",
               display="product__name",
               linked=True,
               filter_config=TextFilter(
                   filter_field="product__translations__name",
                   placeholder=_("Filter by name..."))),
        Column(
            "supplier",
            _("Supplier"),
            display="supplier",
            linked=False,
            filter_config=ChoicesFilter(
                Supplier.objects.filter(module_identifier="simple_supplier"))),
        Column("stock_information",
               _("Stock information"),
               display="get_stock_information",
               linked=False,
               sortable=False,
               raw=True),
        Column("adjust_stock",
               _("Adjust stock"),
               display="get_stock_adjustment_form",
               sortable=False,
               linked=False,
               raw=True)
    ]

    def __init__(self):
        super(StocksListView, self).__init__()
        self.columns = self.default_columns

    def get_object_abstract(self, instance, item):
        item.update({
            "_linked_in_mobile": False,
            "_url": self.get_object_url(instance.product)
        })
        return [
            {
                "text": item.get("name"),
                "class": "header"
            },
            {
                "title": "",
                "text": item.get("sku")
            },
            {
                "title": "",
                "text": " ",
                "raw": item.get("stock_information")
            },
            {
                "title": "",
                "text": " ",
                "raw": item.get("adjust_stock")
            },
        ]

    def get_queryset(self):
        return StockCount.objects.filter(
            supplier__module_identifier="simple_supplier",
            product__stock_behavior=StockBehavior.STOCKED,
            product__deleted=False).order_by("product__id")

    def get_context_data(self, **kwargs):
        context = super(PicotableListView, self).get_context_data(**kwargs)
        context["toolbar"] = None
        context["title"] = _("Stock management")
        return context

    def get_stock_information(self, instance):
        return get_stock_information_html(instance.supplier, instance.product)

    def get_stock_adjustment_form(self, instance):
        return get_stock_adjustment_div(self.request, instance.supplier,
                                        instance.product)
Example #30
0
class OrderListView(PicotableListView):
    model = Order
    default_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"),
               display="format_customer_name",
               filter_config=MultiFieldTextFilter(
                   filter_fields=("customer__email", "customer__name",
                                  "billing_address__name",
                                  "shipping_address__name", "orderer__name"))),
        Column(
            "status",
            _(u"Status"),
            filter_config=OrderStatusChoicesFilter(
                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_value",
               _(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")),
    ]
    mass_actions = [
        "wshop.admin.modules.orders.mass_actions:CancelOrderAction",
        "wshop.admin.modules.orders.mass_actions:OrderConfirmationPdfAction",
        "wshop.admin.modules.orders.mass_actions:OrderDeliveryPdfAction",
    ]

    def get_toolbar(self):
        return Toolbar([
            NewActionButton.for_model(Order,
                                      url=reverse("wshop_admin:order.new")),
            SettingsActionButton.for_model(Order, return_url="order")
        ])

    def get_queryset(self):
        return super(OrderListView, self).get_queryset().exclude(
            deleted=True).filter(shop=self.request.shop)

    def format_customer_name(self, instance, *args, **kwargs):
        return instance.get_customer_name() or ""

    def format_order_date(self, instance, *args, **kwargs):
        return get_locally_formatted_datetime(instance.order_date)

    def format_taxful_total_price(self, instance, *args, **kwargs):
        return escape(format_money(instance.taxful_total_price))

    def label(self, instance, *args, **kwargs):
        # format label to make it human readable
        return instance.label.replace("_", " ").title()

    def get_object_abstract(self, instance, item):
        return [{
            "text": "%s" % instance,
            "class": "header"
        }, {
            "title": _(u"Total"),
            "text": item.get("taxful_total_price_value")
        }, {
            "title": _(u"Status"),
            "text": item.get("status")
        }]