Example #1
0
def get_menu_entry_categories(request):
    menu_categories = OrderedDict()
    menu_category_icons = {}
    for identifier, category_name, icon in MENU_CATEGORIES:
        menu_categories[identifier] = _MenuCategory(category_name, icon)
        menu_category_icons[identifier] = icon

    modules = list(get_modules())
    for module in modules:
        menu_category_icons.update(
            (force_text(key), force_text(value))
            for (key, value) in module.get_menu_category_icons().items()
            if key not in menu_category_icons
        )

    for module in modules:
        if get_missing_permissions(request.user, module.get_required_permissions()):
            continue
        for entry in (module.get_menu_entries(request=request) or ()):
            category_identifier = entry.category
            category = menu_categories.get(category_identifier) if category_identifier else None
            if not category:
                category_identifier = force_text(category_identifier or module.name)
                category = menu_categories.get(category_identifier)
                if not category:
                    menu_categories[category_identifier] = category = _MenuCategory(
                        name=category_identifier,
                        icon=menu_category_icons.get(category_identifier, "fa fa-circle")
                    )
            category.entries.append(entry)
    return [c for identifier, c in six.iteritems(menu_categories) if len(c.entries) > 0]
Example #2
0
def test_dashboard_blocks(rf):
    request = rf.get("/")
    with replace_modules([ATestModule]):
        block_ids = set()
        for block in chain(*(m.get_dashboard_blocks(request) for m in get_modules())):
            block_ids.add(block.id)
        assert block_ids >= set(["test-0", "test-1", "test-2", "test-3", "test-4"])
Example #3
0
def test_dashboard_blocks(rf):
    request = rf.get("/")
    with replace_modules([ATestModule]):
        block_ids = set()
        for block in chain(*(m.get_dashboard_blocks(request) for m in get_modules())):
            block_ids.add(block.id)
        assert block_ids >= set(["test-0", "test-1", "test-2", "test-3", "test-4"])
Example #4
0
def test_permissions_for_menu_entries(rf, admin_user):
    permissions = set(
        ["shuup.add_product", "shuup.delete_product", "shuup.change_product"])

    request = rf.get("/")
    request.user = StaffUser()
    request.user.permissions = permissions

    with replace_modules([ARestrictedTestModule]):
        modules = [m for m in get_modules()]
        assert request.user.permissions == modules[0].get_required_permissions(
        )

        categories = get_menu_entry_categories(request)
        assert categories

        # Make sure category is displayed if user has correct permissions
        test_category_menu_entries = [
            cat for cat in categories if cat.name == "RestrictedTest"
        ][0]
        assert any(me.text == "OK" for me in test_category_menu_entries)

        # No menu items should be displayed if user has no permissions
        request.user.permissions = []
        categories = get_menu_entry_categories(request)
        assert not categories
Example #5
0
def get_search_results(request, query):
    fuzzer = FuzzyMatcher(query)
    normal_results = []
    menu_entry_results = []
    for module in get_modules():
        if get_missing_permissions(request.user, module.get_required_permissions()):
            continue

        normal_results.extend(module.get_search_results(request, query) or ())
        for menu_entry in module.get_menu_entries(request) or ():
            texts = (menu_entry.get_search_query_texts() or ())
            if any(fuzzer.test(text) for text in texts):
                menu_entry_results.append(SearchResult(
                    text=menu_entry.text,
                    url=menu_entry.url,
                    icon=menu_entry.icon,
                    category=menu_entry.category,
                    relevance=90,
                    is_action=True
                ))
    results = sorted(
        chain(normal_results, menu_entry_results),
        key=lambda r: r.relevance,
        reverse=True
    )
    return results
def test_permission_group_form_updates_members():
    with replace_modules([ARestrictedTestModule]):
        modules = [m for m in get_modules()]
        test_module = modules[0]
        module_permissions = test_module.get_required_permissions()

        assert module_permissions

        group = get_default_permission_group()
        PermissionGroupForm(instance=group, prefix=None)

        assert not group.permissions.all()
        assert not group.user_set.all()

        data = {
            "name": "New Name",
        }
        for permission in ARestrictedTestModule().get_required_permissions():
            data["perm:%s" % permission] = permission

        form = PermissionGroupForm(instance=group, prefix=None, data=data)
        form.save()

        assert group.name == "New Name"
        assert set(module_permissions) == get_permissions_from_group(group)

        form = PermissionGroupForm(instance=group,
                                   prefix=None,
                                   data={"name": "Name"})
        form.save()

        assert not group.permissions.all()
        assert not group.user_set.all()
Example #7
0
def test_permission_group_form_updates_members(regular_user):
    with replace_modules([ARestrictedTestModule]):
        modules = [m for m in get_modules()]
        test_module = modules[0]
        module_permissions = test_module.get_required_permissions()

        assert module_permissions

        group = get_default_permission_group()
        form = PermissionGroupForm(instance=group, prefix=None)

        assert not group.permissions.all()
        assert not group.user_set.all()

        data = {
            "name": "New Name",
            "modules": [force_text(test_module.name)],
            "members": [force_text(regular_user.pk)],
        }

        form = PermissionGroupForm(instance=group, prefix=None, data=data)
        form.save()

        module_permissions = [get_permission_object_from_string(m) for m in module_permissions]
        assert group.name == "New Name"
        assert set(module_permissions) == set(group.permissions.all())
        assert regular_user in group.user_set.all()

        form = PermissionGroupForm(instance=group, prefix=None, data={"name": "Name"})
        form.save()

        assert not group.permissions.all()
        assert not group.user_set.all()
Example #8
0
def get_search_results(request, query):
    fuzzer = FuzzyMatcher(query)
    normal_results = []
    menu_entry_results = []
    for module in get_modules():
        if get_missing_permissions(request.user, module.get_required_permissions()):
            continue

        normal_results.extend(module.get_search_results(request, query) or ())
        for menu_entry in module.get_menu_entries(request) or ():
            texts = (menu_entry.get_search_query_texts() or ())
            if any(fuzzer.test(text) for text in texts):
                menu_entry_results.append(SearchResult(
                    text=menu_entry.text,
                    url=menu_entry.url,
                    icon=menu_entry.icon,
                    category=menu_entry.category,
                    relevance=90,
                    is_action=True
                ))
    results = sorted(
        chain(normal_results, menu_entry_results),
        key=lambda r: r.relevance,
        reverse=True
    )
    return results
Example #9
0
def get_activity(request, n_entries=30, cutoff_hours=10):
    """
    Get Activity objects from all modules as a list in latest-first order.

    :param request: Request context
    :type request: django.http.request.HttpRequest
    :param n_entries: Number of entries to return in total.
    :type n_entries: int
    :param cutoff_hours: Calculate cutoff datetime so the oldest entry should be at most this old
    :type cutoff_hours: float
    :return: List of Activity objects
    :rtype: list[Activity]
    """

    cutoff_dt = now() - datetime.timedelta(hours=cutoff_hours)
    activities = []
    for module in get_modules():
        for activity in islice(module.get_activity(request, cutoff=cutoff_dt),
                               n_entries):
            heappush(activities,
                     (-time.mktime(activity.datetime.timetuple()), activity))
    out = []
    while activities and len(out) < n_entries:
        out.append(heappop(activities)[1])
    return out
Example #10
0
def get_menu_entry_categories(request):
    menu_categories = {}
    menu_category_icons = {}
    modules = list(get_modules())

    for module in modules:
        menu_category_icons.update(
            (force_text(key), force_text(value))
            for (key, value) in module.get_menu_category_icons().items())

    for module in modules:
        if not get_missing_permissions(request.user,
                                       module.get_required_permissions()):
            for entry in (module.get_menu_entries(request=request) or ()):
                category_name = force_text(entry.category or module.name)
                category = menu_categories.get(category_name)
                if not category:
                    menu_categories[category_name] = category = _MenuCategory(
                        name=category_name,
                        icon=menu_category_icons.get(category_name,
                                                     "fa fa-circle"))
                category.entries.append(entry)

    return SortedDict(
        sorted(
            (c.name, c)
            for c in sorted(menu_categories.values(), key=lambda c: c.name)))
def test_permission_group_form_updates_members(regular_user):
    with replace_modules([ARestrictedTestModule]):
        modules = [m for m in get_modules()]
        test_module = modules[0]
        module_permissions = test_module.get_required_permissions()

        assert module_permissions

        group = get_default_permission_group()
        PermissionGroupForm(instance=group, prefix=None)

        assert not group.permissions.all()
        assert not group.user_set.all()

        data = {
            "name": "New Name",
            "members": [force_text(regular_user.pk)],
        }
        for permission in ARestrictedTestModule().get_required_permissions():
            data["perm:%s" % permission] = permission

        form = PermissionGroupForm(instance=group, prefix=None, data=data)
        form.save()

        assert group.name == "New Name"
        assert set(module_permissions) == get_permissions_from_group(group)
        assert regular_user in group.user_set.all()

        form = PermissionGroupForm(instance=group, prefix=None, data={"name": "Name"})
        form.save()

        assert not group.permissions.all()
        assert not group.user_set.all()
Example #12
0
def get_menu_entry_categories(request):
    menu_categories = OrderedDict()
    menu_children = OrderedDict()

    menu_category_icons = {}
    for menu_item in MAIN_MENU:
        identifier = menu_item["identifier"]
        icon = menu_item["icon"]
        menu_categories[identifier] = _MenuCategory(
            identifier=identifier,
            name=menu_item["title"],
            icon=icon,
        )
        for child in menu_item["children"]:
            child_identifier = "%s:%s" % (identifier, child["identifier"])
            child_category = _MenuCategory(child["identifier"], child["title"], None)
            menu_children[child_identifier] = child_category
            menu_categories[identifier].children.append(child_category)

        menu_category_icons[identifier] = icon

    modules = list(get_modules())
    for module in modules:
        menu_category_icons.update(
            (force_text(key), force_text(value))
            for (key, value) in module.get_menu_category_icons().items()
            if key not in menu_category_icons
        )

    all_categories = set()
    for module in modules:
        if get_missing_permissions(request.user, module.get_required_permissions()):
            continue

        for entry in (module.get_menu_entries(request=request) or ()):
            category_identifier = entry.category
            subcategory = entry.subcategory

            entry_identifier = "%s:%s" % (category_identifier, subcategory) if subcategory else category_identifier
            menu_items = menu_children if subcategory else menu_categories

            category = menu_items.get(entry_identifier)
            if not category:
                category_identifier = force_text(category_identifier or module.name)
                category = menu_items.get(category_identifier)
                if not category:
                    menu_items[category_identifier] = category = _MenuCategory(
                        identifier=category_identifier,
                        name=category_identifier,
                        icon=menu_category_icons.get(category_identifier, "fa fa-circle")
                    )
            category.entries.append(entry)
            if subcategory:
                parent = menu_categories.get(category_identifier)
                all_categories.add(parent)
            else:
                all_categories.add(category)

    return [c for menu_identifier, c in six.iteritems(menu_categories) if c in all_categories]
Example #13
0
def get_quicklinks(request):
    quicklinks = []
    for module in get_modules():
        if get_missing_permissions(request.user,
                                   module.get_required_permissions()):
            continue
        quicklinks.extend(module.get_help_blocks(request, kind="quicklink"))
    return quicklinks
Example #14
0
def get_menu_entry_categories(request):  # noqa (C901)
    menu_categories = OrderedDict()

    # Update main menu from provides
    main_menu = extend_main_menu(MAIN_MENU)

    menu_category_icons = {}
    for menu_item in main_menu:
        identifier = menu_item["identifier"]
        icon = menu_item["icon"]
        menu_categories[identifier] = _MenuCategory(
            identifier=identifier,
            name=menu_item["title"],
            icon=icon,
        )
        menu_category_icons[identifier] = icon

    modules = list(get_modules())
    for module in modules:
        menu_category_icons.update(
            (force_text(key), force_text(value))
            for (key, value) in module.get_menu_category_icons().items()
            if key not in menu_category_icons)

    all_categories = set()
    for module in modules:
        if get_missing_permissions(request.user,
                                   module.get_required_permissions()):
            continue

        for entry in module.get_menu_entries(request=request) or ():
            category = menu_categories.get(entry.category)
            if not category:
                category_identifier = force_text(entry.category or module.name)
                category = menu_categories.get(category_identifier)
                if not category:
                    menu_categories[
                        category_identifier] = category = _MenuCategory(
                            identifier=category_identifier,
                            name=category_identifier,
                            icon=menu_category_icons.get(
                                category_identifier, "fa fa-circle"),
                        )
            category.entries.append(entry)
            all_categories.add(category)

    # clean categories that eventually have no children or entries
    categories = []
    for cat in all_categories:
        if not cat.entries:
            continue
        categories.append(cat)
    clean_categories = [
        c for menu_identifier, c in six.iteritems(menu_categories)
        if c in categories
    ]

    return customize_menu(clean_categories, request)
Example #15
0
File: home.py Project: carles/shuup
    def get_context_data(self, **kwargs):
        context = super(HomeView, self).get_context_data(**kwargs)
        context["blocks"] = blocks = []
        context["tour_key"] = "home"
        context["tour_complete"] = is_tour_complete(get_shop(self.request), "home")
        wizard_complete = setup_wizard_complete(self.request)

        wizard_url = reverse("shuup_admin:wizard")
        wizard_actions = []
        if not wizard_complete:
            wizard_actions.append({
                "text": _("Complete wizard"),
                "url": wizard_url
            })
        else:
            wizard_steps = load_setup_wizard_panes(
                shop=self.request.shop, request=self.request, visible_only=False)
            for step in wizard_steps:
                wizard_actions.append({
                    "text": step.title,
                    "url": "%s?pane_id=%s" % (wizard_url, step.identifier),
                    "no_redirect": True
                })

        blocks.append(
            SimpleHelpBlock(
                _("Complete the setup wizard"),
                actions=wizard_actions,
                icon_url="shuup_admin/img/configure.png",
                priority=-1,
                done=wizard_complete
            )
        )

        for module in get_modules():
            if not get_missing_permissions(self.request.user, module.get_required_permissions()):
                blocks.extend(module.get_help_blocks(request=self.request, kind="setup"))
        blocks.sort(key=lambda b: b.priority)
        blocks.append(
            SimpleHelpBlock(
                priority=1000,
                text=_("Publish your store"),
                description=_("Let customers browse your store and make purchases"),
                css_class="green ",
                actions=[{
                    "method": "POST",
                    "text": _("Publish shop"),
                    "url": reverse("shuup_admin:shop.enable", kwargs={"pk": self.request.shop.pk}),
                    "data": {
                        "enable": True,
                        "redirect": reverse("shuup_admin:dashboard")
                    }
                }],
                icon_url="shuup_admin/img/publish.png",
                done=not self.request.shop.maintenance_mode
            )
        )
        return context
Example #16
0
    def get_context_data(self, **kwargs):
        context = super(HomeView, self).get_context_data(**kwargs)
        context["blocks"] = blocks = []
        context["tour_key"] = "home"
        context["tour_complete"] = is_tour_complete("home")
        wizard_complete = setup_wizard_complete()

        wizard_url = reverse("shuup_admin:wizard")
        wizard_actions = []
        if not wizard_complete:
            wizard_actions.append({
                "text": _("Complete wizard"),
                "url": wizard_url
            })
        else:
            wizard_steps = load_setup_wizard_panes(
                shop=Shop.objects.first(), request=self.request, visible_only=False)
            for step in wizard_steps:
                wizard_actions.append({
                    "text": step.title,
                    "url": "%s?pane_id=%s" % (wizard_url, step.identifier),
                    "no_redirect": True
                })

        blocks.append(
            SimpleHelpBlock(
                _("Complete the setup wizard"),
                actions=wizard_actions,
                icon_url="shuup_admin/img/configure.png",
                priority=-1,
                done=wizard_complete
            )
        )

        for module in get_modules():
            if not get_missing_permissions(self.request.user, module.get_required_permissions()):
                blocks.extend(module.get_help_blocks(request=self.request, kind="setup"))
        blocks.sort(key=lambda b: b.priority)
        blocks.append(
            SimpleHelpBlock(
                priority=1000,
                text=_("Publish your store"),
                description=_("Let customers browse your store and make purchases"),
                css_class="green",
                actions=[{
                    "method": "POST",
                    "text": _("Publish shop"),
                    "url": reverse("shuup_admin:shop.enable", kwargs={"pk": self.request.shop.pk}),
                    "data": {
                        "enable": True,
                        "redirect": reverse("shuup_admin:dashboard")
                    }
                }],
                icon_url="shuup_admin/img/publish.png"
            )
        )
        return context
Example #17
0
def test_valid_permissions_for_all_modules():
    """
    If a module requires permissions, make sure all url and module-
    level permissions are valid.
    """
    for module in get_modules():
        url_permissions = set(get_permissions_from_urls(module.get_urls()))
        module_permissions = set(module.get_required_permissions())
        for permission in (url_permissions | module_permissions):
            assert get_permission_object_from_string(permission)
Example #18
0
 def _get_enabled_modules(self, permissions):
     if not self.instance.pk:
         return []
     permissions = set(permissions)
     modules = []
     for module in get_modules():
         # Ignore modules that haven't configured a name
         if module.name != "_Base_" and set(module.get_required_permissions()).issubset(permissions):
             modules.append(force_text(module.name))
     return modules
Example #19
0
 def get_context_data(self, **kwargs):
     context = super(DashboardView, self).get_context_data(**kwargs)
     context["version"] = shuup.__version__
     context["notifications"] = notifications = []
     context["blocks"] = blocks = []
     for module in get_modules():
         notifications.extend(module.get_notifications(request=self.request))
         blocks.extend(module.get_dashboard_blocks(request=self.request))
     context["activity"] = get_activity(request=self.request)
     return context
Example #20
0
def test_valid_permissions_for_all_modules():
    """
    If a module requires permissions, make sure all url and module-
    level permissions are valid.
    """
    for module in get_modules():
        url_permissions = set(get_permissions_from_urls(module.get_urls()))
        module_permissions = set(module.get_required_permissions())
        for permission in (url_permissions | module_permissions):
            assert get_permission_object_from_string(permission)
Example #21
0
 def _get_enabled_modules(self, permissions):
     if not self.instance.pk:
         return []
     permissions = set(permissions)
     modules = []
     for module in get_modules():
         # Ignore modules that haven't configured a name
         if module.name != "_Base_" and set(module.get_required_permissions()).issubset(permissions):
             modules.append(force_text(module.name))
     return modules
Example #22
0
    def get_context_data(self, **kwargs):
        context = super(HomeView, self).get_context_data(**kwargs)
        shop = get_shop(self.request)
        context["blocks"] = blocks = []
        context["tour_key"] = "home"
        context["tour_complete"] = is_tour_complete(shop,
                                                    "home",
                                                    user=self.request.user)

        wizard_complete = setup_wizard_complete(self.request)
        wizard_url = reverse("shuup_admin:wizard")
        wizard_actions = []
        if not wizard_complete:
            wizard_actions.append({
                "text": _("Complete wizard"),
                "url": wizard_url
            })
        else:
            wizard_steps = load_setup_wizard_panes(shop=shop,
                                                   request=self.request,
                                                   visible_only=False)
            for step in wizard_steps:
                wizard_actions.append({
                    "text":
                    step.title,
                    "url":
                    "%s?pane_id=%s" % (wizard_url, step.identifier),
                    "no_redirect":
                    True
                })

        if wizard_actions:
            blocks.append(
                SimpleHelpBlock(_("Complete the setup wizard"),
                                actions=wizard_actions,
                                icon_url="shuup_admin/img/configure.png",
                                priority=-1,
                                done=wizard_complete))

        for module in get_modules():
            if not get_missing_permissions(self.request.user,
                                           module.get_required_permissions()):
                blocks.extend(
                    module.get_help_blocks(request=self.request, kind="setup"))
        blocks.sort(key=lambda b: b.priority)

        if not blocks:
            blocks.append(
                SimpleHelpBlock(_("All set. Nothing to be configured"),
                                actions=[],
                                icon_url="shuup_admin/img/configure.png",
                                priority=-1,
                                done=True))

        return context
Example #23
0
def setup_blocks_complete(request):
    """
    Check if any incomplete setup blocks remain.

    :return: whether all setup blocks are complete
    :rtype: Boolean
    """
    for module in get_modules():
        if len([block for block in module.get_help_blocks(request=request, kind="setup") if not block.done]) > 0:
            return False
    return True
Example #24
0
def setup_blocks_complete(request):
    """
    Check if any incomplete setup blocks remain.

    :return: whether all setup blocks are complete
    :rtype: Boolean
    """
    for module in get_modules():
        if len([block for block in module.get_help_blocks(request=request, kind="setup") if not block.done]) > 0:
            return False
    return True
Example #25
0
    def get_context_data(self, **kwargs):
        context = super(HomeView, self).get_context_data(**kwargs)
        shop = get_shop(self.request)
        context["blocks"] = blocks = []
        context["tour_key"] = "home"
        context["tour_complete"] = is_tour_complete(shop, "home", user=self.request.user)

        wizard_complete = setup_wizard_complete(self.request)
        wizard_url = reverse("shuup_admin:wizard")
        wizard_actions = []
        if not wizard_complete:
            wizard_actions.append({
                "text": _("Complete wizard"),
                "url": wizard_url
            })
        else:
            wizard_steps = load_setup_wizard_panes(shop=shop, request=self.request, visible_only=False)
            for step in wizard_steps:
                wizard_actions.append({
                    "text": step.title,
                    "url": "%s?pane_id=%s" % (wizard_url, step.identifier),
                    "no_redirect": True
                })

        if wizard_actions:
            blocks.append(
                SimpleHelpBlock(
                    _("Complete the setup wizard"),
                    actions=wizard_actions,
                    icon_url="shuup_admin/img/configure.png",
                    priority=-1,
                    done=wizard_complete
                )
            )

        for module in get_modules():
            if not get_missing_permissions(self.request.user, module.get_required_permissions()):
                blocks.extend(module.get_help_blocks(request=self.request, kind="setup"))
        blocks.sort(key=lambda b: b.priority)

        if not blocks:
            blocks.append(
                SimpleHelpBlock(
                    _("All set. Nothing to be configured"),
                    actions=[],
                    icon_url="shuup_admin/img/configure.png",
                    priority=-1,
                    done=True
                )
            )

        return context
Example #26
0
 def get_context_data(self, **kwargs):
     context = super(DashboardView, self).get_context_data(**kwargs)
     context["version"] = shuup.__version__
     context["notifications"] = notifications = []
     context["blocks"] = blocks = []
     for module in get_modules():
         if not get_missing_permissions(self.request.user, module.get_required_permissions()):
             notifications.extend(module.get_notifications(request=self.request))
             blocks.extend(module.get_dashboard_blocks(request=self.request))
     context["activity"] = get_activity(request=self.request)
     context["tour_key"] = "dashboard"
     context["tour_complete"] = is_tour_complete("dashboard")
     return context
Example #27
0
 def get_context_data(self, **kwargs):
     context = super(DashboardView, self).get_context_data(**kwargs)
     context["version"] = shuup.__version__
     context["notifications"] = notifications = []
     context["blocks"] = blocks = []
     for module in get_modules():
         if not get_missing_permissions(self.request.user, module.get_required_permissions()):
             notifications.extend(module.get_notifications(request=self.request))
             blocks.extend(module.get_dashboard_blocks(request=self.request))
     context["activity"] = get_activity(request=self.request)
     context["tour_key"] = "dashboard"
     context["tour_complete"] = is_tour_complete("dashboard")
     return context
Example #28
0
def test_valid_permissions_for_all_modules():
    """
    If a module requires permissions, make sure all url and module-
    level permissions are valid.

    Modules that add permissions using migrations must be checked
    manually since their permissions will not be in the test database.
    """
    for module in get_modules():
        url_permissions = set(get_permissions_from_urls(module.get_urls()))
        module_permissions = set(module.get_required_permissions())
        for permission in (url_permissions | module_permissions):
            # Only requirement for permissions are that they
            # are list of strings
            assert isinstance(permission, six.string_types)
Example #29
0
def test_valid_permissions_for_all_modules():
    """
    If a module requires permissions, make sure all url and module-
    level permissions are valid.

    Modules that add permissions using migrations must be checked
    manually since their permissions will not be in the test database.
    """
    for module in get_modules():
        url_permissions = set(get_permissions_from_urls(module.get_urls()))
        module_permissions = set(module.get_required_permissions())
        for permission in (url_permissions | module_permissions):
            # Only requirement for permissions are that they
            # are list of strings
            assert isinstance(permission, six.string_types)
Example #30
0
def get_quicklinks(request):
    quicklinks = OrderedDict()
    for block in QUICKLINK_ORDER:
        quicklinks[block] = []

    for module in get_modules():
        if get_missing_permissions(request.user, module.get_required_permissions()):
            continue
        for help_block in module.get_help_blocks(request, kind="quicklink"):
            quicklinks[help_block.category].append(help_block)

    links = quicklinks.copy()
    for block, data in six.iteritems(links):
        if not quicklinks[block]:
            quicklinks.pop(block)
    return quicklinks
Example #31
0
    def get_context_data(self, **kwargs):
        context = super(DashboardView, self).get_context_data(**kwargs)
        context["version"] = shuup.__version__
        context["notifications"] = notifications = []
        context["blocks"] = blocks = []
        for module in get_modules():
            if not get_missing_permissions(self.request.user, module.get_required_permissions()):
                notifications.extend(module.get_notifications(request=self.request))
                blocks.extend(module.get_dashboard_blocks(request=self.request))

        # sort blocks by sort order and size, trying to make them fit better
        blocks.sort(key=lambda block: (block.sort_order, DashboardBlock.SIZES.index(block.size)))
        context["activity"] = get_activity(request=self.request)
        context["tour_key"] = "dashboard"
        context["tour_complete"] = is_tour_complete(get_shop(self.request), "dashboard", user=self.request.user)
        return context
Example #32
0
def test_valid_permissions_for_all_modules():
    """
    If a module requires permissions, make sure all url and module-
    level permissions are valid.

    Modules that add permissions using migrations must be checked
    manually since their permissions will not be in the test database.
    """
    for module in get_modules():
        url_permissions = set(get_permissions_from_urls(module.get_urls()))
        module_permissions = set(module.get_required_permissions())
        for permission in (url_permissions | module_permissions):
            if module.__class__ in migrated_permissions:
                assert permission in migrated_permissions[module.__class__]
            else:
                assert get_permission_object_from_string(permission)
Example #33
0
    def get_context_data(self, **kwargs):
        context = super(DashboardView, self).get_context_data(**kwargs)
        context["version"] = shuup.__version__
        context["notifications"] = notifications = []
        context["blocks"] = blocks = []
        for module in get_modules():
            if not get_missing_permissions(self.request.user, module.get_required_permissions()):
                notifications.extend(module.get_notifications(request=self.request))
                blocks.extend(module.get_dashboard_blocks(request=self.request))

        # sort blocks by sort order and size, trying to make them fit better
        blocks.sort(key=lambda block: (block.sort_order, DashboardBlock.SIZES.index(block.size)))
        context["activity"] = get_activity(request=self.request)
        context["tour_key"] = "dashboard"
        context["tour_complete"] = is_tour_complete(get_shop(self.request), "dashboard", user=self.request.user)
        return context
Example #34
0
def get_quicklinks(request):
    quicklinks = OrderedDict()
    for block in QUICKLINK_ORDER:
        quicklinks[block] = []

    for module in get_modules():
        if get_missing_permissions(request.user, module.get_required_permissions()):
            continue
        for help_block in module.get_help_blocks(request, kind="quicklink"):
            quicklinks[help_block.category].append(help_block)

    links = quicklinks.copy()
    for block, data in six.iteritems(links):
        if not quicklinks[block]:
            quicklinks.pop(block)
    return quicklinks
Example #35
0
def test_valid_permissions_for_all_modules():
    """
    If a module requires permissions, make sure all url and module-
    level permissions are valid.

    Modules that add permissions using migrations must be checked
    manually since their permissions will not be in the test database.
    """
    for module in get_modules():
        url_permissions = set(get_permissions_from_urls(module.get_urls()))
        module_permissions = set(module.get_required_permissions())
        for permission in (url_permissions | module_permissions):
            if module.__class__ in migrated_permissions:
                assert permission in migrated_permissions[module.__class__]
            else:
                assert get_permission_object_from_string(permission)
Example #36
0
def get_model_url(object,
                  kind="detail",
                  user=None,
                  required_permissions=None,
                  shop=None,
                  **kwargs):
    """
    Get a an admin object URL for the given object or object class by
    interrogating each admin module.

    If a user is provided, checks whether user has correct permissions
    before returning URL.

    Raises `NoModelUrl` if lookup fails

    :param object: Model or object class.
    :type object: class
    :param kind: URL kind. Currently "new", "list", "edit", "detail".
    :type kind: str
    :param user: Optional instance to check for permissions
    :type user: django.contrib.auth.models.User|None
    :param required_permissions: Optional iterable of permission strings
    :type required_permissions: Iterable[str]|None
    :param shop: The shop that owns the resource
    :type request: shuup.core.models.Shop|None
    :return: Resolved URL.
    :rtype: str
    """
    for module in get_modules():
        url = module.get_model_url(object, kind, shop)
        if not url:
            continue
        if user is None:
            return url
        else:
            permissions = ()
            if required_permissions is not None:
                permissions = required_permissions
            else:
                # TODO: Check permission type based on kind
                permissions = get_default_model_permissions(object)
            if not get_missing_permissions(user, permissions):
                return url
    raise NoModelUrl("Can't get object URL of kind %s: %r" %
                     (kind, force_text(object)))
Example #37
0
    def get_context_data(self, **kwargs):
        context = super(HomeView, self).get_context_data(**kwargs)
        context["blocks"] = blocks = []
        context["tour_key"] = "home"
        context["tour_complete"] = is_tour_complete("home")
        wizard_complete = setup_wizard_complete()
        blocks.append(
            SimpleHelpBlock(_("Complete the setup wizard"),
                            actions=[{
                                "text": _("Complete wizard"),
                                "url": reverse("shuup_admin:wizard")
                            }] if not wizard_complete else [],
                            icon_url="shuup_admin/img/configure.png",
                            priority=-1,
                            done=wizard_complete))

        for module in get_modules():
            if not get_missing_permissions(self.request.user,
                                           module.get_required_permissions()):
                blocks.extend(
                    module.get_help_blocks(request=self.request, kind="setup"))
        blocks.sort(key=lambda b: b.priority)
        blocks.append(
            SimpleHelpBlock(
                priority=1000,
                text=_("Publish your store"),
                description=_(
                    "Let customers browse your store and make purchases"),
                css_class="green",
                actions=[{
                    "method":
                    "POST",
                    "text":
                    _("Publish shop"),
                    "url":
                    reverse("shuup_admin:shop.enable",
                            kwargs={"pk": self.request.shop.pk}),
                    "data": {
                        "enable": True,
                        "redirect": reverse("shuup_admin:dashboard")
                    }
                }],
                icon_url="shuup_admin/img/publish.png"))
        return context
Example #38
0
def get_model_url(object, kind="detail"):
    """
    Get a an admin object URL for the given object or object class by interrogating
    each admin module.

    Raises `NoModelUrl` if lookup fails

    :param object: Model or object class.
    :type object: class
    :param kind: URL kind. Currently "new", "list", "edit", "detail".
    :type kind: str
    :return: Resolved URL.
    :rtype: str
    """
    for module in get_modules():
        url = module.get_model_url(object, kind)
        if url:
            return url
    raise NoModelUrl("Can't get object URL of kind %s: %r" % (kind, object))
Example #39
0
def get_model_url(object, kind="detail"):
    """
    Get a an admin object URL for the given object or object class by interrogating
    each admin module.

    Raises `NoModelUrl` if lookup fails

    :param object: Model or object class.
    :type object: class
    :param kind: URL kind. Currently "new", "list", "edit", "detail".
    :type kind: str
    :return: Resolved URL.
    :rtype: str
    """
    for module in get_modules():
        url = module.get_model_url(object, kind)
        if url:
            return url
    raise NoModelUrl("Can't get object URL of kind %s: %r" % (kind, object))
Example #40
0
def test_permissions_for_menu_entries(rf, admin_user):
    permissions = set(["shuup.add_product", "shuup.delete_product", "shuup.change_product"])

    request = rf.get("/")
    request.user = StaffUser()
    request.user.permissions = permissions

    with replace_modules([ARestrictedTestModule]):
        modules = [m for m in get_modules()]
        assert request.user.permissions == modules[0].get_required_permissions()

        categories = get_menu_entry_categories(request)
        assert categories

        # Make sure category is displayed if user has correct permissions
        test_category_menu_entries = categories.get("RestrictedTest")
        assert any(me.text == "OK" for me in test_category_menu_entries)

        # No menu items should be displayed if user has no permissions
        request.user.permissions = []
        categories = get_menu_entry_categories(request)
        assert not categories
Example #41
0
def get_menu_entry_categories(request):
    menu_categories = OrderedDict()
    menu_category_icons = {}
    for identifier, category_name, icon in MENU_CATEGORIES:
        menu_categories[identifier] = _MenuCategory(identifier, category_name,
                                                    icon)
        menu_category_icons[identifier] = icon

    modules = list(get_modules())
    for module in modules:
        menu_category_icons.update(
            (force_text(key), force_text(value))
            for (key, value) in module.get_menu_category_icons().items()
            if key not in menu_category_icons)

    for module in modules:
        if get_missing_permissions(request.user,
                                   module.get_required_permissions()):
            continue
        for entry in (module.get_menu_entries(request=request) or ()):
            category_identifier = entry.category
            category = menu_categories.get(
                category_identifier) if category_identifier else None
            if not category:
                category_identifier = force_text(category_identifier
                                                 or module.name)
                category = menu_categories.get(category_identifier)
                if not category:
                    menu_categories[
                        category_identifier] = category = _MenuCategory(
                            identifier=category_identifier,
                            name=category_identifier,
                            icon=menu_category_icons.get(
                                category_identifier, "fa fa-circle"))
            category.entries.append(entry)
    return [
        c for identifier, c in six.iteritems(menu_categories)
        if len(c.entries) > 0
    ]
Example #42
0
def get_activity(request, n_entries=30, cutoff_hours=10):
    """
    Get Activity objects from all modules as a list in latest-first order.

    :param request: Request context
    :type request: django.http.request.HttpRequest
    :param n_entries: Number of entries to return in total.
    :type n_entries: int
    :param cutoff_hours: Calculate cutoff datetime so the oldest entry should be at most this old
    :type cutoff_hours: float
    :return: List of Activity objects
    :rtype: list[Activity]
    """

    cutoff_dt = now() - datetime.timedelta(hours=cutoff_hours)
    activities = []
    for module in get_modules():
        for activity in islice(module.get_activity(request, cutoff=cutoff_dt), n_entries):
            heappush(activities, (-time.mktime(activity.datetime.timetuple()), activity))
    out = []
    while activities and len(out) < n_entries:
        out.append(heappop(activities)[1])
    return out
Example #43
0
def get_model_url(object, kind="detail", user=None, required_permissions=None):
    """
    Get a an admin object URL for the given object or object class by
    interrogating each admin module.

    If a user is provided, checks whether user has correct permissions
    before returning URL.

    Raises `NoModelUrl` if lookup fails

    :param object: Model or object class.
    :type object: class
    :param kind: URL kind. Currently "new", "list", "edit", "detail".
    :type kind: str
    :param user: Optional instance to check for permissions
    :type user: django.contrib.auth.models.User|None
    :param required_permissions: Optional iterable of permission strings
    :type required_permissions: Iterable[str]|None
    :return: Resolved URL.
    :rtype: str
    """
    for module in get_modules():
        url = module.get_model_url(object, kind)
        if not url:
            continue
        if user is None:
            return url
        else:
            permissions = ()
            if required_permissions is not None:
                permissions = required_permissions
            else:
                # TODO: Check permission type based on kind
                permissions = get_default_model_permissions(object)
            if not get_missing_permissions(user, permissions):
                return url
    raise NoModelUrl("Can't get object URL of kind %s: %r" % (kind, force_text(object)))
Example #44
0
def get_menu_entry_categories(request):
    menu_categories = {}
    menu_category_icons = {}
    modules = list(get_modules())

    for module in modules:
        menu_category_icons.update(
            (force_text(key), force_text(value))
            for (key, value) in module.get_menu_category_icons().items()
        )

    for module in modules:
        if not get_missing_permissions(request.user, module.get_required_permissions()):
            for entry in (module.get_menu_entries(request=request) or ()):
                category_name = force_text(entry.category or module.name)
                category = menu_categories.get(category_name)
                if not category:
                    menu_categories[category_name] = category = _MenuCategory(
                        name=category_name,
                        icon=menu_category_icons.get(category_name, "fa fa-circle")
                    )
                category.entries.append(entry)

    return SortedDict(sorted((c.name, c) for c in sorted(menu_categories.values(), key=lambda c: c.name)))
Example #45
0
 def _get_required_permissions(self, modules):
     permissions = set()
     for module in [m for m in get_modules() if m.name in modules]:
         permissions.update(set(module.get_required_permissions()))
     return permissions
Example #46
0
def _get_admin_module_for_url(url_names):
    for module in get_modules():
        for url in module.get_urls():
            if url.name in url_names:
                return module
Example #47
0
 def _get_module_choices(self):
     modules = [module for module in get_modules() if module.name != "_Base_"]
     modules.sort(key=lambda module: module.name)
     return modules
Example #48
0
def get_model_url(object,
                  kind="detail",
                  user=None,
                  required_permissions=None,
                  shop=None,
                  raise_permission_denied=False,
                  **kwargs):
    """
    Get a an admin object URL for the given object or object class by
    interrogating each admin module.

    If a user is provided, checks whether user has correct permissions
    before returning URL.

    Raises `NoModelUrl` if lookup fails

    :param object: Model or object class.
    :type object: class
    :param kind: URL kind. Currently "new", "list", "edit", "detail".
    :type kind: str
    :param user: Optional instance to check for permissions.
    :type user: django.contrib.auth.models.User|None
    :param required_permissions: Optional iterable of permission strings.
    :type required_permissions: Iterable[str]|None
    :param shop: The shop that owns the resource.
    :type request: shuup.core.models.Shop|None
    :param raise_permission_denied: raise PermissionDenied exception if the url
        is found but user has not permission. If false, None will be returned instead.
        Default is False.
    :type raise_permission_denied: bool
    :return: Resolved URL.
    :rtype: str
    """
    for module in get_modules():
        url = module.get_model_url(object, kind, shop)

        if not url:
            continue

        if user is None:
            return url

        from shuup.utils.django_compat import Resolver404, resolve

        try:
            if required_permissions is not None:
                warnings.warn(
                    "Warning! `required_permissions` parameter will be deprecated "
                    "in Shuup 2.0 as unused for this util.",
                    DeprecationWarning,
                )
                permissions = required_permissions
            else:
                resolved = resolve(url)
                from shuup.admin.utils.permissions import get_permissions_for_module_url

                permissions = get_permissions_for_module_url(
                    module, resolved.url_name)

            missing_permissions = get_missing_permissions(user, permissions)

            if not missing_permissions:
                return url

            if raise_permission_denied:
                from django.core.exceptions import PermissionDenied

                reason = _(
                    "Can't view this page. You do not have the required permission(s): `{permissions}`."
                ).format(permissions=", ".join(missing_permissions))
                raise PermissionDenied(reason)

        except Resolver404:
            # what are you doing developer?
            return url

    raise NoModelUrl("Error! Can't get object URL of kind %s: %r." %
                     (kind, force_text(object)))
Example #49
0
def get_model_url(object, kind="detail", user=None,
                  required_permissions=None, shop=None,
                  raise_permission_denied=False, **kwargs):
    """
    Get a an admin object URL for the given object or object class by
    interrogating each admin module.

    If a user is provided, checks whether user has correct permissions
    before returning URL.

    Raises `NoModelUrl` if lookup fails

    :param object: Model or object class.
    :type object: class
    :param kind: URL kind. Currently "new", "list", "edit", "detail".
    :type kind: str
    :param user: Optional instance to check for permissions
    :type user: django.contrib.auth.models.User|None
    :param required_permissions: Optional iterable of permission strings
    :type required_permissions: Iterable[str]|None
    :param shop: The shop that owns the resource
    :type request: shuup.core.models.Shop|None
    :param raise_permission_denied: raise PermissionDenied exception if the url
        is found but user has not permission. If false, None will be returned instead.
        Default is False
    :type raise_permission_denied: bool
    :return: Resolved URL.
    :rtype: str
    """
    for module in get_modules():
        url = module.get_model_url(object, kind, shop)

        if not url:
            continue

        if user is None:
            return url

        from django.core.urlresolvers import resolve, Resolver404

        try:
            if required_permissions is not None:
                warnings.warn(
                    "required_permissions parameter will be deprecated in Shuup 2.0 as unused for this util.",
                    DeprecationWarning
                )
                permissions = required_permissions
            else:
                resolved = resolve(url)
                from shuup.admin.utils.permissions import get_permissions_for_module_url
                permissions = get_permissions_for_module_url(module, resolved.url_name)

            missing_permissions = get_missing_permissions(user, permissions)

            if not missing_permissions:
                return url

            if raise_permission_denied:
                from django.core.exceptions import PermissionDenied
                reason = _("Can't view this page. You do not have the required permission(s): {permissions}").format(
                    permissions=", ".join(missing_permissions)
                )
                raise PermissionDenied(reason)

        except Resolver404:
            # what are you doing developer?
            return url

    raise NoModelUrl("Can't get object URL of kind %s: %r" % (kind, force_text(object)))
Example #50
0
def test_notifications(rf):
    request = rf.get("/")
    with replace_modules([ATestModule]):
        assert any(n.text == "OK" for n in chain(*(m.get_notifications(request) for m in get_modules())))
Example #51
0
 def _get_module_choices(self):
     return set((force_text(m.name), force_text(m.name))
                for m in get_modules() if m.name != "_Base_")
Example #52
0
 def _get_module_choices(self):
     return set((force_text(m.name), force_text(m.name)) for m in get_modules() if m.name != "_Base_")
Example #53
0
 def _get_required_permissions(self, modules):
     permissions = set()
     for module in [m for m in get_modules() if m.name in modules]:
         permissions.update(set(module.get_required_permissions()))
     return permissions
Example #54
0
def test_notifications(rf):
    request = rf.get("/")
    with replace_modules([ATestModule]):
        assert any(n.text == "OK" for n in chain(*(m.get_notifications(request) for m in get_modules())))
Example #55
0
def _get_admin_module_for_url(url_names):
    for module in get_modules():
        for url in module.get_urls():
            if url.name in url_names:
                return module
Example #56
0
 def _get_module_choices(self):
     modules = [
         module for module in get_modules() if module.name != "_Base_"
     ]
     modules.sort(key=lambda module: module.name)
     return modules