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
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"])
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
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
def get_context_data(self, **kwargs): context = super(DashboardView, self).get_context_data(**kwargs) context["version"] = wshop.__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
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
def get_model_url(object, kind="detail", user=None, required_permissions=None, shop=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 :param shop: The shop that owns the resource :type request: wshop.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)))
def get_search_results(request, query): fuzzer = FuzzyMatcher(query) normal_results = [] menu_entry_results = [] for module in get_modules(): 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(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()
def get_menu_entry_categories(request): menu_categories = OrderedDict() menu_children = 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, ) 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 ]
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
def _get_module_choices(self): return set((force_text(m.name), force_text(m.name)) for m in get_modules() if m.name != "_Base_")
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())))