generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 3
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
#639 Add Image model, update related code #646
Merged
Merged
Changes from 32 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
d62ea41
profile image model added, profile model upd
Lvyshnevska c0fb468
completeness counter, image validation upd
Lvyshnevska 9d9c34f
images urls changed
Lvyshnevska fb5305c
images views, serializer upd
Lvyshnevska ff0b83a
profile serializers upd
Lvyshnevska 331c72a
typo fixed
Lvyshnevska 1f0bd3e
queryset removed
Lvyshnevska a120789
fields added
Lvyshnevska d6d806f
migration error fixed
Lvyshnevska ac0be47
administration updated
Lvyshnevska 60102d7
image type to context
Lvyshnevska 7bc72e5
extension validation to model
Lvyshnevska 0171851
tests add images
Lvyshnevska 84ec34c
tests crud profile updated
Lvyshnevska fb62b4d
formatted by black
Lvyshnevska 0556cb5
import fixed
Lvyshnevska 1a8b2e4
extend schema upd
Lvyshnevska 56595e0
search serializers, tests updated
Lvyshnevska e791900
profile instance to counter
Lvyshnevska b0d94c9
new members tests updated
Lvyshnevska ddc25e5
created_at field updated
Lvyshnevska ac30cf4
added lookup_field and lookup_url_kwarg for ImageDestroyAPIView
YanZhylavy 6abe959
Added permission isAdminUser for ImageDestroyAPIView
YanZhylavy 066209f
name fixed
Lvyshnevska b718f24
delete images tests
Lvyshnevska 4421f9b
formatted
Lvyshnevska 04797f9
check if deleted
Lvyshnevska 5c66a13
Fixed tests for images
YanZhylavy a0118b6
permission to delete images
Lvyshnevska 7bdd690
permission added, tests updated
Lvyshnevska 6f870f2
profile images factory updated
Lvyshnevska bf15aaf
view updated
Lvyshnevska cb73ce1
move image files function updated
Lvyshnevska 5e823ef
redundant variables removed
Lvyshnevska 324e900
image type
Lvyshnevska 69dc59d
content type to lower case
Lvyshnevska c52ef82
custom profile image field
Lvyshnevska 7c31423
search tests updated
Lvyshnevska 833b0b3
content type to lower case in factory
Lvyshnevska e6422e0
validation updated
Lvyshnevska f9707a0
redundant variable, return removed
Lvyshnevska 49e6718
image path in tests updated
Lvyshnevska File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import factory.fuzzy | ||
|
||
from .models import ProfileImage | ||
|
||
|
||
class ProfileimageFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = ProfileImage | ||
|
||
uuid = factory.Faker("uuid4") | ||
created_by = factory.SubFactory("authentication.factories.UserFactory") | ||
image_type = factory.fuzzy.FuzzyChoice( | ||
ProfileImage.IMAGE_TYPES, getter=lambda c: c[0] | ||
) | ||
content_type = "JPEG" | ||
image_path = factory.django.ImageField(filename="test.jpeg") | ||
is_approved = False | ||
is_deleted = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Generated by Django 4.2.3 on 2024-06-12 12:22 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import images.models | ||
import uuid | ||
|
||
|
||
class Migration(migrations.Migration): | ||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="ProfileImage", | ||
fields=[ | ||
( | ||
"uuid", | ||
models.UUIDField( | ||
default=uuid.uuid4, primary_key=True, serialize=False | ||
), | ||
), | ||
("created_at", models.DateField(auto_now_add=True)), | ||
( | ||
"image_type", | ||
models.CharField( | ||
choices=[("banner", "banner"), ("logo", "logo")], max_length=10 | ||
), | ||
), | ||
("content_type", models.CharField(blank=True, max_length=5)), | ||
( | ||
"image_path", | ||
models.ImageField( | ||
blank=True, upload_to=images.models.image_directory_path | ||
), | ||
), | ||
("image_size", models.PositiveIntegerField(null=True)), | ||
("hash_md5", models.CharField(blank=True, max_length=32)), | ||
("is_approved", models.BooleanField(default=False)), | ||
("is_deleted", models.BooleanField(default=False)), | ||
( | ||
"created_by", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="created_images", | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
], | ||
), | ||
] |
32 changes: 32 additions & 0 deletions
32
BackEnd/images/migrations/0002_alter_profileimage_created_at_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Generated by Django 4.2.3 on 2024-06-19 12:51 | ||
|
||
import django.core.validators | ||
from django.db import migrations, models | ||
import images.models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("images", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="profileimage", | ||
name="created_at", | ||
field=models.DateTimeField(auto_now_add=True), | ||
), | ||
migrations.AlterField( | ||
model_name="profileimage", | ||
name="image_path", | ||
field=models.ImageField( | ||
blank=True, | ||
upload_to=images.models.image_directory_path, | ||
validators=[ | ||
django.core.validators.FileExtensionValidator( | ||
allowed_extensions=["jpeg", "png", "jpg"] | ||
) | ||
], | ||
), | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from uuid import uuid4 | ||
from django.db import models | ||
from django.core.validators import FileExtensionValidator | ||
from authentication.models import CustomUser | ||
|
||
|
||
def image_directory_path(instance, filename): | ||
return f"{instance.image_type}/{filename}" | ||
|
||
|
||
class ProfileImage(models.Model): | ||
|
||
BANNER = "banner" | ||
LOGO = "logo" | ||
|
||
IMAGE_TYPES = ( | ||
(BANNER, "banner"), | ||
(LOGO, "logo"), | ||
) | ||
|
||
uuid = models.UUIDField(primary_key=True, default=uuid4) | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
created_by = models.ForeignKey( | ||
CustomUser, on_delete=models.CASCADE, related_name="created_images" | ||
) | ||
image_type = models.CharField(max_length=10, choices=IMAGE_TYPES) | ||
content_type = models.CharField(max_length=5, blank=True) | ||
image_path = models.ImageField( | ||
upload_to=image_directory_path, | ||
validators=[ | ||
FileExtensionValidator(allowed_extensions=["jpeg", "png", "jpg"]) | ||
], | ||
blank=True, | ||
) | ||
image_size = models.PositiveIntegerField(null=True) | ||
hash_md5 = models.CharField(max_length=32, blank=True) | ||
is_approved = models.BooleanField(default=False) | ||
is_deleted = models.BooleanField(default=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,45 @@ | ||
from rest_framework import serializers | ||
from profiles.models import Profile | ||
from images.models import ProfileImage | ||
|
||
|
||
class BannerSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Profile | ||
fields = ("banner_image",) | ||
from validation.validate_image import ( | ||
validate_banner_size, | ||
validate_logo_size, | ||
) | ||
|
||
|
||
class LogoSerializer(serializers.ModelSerializer): | ||
class ImageSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Profile | ||
fields = ("logo_image",) | ||
model = ProfileImage | ||
fields = ( | ||
"uuid", | ||
"image_type", | ||
"image_path", | ||
"created_by", | ||
"content_type", | ||
"image_size", | ||
"hash_md5", | ||
"is_approved", | ||
"is_deleted", | ||
"created_at", | ||
) | ||
read_only_fields = ( | ||
"uuid", | ||
"created_at", | ||
"created_by", | ||
"image_type", | ||
"content_type", | ||
"image_size", | ||
"hash_md5", | ||
"is_approved", | ||
"is_deleted", | ||
) | ||
|
||
def validate(self, value): | ||
image = value.get("image_path") | ||
image_type = self.context.get("image_type") | ||
if image: | ||
if image_type == "banner": | ||
validate_banner_size(image) | ||
else: | ||
validate_logo_size(image) | ||
return value | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
idk about if/else here. When you'll add some third data type — chances you'll remember to add it here are pretty slim (especially if it will be OK to use
validate_logo_size()
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done