summaryrefslogtreecommitdiff
path: root/procurement/views.py
blob: c1e66c9ff226b197db06942ce08dd45f00ff7da3 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from django.views.generic import FormView, TemplateView

from procurement.forms import ComponentSearchForm, SupplierSearchForm
from procurement.models import Supplier, Component, Representative


class ComponentSearchView(FormView):
    template_name = 'procurement/source_components.html'
    form_class = ComponentSearchForm

    component = None
    supplier_results = None

    def get_context_data(self):
        context = super().get_context_data()

        try:
            suppliers_last_updated = Supplier.objects.latest('updated').time_since_update
        except Supplier.DoesNotExist:
            suppliers_last_updated = ''

        try:
            representatives_last_updated = Representative.objects.latest('updated').time_since_update
        except Representative.DoesNotExist:
            representatives_last_updated = ''

        try:
            components_last_updated = Component.objects.latest('updated').time_since_update
        except Component.DoesNotExist:
            components_last_updated = ''

        context.update({
            'page_name': 'Component Search',
            'component': self.component,
            'supplier_results': self.supplier_results,
            'supplier_count': Supplier.objects.all().count(),
            'suppliers_last_updated': suppliers_last_updated,
            'component_count': Component.objects.all().count(),
            'components_last_updated': components_last_updated,
            'representative_count': Representative.objects.all().count(),
            'representatives_last_updated': representatives_last_updated,
        })
        return context

    def form_valid(self, form):
        self.component = form.cleaned_data['component']
        if self.component:
            self.supplier_results = self.component.suppliers.filter(is_authorized=True)

        return super(ComponentSearchView, self).get(self.request)

class SupplierSearchView(FormView):
    template_name = "procurement/view_suppliers.html"
    form_class = SupplierSearchForm

    supplier = None
    component_form = None

    def form_valid(self, form):
        self.supplier = form.cleaned_data['supplier']
        if self.supplier:
            self.component_form = ComponentSearchForm()
            self.component_form.fields["component"].queryset = self.supplier.components
        return super(SupplierSearchView, self).get(self.request)

    def get_context_data(self):
        context = super().get_context_data()
        context["supplier"] = self.supplier
        context["component_form"] = self.component_form
        context["page_name"] = "View Supplier Details"
        return context

class DocumentationView(TemplateView):
    template_name = 'procurement/documentation.html'

    def get_context_data(self):
        context = super().get_context_data()
        context.update({
            'page_name': 'Documentation',
        })
        return context