예제 #1
0
 def destroy(self, comment=u'', destroy_item=False):
     action = DESTROY_ALL if destroy_item else DESTROY_REV
     item_modified.send(app, fqname=self.fqname, action=action, meta=self.meta,
                        content=self.rev.data, comment=comment)
     # called from destroy UI/POST
     if destroy_item:
         # destroy complete item with all revisions, metadata, etc.
         self.rev.item.destroy_all_revisions()
     else:
         # just destroy this revision
         self.rev.item.destroy_revision(self.rev.revid)
예제 #2
0
 def destroy(self, comment=u'', destroy_item=False):
     action = DESTROY_ALL if destroy_item else DESTROY_REV
     item_modified.send(app, fqname=self.fqname, action=action, meta=self.meta,
                        content=self.rev.data, comment=comment)
     # called from destroy UI/POST
     if destroy_item:
         # destroy complete item with all revisions, metadata, etc.
         self.rev.item.destroy_all_revisions()
     else:
         # just destroy this revision
         self.rev.item.destroy_revision(self.rev.revid)
예제 #3
0
    def _save(self, meta, data=None, name=None, action=ACTION_SAVE, contenttype_guessed=None, comment=None,
              overwrite=False, delete=False):
        backend = flaskg.storage
        storage_item = backend.get_item(**self.fqname.query)
        try:
            currentrev = storage_item.get_revision(CURRENT)
            rev_id = currentrev.revid
            contenttype_current = currentrev.meta.get(CONTENTTYPE)
        except KeyError:  # XXX was: NoSuchRevisionError:
            currentrev = None
            rev_id = None
            contenttype_current = None

        meta = dict(meta)  # we may get a read-only dict-like, copy it

        if 'acl' in meta:
            # we treat this as nothing specified, so fallback to default
            if meta['acl'] == 'None':
                meta.pop('acl')
            # this is treated as a rule which matches nothing
            elif meta['acl'] == 'Empty':
                meta['acl'] = ''

        # we store the previous (if different) and current item name into revision metadata
        # this is useful for rename history and backends that use item uids internally
        if self.fqname.field == NAME_EXACT:
            if name is None:
                name = self.fqname.value
            oldname = meta.get(NAME)
            if oldname:
                if not isinstance(oldname, list):
                    oldname = [oldname]
                if delete or name not in oldname:  # this is a delete or rename
                    try:
                        oldname.remove(self.name)
                    except ValueError:
                        pass
                    if not delete:
                        oldname.append(name)
                    meta[NAME] = oldname
            elif not meta.get(ITEMID):
                meta[NAME] = [name]

        if meta.get(NAMESPACE) is None:
            meta[NAMESPACE] = self.fqname.namespace

        if comment is not None:
            meta[COMMENT] = unicode(comment)

        if currentrev:
            current_names = currentrev.meta.get(NAME, [])
            new_names = meta.get(NAME, [])
            deleted_names = set(current_names) - set(new_names)
            if deleted_names:  # some names have been deleted.
                meta[NAME_OLD] = current_names
                if not new_names:  # if no names left, then set the trash flag.
                    meta[TRASH] = True

        if not overwrite and REVID in meta:
            # we usually want to create a new revision, thus we must remove the existing REVID
            del meta[REVID]

        if data is None:
            if currentrev is not None:
                # we don't have (new) data, just copy the old one.
                # a valid usecase of this is to just edit metadata.
                data = currentrev.data
            else:
                data = ''

        if isinstance(data, unicode):
            data = data.encode(CHARSET)  # XXX wrong! if contenttype gives a coding, we MUST use THAT.

        if isinstance(data, str):
            data = StringIO(data)
        newrev = storage_item.store_revision(meta, data, overwrite=overwrite,
                                             action=unicode(action),
                                             contenttype_current=contenttype_current,
                                             contenttype_guessed=contenttype_guessed,
                                             return_rev=True,
                                             )
        item_modified.send(app, fqname=self.fqname, action=action)
        return newrev.revid, newrev.meta[SIZE]
예제 #4
0
    def _save(self,
              meta,
              data=None,
              name=None,
              action=ACTION_SAVE,
              contenttype_guessed=None,
              comment=None,
              overwrite=False,
              delete=False):
        backend = flaskg.storage
        storage_item = backend.get_item(**self.fqname.query)
        try:
            currentrev = storage_item.get_revision(CURRENT)
            rev_id = currentrev.revid
            contenttype_current = currentrev.meta.get(CONTENTTYPE)
        except KeyError:  # XXX was: NoSuchRevisionError:
            currentrev = None
            rev_id = None
            contenttype_current = None

        meta = dict(meta)  # we may get a read-only dict-like, copy it

        if 'acl' in meta:
            # we treat this as nothing specified, so fallback to default
            if meta['acl'] == 'None':
                meta.pop('acl')
            # this is treated as a rule which matches nothing
            elif meta['acl'] == 'Empty':
                meta['acl'] = ''

        # we store the previous (if different) and current item name into revision metadata
        # this is useful for rename history and backends that use item uids internally
        if self.fqname.field == NAME_EXACT:
            if name is None:
                name = self.fqname.value
            oldname = meta.get(NAME)
            if oldname:
                if not isinstance(oldname, list):
                    oldname = [oldname]
                if delete or name not in oldname:  # this is a delete or rename
                    try:
                        oldname.remove(self.name)
                    except ValueError:
                        pass
                    if not delete:
                        oldname.append(name)
                    meta[NAME] = oldname
            elif not meta.get(ITEMID):
                meta[NAME] = [name]

        if meta.get(NAMESPACE) is None:
            meta[NAMESPACE] = self.fqname.namespace

        if comment is not None:
            meta[COMMENT] = unicode(comment)

        if currentrev:
            current_names = currentrev.meta.get(NAME, [])
            new_names = meta.get(NAME, [])
            deleted_names = set(current_names) - set(new_names)
            if deleted_names:  # some names have been deleted.
                meta[NAME_OLD] = current_names
                if not new_names:  # if no names left, then set the trash flag.
                    meta[TRASH] = True

        if not overwrite and REVID in meta:
            # we usually want to create a new revision, thus we must remove the existing REVID
            del meta[REVID]

        if data is None:
            if currentrev is not None:
                # we don't have (new) data, just copy the old one.
                # a valid usecase of this is to just edit metadata.
                data = currentrev.data
            else:
                data = ''

        if isinstance(data, unicode):
            data = data.encode(
                CHARSET
            )  # XXX wrong! if contenttype gives a coding, we MUST use THAT.

        if isinstance(data, str):
            data = StringIO(data)
        newrev = storage_item.store_revision(
            meta,
            data,
            overwrite=overwrite,
            action=unicode(action),
            contenttype_current=contenttype_current,
            contenttype_guessed=contenttype_guessed,
            return_rev=True,
        )
        item_modified.send(app, fqname=self.fqname, action=action)
        return newrev.revid, newrev.meta[SIZE]