custom dependent select

admin.py, and forms.py
mail@pastecode.io avatar
unknown
python
2 years ago
4.4 kB
2
Indexable
Never



# admin.py
class OrderAdmin(admin.ModelAdmin):
    form = OrderAdminForm
    # change_form_template = 'admin/cargo/order/custom_form.html'
    actions = [send_mass_invoices]

    list_display = ['invoice_no', 'order_sender', 'order_total_price', 'order_payment', 'send_single_invoice']
    list_filter = ['order_sender__user', 'created', 'order_payment']
    jazzmin_section_order = list_display

    fieldsets = (
        (None, { 'fields': ('order_sender', 'invoice_no', 'order_partner', 'order_shipping_variant', 'order_weight', 'order_notes', 'order_payment', 'order_total_price') }),
        ('Asal', {'fields': ('orig_province', 'orig_city')}),
        ('Tujuan', {'fields': ('dest_province', 'dest_city', 'dest_subdistrict',)}),
    )

    class Media:
        js = [
            'https://code.jquery.com/jquery-3.6.1.min.js',
            # 'https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/bootstrap-select.min.js',
            '/static/js/national-address-chain.js'
        ]

    # def add_view(self, request, form_url="", extra_context=None):
    #     extra_context = extra_context or {}
    #     extra_context['form'] = self.get_form(request)
    #     return super(OrderAdmin, self).add_view(request, form_url=form_url, extra_context=extra_context)

    def user_sender_name(self, obj):
        return obj.order_sender.fullname

    def get_urls(self):
        urls = super().get_urls()
        custom_urls = [
            path(
                'send_invoice/<int:id>',
                self.admin_site.admin_view(self.process_invoice),
                name='send_invoice'
            )
        ]
        return custom_urls + urls

    def send_single_invoice(self, obj):
        if obj.order_payment == 'NoPayment':
            return format_html(
                '<a  class="button" style="background-color:blue; color: white; padding: 5px; border-radius:5px;" href="{}">Send Invoice</a>&nbsp;',
                reverse('admin:send_invoice', args=[obj.pk]),
            )
        else:
            # return obj.order_payment
            return format_html(
                f'<a style="background-color:green; color: white; padding: 5px; border-radius:5px;" class="button">{obj.get_order_payment_display()}</a>&nbsp;',
            )

    send_single_invoice.short_description = 'Send Invoice'
    send_single_invoice.allow_tags = True

    def process_invoice(self, request, id, *args, **kwargs):
        obj = Order.objects.get(id=id)
        obj.order_payment = 'SendInvoice'
        obj.save()
        messages.success(request, 'Invoice has been sent!')
        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))



# forms.py
# this form is used for the admin dashboard, the goal is to make dependent select in the given addresses 
class OrderAdminForm(forms.ModelForm):
    class Meta:
        model = Order
        fields = [
            'order_from_address',
            'order_to_address',
            'order_weight',
            'order_shipping_variant',
            'orig_province',
            'orig_city',
            'dest_province',
            'dest_city',
            'dest_subdistrict',
            'order_notes'
        ]
        
    order_total_price = forms.CharField(label='Biaya Kirim Total', help_text='Automatic filled',
                                        widget=forms.TextInput({'readonly': True}))

    orig_province = forms.ChoiceField(
        label='Provinsi Asal')
    orig_city = forms.CharField(label='Kota Asal')
    dest_province = forms.CharField(label='Provinsi Tujuan')
    dest_city = forms.CharField(label='Kota Tujuan')
    dest_subdistrict = forms.CharField(label='Kecamatan Tujuan')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for f in ['orig_province', 'orig_city', 'dest_province', 'dest_city', 'dest_subdistrict', ]:
            self.fields[f].show_hidden_initial = True
            self.fields[f].initial = '-----'
            selectAttrs = {'class': 'selectpicker',
                           'data-live-search': 'true',
                           'title': '-----'}
            if f != 'orig_province':
                selectAttrs['disabled'] = 'true'
            self.fields[f].widget = forms.Select(attrs=selectAttrs)
        self.fields['orig_province'].choices = [
            (p, p.title()) for p in SicepatPrice().get_orig_province()]