bgneal@1012: """bio_image_check
bgneal@1012: 
bgneal@1012: A management command to check profiles for non-secure images.
bgneal@1012: """
bgneal@1012: from optparse import make_option
bgneal@1012: 
bgneal@1012: from django.core.management.base import NoArgsCommand
bgneal@1012: 
bgneal@1012: from bio.models import UserProfile
bgneal@1012: from core.html import image_check
bgneal@1012: from core.html import ImageCheckError
bgneal@1012: 
bgneal@1012: 
bgneal@1012: def _image_check(html):
bgneal@1012:     try:
bgneal@1012:         image_check(html)
bgneal@1012:     except ImageCheckError:
bgneal@1012:         return False
bgneal@1012:     return True
bgneal@1012: 
bgneal@1012: 
bgneal@1012: class Command(NoArgsCommand):
bgneal@1012:     help = "Checks user profiles for non-secure images"
bgneal@1012:     option_list = NoArgsCommand.option_list + (
bgneal@1012:             make_option('--resave',
bgneal@1012:                 action='store_true',
bgneal@1012:                 dest='resave',
bgneal@1012:                 default=False,
bgneal@1012:                 help='Re-save profile if issue found'),
bgneal@1012:     )
bgneal@1012: 
bgneal@1012:     def handle_noargs(self, **options):
bgneal@1012: 
bgneal@1012:         resave = options.get('resave', False)
bgneal@1012: 
bgneal@1012:         count = 0
bgneal@1012:         for p in UserProfile.objects.iterator():
bgneal@1012:             issue_found = False
bgneal@1012:             if p.profile_html and not _image_check(p.profile_html):
bgneal@1012:                 self.stdout.write("%s: profile_text\n" % p.user.username)
bgneal@1012:                 count += 1
bgneal@1012:                 issue_found = True
bgneal@1012:             if p.signature_html and not _image_check(p.signature_html):
bgneal@1012:                 self.stdout.write("%s: signature\n" % p.user.username)
bgneal@1012:                 count += 1
bgneal@1012:                 issue_found = True
bgneal@1012: 
bgneal@1012:             if issue_found and resave:
bgneal@1012:                 p.save(content_update=True)
bgneal@1012: 
bgneal@1012:         self.stdout.write("%d problem field(s) found\n" % count)