Python django.utils.translation 模块,pgettext_lazy() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.utils.translation.pgettext_lazy()

项目:mos-horizon    作者:Mirantis    | 项目源码 | 文件源码
def _set_external_network(self, router, ext_net_dict):
        gateway_info = router.external_gateway_info
        if gateway_info:
            ext_net_id = gateway_info['network_id']
            if ext_net_id in ext_net_dict:
                gateway_info['network'] = ext_net_dict[ext_net_id]
            else:
                msg_params = {'ext_net_id': ext_net_id, 'router_id': router.id}
                msg = _('External network "%(ext_net_id)s" expected but not '
                        'found for router "%(router_id)s".') % msg_params
                messages.error(self.request, msg)
                # gateway_info['network'] is just the network name, so putting
                # in a smallish error message in the table is reasonable.
                # Translators: The usage is "<UUID of ext_net> (Not Found)"
                gateway_info['network'] = pgettext_lazy(
                    'External network not found',
                    u'%s (Not Found)') % ext_net_id
项目:CodingDojo    作者:ComputerSocietyUNB    | 项目源码 | 文件源码
def resolve(self, context):
        """Resolve this variable against a given context."""
        if self.lookups is not None:
            # We're dealing with a variable that needs to be resolved
            value = self._resolve_lookup(context)
        else:
            # We're dealing with a literal, so it's already been "resolved"
            value = self.literal
        if self.translate:
            is_safe = isinstance(value, SafeData)
            msgid = value.replace('%', '%%')
            msgid = mark_safe(msgid) if is_safe else msgid
            if self.message_context:
                return pgettext_lazy(self.message_context, msgid)
            else:
                return ugettext_lazy(msgid)
        return value
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def product_class_edit(request, pk):
    product_class = get_object_or_404(
        ProductClass, pk=pk)
    form = forms.ProductClassForm(request.POST or None,
                                  instance=product_class)
    if form.is_valid():
        product_class = form.save()
        msg = pgettext_lazy(
            'Dashboard message', 'Updated product type %s') % product_class
        messages.success(request, msg)
        return redirect('dashboard:product-class-update', pk=pk)
    ctx = {'form': form, 'product_class': product_class}
    return TemplateResponse(
        request,
        'dashboard/product/product_class/form.html',
        ctx)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def product_class_delete(request, pk):
    product_class = get_object_or_404(ProductClass, pk=pk)
    if request.method == 'POST':
        product_class.delete()
        messages.success(
            request,
            pgettext_lazy(
                'Dashboard message',
                'Deleted product type %s') % product_class)
        return redirect('dashboard:product-class-list')
    ctx = {'product_class': product_class,
           'products': product_class.products.all()}
    return TemplateResponse(
        request,
        'dashboard/product/product_class/modal/confirm_delete.html',
        ctx)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def stock_edit(request, product_pk, variant_pk, stock_pk=None):
    product = get_object_or_404(Product, pk=product_pk)
    variant = get_object_or_404(product.variants, pk=variant_pk)
    if stock_pk:
        stock = get_object_or_404(variant.stock, pk=stock_pk)
    else:
        stock = Stock()
    form = forms.StockForm(
        request.POST or None, instance=stock, variant=variant)
    if form.is_valid():
        form.save()
        messages.success(
            request, pgettext_lazy('Dashboard message', 'Saved stock'))
        return redirect(
            'dashboard:variant-details', product_pk=product.pk,
            variant_pk=variant.pk)
    ctx = {'form': form, 'product': product,
           'variant': variant, 'stock': stock}
    return TemplateResponse(
        request,
        'dashboard/product/stock/form.html',
        ctx)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def stock_delete(request, product_pk, variant_pk, stock_pk):
    product = get_object_or_404(Product, pk=product_pk)
    variant = get_object_or_404(product.variants, pk=variant_pk)
    stock = get_object_or_404(Stock, pk=stock_pk)
    if request.method == 'POST':
        stock.delete()
        messages.success(
            request, pgettext_lazy('Dashboard message', 'Deleted stock'))
        return redirect(
            'dashboard:variant-details', product_pk=product.pk,
            variant_pk=variant.pk)
    ctx = {'product': product, 'stock': stock, 'variant': variant}
    return TemplateResponse(
        request,
        'dashboard/product/stock/modal/confirm_delete.html',
        ctx)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def product_image_edit(request, product_pk, img_pk=None):
    product = get_object_or_404(Product, pk=product_pk)
    if img_pk:
        product_image = get_object_or_404(product.images, pk=img_pk)
    else:
        product_image = ProductImage(product=product)
    form = forms.ProductImageForm(
        request.POST or None, request.FILES or None, instance=product_image)
    if form.is_valid():
        product_image = form.save()
        if img_pk:
            msg = pgettext_lazy(
                'Dashboard message',
                'Updated image %s') % product_image.image.name
        else:
            msg = pgettext_lazy(
                'Dashboard message',
                'Added image %s') % product_image.image.name
        messages.success(request, msg)
        return redirect('dashboard:product-image-list', product_pk=product.pk)
    ctx = {'form': form, 'product': product, 'product_image': product_image}
    return TemplateResponse(
        request,
        'dashboard/product/product_image/form.html',
        ctx)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def variant_delete(request, product_pk, variant_pk):
    product = get_object_or_404(Product, pk=product_pk)
    variant = get_object_or_404(product.variants, pk=variant_pk)
    if request.method == 'POST':
        variant.delete()
        messages.success(
            request,
            pgettext_lazy(
                'Dashboard message', 'Deleted variant %s') % variant.name)
        return redirect('dashboard:product-detail', pk=product.pk)

    ctx = {'is_only_variant': product.variants.count() == 1,
           'product': product,
           'variant': variant}
    return TemplateResponse(
        request,
        'dashboard/product/product_variant/modal/confirm_delete.html',
        ctx)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def attribute_edit(request, pk=None):
    if pk:
        attribute = get_object_or_404(ProductAttribute, pk=pk)
    else:
        attribute = ProductAttribute()
    form = forms.ProductAttributeForm(request.POST or None, instance=attribute)
    formset = forms.AttributeChoiceValueFormset(
        request.POST or None, request.FILES or None, instance=attribute)
    if all([form.is_valid(), formset.is_valid()]):
        attribute = form.save()
        formset.save()
        msg = pgettext_lazy(
            'Dashboard message', 'Updated attribute') if pk else pgettext_lazy(
                'Dashboard message', 'Added attribute')
        messages.success(request, msg)
        return redirect('dashboard:product-attribute-detail', pk=attribute.pk)
    ctx = {'attribute': attribute, 'form': form, 'formset': formset}
    return TemplateResponse(
        request,
        'dashboard/product/product_attribute/form.html',
        ctx)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def stock_location_edit(request, location_pk=None):
    if location_pk:
        location = get_object_or_404(StockLocation, pk=location_pk)
    else:
        location = StockLocation()
    form = forms.StockLocationForm(request.POST or None, instance=location)
    if form.is_valid():
        form.save()
        msg = pgettext_lazy(
            'Dashboard message for stock location',
            'Updated location') if location_pk else pgettext_lazy(
                'Dashboard message for stock location', 'Added location')
        messages.success(request, msg)
        return redirect('dashboard:product-stock-location-list')
    return TemplateResponse(
        request,
        'dashboard/product/stock_location/form.html',
        {'form': form, 'location': location})
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def order_add_note(request, order_pk):
    order = get_object_or_404(Order, pk=order_pk)
    note = OrderNote(order=order, user=request.user)
    form = OrderNoteForm(request.POST or None, instance=note)
    status = 200
    if form.is_valid():
        form.save()
        msg = pgettext_lazy(
            'Dashboard message related to an order',
            'Added note')
        order.create_history_entry(comment=msg, user=request.user)
        messages.success(request, msg)
    elif form.errors:
        status = 400
    ctx = {'order': order, 'form': form}
    ctx.update(csrf(request))
    template = 'dashboard/order/modal/add_note.html'
    return TemplateResponse(request, template, ctx, status=status)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def capture_payment(request, order_pk, payment_pk):
    order = get_object_or_404(Order, pk=order_pk)
    payment = get_object_or_404(order.payments, pk=payment_pk)
    amount = order.get_total().quantize('0.01').gross
    form = CapturePaymentForm(request.POST or None, payment=payment,
                              initial={'amount': amount})
    if form.is_valid() and form.capture():
        amount = form.cleaned_data['amount']
        msg = pgettext_lazy(
            'Dashboard message related to a payment',
            'Captured %(amount)s') % {'amount': gross(amount)}
        payment.order.create_history_entry(comment=msg, user=request.user)
        messages.success(request, msg)
        return redirect('dashboard:order-details', order_pk=order.pk)
    status = 400 if form.errors else 200
    ctx = {'captured': payment.captured_amount, 'currency': payment.currency,
           'form': form, 'order': order, 'payment': payment}
    return TemplateResponse(request, 'dashboard/order/modal/capture.html', ctx,
                            status=status)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def orderline_cancel(request, order_pk, line_pk):
    order = get_object_or_404(Order, pk=order_pk)
    item = get_object_or_404(OrderedItem.objects.filter(
        delivery_group__order=order), pk=line_pk)
    form = CancelItemsForm(data=request.POST or None, item=item)
    status = 200
    if form.is_valid():
        msg = pgettext_lazy(
            'Dashboard message related to an order line',
            'Cancelled item %s') % item
        with transaction.atomic():
            form.cancel_item()
            order.create_history_entry(comment=msg, user=request.user)
            messages.success(request, msg)
        return redirect('dashboard:order-details', order_pk=order.pk)
    elif form.errors:
        status = 400
    ctx = {'order': order, 'item': item, 'form': form}
    return TemplateResponse(
        request, 'dashboard/order/modal/cancel_line.html',
        ctx, status=status)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def ship_delivery_group(request, order_pk, group_pk):
    order = get_object_or_404(Order, pk=order_pk)
    group = get_object_or_404(order.groups.all(), pk=group_pk)
    form = ShipGroupForm(request.POST or None, instance=group)
    status = 200
    if form.is_valid():
        with transaction.atomic():
            form.save()
        msg = pgettext_lazy(
            'Dashboard message related to a delivery group',
            'Shipped %s') % group
        messages.success(request, msg)
        group.order.create_history_entry(comment=msg, user=request.user)
        return redirect('dashboard:order-details', order_pk=order_pk)
    elif form.errors:
        status = 400
    ctx = {'order': order, 'group': group, 'form': form}
    template = 'dashboard/order/modal/ship_delivery_group.html'
    return TemplateResponse(request, template, ctx, status=status)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def address_view(request, order_pk, address_type):
    order = Order.objects.get(pk=order_pk)
    if address_type == 'shipping':
        address = order.shipping_address
        success_msg = pgettext_lazy(
            'Dashboard message',
            'Updated shipping address')
    else:
        address = order.billing_address
        success_msg = pgettext_lazy(
            'Dashboard message',
            'Updated billing address')
    form = AddressForm(request.POST or None, instance=address)
    if form.is_valid():
        form.save()
        order.create_history_entry(comment=success_msg, user=request.user)
        messages.success(request, success_msg)
        return redirect('dashboard:order-details', order_pk=order_pk)
    ctx = {'order': order, 'address_type': address_type, 'form': form}
    return TemplateResponse(request, 'dashboard/order/address_form.html', ctx)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def cancel_order(request, order_pk):
    status = 200
    order = get_object_or_404(Order, pk=order_pk)
    form = CancelOrderForm(request.POST or None, order=order)
    if form.is_valid():
        msg = pgettext_lazy('Dashboard message', 'Cancelled order')
        with transaction.atomic():
            form.cancel_order()
            order.create_history_entry(comment=msg, user=request.user)
        messages.success(request, 'Order cancelled')
        return redirect('dashboard:order-details', order_pk=order.pk)
        # TODO: send status confirmation email
    elif form.errors:
        status = 400
    ctx = {'order': order}
    return TemplateResponse(request, 'dashboard/order/modal/cancel_order.html',
                            ctx, status=status)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def remove_order_voucher(request, order_pk):
    status = 200
    order = get_object_or_404(Order, pk=order_pk)
    form = RemoveVoucherForm(request.POST or None, order=order)
    if form.is_valid():
        msg = pgettext_lazy('Dashboard message', 'Removed voucher from order')
        with transaction.atomic():
            form.remove_voucher()
            order.create_history_entry(comment=msg, user=request.user)
        messages.success(request, msg)
        return redirect('dashboard:order-details', order_pk=order.pk)
    elif form.errors:
        status = 400
    ctx = {'order': order}
    return TemplateResponse(request,
                            'dashboard/order/modal/order_remove_voucher.html',
                            ctx, status=status)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def sale_edit(request, pk=None):
    if pk:
        instance = get_object_or_404(Sale, pk=pk)
    else:
        instance = Sale()
    form = forms.SaleForm(
        request.POST or None, instance=instance)
    if form.is_valid():
        instance = form.save()
        msg = pgettext_lazy(
            'Sale (discount) message', 'Updated sale') if pk else pgettext_lazy(
                'Sale (discount) message', 'Added sale')
        messages.success(request, msg)
        return redirect('dashboard:sale-update', pk=instance.pk)
    ctx = {'sale': instance, 'form': form}
    return TemplateResponse(request, 'dashboard/discount/sale/form.html', ctx)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def get_display(status):
        if status == ProductAvailabilityStatus.NOT_PUBLISHED:
            return pgettext_lazy('Product status', 'not published')
        elif status == ProductAvailabilityStatus.VARIANTS_MISSSING:
            return pgettext_lazy('Product status', 'variants missing')
        elif status == ProductAvailabilityStatus.NOT_CARRIED:
            return pgettext_lazy('Product status', 'not carried')
        elif status == ProductAvailabilityStatus.OUT_OF_STOCK:
            return pgettext_lazy('Product status', 'out of stock')
        elif status == ProductAvailabilityStatus.LOW_STOCK:
            return pgettext_lazy('Product status', 'stock running low')
        elif status == ProductAvailabilityStatus.NOT_YET_AVAILABLE:
            return pgettext_lazy('Product status', 'not yet available')
        elif status == ProductAvailabilityStatus.READY_FOR_PURCHASE:
            return pgettext_lazy('Product status', 'ready for purchase')
        else:
            raise NotImplementedError('Unknown status: %s' % status)
项目:lifesoundtrack    作者:MTG    | 项目源码 | 文件源码
def resolve(self, context):
        """Resolve this variable against a given context."""
        if self.lookups is not None:
            # We're dealing with a variable that needs to be resolved
            value = self._resolve_lookup(context)
        else:
            # We're dealing with a literal, so it's already been "resolved"
            value = self.literal
        if self.translate:
            is_safe = isinstance(value, SafeData)
            msgid = value.replace('%', '%%')
            msgid = mark_safe(msgid) if is_safe else msgid
            if self.message_context:
                return pgettext_lazy(self.message_context, msgid)
            else:
                return ugettext_lazy(msgid)
        return value
项目:liberator    作者:libscie    | 项目源码 | 文件源码
def resolve(self, context):
        """Resolve this variable against a given context."""
        if self.lookups is not None:
            # We're dealing with a variable that needs to be resolved
            value = self._resolve_lookup(context)
        else:
            # We're dealing with a literal, so it's already been "resolved"
            value = self.literal
        if self.translate:
            is_safe = isinstance(value, SafeData)
            msgid = value.replace('%', '%%')
            msgid = mark_safe(msgid) if is_safe else msgid
            if self.message_context:
                return pgettext_lazy(self.message_context, msgid)
            else:
                return ugettext_lazy(msgid)
        return value
项目:djanoDoc    作者:JustinChavez    | 项目源码 | 文件源码
def resolve(self, context):
        """Resolve this variable against a given context."""
        if self.lookups is not None:
            # We're dealing with a variable that needs to be resolved
            value = self._resolve_lookup(context)
        else:
            # We're dealing with a literal, so it's already been "resolved"
            value = self.literal
        if self.translate:
            is_safe = isinstance(value, SafeData)
            msgid = value.replace('%', '%%')
            msgid = mark_safe(msgid) if is_safe else msgid
            if self.message_context:
                return pgettext_lazy(self.message_context, msgid)
            else:
                return ugettext_lazy(msgid)
        return value
项目:django-next-train    作者:bitpixdigital    | 项目源码 | 文件源码
def resolve(self, context):
        """Resolve this variable against a given context."""
        if self.lookups is not None:
            # We're dealing with a variable that needs to be resolved
            value = self._resolve_lookup(context)
        else:
            # We're dealing with a literal, so it's already been "resolved"
            value = self.literal
        if self.translate:
            is_safe = isinstance(value, SafeData)
            msgid = value.replace('%', '%%')
            msgid = mark_safe(msgid) if is_safe else msgid
            if self.message_context:
                return pgettext_lazy(self.message_context, msgid)
            else:
                return ugettext_lazy(msgid)
        return value
项目:mos-horizon    作者:Mirantis    | 项目源码 | 文件源码
def get_format(image):
    format = getattr(image, "disk_format", "")
    # The "container_format" attribute can actually be set to None,
    # which will raise an error if you call upper() on it.
    if not format:
        return format
    if format == "raw":
        if getattr(image, "container_format") == 'docker':
            return pgettext_lazy("Image format for display in table",
                                 u"Docker")
        # Most image formats are untranslated acronyms, but raw is a word
        # and should be translated
        return pgettext_lazy("Image format for display in table", u"Raw")
    return format.upper()
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def check_product_availability_and_warn(request, cart):
    if contains_unavailable_variants(cart):
        msg = pgettext_lazy(
            'Cart warning message',
            'Sorry. We don\'t have that many items in stock. '
            'Quantity was set to maximum available for now.')
        messages.warning(request, msg)
        remove_unavailable_variants(cart)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def staff_details(request, pk):
    queryset = User.objects.filter(is_staff=True)
    staff_member = get_object_or_404(queryset, pk=pk)
    form = StaffForm(
        request.POST or None, instance=staff_member, user=request.user)
    if form.is_valid():
        form.save()
        msg = pgettext_lazy(
            'Dashboard message', 'Updated staff member %s') % staff_member
        messages.success(request, msg)
        redirect('dashboard:staff-list')
    ctx = {'staff_member': staff_member, 'form': form}
    return TemplateResponse(request, 'dashboard/staff/detail.html', ctx)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def staff_create(request):
    staff = User()
    form = StaffForm(request.POST or None, instance=staff)
    if form.is_valid():
        form.save()
        msg = pgettext_lazy(
            'Dashboard message', 'Added staff member %s') % staff
        messages.success(request, msg)
        send_set_password_email(staff)
        return redirect('dashboard:staff-list')
    ctx = {'form': form}
    return TemplateResponse(request, 'dashboard/staff/detail.html', ctx)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def staff_delete(request, pk):
    queryset = User.objects.prefetch_related(
        'orders')
    staff = get_object_or_404(queryset, pk=pk)
    all_orders_count = staff.orders.count()
    if request.method == 'POST':
        staff.delete()
        msg = pgettext_lazy(
            'Dashboard message', 'Deleted staff member %s') % staff
        messages.success(request, msg)
        return redirect('dashboard:staff-list')
    return TemplateResponse(
        request, 'dashboard/staff/modal/confirm_delete.html',
        {'staff': staff, 'orders': all_orders_count})
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def shipping_method_edit(request, method):
    form = ShippingMethodForm(request.POST or None, instance=method)
    formset = ShippingMethodCountryFormSet(request.POST or None, instance=method)
    if form.is_valid() and formset.is_valid():
        shipping_method = form.save()
        formset.save()
        msg = pgettext_lazy(
            'Dashboard message', '%(shipping_method_name)s method saved') % {
                'shipping_method_name': shipping_method}
        messages.success(request, msg)
        return redirect(
            'dashboard:shipping-method-detail', pk=shipping_method.pk)
    ctx = {'shipping_method_form': form,
           'price_per_country_formset': formset, 'shipping_method': method}
    return TemplateResponse(request, 'dashboard/shipping/form.html', ctx)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def customer_promote_to_staff(request, pk):
    customer = get_object_or_404(User, pk=pk)
    if request.method == 'POST':
        customer.is_staff = True
        customer.save()
        msg = pgettext_lazy(
            'Dashboard message',
            'Customer %s promoted to staff member') % customer
        messages.success(request, msg)
        return redirect('dashboard:customer-details', pk=customer.pk)
    return TemplateResponse(
        request, 'dashboard/customer/modal/confirm_promote.html',
        {'customer': customer})
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def product_class_create(request):
    product_class = ProductClass()
    form = forms.ProductClassForm(request.POST or None,
                                  instance=product_class)
    if form.is_valid():
        product_class = form.save()
        msg = pgettext_lazy(
            'Dashboard message', 'Added product type %s') % product_class
        messages.success(request, msg)
        return redirect('dashboard:product-class-list')
    ctx = {'form': form, 'product_class': product_class}
    return TemplateResponse(
        request,
        'dashboard/product/product_class/form.html',
        ctx)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def product_create(request, class_pk):
    product_class = get_object_or_404(ProductClass, pk=class_pk)
    create_variant = not product_class.has_variants
    product = Product()
    product.product_class = product_class
    product_form = forms.ProductForm(request.POST or None, instance=product)
    if create_variant:
        variant = ProductVariant(product=product)
        variant_form = forms.ProductVariantForm(
            request.POST or None, instance=variant, prefix='variant')
        variant_errors = not variant_form.is_valid()
    else:
        variant_form = None
        variant_errors = False

    if product_form.is_valid() and not variant_errors:
        product = product_form.save()
        if create_variant:
            variant.product = product
            variant_form.save()
        msg = pgettext_lazy(
            'Dashboard message', 'Added product %s') % product
        messages.success(request, msg)
        return redirect('dashboard:product-detail', pk=product.pk)
    ctx = {
        'product_form': product_form, 'variant_form': variant_form,
        'product': product}
    return TemplateResponse(request, 'dashboard/product/form.html', ctx)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def product_delete(request, pk):
    product = get_object_or_404(Product, pk=pk)
    if request.method == 'POST':
        product.delete()
        messages.success(
            request,
            pgettext_lazy('Dashboard message', 'Deleted product %s') % product)
        return redirect('dashboard:product-list')
    return TemplateResponse(
        request,
        'dashboard/product/modal/confirm_delete.html',
        {'product': product})
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def product_image_delete(request, product_pk, img_pk):
    product = get_object_or_404(Product, pk=product_pk)
    image = get_object_or_404(product.images, pk=img_pk)
    if request.method == 'POST':
        image.delete()
        messages.success(
            request,
            pgettext_lazy(
                'Dashboard message',
                'Deleted image %s') % image.image.name)
        return redirect('dashboard:product-image-list', product_pk=product.pk)
    return TemplateResponse(
        request,
        'dashboard/product/product_image/modal/confirm_delete.html',
        {'product': product, 'image': image})
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def attribute_delete(request, pk):
    attribute = get_object_or_404(ProductAttribute, pk=pk)
    if request.method == 'POST':
        attribute.delete()
        messages.success(
            request,
            pgettext_lazy(
                'Dashboard message',
                'Deleted attribute %s') % (attribute.name,))
        return redirect('dashboard:product-attributes')
    return TemplateResponse(
        request,
        'dashboard/product/product_attribute/modal/confirm_delete.html',
        {'attribute': attribute})
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def stock_location_delete(request, location_pk):
    location = get_object_or_404(StockLocation, pk=location_pk)
    stock_count = location.stock_set.count()
    if request.method == 'POST':
        location.delete()
        messages.success(
            request, pgettext_lazy(
                'Dashboard message for stock location',
                'Deleted location %s') % location)
        return redirect('dashboard:product-stock-location-list')
    ctx = {'location': location, 'stock_count': stock_count}
    return TemplateResponse(
        request,
        'dashboard/product/stock_location/modal/confirm_delete.html',
        ctx)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def clean_location(self):
        location = self.cleaned_data['location']
        if (
            not self.instance.pk and
                self.variant.stock.filter(location=location).exists()):
            self.add_error(
                'location',
                pgettext_lazy(
                    'stock form error',
                    'Stock item for this location and variant already exists'))
        return location
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def clean(self):
        data = super(ProductClassForm, self).clean()
        has_variants = self.cleaned_data['has_variants']
        product_attr = set(self.cleaned_data['product_attributes'])
        variant_attr = set(self.cleaned_data['variant_attributes'])
        if not has_variants and len(variant_attr) > 0:
            msg = pgettext_lazy(
                'Product class form error',
                'Product variants are disabled.')
            self.add_error('variant_attributes', msg)
        if len(product_attr & variant_attr) > 0:
            msg = pgettext_lazy(
                'Product class form error',
                'A single attribute can\'t belong to both a product '
                'and its variant.')
            self.add_error('variant_attributes', msg)

        if self.instance.pk:
            variants_changed = not (self.fields['has_variants'].initial ==
                                    has_variants)
            if variants_changed:
                query = self.instance.products.all()
                query = query.annotate(variants_counter=Count('variants'))
                query = query.filter(variants_counter__gt=1)
                if query.exists():
                    msg = pgettext_lazy(
                        'Product class form error',
                        'Some products of this type have more than '
                        'one variant.')
                    self.add_error('has_variants', msg)
        return data
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        self.product_attributes = []
        super(ProductForm, self).__init__(*args, **kwargs)
        self.fields['categories'].widget.attrs['data-placeholder'] = (
            pgettext_lazy('Product form placeholder', 'Search'))
        product_class = self.instance.product_class
        self.product_attributes = product_class.product_attributes.all()
        self.product_attributes = self.product_attributes.prefetch_related(
            'values')
        self.prepare_fields_for_attributes()
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def release_payment(request, order_pk, payment_pk):
    order = get_object_or_404(Order, pk=order_pk)
    payment = get_object_or_404(order.payments, pk=payment_pk)
    form = ReleasePaymentForm(request.POST or None, payment=payment)
    if form.is_valid() and form.release():
        msg = pgettext_lazy('Dashboard message', 'Released payment')
        payment.order.create_history_entry(comment=msg, user=request.user)
        messages.success(request, msg)
        return redirect('dashboard:order-details', order_pk=order.pk)
    status = 400 if form.errors else 200
    ctx = {'captured': payment.captured_amount, 'currency': payment.currency,
           'form': form, 'order': order, 'payment': payment}
    return TemplateResponse(request, 'dashboard/order/modal/release.html', ctx,
                            status=status)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def orderline_change_quantity(request, order_pk, line_pk):
    order = get_object_or_404(Order, pk=order_pk)
    item = get_object_or_404(OrderedItem.objects.filter(
        delivery_group__order=order), pk=line_pk)
    variant = get_object_or_404(
        ProductVariant, sku=item.product_sku)
    form = ChangeQuantityForm(
        request.POST or None, instance=item, variant=variant)
    status = 200
    old_quantity = item.quantity
    if form.is_valid():
        with transaction.atomic():
            form.save()
        msg = pgettext_lazy(
            'Dashboard message related to an order line',
            'Changed quantity for product %(product)s from'
            ' %(old_quantity)s to %(new_quantity)s') % {
                'product': item.product, 'old_quantity': old_quantity,
                'new_quantity': item.quantity}
        order.create_history_entry(comment=msg, user=request.user)
        messages.success(request, msg)
        return redirect('dashboard:order-details', order_pk=order.pk)
    elif form.errors:
        status = 400
    ctx = {'order': order, 'object': item, 'form': form}
    template = 'dashboard/order/modal/change_quantity.html'
    return TemplateResponse(request, template, ctx, status=status)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def orderline_split(request, order_pk, line_pk):
    order = get_object_or_404(Order, pk=order_pk)
    item = get_object_or_404(OrderedItem.objects.filter(
        delivery_group__order=order), pk=line_pk)
    form = MoveItemsForm(request.POST or None, item=item)
    line_pk = None
    if item:
        line_pk = item.pk
    status = 200
    if form.is_valid():
        old_group = item.delivery_group
        how_many = form.cleaned_data['quantity']
        with transaction.atomic():
            target_group = form.move_items()
        if not old_group.pk:
            old_group = pgettext_lazy(
                'Dashboard message related to a delivery group',
                'removed group')
        msg = pgettext_lazy(
            'Dashboard message related to delivery groups',
            'Moved %(how_many)s items %(item)s from %(old_group)s'
            ' to %(new_group)s') % {
                'how_many': how_many, 'item': item, 'old_group': old_group,
                'new_group': target_group}
        order.create_history_entry(comment=msg, user=request.user)
        messages.success(request, msg)
        return redirect('dashboard:order-details', order_pk=order.pk)
    elif form.errors:
        status = 400
    ctx = {'order': order, 'object': item, 'form': form, 'line_pk': line_pk}
    template = 'dashboard/order/modal/split_order_line.html'
    return TemplateResponse(request, template, ctx, status=status)
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def clean(self):
        if self.payment.status != PaymentStatus.PREAUTH:
            raise forms.ValidationError(
                pgettext_lazy(
                    'Payment form error',
                    'Only pre-authorized payments can be captured'))
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def capture(self):
        amount = self.cleaned_data['amount']
        try:
            self.payment.capture(amount.gross)
        except (PaymentError, ValueError) as e:
            self.add_error(
                None,
                pgettext_lazy(
                    'Payment form error',
                    'Payment gateway error: %s') % e.message)
            return False
        return True
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def refund(self):
        amount = self.cleaned_data['amount']
        try:
            self.payment.refund(amount.gross)
        except (PaymentError, ValueError) as e:
            self.add_error(
                None,
                pgettext_lazy(
                    'Payment form error',
                    'Payment gateway error: %s') % e.message)
            return False
        return True
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def clean(self):
        if self.payment.status != PaymentStatus.PREAUTH:
            raise forms.ValidationError(
                pgettext_lazy(
                    'Payment form error',
                    'Only pre-authorized payments can be released'))
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def release(self):
        try:
            self.payment.release()
        except (PaymentError, ValueError) as e:
            self.add_error(
                None,
                pgettext_lazy(
                    'Payment form error',
                    'Payment gateway error: %s') % e.message)
            return False
        return True
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def get_delivery_group_choices(self):
        group = self.item.delivery_group
        groups = group.order.groups.exclude(pk=group.pk).exclude(
            status='cancelled')
        choices = [(self.NEW_SHIPMENT, pgettext_lazy(
            'Delivery group value for `target_group` field',
            'New shipment'))]
        choices.extend([(g.pk, str(g)) for g in groups])
        return choices
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        super(ShipGroupForm, self).__init__(*args, **kwargs)
        self.fields['tracking_number'].widget.attrs.update(
            {'placeholder': pgettext_lazy(
                'Ship group form field placeholder',
                'Parcel tracking number')})
项目:planet-b-saleor    作者:planet-b    | 项目源码 | 文件源码
def clean(self):
        data = super(CancelOrderForm, self).clean()
        if not self.order.can_cancel():
            raise forms.ValidationError(
                pgettext_lazy(
                    'Cancel order form error',
                    'This order can\'t be cancelled'))
        return data