Skip to content

Commit

Permalink
Merge pull request #772 from ita-social-projects/#764-Banner/Logo-upd…
Browse files Browse the repository at this point in the history
…ate-with-previously-approved-content

#764 banner/logo update with previously approved content
  • Loading branch information
Andrewakiv authored Sep 14, 2024
2 parents 2751d85 + 9c0cbbe commit 8ae4c0e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
3 changes: 2 additions & 1 deletion BackEnd/profiles/tests/test_email_sending.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ def test_needs_moderation_deleted_image(self):

@mock.patch("utils.moderation.image_moderation.now", return_value=now())
def test_update_pending_status(self, mock_now):
self.manager.update_pending_status()
self.profile.banner = self.banner
self.manager.update_pending_status("banner", self.profile.banner)
self.assertEqual(self.profile.status, self.profile.PENDING)
self.assertEqual(self.profile.status_updated_at, mock_now.return_value)
self.assertTrue(self.manager.moderation_is_needed)
Expand Down
44 changes: 35 additions & 9 deletions BackEnd/utils/moderation/image_moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
from kombu.exceptions import OperationalError
from redis.exceptions import ConnectionError

from images.models import ProfileImage
from administration.models import AutoapproveTask, AutoModeration
from profiles.tasks import celery_autoapprove
from utils.completeness_counter import completeness_count


logger = logging.getLogger(__name__)
Expand All @@ -19,6 +21,13 @@ def __init__(self, profile):
self.images = {"banner": None, "logo": None}
self.content_deleted = False

def handle_both_approved(self):
if self.profile.banner.is_approved and self.profile.logo.is_approved:
if self.profile.status == self.profile.PENDING:
self.content_deleted = True
self.profile.status = self.profile.APPROVED
self.profile.save()

def handle_approved_status(self, secondary_image):
if (
self.profile.status == self.profile.PENDING
Expand All @@ -40,29 +49,46 @@ def handle_undefined_status(self):
self.profile.status_updated_at = now()
self.profile.save()

def update_pending_status(self):
self.profile.status = self.profile.PENDING
self.profile.status_updated_at = now()
self.profile.save()
self.moderation_is_needed = True
def update_pending_status(self, image_type, image):
existing_image = ProfileImage.objects.filter(
hash_md5=image.hash_md5, is_approved=True
).first()
if existing_image:
image.is_approved = True
image.save()
if self.profile.status != self.profile.PENDING:
self.profile.status = self.profile.APPROVED
self.profile.status_updated_at = now()
setattr(self.profile, f"{image_type}_approved", image)
self.profile.save()
completeness_count(self.profile)
else:
updated_image = getattr(self.profile, image_type)
self.profile.status = self.profile.PENDING
self.profile.status_updated_at = now()
self.images[image_type] = updated_image
self.profile.save()
self.moderation_is_needed = True

def needs_moderation(self, image):
return image and not image.is_approved

def check_for_moderation(self):
if self.needs_moderation(self.profile.banner):
self.update_pending_status()
self.images["banner"] = self.profile.banner
self.update_pending_status("banner", self.profile.banner)
elif not self.profile.banner and self.profile.logo:
self.handle_approved_status(self.profile.logo)

if self.needs_moderation(self.profile.logo):
self.update_pending_status()
self.images["logo"] = self.profile.logo
self.update_pending_status("logo", self.profile.logo)
elif not self.profile.logo and self.profile.banner:
self.handle_approved_status(self.profile.banner)

self.handle_undefined_status()
if self.profile.banner and self.profile.logo:
self.handle_both_approved()
if not self.profile.banner and self.profile.logo:
self.handle_approved_status(self.profile.logo)
return self.moderation_is_needed

def schedule_autoapprove(self):
Expand Down

0 comments on commit 8ae4c0e

Please sign in to comment.