Merge pull request #133 from SchrodingersGat/part-files

Part files
This commit is contained in:
Oliver 2019-04-18 00:23:09 +10:00 committed by GitHub
commit 6b05078fd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 9 deletions

View File

@ -2,6 +2,7 @@ from django.contrib import admin
from import_export.admin import ImportExportModelAdmin
from .models import PartCategory, Part
from .models import PartAttachment
from .models import SupplierPart
from .models import BomItem
@ -16,6 +17,11 @@ class PartCategoryAdmin(admin.ModelAdmin):
list_display = ('name', 'pathstring', 'description')
class PartAttachmentAdmin(admin.ModelAdmin):
list_display = ('part', 'attachment')
class BomItemAdmin(ImportExportModelAdmin):
list_display = ('part', 'sub_part', 'quantity')
@ -35,5 +41,6 @@ class ParameterAdmin(admin.ModelAdmin):
admin.site.register(Part, PartAdmin)
admin.site.register(PartCategory, PartCategoryAdmin)
admin.site.register(PartAttachment, PartAttachmentAdmin)
admin.site.register(BomItem, BomItemAdmin)
admin.site.register(SupplierPart, SupplierPartAdmin)

View File

@ -310,15 +310,8 @@ class Part(models.Model):
def attach_file(instance, filename):
base = 'part_files'
# TODO - For a new PartAttachment object, PK is NULL!!
# Prefix the attachment ID to the filename
fn = "{id}_{fn}".format(id=instance.pk, fn=filename)
return os.path.join(base, fn)
# Construct a path to store a file attachment
return os.path.join('part_files', str(instance.part.id), filename)
class PartAttachment(models.Model):
@ -331,6 +324,10 @@ class PartAttachment(models.Model):
attachment = models.FileField(upload_to=attach_file, null=True, blank=True)
@property
def basename(self):
return os.path.basename(self.attachment.name)
class BomItem(models.Model):
""" A BomItem links a part to its component items.

View File

@ -62,6 +62,13 @@
</tr>
{% endif %}
</table>
<h4>Attachments</h4>
<ul>
{% for attachment in part.attachments.all %}
<li><a href="/media/{{ attachment.attachment }}">{{ attachment.basename }}</a></li>
{% endfor %}
</ul>
</div>
</div>