Added image field to part

- Images are automatically renamed upon upload
- Part page displays img
This commit is contained in:
Oliver 2018-04-14 17:44:22 +10:00
parent bc7e29aeb4
commit 72b44e15f4
6 changed files with 64 additions and 16 deletions

3
.gitignore vendored
View File

@ -27,3 +27,6 @@ var/
local_settings.py
*.sqlite3
**/migrations/*
# Local media storage (only when running in development mode)
InvenTree/media

View File

@ -135,3 +135,8 @@ USE_TZ = True
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

View File

@ -76,4 +76,11 @@ urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^auth/', include('rest_framework.urls', namespace='rest_framework')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
]
# Static file access
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
if settings.DEBUG:
# Media file access
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View File

@ -6,6 +6,8 @@ from django.core.validators import MinValueValidator
from InvenTree.models import InvenTreeTree
import os
#from django.db.models.signals.pre_delete
#from django.dispatch import receiver
@ -21,15 +23,24 @@ class PartCategory(InvenTreeTree):
def parts(self):
return self.part_set.all()
"""
@receiver(pre_delete, sender=PartCategory)
def reset_tag(sender, **kwargs):
cat = kwargs['instance']
for book in books.filter(tag=tag):
book.tag = book.author.tags.first()
book.save()
"""
# Function to automatically rename a part image on upload
# Format: part_pk.<img>
def rename_part_image(instance, filename):
base = 'part_images'
if filename.count('.') > 0:
ext = filename.split('.')[-1]
else:
ext = ''
fn = 'part_{pk}_img'.format(pk=instance.pk)
if ext:
fn += '.' + ext
return os.path.join(base, fn)
class Part(models.Model):
""" Represents an abstract part
@ -54,6 +65,8 @@ class Part(models.Model):
null=True, blank=True,
on_delete=models.SET_NULL)
image = models.ImageField(upload_to=rename_part_image, max_length=255, null=True, blank=True)
# Minimum "allowed" stock level
minimum_stock = models.PositiveIntegerField(default=0, validators=[MinValueValidator(0)])

View File

@ -4,13 +4,26 @@
{% include "part/cat_link.html" with category=part.category %}
<a href="{% url 'part-detail' part.id %}">{{ part.name }}</a>
<br>
{{ part.description }}
<br>
IPN: {% if part.IPN %}{{ part.IPN }}{% else %}N/A{% endif %}
<br>
<div class="media">
<img class="mr-3"
{% if part.image %}
src="{{ part.image.url }}"
{% else %}
src="/media/part_images/missing.png"
{% endif %}/>
<div class="media-body">
<h5>{{ part.name }}</h5>
{% if part.description %}
<p><i>{{ part.description }}</i></p>
{% endif %}
{% if part.IPN %}
<p><b>IPN:</b> {{ part.IPN }}</p>
{% endif %}
{% if part.URL %}
<p>{% include 'url.html' with url=part.URL %}</p>
{% endif %}
</div>
</div>
{% block details %}

View File

@ -0,0 +1,7 @@
{% if url %}
<a href="{{ url }}">
{% if text %}{{ text }}
{% else %}{{ url }}
{% endif %}
</a>
{% endif %}