'Handle creating/editing the ExtensionVersion instance and saving it
to db, as well as file operations, from a FileUpload instance.
`parent` parameter must be passed so that we can attach the instance to
an Extension. `manifest_contents` can be passed to avoid parsing twice
if that was already done by the caller.
Can throw a ParseError, so should always be called
within a try/except.'

@classmethod
def from_upload(cls, upload, parent=None, manifest_contents=None):

    if (parent is None):
        raise ValueError('ExtensionVersion.from_upload()    needs    a    parent.')
    if parent.is_blocked():
        raise BlockedExtensionError
    if (manifest_contents is None):
        manifest_contents = Extension.extract_and_validate_upload(upload)
    fields = ('default_language', 'version')
    data = Extension.extract_manifest_fields(manifest_contents, fields)
    data['manifest'] = manifest_contents
    data['extension'] = parent
    try:
        version = parent.latest_version.version
        if (not cls.is_version_number_higher(data['version'], version)):
            raise ParseError(_((u'Version    number    must    be    higher    than    the    latest    version    uploaded    for    this    Add-on,    which    is    "%s".' % version)))
    except cls.DoesNotExist:
        pass
    try:
        with transaction.atomic():
            instance = cls.objects.create(**data)
    except IntegrityError:
        raise ParseError(_(u'An    Add-on    with    this    version    number    already    exists.'))
    size = instance.handle_file_upload_operations(upload)
    instance.set_older_pending_versions_as_obsolete()
    instance.update(size=size, status=STATUS_PENDING)
    return instance
