2019-09-13 12:39:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-03-25 12:07:43 +00:00
|
|
|
from django.contrib import admin
|
2019-09-13 12:39:15 +00:00
|
|
|
|
2019-09-13 12:29:11 +00:00
|
|
|
from import_export.admin import ImportExportModelAdmin
|
2019-09-13 11:39:37 +00:00
|
|
|
from import_export.resources import ModelResource
|
|
|
|
from import_export.fields import Field
|
2019-09-13 12:08:31 +00:00
|
|
|
import import_export.widgets as widgets
|
2017-03-25 12:07:43 +00:00
|
|
|
|
2018-04-12 06:27:26 +00:00
|
|
|
from .models import PartCategory, Part
|
2019-05-04 22:50:14 +00:00
|
|
|
from .models import PartAttachment, PartStar
|
2018-04-14 04:19:03 +00:00
|
|
|
from .models import BomItem
|
2019-08-20 03:02:00 +00:00
|
|
|
from .models import PartParameterTemplate, PartParameter
|
2017-03-28 12:31:41 +00:00
|
|
|
|
2019-09-13 12:08:31 +00:00
|
|
|
from stock.models import StockLocation
|
|
|
|
from company.models import SupplierPart
|
|
|
|
|
2019-04-17 14:20:49 +00:00
|
|
|
|
2019-09-13 11:39:37 +00:00
|
|
|
class PartResource(ModelResource):
|
2019-09-13 12:11:31 +00:00
|
|
|
""" Class for managing Part data import/export """
|
2019-09-13 11:39:37 +00:00
|
|
|
|
2019-09-13 13:15:34 +00:00
|
|
|
# ForeignKey fields
|
2019-09-13 12:08:31 +00:00
|
|
|
category = Field(attribute='category', widget=widgets.ForeignKeyWidget(PartCategory))
|
2019-09-13 11:39:37 +00:00
|
|
|
|
2019-09-13 12:08:31 +00:00
|
|
|
default_location = Field(attribute='default_location', widget=widgets.ForeignKeyWidget(StockLocation))
|
2019-09-13 11:39:37 +00:00
|
|
|
|
2019-09-13 13:15:34 +00:00
|
|
|
default_supplier = Field(attribute='default_supplier', widget=widgets.ForeignKeyWidget(SupplierPart))
|
2019-09-13 12:29:11 +00:00
|
|
|
|
2019-09-13 12:08:31 +00:00
|
|
|
category_name = Field(attribute='category__name', readonly=True)
|
|
|
|
|
|
|
|
variant_of = Field(attribute='variant_of', widget=widgets.ForeignKeyWidget(Part))
|
2019-09-13 11:39:37 +00:00
|
|
|
|
2019-09-15 09:52:28 +00:00
|
|
|
suppliers = Field(attribute='supplier_count', readonly=True)
|
|
|
|
|
2019-09-13 13:15:34 +00:00
|
|
|
# Extra calculated meta-data (readonly)
|
|
|
|
in_stock = Field(attribute='total_stock', readonly=True, widget=widgets.IntegerWidget())
|
|
|
|
|
|
|
|
on_order = Field(attribute='on_order', readonly=True, widget=widgets.IntegerWidget())
|
|
|
|
|
|
|
|
used_in = Field(attribute='used_in_count', readonly=True, widget=widgets.IntegerWidget())
|
|
|
|
|
|
|
|
allocated = Field(attribute='allocation_count', readonly=True, widget=widgets.IntegerWidget())
|
|
|
|
|
|
|
|
building = Field(attribute='quantity_being_built', readonly=True, widget=widgets.IntegerWidget())
|
|
|
|
|
2019-09-13 11:39:37 +00:00
|
|
|
class Meta:
|
|
|
|
model = Part
|
2019-09-13 12:11:31 +00:00
|
|
|
skip_unchanged = True
|
|
|
|
report_skipped = False
|
2019-09-15 09:58:05 +00:00
|
|
|
clean_model_instances = True
|
2019-09-13 11:39:37 +00:00
|
|
|
exclude = [
|
2019-09-13 12:11:31 +00:00
|
|
|
'bom_checksum', 'bom_checked_by', 'bom_checked_date'
|
2019-09-13 11:39:37 +00:00
|
|
|
]
|
|
|
|
|
2019-09-13 13:15:34 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
""" Prefetch related data for quicker access """
|
|
|
|
|
|
|
|
query = super().get_queryset()
|
|
|
|
query = query.prefetch_related(
|
|
|
|
'category',
|
|
|
|
'used_in',
|
|
|
|
'builds',
|
|
|
|
'supplier_parts__purchase_order_line_items',
|
|
|
|
'stock_items__allocations'
|
|
|
|
)
|
|
|
|
|
|
|
|
return query
|
|
|
|
|
2019-09-13 11:39:37 +00:00
|
|
|
|
2018-04-15 14:44:32 +00:00
|
|
|
class PartAdmin(ImportExportModelAdmin):
|
2020-01-31 09:28:54 +00:00
|
|
|
|
2019-09-13 11:39:37 +00:00
|
|
|
resource_class = PartResource
|
|
|
|
|
2019-05-12 02:16:04 +00:00
|
|
|
list_display = ('full_name', 'description', 'total_stock', 'category')
|
2017-03-27 10:03:46 +00:00
|
|
|
|
2019-09-13 13:27:22 +00:00
|
|
|
list_filter = ('active', 'assembly', 'is_template', 'virtual')
|
|
|
|
|
|
|
|
search_fields = ('name', 'description', 'category__name', 'category__description', 'IPN')
|
|
|
|
|
2017-03-28 06:53:54 +00:00
|
|
|
|
2019-09-13 12:20:08 +00:00
|
|
|
class PartCategoryResource(ModelResource):
|
|
|
|
""" Class for managing PartCategory data import/export """
|
|
|
|
|
|
|
|
parent = Field(attribute='parent', widget=widgets.ForeignKeyWidget(PartCategory))
|
|
|
|
|
|
|
|
parent_name = Field(attribute='parent__name', readonly=True)
|
|
|
|
|
2019-09-13 12:44:50 +00:00
|
|
|
default_location = Field(attribute='default_location', widget=widgets.ForeignKeyWidget(StockLocation))
|
|
|
|
|
2019-09-13 12:20:08 +00:00
|
|
|
class Meta:
|
|
|
|
model = PartCategory
|
|
|
|
skip_unchanged = True
|
|
|
|
report_skipped = False
|
2019-09-15 09:58:05 +00:00
|
|
|
clean_model_instances = True
|
2019-09-13 12:20:08 +00:00
|
|
|
|
|
|
|
exclude = [
|
|
|
|
# Exclude MPTT internal model fields
|
|
|
|
'lft', 'rght', 'tree_id', 'level',
|
|
|
|
]
|
|
|
|
|
|
|
|
def after_import(self, dataset, result, using_transactions, dry_run, **kwargs):
|
|
|
|
|
|
|
|
super().after_import(dataset, result, using_transactions, dry_run, **kwargs)
|
|
|
|
|
2019-09-13 12:44:50 +00:00
|
|
|
# Rebuild the PartCategory tree(s)
|
2019-09-13 12:20:08 +00:00
|
|
|
PartCategory.objects.rebuild()
|
|
|
|
|
2019-09-13 12:23:40 +00:00
|
|
|
|
2019-04-18 11:28:09 +00:00
|
|
|
class PartCategoryAdmin(ImportExportModelAdmin):
|
2017-04-11 07:21:05 +00:00
|
|
|
|
2019-09-13 12:20:08 +00:00
|
|
|
resource_class = PartCategoryResource
|
|
|
|
|
2018-04-13 12:36:59 +00:00
|
|
|
list_display = ('name', 'pathstring', 'description')
|
2017-04-11 07:21:05 +00:00
|
|
|
|
2019-09-13 13:32:49 +00:00
|
|
|
search_fields = ('name', 'description')
|
|
|
|
|
2018-04-15 15:02:17 +00:00
|
|
|
|
2019-04-17 13:58:13 +00:00
|
|
|
class PartAttachmentAdmin(admin.ModelAdmin):
|
|
|
|
|
2019-04-30 23:40:49 +00:00
|
|
|
list_display = ('part', 'attachment', 'comment')
|
2019-04-17 13:58:13 +00:00
|
|
|
|
|
|
|
|
2019-05-04 22:50:14 +00:00
|
|
|
class PartStarAdmin(admin.ModelAdmin):
|
|
|
|
|
|
|
|
list_display = ('part', 'user')
|
|
|
|
|
|
|
|
|
2019-09-13 12:23:40 +00:00
|
|
|
class BomItemResource(ModelResource):
|
|
|
|
""" Class for managing BomItem data import/export """
|
|
|
|
|
2020-02-11 11:32:36 +00:00
|
|
|
level = Field(attribute='level', readonly=True)
|
|
|
|
|
2020-02-11 23:18:20 +00:00
|
|
|
bom_id = Field(attribute='pk')
|
|
|
|
|
|
|
|
parent_part_id = Field(attribute='part', widget=widgets.ForeignKeyWidget(Part))
|
2019-09-13 12:23:40 +00:00
|
|
|
|
2020-02-11 11:32:36 +00:00
|
|
|
parent_part_name = Field(attribute='part__full_name', readonly=True)
|
2019-09-15 12:21:12 +00:00
|
|
|
|
2020-02-11 23:18:20 +00:00
|
|
|
sub_part_id = Field(attribute='sub_part', widget=widgets.ForeignKeyWidget(Part))
|
2019-09-15 12:21:12 +00:00
|
|
|
|
2019-09-15 12:23:28 +00:00
|
|
|
sub_part_name = Field(attribute='sub_part__full_name', readonly=True)
|
2019-09-15 12:21:12 +00:00
|
|
|
|
2020-02-11 11:32:36 +00:00
|
|
|
sub_assembly = Field(attribute='sub_part__assembly', readonly=True)
|
2019-09-13 12:23:40 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = BomItem
|
|
|
|
skip_unchanged = True
|
|
|
|
report_skipped = False
|
2019-09-15 09:58:05 +00:00
|
|
|
clean_model_instances = True
|
2019-09-13 12:23:40 +00:00
|
|
|
|
2020-02-11 23:18:20 +00:00
|
|
|
exclude = [
|
|
|
|
'checksum',
|
|
|
|
'id',
|
|
|
|
'part',
|
|
|
|
'sub_part',
|
|
|
|
]
|
2019-09-15 12:21:12 +00:00
|
|
|
|
2019-09-13 12:23:40 +00:00
|
|
|
|
2018-04-15 14:44:32 +00:00
|
|
|
class BomItemAdmin(ImportExportModelAdmin):
|
2019-09-13 12:23:40 +00:00
|
|
|
|
|
|
|
resource_class = BomItemResource
|
|
|
|
|
2018-04-15 15:02:17 +00:00
|
|
|
list_display = ('part', 'sub_part', 'quantity')
|
|
|
|
|
2019-09-13 13:32:49 +00:00
|
|
|
search_fields = ('part__name', 'part__description', 'sub_part__name', 'sub_part__description')
|
|
|
|
|
2018-04-14 04:19:03 +00:00
|
|
|
|
2019-08-20 08:04:22 +00:00
|
|
|
class ParameterTemplateAdmin(ImportExportModelAdmin):
|
2019-08-20 03:02:00 +00:00
|
|
|
list_display = ('name', 'units')
|
|
|
|
|
2017-03-29 04:02:59 +00:00
|
|
|
|
2019-09-13 12:27:32 +00:00
|
|
|
class ParameterResource(ModelResource):
|
|
|
|
""" Class for managing PartParameter data import/export """
|
|
|
|
|
|
|
|
part = Field(attribute='part', widget=widgets.ForeignKeyWidget(Part))
|
|
|
|
|
|
|
|
part_name = Field(attribute='part__name', readonly=True)
|
|
|
|
|
|
|
|
template = Field(attribute='template', widget=widgets.ForeignKeyWidget(PartParameterTemplate))
|
|
|
|
|
|
|
|
template_name = Field(attribute='template__name', readonly=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = PartParameter
|
|
|
|
skip_unchanged = True
|
|
|
|
report_skipped = False
|
2019-09-15 09:58:05 +00:00
|
|
|
clean_model_instance = True
|
2019-09-13 12:27:32 +00:00
|
|
|
|
|
|
|
|
2019-08-20 08:04:22 +00:00
|
|
|
class ParameterAdmin(ImportExportModelAdmin):
|
2019-09-13 12:27:32 +00:00
|
|
|
|
|
|
|
resource_class = ParameterResource
|
|
|
|
|
2019-08-20 03:02:00 +00:00
|
|
|
list_display = ('part', 'template', 'data')
|
2019-08-20 02:43:23 +00:00
|
|
|
|
2017-04-11 07:21:05 +00:00
|
|
|
|
2017-03-28 06:49:01 +00:00
|
|
|
admin.site.register(Part, PartAdmin)
|
2017-03-28 12:31:41 +00:00
|
|
|
admin.site.register(PartCategory, PartCategoryAdmin)
|
2019-04-17 13:58:13 +00:00
|
|
|
admin.site.register(PartAttachment, PartAttachmentAdmin)
|
2019-05-04 22:50:14 +00:00
|
|
|
admin.site.register(PartStar, PartStarAdmin)
|
2018-04-14 04:19:03 +00:00
|
|
|
admin.site.register(BomItem, BomItemAdmin)
|
2019-08-20 03:02:00 +00:00
|
|
|
admin.site.register(PartParameterTemplate, ParameterTemplateAdmin)
|
2019-08-20 02:43:23 +00:00
|
|
|
admin.site.register(PartParameter, ParameterAdmin)
|