def decode(self, obj): if len(obj) == 1: tag, val = first(obj.items()) info = self.tags.get(tag) if info: return info["decoder"](val) return obj
def get_taxes_of_effective_rules(taxing_context, tax_rules): """ Get taxes grouped by priority from effective tax rules. Effective tax rules is determined by first limiting the scope to the rules that match the given taxing context (see `TaxRule.match`) and then further limiting the matching rules by selecting only the rules in the highest numbered override group. The `Tax` objects in the effective rules will be grouped by the priority of the rules. The tax groups are returned as list of tax lists. :type taxing_context: shuup.core.taxing.TaxingContext :param tax_rules: Tax rules to filter from. These should be ordered desceding by override group and then ascending by priority. :type tax_rules: Iterable[TaxRule] :rtype: list[list[shuup.core.models.Tax]] """ # Limit our scope to only matching rules matching_rules = (tax_rule for tax_rule in tax_rules if tax_rule.matches(taxing_context)) # Further limit our scope to the highest numbered override group grouped_by_override = groupby(matching_rules, attrgetter("override_group")) highest_override_group = first(grouped_by_override, (None, []))[1] # Group rules by priority grouped_rules = groupby(highest_override_group, attrgetter("priority")) tax_groups = [[rule.tax for rule in rules] for (_, rules) in grouped_rules] return tax_groups
def get_taxes_of_effective_rules(taxing_context, tax_rules): """ Get taxes grouped by priority from effective tax rules. Effective tax rules is determined by first limiting the scope to the rules that match the given taxing context (see `TaxRule.match`) and then further limiting the matching rules by selecting only the rules in the highest numbered override group. The `Tax` objects in the effective rules will be grouped by the priority of the rules. The tax groups are returned as list of tax lists. :type taxing_context: shuup.core.taxing.TaxingContext :param tax_rules: Tax rules to filter from. These should be ordered desceding by override group and then ascending by priority. :type tax_rules: Iterable[TaxRule] :rtype: list[list[shuup.core.models.Tax]] """ # Limit our scope to only matching rules matching_rules = (tax_rule for tax_rule in tax_rules if tax_rule.matches(taxing_context)) # Further limit our scope to the highest numbered override group grouped_by_override = groupby(matching_rules, attrgetter('override_group')) highest_override_group = first(grouped_by_override, (None, []))[1] # Group rules by priority grouped_rules = groupby(highest_override_group, attrgetter('priority')) tax_groups = [[rule.tax for rule in rules] for (_, rules) in grouped_rules] return tax_groups
def get_first_day_of_the_current_week(today_start): locale = i18n.get_current_babel_locale() first_day_of_the_week = today_start if today_start.weekday() == locale.first_week_day: return first_day_of_the_week def get_prospect(i): return (today_start - datetime.timedelta(days=i)) return iterables.first([get_prospect(i) for i in range(1, 7) if get_prospect(i).weekday() == locale.first_week_day])
def get_first_day_of_the_current_week(today_start): locale = i18n.get_current_babel_locale() first_day_of_the_week = today_start if today_start.weekday() == locale.first_week_day: return first_day_of_the_week def get_prospect(i): return today_start - datetime.timedelta(days=i) return iterables.first([get_prospect(i) for i in range(1, 7) if get_prospect(i).weekday() == locale.first_week_day])
def get_orderable_variation_children(product, request, variation_variables, supplier=None): # noqa (C901) if not variation_variables: variation_variables = product.variation_variables.all( ).prefetch_related("values") key, val = context_cache.get_cached_value( identifier="orderable_variation_children", item=product, context=request, variation_variables=variation_variables, supplier=supplier) if val is not None: return _unpack_orderable_variation_children_from_cache(val) orderable_variation_children = OrderedDict() orderable = 0 shop = request.shop product_queryset = product.variation_children.visible( shop=shop, customer=request.customer).values_list("pk", flat=True) all_combinations = list(product.get_all_available_combinations()) for shop_product in (ShopProduct.objects.filter( shop=shop, product__id__in=product_queryset).select_related( "product").prefetch_related("suppliers")): shop_product.shop = shop # To avoid query on orderability checks combo_data = first( combo for combo in all_combinations if combo["result_product_pk"] == shop_product.product.id) if not combo_data: continue combo = combo_data["variable_to_value"] for variable, values in six.iteritems(combo): if variable not in orderable_variation_children: orderable_variation_children[variable] = [] if shop_product.is_orderable( supplier=supplier, customer=request.customer, quantity=shop_product.minimum_purchase_quantity): orderable += 1 for variable, value in six.iteritems(combo): if value not in orderable_variation_children[variable]: orderable_variation_children[variable].append(value) orderable = (orderable > 0) values = (orderable_variation_children, orderable) context_cache.set_cached_value( key, _pack_orderable_variation_children_to_cache(*values)) return values
def get_method_checkout_phase_object(self): """ :rtype: shuup.front.checkout.CheckoutPhaseViewMixin|None """ if hasattr(self, "_checkout_phase_object"): return self._checkout_phase_object method = self.get_method() if not method: return None phases = get_checkout_phases_for_service(self.checkout_process, method) phase = first(phases) if not phase: return None self._checkout_phase_object = phase return phase
def get_context_data(self, **kwargs): context = super(AddonUploadConfirmView, self).get_context_data(**kwargs) with zipfile.ZipFile(self.get_addon_path()) as zf: context["filenames"] = sorted(zf.namelist()) pkg_info_path = first(filename for filename in context["filenames"] if filename.endswith("PKG-INFO")) if pkg_info_path: context["pkg_info"] = zf.read(pkg_info_path).decode("UTF-8", "replace") context["toolbar"] = Toolbar([ PostActionButton( icon="fa fa-download", form_id="install_form", text=_("Install Addon"), extra_css_class="btn-success", ) ]) return context
def get_by_object(self, obj): return first(form_info for form_info in self.values() if isinstance(obj, form_info.model))
def get_by_object(self, obj): return first( fi for fi in self.values() if isinstance(obj, fi.model))
def get_by_object(self, obj): return first( form_info for form_info in self.values() if isinstance(obj, form_info.model))
def get_selected_reload_method(self): return first(rm for rm in self.reload_methods if rm.identifier == self.cleaned_data["reload_method"])
def customize_menu(entries, request): # noqa (C901) """ Merge system menu with customized admin menu """ customized_admin_menu = configuration.get( None, CUSTOM_ADMIN_MENU_USER_PREFIX.format(request.user.pk)) if not customized_admin_menu: supplier = get_supplier(request) if supplier: customized_admin_menu = configuration.get( None, CUSTOM_ADMIN_MENU_SUPPLIER_KEY) elif getattr(request.user, "is_superuser", False): customized_admin_menu = configuration.get( None, CUSTOM_ADMIN_MENU_SUPERUSER_KEY) else: customized_admin_menu = configuration.get( None, CUSTOM_ADMIN_MENU_STAFF_KEY) if customized_admin_menu and isinstance(customized_admin_menu, dict): """ If menu configuration is stored to dict try to find right configuration with current language and fallback to first stored configuration """ customized_admin_menu = customized_admin_menu.get( get_language(), customized_admin_menu.get(first(customized_admin_menu))) if customized_admin_menu: def unset_mismatched(menu): """ find and remove unmatched entries from customized menu tree it can be when menu entry was removed from system """ indexes = [] for index, entry in enumerate(menu.get('entries', [])): unset_mismatched(entry) if isinstance(entry, dict): indexes.append(index) for index in indexes[::-1]: del menu['entries'][index] def find_entry(menu, entry): """ find recursively entry in menu """ if menu['id'] == entry.id: return menu for node in menu.get('entries', []): n = find_entry(node, entry) if n: return n def assign_entry(customized_menu, entry): """ Find and replace customized entry with system menu entry set entry name, hidden flag from customized menu entry """ custom_entries = customized_menu.get('entries', []) for index, node in enumerate(custom_entries): if node['id'] == entry.id: custom_entry = custom_entries[index] entry.name = custom_entry['name'] entry.is_hidden = custom_entry['is_hidden'] entry.entries = custom_entry.get('entries', []) entry.ordering = index custom_entries[index] = entry return custom_entries[index] else: return_entry = assign_entry(custom_entries[index], entry) if return_entry: return return_entry def transform_menu(customized_menu, menu): """ Recursively sort system menu entries and assign it to the customized menu """ indexes = [] for index, entry in enumerate(menu.entries): transform_menu(customized_menu, entry) custom_entry = assign_entry(customized_menu, entry) if not custom_entry: parent_menu = find_entry(customized_menu, menu) if parent_menu: parent_menu.get('entries', []).append(entry) indexes.append(index) else: indexes.append(index) # remove assigned entries from system menu for index in indexes[::-1]: del menu.entries[index] return menu customized_menu = { 'id': 'root', 'name': 'root', 'entries': customized_admin_menu, } system_menu = BaseMenuEntry() system_menu.identifier = 'root' system_menu.entries = entries transform_menu(customized_menu, system_menu) unset_mismatched(customized_menu) return customized_menu['entries'] + system_menu['entries'] else: return entries
def get_suppliers_column(iterable): return first([col for col in iterable if col.id in ["suppliers", "shopproduct_suppliers"]], default=None)
def current_pane(self): pane_id = self.request.POST.get("pane_id") return first(filter(lambda x: x.identifier == pane_id, self.panes), None)