示例#1
0
    def handle(self, *args, **options):
        if len(args) > 1:
            print('Composite only takes one argument: Either slug or name of the heroine to composite.')
            exit()

        from django.conf import settings
        from rh.compositer import composite_pack
        from rh.models import Heroine
        heroine = Heroine.objects.get(Q(name=args[0]) | Q(slug=args[0]))

        packs_dir = os.path.join(settings.MEDIA_ROOT, 'packs', heroine.slug)

        if not os.path.exists(packs_dir):
            print('Directory "{0}" doesn\'t exist. No images to composite.'.format(packs_dir))
            exit(1)
        
        composite_path = composite_pack(packs_dir)

        print('Saved to "{0}".'.format(composite_path))
示例#2
0
    def handle(self, *args, **options):
        if len(args) > 0:
            print('`composite_all` Doesn\'t takes any arguments.')
            exit()

        from django.conf import settings
        from rh.compositer import composite_pack
        from rh.models import Heroine
        heroines = Heroine.objects.all()

        for heroine in heroines:
            packs_dir = os.path.join(settings.MEDIA_ROOT, 'packs', heroine.slug)

            if not os.path.exists(packs_dir):
                print('Directory "{0}" doesn\'t exist. No images to composite for "{1}".'.format(packs_dir, heroine.name))
                continue
            
            composite_path = composite_pack(packs_dir)

            print('Saved to "{0}".'.format(composite_path))
示例#3
0
    def save(self, *args, **kwargs):
        quick_save = kwargs.pop('quick_save', False)
        if quick_save:
            super(Heroine, self).save(*args, **kwargs)
            return

        self.description_html = markdown.markdown(self.description, extensions=['extra', 'nl2br', 'admonition', 'headerid', 'sane_lists'])
        # Remove tags which was generated with the markup processor
        text = strip_tags(self.description_html)
        # Unescape entities which was generated with the markup processor
        self.description_text = unescape(text)

        super(Heroine, self).save(*args, **kwargs)
        # unzip the file, then delete it
        should_resave = False
        packs_dir = os.path.join(settings.MEDIA_ROOT, 'packs', self.slug)
        if self.animation_pack:
            #delete the old file.
            zip_path = os.path.join(settings.MEDIA_ROOT, 'packs/{0}.zip'.format(self.slug))
            try:
                if zip_path != self.animation_pack.path:
                    os.unlink(zip_path)
                    should_resave = True
            except:
                # only delete it if it doesn't exist
                logger.warning('Couldn\'t delete animation pack.')
                pass

            try:
                os.rename(self.animation_pack.path, zip_path)
                self.animation_pack = zip_path.replace(settings.MEDIA_ROOT + '/', '')
            except:
                zip_path = self.animation_pack.path

            if zipfile.is_zipfile(zip_path):
                if not os.path.exists(packs_dir):
                    os.mkdir(packs_dir)
                pack_zip = zipfile.ZipFile(zip_path)
                files = pack_zip.namelist()
                # if it's in a subfolder, remove the subfolder
                allowed_files = [
                    'eyes-closed.png',
                    'eyes-opened.png',
                    'torso.png',
                    'head.png',
                    'mouth.png',
                    'hair.png',
                ]

                for f in files:
                    for allowed_file in allowed_files:
                        # extra bit for the __MACOSX
                        if f.endswith(allowed_file) and not f.startswith('__'):
                            source = pack_zip.open(f)
                            contents = source.read()
                            target_path = os.path.join(packs_dir, allowed_file)
                            target = open(target_path, 'wb')
                            target.write(contents)
                            target.close()
                            source.close()

                pack_zip.close()

        # if there isn't a grid image provided, just set it to this
        if not self.hero_image or self.hero_image.path.endswith('composite.png'):
            # composite the images together
            composite_path = composite_pack(packs_dir)
            self.hero_image = composite_path.replace(settings.MEDIA_ROOT + '/', '')
            should_resave = True

        if should_resave:
            self.save(quick_save=True)

        # regen thumbs
        # if self.grid_image_thumbnail:
        #   try:
        #     logger.info('Deleting grid image thumbnail {0}.'.format(self.grid_image_thumbnail.path))
        #     os.unlink(self.grid_image_thumbnail.path)
        #   except:
        #     logger.warning('Error deleting grid image thumbnail.')


        # if self.timeline_image_thumbnail:
        #   try:
        #     logger.info('Deleting timeline image thumbnail {0}.'.format(self.timeline_image_thumbnail.path))
        #     os.unlink(self.timeline_image_thumbnail.path)
        #   except:
        #     logger.warning('Error deleting timeline image thumbnail.')

        self.grid_image_thumbnail.generate()
        self.timeline_image_thumbnail.generate()
        self.pinnable_image.generate()

        heroines = Heroine.objects.all()
        public_heroines = []
        all_heroines = []

        logger.info('Generating heroines.json.')
        for h in heroines:
            birthdate_string = None
            if h.birthdate:
                if h.birthdate_is_approximate:
                    birthdate_string = h.birthdate.strftime('c. %Y')
                else:
                    birthdate_string = h.birthdate.strftime('%Y-%m-%d')
            deathdate_string = None
            if h.deathdate:
                if h.deathdate_is_approximate:
                    deathdate_string = h.deathdate.strftime('c. %Y')
                else:
                    deathdate_string = h.deathdate.strftime('%Y-%m-%d')

            try:
                hero_image_url = h.hero_image.url
            except ValueError:
                hero_image_url = None

            heroine_object = {
                'name': h.name,
                'title': h.title,
                'nickname': h.nickname,
                'slug': h.slug,
                'topOffset': h.top_offset,
                'birthdate': birthdate_string,
                'deathdate': deathdate_string,
                'country': h.country,
                'descriptionHtml': h.description_html,
                'descriptionText': h.description_text,
                'heroImage': hero_image_url,
                'gridImageThumbnail': h.grid_image_thumbnail.url,
                'timelineImageThumbnail': h.timeline_image_thumbnail.url,
                'pinnableImage': h.pinnable_image.url,
            }

            logger.info('JSON using {0}.'.format(h.grid_image_thumbnail.path))


            if h.is_public:
                public_heroines.append(heroine_object)
            all_heroines.append(heroine_object)

        public_json_filename = '{0}/heroines.json'.format(settings.MEDIA_ROOT)
        private_json_filename = '{0}/heroines-preview.json'.format(settings.MEDIA_ROOT)
        try:
            fh = open(public_json_filename, 'w')
            json.dump(public_heroines, fh)
            fh.close()
        except IOError:
            logger.warning('Failed to write json updates for heroines at {0}'.format(public_json_filename))

        try:
            fh = open(private_json_filename, 'w')
            json.dump(all_heroines, fh)
            fh.close()
        except IOError:
            logger.warning('Failed to write json updates for heroines at {0}'.format(private_json_filename))