def create(cls, request, unzip_file=True, add_received=True, version=None, hashedID=None):
   state = State()
   if unzip_file:
     content = util.unzip_stream(request)
   else:
     content = request
   root = root_from_file(State.fix_xml(content))
   
   if add_received:
     root = add_received_xml(request, hashedID, version, root)
   else:
     if len(root) == 1 and root[0].tag == 'received':
       raise EmptyState()
     
   try:
     xmlconversion.from_xml(state, root)
   except Exception:
     raise BrokenState()
   
   state.device = Device.create(state.received_ID)
   
   if os.path.exists(state.filename):
     raise BackendValidationError("state file already exists")
   else:
     write_canonical_xml(root, state.filename)
   
   try:
     state.full_clean()
   except:
     do_remove_linked_file(state.filename, state.basedir())
     raise
     
   return state
 def create(cls, request, version, hashedID, packagename, filename):
   upload = Upload(device=Device.create(hashedID), version=version, received_time=now(),
                   packagename=packagename, upload_filename=filename)
   
   content = util.unzip_stream(request).read().decode('utf-8', errors='ignore')
   upload.bytes = len(content)
   
   if os.path.exists(upload.filename):
     raise BackendValidationError("upload file already exists")
   else:
     util.stream_to_file(StringIO.StringIO(content), upload.filename)
   
   try:
     upload.full_clean()
     upload.compress(save=False)
   except:
     do_remove_linked_file(upload.filename, upload.basedir())
     raise
   
   return upload
    def create(cls,
               request,
               unzip_file=True,
               add_received=True,
               version=None,
               hashedID=None):
        state = State()
        if unzip_file:
            content = util.unzip_stream(request)
        else:
            content = request
        root = root_from_file(State.fix_xml(content))

        if add_received:
            root = add_received_xml(request, hashedID, version, root)
        else:
            if len(root) == 1 and root[0].tag == 'received':
                raise EmptyState()

        try:
            xmlconversion.from_xml(state, root)
        except Exception:
            raise BrokenState()

        state.device = Device.create(state.received_ID)

        if os.path.exists(state.filename):
            raise BackendValidationError("state file already exists")
        else:
            write_canonical_xml(root, state.filename)

        try:
            state.full_clean()
        except:
            do_remove_linked_file(state.filename, state.basedir())
            raise

        return state
    def create(cls, request, version, hashedID, packagename, filename):
        upload = Upload(device=Device.create(hashedID),
                        version=version,
                        received_time=now(),
                        packagename=packagename,
                        upload_filename=filename)

        content = util.unzip_stream(request).read().decode('utf-8',
                                                           errors='ignore')
        upload.bytes = len(content)

        if os.path.exists(upload.filename):
            raise BackendValidationError("upload file already exists")
        else:
            util.stream_to_file(StringIO.StringIO(content), upload.filename)

        try:
            upload.full_clean()
            upload.compress(save=False)
        except:
            do_remove_linked_file(upload.filename, upload.basedir())
            raise

        return upload