def create(request):
    try:
        logger.debug('---- Create Image ----')
        image_form = ImageForm(request.POST, request.FILES)
        if (not image_form.is_valid()):
            logger.error('Could not save image for some reason!')
            context = {'image_form': image_form}
            return render(request, 'images/new.html', context)
        logger.debug('Saving form')
        orig_image = image_form.save()
        messages.info(request, 'Image uploaded successfully')
        image_type = request.POST['type']
        image_name = request.POST['name']
        full_path = orig_image.filePath.path
        if re.match('.*\\.vmdk$', full_path):
            converted_image_path = re.sub('\\.vmdk$', '.qcow2', full_path)
            converted_image_file_name = converted_image_path.split('/')[(- 1)]
            if osUtils.convert_vmdk_to_qcow2(full_path, converted_image_path):
                logger.info('Converted vmdk image to qcow2!')
                orig_image.filePath = ('user_images/%s' % converted_image_file_name)
                orig_image.save()
                logger.debug('Removing original vmdk')
                osUtils.remove_instance(full_path)
            else:
                logger.error('Could not convert vmdk!')
        if ((image_type == 'junos_vre_15') and ('jinstall64-vmx-15.1' in full_path)):
            logger.debug('Creating RIOT image for Junos vMX 15.1')
            if ('.' in full_path):
                new_image_path = re.sub('(.*)\\.(.*)$', '\\1_riot.\\2', full_path)
            else:
                new_image_path = (full_path + '_riot.img')
            new_image_file_name = new_image_path.split('/')[(- 1)]
            new_image_name = (image_name + ' Riot PFE')
            if osUtils.copy_image_to_clone(full_path, new_image_path):
                logger.debug(('Copied from %s' % full_path))
                logger.debug(('Copied to %s' % new_image_path))
                image = Image()
                image.name = new_image_name
                image.type = 'junos_riot'
                image.description = (orig_image.description + '\nRiot PFE')
                image.filePath = ('user_images/' + new_image_file_name)
                image.save()
        return HttpResponseRedirect('/images')
    except Exception as e:
        logger.error(e)
        messages.info(request, 'Could not create image!')
        return HttpResponseRedirect('/images/')
def create(request):
    try:
        logger.debug('---- Create Image ----')
        image_form = ImageForm(request.POST, request.FILES)
        if (not image_form.is_valid()):
            logger.error('Could not save image for some reason!')
            context = {'image_form': image_form}
            return render(request, 'images/new.html', context)
        logger.debug('Saving form')
        orig_image = image_form.save()
        messages.info(request, 'Image uploaded successfully')
        image_type = request.POST['type']
        image_name = request.POST['name']
        full_path = orig_image.filePath.path
        if re.match('.*\\.vmdk$', full_path):
            converted_image_path = re.sub('\\.vmdk$', '.qcow2', full_path)
            converted_image_file_name = converted_image_path.split('/')[(- 1)]
            if osUtils.convert_vmdk_to_qcow2(full_path, converted_image_path):
                logger.info('Converted vmdk image to qcow2!')
                orig_image.filePath = ('user_images/%s' % converted_image_file_name)
                orig_image.save()
                logger.debug('Removing original vmdk')
                osUtils.remove_instance(full_path)
            else:
                logger.error('Could not convert vmdk!')
        if ((image_type == 'junos_vre_15') and ('jinstall64-vmx-15.1' in full_path)):
            logger.debug('Creating RIOT image for Junos vMX 15.1')
            if ('.' in full_path):
                new_image_path = re.sub('(.*)\\.(.*)$', '\\1_riot.\\2', full_path)
            else:
                new_image_path = (full_path + '_riot.img')
            new_image_file_name = new_image_path.split('/')[(- 1)]
            new_image_name = (image_name + ' Riot PFE')
            if osUtils.copy_image_to_clone(full_path, new_image_path):
                logger.debug(('Copied from %s' % full_path))
                logger.debug(('Copied to %s' % new_image_path))
                image = Image()
                image.name = new_image_name
                image.type = 'junos_riot'
                image.description = (orig_image.description + '\nRiot PFE')
                image.filePath = ('user_images/' + new_image_file_name)
                image.save()
        return HttpResponseRedirect('/images')
    except Exception as e:
        logger.error(e)
        messages.info(request, 'Could not create image!')
        return HttpResponseRedirect('/images/')