Last active
December 30, 2020 07:43
-
-
Save LowerDeez/2e3ad004654e1338af85515900869784 to your computer and use it in GitHub Desktop.
Django. Custom views for admin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@admin.register(Order) | |
class OrderAdmin(admin.ModelAdmin): | |
list_display = [ | |
'name', | |
'status', | |
'add_product_action', | |
'add_mattress_action' | |
] | |
list_display_links = ['name', 'status'] | |
readonly_fields = [ | |
'add_product_action', | |
'add_mattress_action', | |
] | |
search_fields = ['name'] | |
def get_urls(self): | |
urls = super().get_urls() | |
custom_urls = [ | |
url( | |
r'^(?P<order_id>.+)/add_matress/$', | |
self.admin_site.admin_view(self.add_mattress_view), | |
name='add_mattress_to_order', | |
), | |
url( | |
r'^(?P<order_id>.+)/add_product/$', | |
self.admin_site.admin_view(self.add_product_view), | |
name='add_product_to_order', | |
), | |
] | |
return custom_urls + urls | |
def add_product_action(self, obj): | |
return format_html( | |
'<a class="button" href="{}">{}</a>', | |
reverse('admin:add_product_to_order', args=[obj.pk]), | |
_("Add product") | |
) | |
add_product_action.short_description = _('Add product') | |
add_product_action.allow_tags = True | |
def add_mattress_action(self, obj): | |
return format_html( | |
'<a class="button" href="{}">{}</a>', | |
reverse('admin:add_mattress_to_order', args=[obj.pk]), | |
_("Add mattress") | |
) | |
add_mattress_action.short_description = _('Add mattress') | |
add_mattress_action.allow_tags = True | |
def add_mattress_view(self, request, order_id, **kwargs): | |
order = self.get_object(request, order_id) | |
if request.method != 'POST': | |
form = AddMattressToOrderForm() | |
else: | |
form = AddMattressToOrderForm(request.POST) | |
if form.is_valid(): | |
form.save(order) | |
self.message_user(request, _('Success. Mattress was added to order.')) | |
url = reverse( | |
'admin:orders_order_change', | |
args=[order_id], | |
current_app=self.admin_site.name, | |
) | |
return HttpResponseRedirect(url) | |
context = self.admin_site.each_context(request) | |
context['opts'] = self.model._meta | |
context['form'] = form | |
context['title'] = _('Add mattress to order') | |
return TemplateResponse( | |
request, | |
'admin/custom_action_form.html', | |
context, | |
) | |
def add_product_view(self, request, order_id, **kwargs): | |
order = self.get_object(request, order_id) | |
if request.method != 'POST': | |
form = AddProductToOrderForm() | |
else: | |
form = AddProductToOrderForm(request.POST) | |
if form.is_valid(): | |
form.save(order) | |
self.message_user(request, _('Success. Mattress was product to order.')) | |
url = reverse( | |
'admin:orders_order_change', | |
args=[order_id], | |
current_app=self.admin_site.name, | |
) | |
return HttpResponseRedirect(url) | |
context = self.admin_site.each_context(request) | |
context['opts'] = self.model._meta | |
context['form'] = form | |
context['title'] = _('Add product to order') | |
return TemplateResponse( | |
request, | |
'admin/custom_action_form.html', | |
context, | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "admin/change_form.html" %} | |
{% load i18n admin_static admin_modify %} | |
{% block content %} | |
<div id="content-main"> | |
<form action="" method="POST"> | |
{% csrf_token %} | |
{% if form.non_field_errors|length > 0 %} | |
<p class="errornote"> | |
"Please correct the errors below." | |
</p> | |
{{ form.non_field_errors }} | |
{% endif %} | |
<fieldset class="module aligned"> | |
{% for field in form %} | |
<div class="form-row"> | |
{{ field.errors }} | |
{{ field.label_tag }} | |
{{ field }} | |
{% if field.field.help_text %} | |
<p class="help"> | |
{{ field.field.help_text|safe }} | |
</p> | |
{% endif %} | |
</div> | |
{% endfor %} | |
</fieldset> | |
<div class="submit-row"> | |
<input type="submit" class="default" value="Submit"> | |
</div> | |
</form> | |
</div> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from decimal import Decimal | |
from django import forms | |
from django.forms import ModelChoiceField | |
from apps.mattress.models import MattressSizePrice | |
from apps.orders.const import BLOCK_READY | |
from apps.orders.models import OrderProducts, MattressInWork, OrderMattress | |
from apps.products.models import ProductSizePrice | |
__all__ = ( | |
'AddMattressToOrderForm', | |
'AddProductToOrderForm', | |
'MattressInWorkAdminForm' | |
) | |
class AddMattressToOrderForm(forms.Form): | |
size_mattress = ModelChoiceField( | |
queryset=MattressSizePrice.objects.all(), | |
required=True | |
) | |
def save(self, order): | |
order_mattress = OrderMattress.objects.create( | |
order=order, | |
mattress_size=self.cleaned_data['size_mattress'], | |
build=self.cleaned_data['size_mattress'].mattress.build, | |
price=Decimal(self.cleaned_data['size_mattress'].price) | |
) | |
return order_mattress | |
class AddProductToOrderForm(forms.Form): | |
size_product = ModelChoiceField( | |
queryset=ProductSizePrice.objects.all(), | |
required=True | |
) | |
def save(self, order): | |
order_product = OrderProducts.objects.create( | |
order=order, | |
product_size=self.cleaned_data['size_product'], | |
price=Decimal(self.cleaned_data['size_product'].price) | |
) | |
return order_product |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment