summaryrefslogtreecommitdiff
path: root/procurement/admin.py
blob: f35d7061edf4556f0bf6c6d37cf70b8ab9f054dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from django.contrib import admin
from django.shortcuts import render_to_response, get_object_or_404

from procurement.admin_forms import ComponentAdminForm
from procurement.models import Supplier, Component, Representative

class RepresentativeAdmin(admin.ModelAdmin):
    list_display = ('name', 'email', 'supplier', 'updated')
    ordering = ("supplier",)

class RepresentativeInline(admin.TabularInline):
    model = Representative

class SupplierAdmin(admin.ModelAdmin):
    list_display = ('name', 'get_representatives', 'is_authorized', 'updated')
    filter_horizonal = ('components',)
    inlines = [RepresentativeInline]

    def get_representatives(self, obj):
        return list(str(x) for x in obj.representatives.all())
    get_representatives.short_description = "Representatives"

class ComponentAdmin(admin.ModelAdmin):
    list_display = ('name', 'sku', 'updated')
    form = ComponentAdminForm
    source_components_template = 'procurement/admin_templates/source_components.html'

    def source_components(self, request, pk):
        component = get_object_or_404(Component, pk=pk)

        return render_to_response(self.source_components_template, {
            'title': 'Source Suppliers for: %s' % component,
            'opts': self.model._meta,
            'component': component,
            'supplier_results': component.suppliers.filter(is_authorized=True),

        })


admin.site.register(Representative, RepresentativeAdmin)
admin.site.register(Supplier, SupplierAdmin)
admin.site.register(Component, ComponentAdmin)