Skip to content

Commit

Permalink
fix image model
Browse files Browse the repository at this point in the history
  • Loading branch information
Sascha Dobbelaere committed Jan 2, 2024
1 parent bd6386c commit c5939d6
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.6 on 2024-01-02 11:06

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('contacts', '0004_alter_company_options_alter_internalcompany_options_and_more'),
]

operations = [
migrations.AlterModelOptions(
name='address',
options={'verbose_name_plural': 'addresses'},
),
migrations.AlterModelOptions(
name='invoiceaddress',
options={'verbose_name_plural': 'invoice addresses'},
),
migrations.AlterModelOptions(
name='shippingaddress',
options={'verbose_name_plural': 'shipping addresses'},
),
]
19 changes: 19 additions & 0 deletions OneSila/core/migrations/0013_alter_multitenantuser_timezone.py

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions OneSila/core/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from core.models import MultiTenantCompany
from django.contrib.auth import get_user_model
from model_bakery import baker
from django.test import TestCase as DjangoTestCase


class TestCaseMixin:
def setUp(self):
self.multi_tenant_company = baker.make(MultiTenantCompany)
self.user = baker.make(get_user_model(), multi_tenant_company=self.multi_tenant_company)


class TestCase(TestCaseMixin, DjangoTestCase):
pass
17 changes: 17 additions & 0 deletions OneSila/inventory/migrations/0002_alter_inventory_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.6 on 2024-01-02 11:06

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('inventory', '0001_initial'),
]

operations = [
migrations.AlterModelOptions(
name='inventory',
options={'verbose_name_plural': 'inventories'},
),
]
9 changes: 0 additions & 9 deletions OneSila/media/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ def sanitize_media_filename(filename):
return filename


def get_media_folder_upload_path(instance, filename):
'''Return a dynmic path based on the selection location. And clean the filename'''
path = os.path.join('media_files/', instance.image_location.path, sanitize_media_filename(filename))

logger.debug("Upload path {}".format(path))

return path


def is_landscape(w, h):
'''
Returns True if image is landscape
Expand Down
2 changes: 2 additions & 0 deletions OneSila/media/image_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from imagekit.processors import ResizeToFill, ResizeToFit
from imagekit.utils import get_field_info

import os


class ImageWebSpec(ImageSpec):
'''
Expand Down
7 changes: 3 additions & 4 deletions OneSila/media/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

from django.db import migrations, models
import django.db.models.deletion
import media.helpers
import media.validators
import core.validators


class Migration(migrations.Migration):
Expand All @@ -25,8 +24,8 @@ class Migration(migrations.Migration):
('type', models.CharField(choices=[('IMAGE', 'Image'), ('VIDEO', 'Video')], max_length=5)),
('video_url', models.URLField()),
('image_type', models.CharField(choices=[('MOOD', 'Mood Shot'), ('PACK', 'Pack Shot')], default='PACK', max_length=4)),
('image', models.ImageField(upload_to=media.helpers.get_media_folder_upload_path, validators=[
media.validators.validate_image_extension], verbose_name='Image (High resolution)')),
('image', models.ImageField(upload_to='images/', validators=[
core.validators.validate_image_extension], verbose_name='Image (High resolution)')),
('multi_tenant_company', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='core.multitenantcompany')),
],
options={
Expand Down
20 changes: 20 additions & 0 deletions OneSila/media/migrations/0002_alter_media_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.6 on 2024-01-02 11:06

import core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('media', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='media',
name='image',
field=models.ImageField(upload_to='images/', validators=[core.validators.validate_image_extension,
core.validators.no_dots_in_filename], verbose_name='Image (High resolution)'),
),
]
24 changes: 8 additions & 16 deletions OneSila/media/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill, ResizeToFit

from .validators import no_dots_in_filename, validate_image_extension
from .helpers import get_media_folder_upload_path, is_landscape
from get_absolute_url.helpers import generate_absolute_url

from core.validators import no_dots_in_filename, validate_image_extension
from .image_specs import ImageWebSpec
from .managers import ImageManager, VideoManager

Expand Down Expand Up @@ -44,15 +45,12 @@ class Media(models.Model):

image_type = models.CharField(max_length=4, choices=IMAGE_TYPE_CHOICES, default=PACK_SHOT)
image = models.ImageField(_('Image (High resolution)'),
upload_to=get_media_folder_upload_path, validators=[validate_image_extension])
upload_to='images/', validators=[validate_image_extension, no_dots_in_filename])

image_web = ImageSpecField(source='image',
id='mediapp:image:imagewebspec')

products = models.ManyToManyField('products.Product', through='MediaProductThrough')
# symmetrical=False,
# blank=True,
# related_name='bundles')

objects = models.Manager()
videos = VideoManager()
Expand All @@ -62,17 +60,11 @@ class Media(models.Model):
def image_web_size(self):
return self.image_web.file.image_web.size

def __str__(self):
return self.image.name

def __url__(self):
from django.conf import settings
def image_web_url(self):
if self.image:
return f"{generate_absolute_url(trailing_slash=False)}{self.image_web.url}"

url = 'https://{}{}{}'.format(
settings.DOMAIN_PRODUCTION[-1], # Not sure how to identify the current host. So take the production one for now.
settings.MEDIA_URL,
self.image.name)
return url
return None


class Image(Media):
Expand Down
7 changes: 5 additions & 2 deletions OneSila/media/schema/types/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from core.schema.core.types.types import relay, type, GetQuerysetMultiTenantMixin
from strawberry_django.fields.types import DjangoImageType

from typing import List

Expand All @@ -11,12 +12,14 @@

@type(Media, filters=MediaFilter, order=MediaOrder, pagination=True, fields="__all__")
class MediaType(relay.Node, GetQuerysetMultiTenantMixin):
pass
image_web: DjangoImageType | None
image_web_url: str | None


@type(Image, filters=ImageFilter, order=ImageOrder, pagination=True, fields="__all__")
class ImageType(relay.Node, GetQuerysetMultiTenantMixin):
pass
image_web: DjangoImageType | None
image_web_url: str | None


@type(Video, filters=VideoFilter, order=VideoOrder, pagination=True, fields="__all__")
Expand Down
28 changes: 28 additions & 0 deletions OneSila/media/tests/tests_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from media.models import Media
from core.tests import TestCase
from model_bakery import baker
from django.core.files import File
from django.conf import settings
import os


class MediaTestCase(TestCase):
def test_image_web_url_none(self):
image = baker.make(Media, image=None)
url = image.image_web_url()
self.assertTrue(url is None)

def test_image_web_url(self):
fname = 'red.png'
image_path = os.path.join(settings.BASE_DIR.parent, 'core', 'tests', 'image_files', fname)
image = baker.make(Media)

with open(image_path, 'rb') as f:
image.image.save(fname, File(f))
image.full_clean()
image.save()
url = image.image_web_url()

# Cached images are converted to jpg, no matter what the source.
self.assertTrue(url.endswith('.jpg'))
self.assertTrue(url.startswith('http'))
19 changes: 0 additions & 19 deletions OneSila/media/validators.py

This file was deleted.

1 change: 0 additions & 1 deletion OneSila/sales_prices/factories/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from io import BytesIO
from datetime import datetime
from xlsxwriter.workbook import Workbook
import socket


class SalesPriceListItemGeneratorUpdater:
Expand Down

0 comments on commit c5939d6

Please sign in to comment.