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

Add style field to ExerciseImage #822

Merged
merged 1 commit into from
Sep 19, 2021
Merged
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
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Developers
* Leninux - https://github.com/RedRudeBoy
* Ayush Kumar - https://github.com/gr8ayu
* Gorkem Arslan - https://github.com/gorkemarslan
* Lucas Stone-Drake - https://github.com/LucasSD

Translators
-----------
Expand Down
1 change: 1 addition & 0 deletions wger/exercises/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Meta:
'image',
'is_main',
'status',
'style',
]


Expand Down
1 change: 1 addition & 0 deletions wger/exercises/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Meta:
'is_main',
'license',
'license_author',
'style',
)


Expand Down
32 changes: 32 additions & 0 deletions wger/exercises/migrations/0014_exerciseimage_style.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 3.2.7 on 2021-09-17 15:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('exercises', '0013_auto_20210503_1232'),
]

operations = [
migrations.AddField(
model_name='exerciseimage',
name='style',
field=models.CharField(
choices=[
('1',
'Line'),
('2',
'3D'),
('3',
'Low-poly'),
('4',
'Photo'),
('5',
'Other')],
default='1',
help_text='The art style of your image',
max_length=1),
),
]
50 changes: 39 additions & 11 deletions wger/exercises/models/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,34 @@ def exercise_image_upload_dir(instance, filename):
Returns the upload target for exercise images
"""
ext = pathlib.Path(filename).suffix
return "exercise-images/{0}/{1}{2}".format(instance.exercise_base.id, instance.uuid, ext)
return "exercise-images/{0}/{1}{2}".format(
instance.exercise_base.id, instance.uuid, ext)


class ExerciseImage(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
class ExerciseImage(
AbstractSubmissionModel,
AbstractLicenseModel,
models.Model):
"""
Model for an exercise image
"""

objects = SubmissionManager()
"""Custom manager"""

LINE_ART = '1'
THREE_D = '2'
LOW_POLY = '3'
PHOTO = '4'
OTHER = '5'
STYLE = (
(LINE_ART, _('Line')),
(THREE_D, _('3D')),
(LOW_POLY, _('Low-poly')),
(PHOTO, _('Photo')),
(OTHER, _('Other')),
)

uuid = models.UUIDField(
default=uuid.uuid4,
editable=False,
Expand Down Expand Up @@ -83,6 +100,14 @@ class ExerciseImage(AbstractSubmissionModel, AbstractLicenseModel, models.Model)
)
"""A flag indicating whether the image is the exercise's main image"""

style = models.CharField(
help_text=_('The art style of your image'),
max_length=1,
choices=STYLE,
default=PHOTO,
)
"""The art style of the image"""

class Meta:
"""
Set default ordering
Expand All @@ -95,7 +120,9 @@ def save(self, *args, **kwargs):
Only one image can be marked as main picture at a time
"""
if self.is_main:
ExerciseImage.objects.filter(exercise_base=self.exercise_base).update(is_main=False)
ExerciseImage.objects.filter(
exercise_base=self.exercise_base).update(
is_main=False)
self.is_main = True
else:
if ExerciseImage.objects.accepted()\
Expand All @@ -112,7 +139,8 @@ def save(self, *args, **kwargs):
for language in Language.objects.all():
delete_template_fragment_cache('muscle-overview', language.id)
delete_template_fragment_cache('exercise-overview', language.id)
delete_template_fragment_cache('exercise-overview-mobile', language.id)
delete_template_fragment_cache(
'exercise-overview-mobile', language.id)
delete_template_fragment_cache('equipment-overview', language.id)

# And go on
Expand All @@ -127,16 +155,16 @@ def delete(self, *args, **kwargs):
for language in Language.objects.all():
delete_template_fragment_cache('muscle-overview', language.id)
delete_template_fragment_cache('exercise-overview', language.id)
delete_template_fragment_cache('exercise-overview-mobile', language.id)
delete_template_fragment_cache(
'exercise-overview-mobile', language.id)
delete_template_fragment_cache('equipment-overview', language.id)

# Make sure there is always a main image
if not ExerciseImage.objects.accepted() \
.filter(exercise_base=self.exercise_base, is_main=True).count() \
and ExerciseImage.objects.accepted() \
.filter(exercise_base=self.exercise_base) \
.filter(is_main=False) \
.count():
if not ExerciseImage.objects.accepted() .filter(
exercise_base=self.exercise_base,
is_main=True).count() and ExerciseImage.objects.accepted() .filter(
exercise_base=self.exercise_base) .filter(
is_main=False) .count():

image = ExerciseImage.objects.accepted() \
.filter(exercise_base=self.exercise_base, is_main=False)[0]
Expand Down
5 changes: 3 additions & 2 deletions wger/exercises/tests/test_exercise_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ class AddExerciseImageTestCase(WgerAddTestCase):
data = {
'is_main': True,
'image': open('wger/exercises/tests/protestschwein.jpg', 'rb'),
'license': 1
'license': 1,
'style': '1'
}


Expand All @@ -128,7 +129,7 @@ class EditExerciseImageTestCase(WgerEditTestCase):
object_class = ExerciseImage
url = 'exercise:image:edit'
pk = 2
data = {'is_main': True, 'license': 1}
data = {'is_main': True, 'license': 1, 'style': '1'}


class DeleteExerciseImageTestCase(WgerDeleteTestCase):
Expand Down