Example #1
0
    def delete(self, user=None):
        if not is_admin_user(user):
            raise RuntimeError("Only admin can delete pages.")

        self.update_content("", self.revision, None, user, force_update=False, dont_create_rev=True)
        self.related_links = {}
        self.modifier = None
        self.updated_at = None
        self.revision = 0
        self.put()

        keys = [r.key for r in self.revisions]
        ndb.delete_multi(keys)

        cache.del_titles()
Example #2
0
    def update_content(self, new_body, base_revision, comment, user=None, force_update=False):
        if not force_update and self.body == new_body:
            return False

        # delete rendered body cache
        cache.del_rendered_body(self.title)

        # update body
        old_md = self.metadata
        new_md = WikiPage.parse_metadata(new_body)

        # validate contents
        if u'pub' in new_md and u'redirect' in new_md:
            raise ValueError('You cannot use "pub" and "redirect" metadata at '
                             'the same time.')
        if u'redirect' in new_md and len(WikiPage.remove_metadata(new_body).strip()) != 0:
            raise ValueError('Page with "redirect" metadata cannot have a body '
                             'content.')
        if u'read' in new_md and new_md['content-type'] != 'text/x-markdown':
            raise ValueError('You cannot restrict read access of custom content-typed page.')
        if self.revision < base_revision:
            raise ValueError('Invalid revision number: %d' % base_revision)

        # update model fields
        if self.revision != base_revision:
            # perform 3-way merge if needed
            base = WikiPageRevision.query(WikiPageRevision.title == self.title, WikiPageRevision.revision == base_revision).get().body
            merged = ''.join(Merge3(base, self.body, new_body).merge_lines())
            self.body = merged
        else:
            self.body = new_body

        self.modifier = user
        self.description = self.make_description(200)
        self.acl_read = new_md.get('read', '')
        self.acl_write = new_md.get('write', '')
        self.comment = comment
        self.revision += 1

        if not force_update:
            self.updated_at = datetime.now()

        # publish
        pub_old = u'pub' in old_md
        pub_new = u'pub' in new_md
        pub_old_title = None
        pub_new_title = None
        if pub_old:
            pub_old_title = old_md['pub']
        if pub_new:
            pub_new_title = new_md['pub']

        if pub_old and pub_new and (pub_old_title != pub_new_title):
            # if target page is changed
            self._unpublish(save=False)
            self._publish(title=pub_new_title, save=False)
        else:
            if pub_new:
                self._publish(title=pub_new_title, save=False)
            else:
                self._unpublish(save=False)

        # update related pages if it's first time
        if self.revision == 1:
            for _ in range(5):
                self.update_related_links()

        # update itemtype_path
        self.itemtype_path = schema.get_itemtype_path(self.itemtype)

        # update hashbangs
        self.hashbangs = WikiPage.extract_hashbangs(self.rendered_body)

        # save
        self.put()

        # create revision
        rev_key = self._rev_key()
        rev = WikiPageRevision(parent=rev_key, title=self.title, body=self.body,
                               created_at=self.updated_at, revision=self.revision,
                               comment=self.comment, modifier=self.modifier,
                               acl_read=self.acl_read, acl_write=self.acl_write)
        rev.put()

        # update inlinks and outlinks
        old_redir = old_md.get('redirect')
        new_redir = new_md.get('redirect')
        self.update_links(old_redir, new_redir)

        # invalidate cache
        cache.del_yaml(self.title)
        if self.revision == 1:
            cache.del_titles()

        return True
Example #3
0
    def update_content(self, new_body, base_revision, comment="", user=None, force_update=False, dont_create_rev=False):
        if not force_update and self.body == new_body:
            return False

        # get old data amd metadata
        old_md = self.metadata
        old_data = self.data

        # validate contents
        ## validate schema data
        new_md = PageOperationMixin.parse_metadata(new_body)
        try:
            PageOperationMixin.parse_data(self.title, new_md["schema"], new_body)
        except Exception:
            raise ValueError("Invalid schema data")

        ## validate metadata
        if u"pub" in new_md and u"redirect" in new_md:
            raise ValueError('You cannot use "pub" and "redirect" metadata at ' "the same time.")
        if u"redirect" in new_md and len(PageOperationMixin.remove_metadata(new_body).strip()) != 0:
            raise ValueError('Page with "redirect" metadata cannot have a body ' "content.")
        if u"read" in new_md and new_md["content-type"] != "text/x-markdown":
            raise ValueError("You cannot restrict read access of custom content-typed page.")

        ## validate revision
        if self.revision < base_revision:
            raise ValueError("Invalid revision number: %d" % base_revision)

        ## validate ToC
        if not TocGenerator(md.convert(new_body)).validate():
            raise ValueError("Duplicate paths not allowed")

        if self.revision != base_revision:
            # perform 3-way merge if needed
            base = (
                WikiPageRevision.query(WikiPageRevision.title == self.title, WikiPageRevision.revision == base_revision)
                .get()
                .body
            )
            merged = "".join(Merge3(base, self.body, new_body).merge_lines())
            conflicted = len(re.findall(PageOperationMixin.re_conflicted, merged)) > 0
            if conflicted:
                raise ConflictError("Conflicted", base, new_body, merged)
            else:
                new_body = merged

        # delete rendered body, metadata, data cache
        cache.del_rendered_body(self.title)
        cache.del_hashbangs(self.title)
        cache.del_metadata(self.title)
        cache.del_data(self.title)

        # update model fields
        self.body = new_body
        self.modifier = user
        self.description = self.make_description(200)
        self.acl_read = new_md.get("read", "")
        self.acl_write = new_md.get("write", "")
        self.comment = comment
        if not dont_create_rev:
            self.revision += 1

        if not force_update:
            self.updated_at = datetime.now()

        # publish
        pub_old = u"pub" in old_md
        pub_new = u"pub" in new_md
        pub_old_title = None
        pub_new_title = None
        if pub_old:
            pub_old_title = old_md["pub"]
        if pub_new:
            pub_new_title = new_md["pub"]

        if pub_old and pub_new and (pub_old_title != pub_new_title):
            # if target page is changed
            self._unpublish(save=False)
            self._publish(title=pub_new_title, save=False)
        else:
            if pub_new:
                self._publish(title=pub_new_title, save=False)
            else:
                self._unpublish(save=False)

        # update itemtype_path
        self.itemtype_path = schema.get_itemtype_path(new_md["schema"])

        # save
        self.put()

        # create revision
        if not dont_create_rev:
            rev_key = self._rev_key()
            rev = WikiPageRevision(
                parent=rev_key,
                title=self.title,
                body=self.body,
                created_at=self.updated_at,
                revision=self.revision,
                comment=self.comment,
                modifier=self.modifier,
                acl_read=self.acl_read,
                acl_write=self.acl_write,
            )
            rev.put()

        # deferred update schema data index
        new_data = self.data
        deferred.defer(self.rebuild_data_index_deferred, old_data, new_data)

        # update inlinks and outlinks
        old_redir = old_md.get("redirect")
        new_redir = new_md.get("redirect")
        self.update_links(old_redir, new_redir)

        # delete config and tittle cache
        if self.title == ".config":
            cache.del_config()
        if self.revision == 1:
            cache.del_titles()

        return True