Esempio n. 1
0
 def _to_python(self, value, state):
     image_q = meta.Session.query(model.Image)
     client_dn = request.environ['REPOSITORY_USER_CLIENT_DN']
     uuid = h.image_uuid(client_dn, value)
     if image_q.filter(model.Image.uuid==uuid).first():
         state = 'CONFLICT'
         raise formencode.Invalid('conflict', value, state)
     else:
         return value
Esempio n. 2
0
    def create(self):
        """POST /repository/images: Create a new item"""

        params = validate_new_image(request.params)
        if "file" not in params:
            abort(400, "400 Bad Request")

        # params.pop('file')
        # return json.dumps(params)

        g_uuid = h.group_uuid(params["group"])
        group = meta.Session.query(Group).filter(Group.uuid == g_uuid).first()
        if not group:
            abort(400, "400 Bad Request - group not found")

        # TODO: setting these values is overly verbose.  make it simple
        new_image = Image()
        # User settable values
        new_image.name = params["name"]
        new_image.os_variant = params["os_variant"]
        new_image.os_type = params["os_type"]
        new_image.os_arch = params["os_arch"]
        new_image.hypervisor = params["hypervisor"]
        new_image.owner_r = params["owner_r"]
        new_image.owner_w = params["owner_w"]
        new_image.group_r = params["group_r"]
        new_image.group_r = params["group_w"]
        new_image.group_w = params["other_r"]
        new_image.group_r = params["other_r"]
        new_image.desc = params["desc"]

        # Non-user settable values
        uuid = h.image_uuid(request.environ["REPOSITORY_USER_UUID"], params["name"])
        current_time = datetime.utcfromtimestamp(time())
        file_name = uuid + "_" + new_image.name

        new_image.owner_id = request.environ["REPOSITORY_USER_ID"]
        new_image.group_id = group.id
        new_image.uuid = uuid
        new_image.version = 1
        new_image.uploaded = current_time
        new_image.modified = current_time
        new_image.url = ""
        new_image.previous = ""
        new_image.path = file_name

        # Deal with file upload
        try:
            image_file = params["file"]
            local_path = path.join(app_globals.image_storage, file_name)
            permanent_file = open(local_path, "w")
            shutil.copyfileobj(image_file.file, permanent_file)
            image_file.file.close()
            permanent_file.close()
        except Exception, e:
            abort(500, "500 Internal Error - Error uploading file %s" % e)