Ejemplo n.º 1
0
class UserListView(PicotableListView):
    model = settings.AUTH_USER_MODEL
    default_columns = [
        Column("username", _("Username"), filter_config=TextFilter()),
        Column("email", _("Email"), filter_config=TextFilter()),
        Column("first_name", _("First Name"), filter_config=TextFilter()),
        Column("last_name", _("Last Name"), filter_config=TextFilter()),
        Column(
            "is_active",
            _("Active"),
            filter_config=ChoicesFilter([(False, _("no")), (True, _("yes"))],
                                        default=True),
        ),
        Column("groups",
               _("Groups"),
               filter_config=Select2Filter("get_groups"),
               display="get_groups_display"),
        Column("is_staff",
               _("Access to Admin Panel"),
               filter_config=true_or_false_filter),
    ]
    toolbar_buttons_provider_key = "user_list_toolbar_provider"
    mass_actions_provider_key = "user_list_mass_actions_provider"

    def get_groups(self):
        return list(Group.objects.all().values_list("id", "name"))

    def get_groups_display(self, instance):
        groups = [group.name for group in instance.groups.all()]
        return ", ".join(groups) if groups else _("No group")

    def get_model(self):
        return get_user_model()

    def get_queryset(self):
        model = self.get_model()

        qs = self.get_model().objects.all().prefetch_related("groups")
        if "date_joined" in [f.name for f in model._meta.get_fields()]:
            qs = qs.order_by("-date_joined")

        # non superusers can't see superusers
        if not self.request.user.is_superuser:
            qs = qs.filter(is_superuser=False)

        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):
        return [
            {
                "text": instance.get_username() or _("User"),
                "class": "header"
            },
            {
                "title": _("Email"),
                "text": item.get("email")
            },
            {
                "title": _("First Name"),
                "text": item.get("first_name")
            },
            {
                "title": _("Last Name"),
                "text": item.get("last_name")
            },
            {
                "title": _("Active"),
                "text": item.get("is_active")
            },
            {
                "title": _("Groups"),
                "text": item.get("groups")
            },
            {
                "title": _("Access to Admin Panel"),
                "text": item.get("is_staff")
            },
        ]
Ejemplo n.º 2
0
class ContactListView(PicotableListView):
    model = Contact
    default_columns = [
        Column("name", _(u"Name"), linked=True, filter_config=TextFilter()),
        Column("type", _(u"Type"), display="get_type_display", sortable=False, filter_config=ContactTypeFilter()),
        Column("email", _(u"Email"), filter_config=TextFilter()),
        Column("phone", _(u"Phone"), filter_config=TextFilter()),
        Column(
            "is_active",
            _(u"Active"),
            filter_config=ChoicesFilter([(False, _("no")), (True, _("yes"))], default=True)
        ),
        Column("n_orders", _(u"# Orders"), class_name="text-right", filter_config=RangeFilter(step=1)),
        Column(
            "groups",
            _("Groups"),
            filter_config=Select2Filter("get_groups"),
            display="get_groups_display"
        ),
        Column("shops", _("Shops"), filter_config=Select2Filter("get_shops"), display="get_shops_display"),
        Column("registration_shop", _("Registered in"), filter_config=Select2Filter("get_shops"))
    ]

    mass_actions = [
        "shuup.admin.modules.contacts.mass_actions:EditContactsAction",
        "shuup.admin.modules.contacts.mass_actions:EditContactGroupsAction",
    ]
    toolbar_buttons_provider_key = "contact_list_toolbar_provider"
    mass_actions_provider_key = "contact_list_mass_actions_provider"

    def __init__(self):
        super(ContactListView, self).__init__()
        picture_column = [column for column in self.columns if column.id == "contact_picture"]
        if picture_column:
            picture_column[0].raw = True

    def get_groups(self):
        return list(
            ContactGroup.objects.translated().all_except_defaults().values_list("id", "translations__name")
        )

    def get_shops(self):
        return list(
            Shop.objects.get_for_user(self.request.user).translated().values_list("id", "translations__public_name")
        )

    def get_toolbar(self):
        if self.request.user.is_superuser:
            settings_button = SettingsActionButton.for_model(Contact, return_url="contact")
        else:
            settings_button = None
        return Toolbar([
            NewActionButton.for_model(
                PersonContact, url=reverse("shuup_admin:contact.new") + "?type=person"
            ),
            NewActionButton.for_model(
                CompanyContact, extra_css_class="btn-info", url=reverse("shuup_admin:contact.new") + "?type=company"
            ),
            settings_button
        ], view=self)

    def get_queryset(self):
        qs = super(ContactListView, self).get_queryset()
        groups = self.get_filter().get("groups")
        query = Q(groups__in=groups) if groups else Q()

        # non superusers can't see superusers contacts
        if not self.request.user.is_superuser:
            qs = qs.exclude(PersonContact___user__is_superuser=True)

        if self.request.GET.get("shop"):
            qs = qs.filter(
                shops__in=Shop.objects.get_for_user(self.request.user).filter(pk=self.request.GET["shop"])
            )

        elif request_limited(self.request):
            shop = get_shop(self.request)
            qs = qs.filter(shops=shop)

        return (
            qs
            .filter(query)
            .annotate(n_orders=Count("customer_orders"))
            .order_by("-created_on")
        )

    def get_type_display(self, instance):
        if isinstance(instance, PersonContact):
            return _(u"Person")
        elif isinstance(instance, CompanyContact):
            return _(u"Company")
        else:
            return _(u"Contact")

    def get_groups_display(self, instance):
        groups = [group.name for group in instance.groups.all_except_defaults()]
        return ", ".join(groups) if groups else _("No group")

    def get_shops_display(self, instance):
        user = self.request.user
        shops = [shop.name for shop in instance.shops.get_for_user(user=user)]
        return ", ".join(shops) if shops else _("No shops")

    def get_object_abstract(self, instance, item):
        """
        :type instance: shuup.core.models.Contact
        """
        bits = filter(None, [
            self.get_type_display(instance),
            _("Active") if instance.is_active else _("Inactive"),
            _("Email: %s") % (instance.email or "\u2014"),
            _("Phone: %s") % (instance.phone or "\u2014"),
            _("%d orders") % instance.n_orders,
        ])
        return [
            {"text": instance.name or _("Contact"), "class": "header"},
            {"text": ", ".join([force_text(bit) for bit in bits])}
        ]
Ejemplo n.º 3
0
class ContactListView(PicotableListView):
    model = Contact
    default_columns = [
        Column("name", _(u"Name"), linked=True, filter_config=TextFilter()),
        Column("type",
               _(u"Type"),
               display="get_type_display",
               sortable=False,
               filter_config=ContactTypeFilter()),
        Column("email", _(u"Email"), filter_config=TextFilter()),
        Column("phone", _(u"Phone"), filter_config=TextFilter()),
        Column("is_active",
               _(u"Active"),
               filter_config=ChoicesFilter([(False, _("no")),
                                            (True, _("yes"))],
                                           default=True)),
        Column("n_orders",
               _(u"# Orders"),
               class_name="text-right",
               filter_config=RangeFilter(step=1)),
        Column("groups",
               _("Groups"),
               filter_config=ChoicesFilter(ContactGroup.objects.all(),
                                           "groups"),
               display="get_groups_display"),
        Column("shops",
               _("Shops"),
               filter_config=Select2Filter(Shop.objects.all()),
               display="get_shop_display")
    ]

    mass_actions = [
        "shuup.admin.modules.contacts.mass_actions:EditContactsAction",
        "shuup.admin.modules.contacts.mass_actions:EditContactGroupsAction",
    ]

    def get_toolbar(self):
        if self.request.user.is_superuser:
            settings_button = SettingsActionButton.for_model(
                Contact, return_url="contact")
        else:
            settings_button = None
        return Toolbar([
            NewActionButton.for_model(PersonContact,
                                      url=reverse("shuup_admin:contact.new") +
                                      "?type=person"),
            NewActionButton.for_model(CompanyContact,
                                      extra_css_class="btn-info",
                                      url=reverse("shuup_admin:contact.new") +
                                      "?type=company"), settings_button
        ])

    def get_queryset(self):
        qs = super(ContactListView, self).get_queryset()
        groups = self.get_filter().get("groups")
        query = Q(groups__in=groups) if groups else Q()

        limited = (settings.SHUUP_ENABLE_MULTIPLE_SHOPS
                   and settings.SHUUP_MANAGE_CONTACTS_PER_SHOP
                   and not self.request.user.is_superuser)
        if limited:
            shop = self.request.shop
            qs = qs.filter(shops=shop)

        return (qs.filter(query).annotate(
            n_orders=Count("customer_orders")).order_by("-created_on"))

    def get_type_display(self, instance):
        if isinstance(instance, PersonContact):
            return _(u"Person")
        elif isinstance(instance, CompanyContact):
            return _(u"Company")
        else:
            return _(u"Contact")

    def get_groups_display(self, instance):
        if instance.groups.count():
            return ", ".join(
                instance.groups.values_list("translations__name", flat=True))
        return _("No group")

    def get_shop_display(self, instance):
        if instance.shops.count():
            return ", ".join(
                instance.shops.values_list("translations__name", flat=True))
        return _("No shop")

    def get_object_abstract(self, instance, item):
        """
        :type instance: shuup.core.models.Contact
        """
        bits = filter(None, [
            item.get("type"),
            _("Active") if instance.is_active else _("Inactive"),
            _("Email: %s") % (instance.email or "\u2014"),
            _("Phone: %s") % (instance.phone or "\u2014"),
            _("%d orders") % instance.n_orders,
        ])
        return [{
            "text": instance.name or _("Contact"),
            "class": "header"
        }, {
            "text": ", ".join([force_text(bit) for bit in bits])
        }]