Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

organization image (fixes #1068) #1095

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/apps/volontulo/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ class Meta: # pylint: disable=C0111
name = factory.fuzzy.FuzzyAttribute(_organization_name)
address = factory.Faker("address", locale="pl_PL")
description = factory.Faker("paragraph")
image = ImageField(from_path=os.path.join(
os.path.dirname(__file__),
'static/volontulo/img/volontulo_baner.png'
))


def placeimg_com_download(width, height, category):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def handle(self, *args, **options):

self.stdout.write(self.style.SUCCESS('Creating 15 organizations'))
for _ in tqdm(range(15)):
organization = OrganizationFactory.create()
organization = OrganizationFactory.create(
image=ImageField(
from_func=placeimg_com_download(1000, 400, 'any')
)
)
UserProfileFactory.create(
organizations=(organization,),
)
Expand Down
21 changes: 21 additions & 0 deletions backend/apps/volontulo/migrations/0018_organization_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2018-10-31 18:45
from __future__ import unicode_literals

import apps.volontulo.models
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('volontulo', '0017_remove_offer_recruitment_status'),
]

operations = [
migrations.AddField(
model_name='organization',
name='image',
field=models.ImageField(blank=True, null=True, upload_to=apps.volontulo.models.upload_to_organizations),
),
]
20 changes: 20 additions & 0 deletions backend/apps/volontulo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,32 @@ def upload_to_offers(_, filename):
)


def upload_to_organizations(_, filename):
"""
Upload to organizations path.

This needs to be a full-body func because
migrations requires it to be serializable.
"""
_, file_extension = os.path.splitext(filename)
return os.path.join(
'organizations',
'{}{}'.format(uuid.uuid4(), file_extension),
)


class Organization(models.Model):
"""Model that handles ogranizations/institutions."""
name = models.CharField(max_length=150, db_index=True)
address = models.CharField(max_length=150)
description = models.TextField()

image = models.ImageField(
upload_to=upload_to_organizations,
null=True,
blank=True
)

def __str__(self):
"""Organization model string reprezentation."""
return self.name
Expand Down
32 changes: 17 additions & 15 deletions backend/apps/volontulo/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,33 @@ def run_validation(self, data=empty):
return data


class ImageField(serializers.Field):

"""Custom field for offer's image serialization."""

def to_representation(self, value):
"""Transform internal value into serializer representation."""
return self.context['request'].build_absolute_uri(
location=value.url
) if value else None

def to_internal_value(self, data):
"""Transform serializer representation into internal value."""
return io.BytesIO(base64.b64decode(data))


class OrganizationSerializer(serializers.HyperlinkedModelSerializer):
"""REST API organizations serializer."""
slug = serializers.SerializerMethodField()
image = ImageField(allow_null=True, required=False)

class Meta:
model = models.Organization
fields = (
'address',
'description',
'id',
'image',
'name',
'slug',
'url',
Expand Down Expand Up @@ -77,21 +94,6 @@ def to_internal_value(self, data):
)


class ImageField(serializers.Field):

"""Custom field for offer's image serialization."""

def to_representation(self, value):
"""Transform internal value into serializer representation."""
return self.context['request'].build_absolute_uri(
location=value.url
) if value else None

def to_internal_value(self, data):
"""Transform serializer representation into internal value."""
return io.BytesIO(base64.b64decode(data))


class OfferSerializer(serializers.HyperlinkedModelSerializer):

"""REST API offers serializer."""
Expand Down
1 change: 1 addition & 0 deletions backend/apps/volontulo/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def test_offer_list_fields(self, offer):
self.assertIsInstance(offer['organization'].pop('address'), str)
self.assertIsInstance(offer['organization'].pop('description'), str)
self.assertIsInstance(offer['organization'].pop('id'), int)
self.assertIsInstance(offer['organization'].pop('image'), str)
self.assertIsInstance(offer['organization'].pop('name'), str)
self.assertIsInstance(offer['organization'].pop('slug'), str)
self.assertIsInstance(offer['organization'].pop('url'), str)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_organization_list_fields(self):
self.assertIsInstance(organization.pop('address'), str)
self.assertIsInstance(organization.pop('description'), str)
self.assertIsInstance(organization.pop('id'), int)
self.assertIsInstance(organization.pop('image'), str)
self.assertIsInstance(organization.pop('name'), str)
self.assertIsInstance(organization.pop('slug'), str)
self.assertIsInstance(organization.pop('url'), str)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def _test_organization_read_fields(self, organization):
self.assertIsInstance(organization.pop('address'), str)
self.assertIsInstance(organization.pop('description'), str)
self.assertIsInstance(organization.pop('id'), int)
self.assertIsInstance(organization.pop('image'), str)
self.assertIsInstance(organization.pop('name'), str)
self.assertIsInstance(organization.pop('slug'), str)
self.assertIsInstance(organization.pop('url'), str)
Expand Down