示例#1
0
    def setUpTestData(cls):
        """
        Set up test data for the whole TestCase (only run once for the TestCase)
        """
        f.GroupFactory.create(name='USER')
        f.GroupFactory.create(name='ORG_STEWARD')
        f.GroupFactory.create(name='APPS_MALL_STEWARD')
        f.ProfileFactory.create(user__username='******',
                                display_name='Bob B',
                                user__email='*****@*****.**',
                                dn='bob')
        f.ProfileFactory.create(user__username='******')
        unclass = 'UNCLASSIFIED'

        img_type = models.ImageType(name='listing_small_icon')
        img_type.save()

        icon = models.Image(
            file_extension='png',
            security_marking=unclass,
            image_type=models.ImageType.objects.get(name='listing_small_icon'))
        icon.save()

        f.AgencyFactory.create(title='Three Letter Agency',
                               short_name='TLA',
                               icon=icon)
def migrate_image(image_uuid, image_type):
    """
    Migrate an image

    A image_mapper is not needed, as the old uuid will be the new uuid
    """
    # determine the image's extension
    VALID_IMAGE_TYPES = ['png', 'jpg', 'jpeg', 'gif']
    file_extension = None
    for i in VALID_IMAGE_TYPES:
        filename = IMAGE_FILE_PATH + '/{0!s}.{1!s}'.format(image_uuid, i)
        if os.path.isfile(filename):
            file_extension = i
            break
    if not file_extension:
        logging.error('Error: no file extension found for image {0!s}'.format(
            image_uuid))
        return

    # set default security marking
    image_type = models.ImageType.objects.get(name=image_type)
    img = models.Image(uuid=image_uuid,
                       security_marking=DEFAULT_SECURITY_MARKING,
                       file_extension=file_extension,
                       image_type=image_type)
    img.save()

    src = filename
    dest = settings.MEDIA_ROOT + str(
        img.id) + '_' + img.image_type.name + '.' + file_extension
    shutil.copy(src, dest)
    logging.info('Migrated image: {0!s}, type: {1!s}'.format(
        image_uuid, image_type))
    return img