Exemplo n.º 1
0
    def post(self):
        from user_sn import confirm
        from cache import (
            can_operate_on,
            get_copy,
            ContentCopy,
            get_cache_by_name,
            TYPE_COMPUTER,
            TYPE_EXTERN_FD,
            TYPE_LOCAL_FD,
            TYPE_EXTERNAL_RING,
        )
        from Content import content_by_id

        u = confirm(self)
        cname = self.request.get("cache")
        c = get_cache_by_name(cname)
        if not can_operate_on(c, u):
            raise Exception("You're not permitted to sync this cache.")
        deletes = self.request.get("deletes")
        for item in deletes.split("\n"):
            if item == "":
                continue
            logging.info("deleting %s" % item)
            get_copy(cname, item).delete()
        adds = self.request.get("adds")
        for item in adds.split("\n"):
            if item == "":
                continue
            logging.info("adding %s" % item)
            # see if there's an RC associated with this
            from request import RequestCopy

            content = content_by_id(item)
            rcs = RequestCopy.all().filter("file =", content)
            if c.type == TYPE_COMPUTER:
                rcs = rcs.filter("dest =", u).fetch(limit=1000)
            elif c.type == TYPE_LOCAL_FD:
                rcs = (
                    rcs.filter("dest =", c.permanent_location.team_responsible)
                    .filter("dest_int =", TYPE_LOCAL_FD)
                    .fetch(limit=1000)
                )
            elif c.type == TYPE_EXTERN_FD:
                # the common case (closing the RC associated with a swap) is handled in runComplete.
                rcs = (
                    RequestCopy.all()
                    .filter("file =", content)
                    .filter("dest_int =", TYPE_EXTERNAL_RING)
                    .fetch(limit=1000)
                )
            for rc in rcs:
                logging.info("Closing %s" % rc.key())
                rc.delete()
            co = ContentCopy(where=c, content=content)
            if co.content is None:
                logging.warning("Not adding %s" % item)
            else:
                co.put()
        c.space_left = int(self.request.get("size"))
        c.last_touched = u
        c.put()