From 95032141ce4c7384f1731b23bb0276adec003bc9 Mon Sep 17 00:00:00 2001
From: Oliver Walters <oliver.henry.walters@gmail.com>
Date: Tue, 7 Apr 2020 11:38:57 +1000
Subject: [PATCH] Toot toot! It's the refactor tractor.

- Create helper functions to qualify media and static files
---
 InvenTree/InvenTree/helpers.py | 35 ++++++++++++++++++++++++++++++++++
 InvenTree/company/models.py    | 11 ++++++-----
 InvenTree/part/models.py       | 10 +++++-----
 3 files changed, 46 insertions(+), 10 deletions(-)

diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py
index 5492f13a05..d46f35daf4 100644
--- a/InvenTree/InvenTree/helpers.py
+++ b/InvenTree/InvenTree/helpers.py
@@ -14,6 +14,41 @@ from django.core.exceptions import ValidationError
 from django.utils.translation import ugettext as _
 
 from .version import inventreeVersion, inventreeInstanceName
+from .settings import MEDIA_URL, STATIC_URL
+
+
+def getMediaUrl(filename):
+    """
+    Return the qualified access path for the given file,
+    under the media directory.
+    """
+
+    return os.path.join(MEDIA_URL, str(filename))
+
+
+def getStaticUrl(filename):
+    """
+    Return the qualified access path for the given file,
+    under the static media directory.
+    """
+
+    return os.path.join(STATIC_URL, str(filename))
+
+
+def getBlankImage():
+    """
+    Return the qualified path for the 'blank image' placeholder.
+    """
+
+    return getStaticUrl("img/blank_image.png")
+
+
+def getBlankThumbnail():
+    """
+    Return the qualified path for the 'blank image' thumbnail placeholder.
+    """
+
+    return getStaticUrl("img/blank_image.thumbnail.png")
 
 
 def TestIfImage(img):
diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py
index 69c98a008a..0493cbbfa1 100644
--- a/InvenTree/company/models.py
+++ b/InvenTree/company/models.py
@@ -23,6 +23,7 @@ from markdownx.models import MarkdownxField
 
 from stdimage.models import StdImageField
 
+from InvenTree.helpers import getMediaUrl, getBlankImage, getBlankThumbnail
 from InvenTree.fields import InvenTreeURLField, RoundingDecimalField
 from InvenTree.status_codes import OrderStatus
 from common.models import Currency
@@ -118,18 +119,18 @@ class Company(models.Model):
         """ Return the URL of the image for this company """
 
         if self.image:
-            return os.path.join(settings.MEDIA_URL, str(self.image.url))
+            return getMediaUrl(self.image.url)
         else:
-            return os.path.join(settings.STATIC_URL, 'img/blank_image.png')
+            return getBlankImage()
 
     def get_thumbnail_url(self):
         """ Return the URL for the thumbnail image for this Company """
 
         if self.image:
-            return os.path.join(settings.MEDIA_URL, str(self.image.thumbnail.url))
+            return getMediaUrl(self.image.thumbnail.url)
         else:
-            return os.path.join(settings.STATIC_URL, 'img/blank_image.thumbnail.png')
-
+            return getBlankThumbnail()
+            
     @property
     def part_count(self):
         """ The number of parts supplied by this company """
diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py
index da0bff16b8..f255d2a77b 100644
--- a/InvenTree/part/models.py
+++ b/InvenTree/part/models.py
@@ -300,9 +300,9 @@ class Part(models.Model):
         """ Return the URL of the image for this part """
 
         if self.image:
-            return os.path.join(settings.MEDIA_URL, str(self.image.url))
+            return helpers.getMediaUrl(self.image.url)
         else:
-            return os.path.join(settings.STATIC_URL, 'img/blank_image.png')
+            return helpers.getBlankImage()
 
     def get_thumbnail_url(self):
         """
@@ -310,10 +310,10 @@ class Part(models.Model):
         """
 
         if self.image:
-            return os.path.join(settings.MEDIA_URL, str(self.image.thumbnail.url))
+            return helpers.getMediaUrl(self.image.thumbnail.url)
         else:
-            return os.path.join(settings.STATIC_URL, 'img/blank_image.thumbnail.png')
-
+            return helpers.getBlankThumbnail()
+            
     def validate_unique(self, exclude=None):
         """ Validate that a part is 'unique'.
         Uniqueness is checked across the following (case insensitive) fields: