예제 #1
0
 def refresh(cls, skelValues):  # ViUR2 Compatibility
     super().refresh(skelValues)
     if not skelValues["parententry"] and skelValues.dbEntity.get(
             "parentdir"):  # parentdir for viur2 compatibility
         skelValues["parententry"] = utils.normalizeKey(
             db.KeyClass.from_legacy_urlsafe(
                 skelValues.dbEntity["parentdir"]))
예제 #2
0
    def unserialize(self, skeletonValues: 'viur.core.skeleton.SkeletonValues',
                    name: str) -> bool:
        """
			Inverse of serialize. Evaluates whats
			read from the datastore and populates
			this bone accordingly.
			:param name: The property-name this bone has in its Skeleton (not the description!)
			:type name: str
			:param expando: An instance of the dictionary-like db.Entity class
			:type expando: db.Entity
			:returns: bool
		"""
        assert name == "key", "Keybones must be named key!"
        if isinstance(
                skeletonValues.entity, Entity
        ) and skeletonValues.entity.key and not skeletonValues.entity.key.is_partial:
            skeletonValues.accessedValues[name] = skeletonValues.entity.key
            return True
        elif "key" in skeletonValues.entity:
            val = skeletonValues.entity["key"]
            if isinstance(val, str):
                try:
                    val = normalizeKey(KeyClass.from_legacy_urlsafe(val))
                except:
                    val = None
            elif not isinstance(val, KeyClass):
                val = None
            skeletonValues.accessedValues["key"] = val
            return True
        return False
예제 #3
0
def keyHelper(
        inKey: Union[KeyClass, str, int],
        targetKind: str,
        additionalAllowdKinds: Union[None, List[str]] = None) -> KeyClass:
    if isinstance(inKey, str):
        try:
            decodedKey = utils.normalizeKey(
                KeyClass.from_legacy_urlsafe(inKey))
        except:
            decodedKey = None
        if decodedKey:  # If it did decode, don't try any further
            if decodedKey.kind != targetKind and (
                    not additionalAllowdKinds
                    or inKey.kind not in additionalAllowdKinds):
                raise ValueError("Kin1d mismatch: %s != %s" %
                                 (decodedKey.kind, targetKind))
            return decodedKey
        if inKey.isdigit():
            inKey = int(inKey)
        return Key(targetKind, inKey)
    elif isinstance(inKey, int):
        return Key(targetKind, inKey)
    elif isinstance(inKey, KeyClass):
        if inKey.kind != targetKind and (not additionalAllowdKinds
                                         or inKey.kind
                                         not in additionalAllowdKinds):
            raise ValueError("Kin1d mismatch: %s != %s (%s)" %
                             (inKey.kind, targetKind, additionalAllowdKinds))
        return inKey
    else:
        raise ValueError("Unknown key type %r" % type(inKey))
예제 #4
0
    def storeEntry2(self, e, key):
        if not self._checkKey(key, export=False):
            raise errors.Forbidden()
        entry = pickle.loads(e.decode("HEX"))
        if not "key" in entry and "id" in entry:
            entry["key"] = entry["id"]
        for k in list(entry.keys())[:]:
            if isinstance(entry[k], str):
                entry[k] = entry[k].decode("UTF-8")
        key = db.Key(encoded=utils.normalizeKey(entry["key"]))

        dbEntry = db.Entity(kind=key.kind(),
                            parent=key.parent(),
                            id=key.id(),
                            name=key.name())  # maybe some more fixes here ?
        for k in entry.keys():
            if k != "key":
                val = entry[k]
                dbEntry[k] = val
        db.Put(dbEntry)
        if dbEntry.key().id():
            # Ensure the Datastore knows that it's id is in use
            datastore._GetConnection()._reserve_keys([dbEntry.key()])
        try:
            skel = skeletonByKind(key.kind())()
        except:
            logging.error("Unknown Skeleton - skipping")
        skel.fromDB(str(dbEntry.key()))
        skel.refresh()
        skel.toDB(clearUpdateTag=True)
예제 #5
0
 def fixVals(val):
     if isinstance(val, str):
         try:
             val = normalizeKey(KeyClass.from_legacy_urlsafe(val))
         except:
             val = None
     elif not isinstance(val, KeyClass):
         val = None
     return val
예제 #6
0
    def add(self, *args, **kwargs):
        """
			Allows guests to register a new account if self.registrationEnabled is set to true

			.. seealso:: :func:`addSkel`, :func:`onAdded`, :func:`canAdd`

			:returns: The rendered, added object of the entry, eventually with error hints.

			:raises: :exc:`server.errors.Unauthorized`, if the current user does not have the required permissions.
			:raises: :exc:`server.errors.PreconditionFailed`, if the *skey* could not be verified.
		"""
        if "skey" in kwargs:
            skey = kwargs["skey"]
        else:
            skey = ""
        if not self.canAdd():
            raise errors.Unauthorized()
        skel = self.addSkel()
        if (len(kwargs) == 0  # no data supplied
                or skey == ""  # no skey supplied
                or not currentRequest.get().
                isPostRequest  # bail out if not using POST-method
                or not skel.fromClient(
                    kwargs)  # failure on reading into the bones
                or ("bounce" in kwargs
                    and kwargs["bounce"] == "1")):  # review before adding
            # render the skeleton in the version it could as far as it could be read.
            return self.userModule.render.add(skel)
        if not securitykey.validate(skey, useSessionKey=True):
            raise errors.PreconditionFailed()
        skel.toDB()
        if self.registrationEmailVerificationRequired and str(
                skel["status"]) == "1":
            # The user will have to verify his email-address. Create an skey and send it to his address
            skey = securitykey.create(duration=60 * 60 * 24 * 7,
                                      userKey=utils.normalizeKey(skel["key"]),
                                      name=skel["name"])
            skel.skey = baseBone(descr="Skey")
            skel["skey"] = skey
            email.sendEMail(dests=[skel["name"]],
                            tpl=self.userModule.verifyEmailAddressMail,
                            skel=skel)
        self.userModule.onAdded(skel)  # Call onAdded on our parent user module
        return self.userModule.render.addSuccess(skel)
예제 #7
0
파일: tree.py 프로젝트: phorward/viur-core
		:raises: :exc:`viur.core.errors.NotFound`, when no entry with the given *key* was found.
		:raises: :exc:`viur.core.errors.Unauthorized`, if the current user does not have the required permissions.
		:raises: :exc:`viur.core.errors.PreconditionFailed`, if the *skey* could not be verified.
		"""
        if not (skelType := self._checkSkelType(skelType)):
            raise errors.NotAcceptable(f"Invalid skelType provided.")

        skel = self.editSkel(skelType)  # srcSkel - the skeleton to be moved
        parentNodeSkel = self.baseSkel(
            "node")  # destSkel - the node it should be moved into

        if not skel.fromDB(key):
            raise errors.NotFound("Cannot find key entity")

        if not parentNodeSkel.fromDB(parentNode):
            parentNode = utils.normalizeKey(
                db.Key.from_legacy_urlsafe(parentNode))

            if parentNode.kind != parentNodeSkel.kindName:
                raise errors.NotFound(
                    f"You provided a key of kind {parentNode.kind}, but require a {parentNodeSkel.kindName}."
                )

            raise errors.NotFound("Cannot find parentNode entity")

        if not self.canMove(skelType, skel, parentNodeSkel):
            raise errors.Unauthorized()

        if skel["key"] == parentNodeSkel["key"]:
            raise errors.NotAcceptable("Cannot move a node into itself")

        ## Test for recursion
예제 #8
0
def iterImport(module, target, exportKey, cursor=None, amount=0):
    """
		Processes 100 Entries and calls the next batch
	"""
    urlfetch.set_default_fetch_deadline(20)

    payload = {"module": module, "key": exportKey}
    if cursor:
        payload.update({"cursor": cursor})

    result = urlfetch.fetch(
        url=target,
        payload=urllib.urlencode(payload),
        method=urlfetch.POST,
        headers={'Content-Type': 'application/x-www-form-urlencoded'})

    if result.status_code == 200:
        res = pickle.loads(result.content.decode("HEX"))
        skel = skeletonByKind(module)()
        logging.info("%s: %d new entries fetched, total %d entries fetched" %
                     (module, len(res["values"]), amount))

        if len(res["values"]) == 0:
            try:
                utils.sendEMailToAdmins(
                    "Import of kind %s finished with %d entities" %
                    (module, amount), "ViUR finished to import %d entities of "
                    "kind %s from %s.\n" % (amount, module, target))
            except:  # OverQuota, whatever
                logging.error("Unable to send Email")

            return

        for entry in res["values"]:
            for k in list(entry.keys())[:]:
                if isinstance(entry[k], str):
                    entry[k] = entry[k].decode("UTF-8")

            if not "key" in entry:
                entry["key"] = entry["id"]

            key = db.Key(encoded=utils.normalizeKey(entry["key"]))

            # Special case: Convert old module root nodes!!!
            if module.endswith(
                    "_rootNode") and key.name() and "_modul_" in key.name():
                name = key.name().replace("_modul_", "_module_")
            else:
                name = key.name()

            dbEntry = db.Entity(kind=key.kind(),
                                parent=key.parent(),
                                id=key.id(),
                                name=name)

            for k in entry.keys():
                if k == "key":
                    continue

                dbEntry[k] = entry[k]

                # Special case: Convert old module root nodes!!!
                if (isinstance(skel, (HierarchySkel, TreeLeafSkel))
                        and k in ["parentdir", "parententry", "parentrepo"]
                        and entry[k]):

                    key = db.Key(encoded=str(entry[k]))
                    if key.parent():
                        parent = db.Key(
                            encoded=utils.normalizeKey(key.parent()))
                    else:
                        parent = None

                    if key.id_or_name() and "_modul_" in str(key.id_or_name()):
                        name = key.id_or_name().replace("_modul_", "_module_")
                    else:
                        name = key.id_or_name()

                    dbEntry[k] = str(
                        db.Key.from_path(key.kind(), name, parent=parent))

            db.Put(dbEntry)
            skel.fromDB(str(dbEntry.key()))
            skel.refresh()
            skel.toDB(clearUpdateTag=True)
            amount += 1

        iterImport(module, target, exportKey, res["cursor"], amount)