Ejemplo n.º 1
0
    def create_urlpath(
            cls,
            parent,
            slug,
            site=None,
            title="Root",
            article_kwargs={},
            request=None,
            article_w_permissions=None,
            **revision_kwargs):
        """
        Utility function:
        Creates a new urlpath with an article and a new revision for the
        article

        :returns: A new URLPath instance
        """
        if not site:
            site = Site.objects.get_current()
        article = Article(**article_kwargs)
        article.add_revision(ArticleRevision(title=title, **revision_kwargs),
                             save=True)
        article.save()
        newpath = cls.objects.create(
            site=site,
            parent=parent,
            slug=slug,
            article=article)
        article.add_object_relation(newpath)
        return newpath
Ejemplo n.º 2
0
 def get_lost_and_found():
     if ns.lost_and_found:
         return ns.lost_and_found
     try:
         ns.lost_and_found = URLPath.objects.get(
             slug=settings.LOST_AND_FOUND_SLUG,
             parent=URLPath.root(),
             site=site)
     except URLPath.DoesNotExist:
         article = Article(group_read=True,
                           group_write=False,
                           other_read=False,
                           other_write=False)
         article.add_revision(
             ArticleRevision(
                 content=_(
                     'Articles who lost their parents\n'
                     '===============================\n\n'
                     'The children of this article have had their parents deleted. You should probably find a new home for them.'),
                 title=_("Lost and found")))
         ns.lost_and_found = URLPath.objects.create(
             slug=settings.LOST_AND_FOUND_SLUG,
             parent=URLPath.root(),
             site=site,
             article=article)
         article.add_object_relation(ns.lost_and_found)
     return ns.lost_and_found
Ejemplo n.º 3
0
 def get_lost_and_found():
     if ns.lost_and_found:
         return ns.lost_and_found
     try:
         ns.lost_and_found = URLPath.objects.get(
             slug=settings.LOST_AND_FOUND_SLUG,
             parent=URLPath.root(),
             site=site)
     except URLPath.DoesNotExist:
         article = Article(group_read=True,
                           group_write=False,
                           other_read=False,
                           other_write=False)
         article.add_revision(
             ArticleRevision(
                 content=_(
                     'Articles who lost their parents\n'
                     '===============================\n\n'
                     'The children of this article have had their parents deleted. You should probably find a new home for them.'),
                 title=_("Lost and found")))
         ns.lost_and_found = URLPath.objects.create(
             slug=settings.LOST_AND_FOUND_SLUG,
             parent=URLPath.root(),
             site=site,
             article=article)
         article.add_object_relation(ns.lost_and_found)
     return ns.lost_and_found
Ejemplo n.º 4
0
def on_article_delete(instance, *args, **kwargs):
    # If an article is deleted, then throw out its URLPaths
    # But move all descendants to a lost-and-found node.
    site = Site.objects.get_current()

    # Get the Lost-and-found path or create a new one
    try:
        lost_and_found = URLPath.objects.get(slug=settings.LOST_AND_FOUND_SLUG, parent=URLPath.root(), site=site)
    except URLPath.DoesNotExist:
        article = Article(group_read=True, group_write=False, other_read=False, other_write=False)
        article.add_revision(
            ArticleRevision(
                content=_(
                    u"Articles who lost their parents\n"
                    "===============================\n\n"
                    "The children of this article have had their parents deleted. You should probably find a new home for them."
                ),
                title=_(u"Lost and found"),
            )
        )
        lost_and_found = URLPath.objects.create(
            slug=settings.LOST_AND_FOUND_SLUG, parent=URLPath.root(), site=site, article=article
        )
        article.add_object_relation(lost_and_found)

    for urlpath in URLPath.objects.filter(articles__article=instance, site=site):
        # Delete the children
        for child in urlpath.get_children():
            child.move_to(lost_and_found)
        # ...and finally delete the path itself
        # TODO: This should be unnecessary because of URLPath.article(...ondelete=models.CASCADE)
        urlpath.delete()
Ejemplo n.º 5
0
    def create_urlpath(
            cls,
            parent,
            slug,
            site=None,
            title="Root",
            article_kwargs={},
            request=None,
            article_w_permissions=None,
            **revision_kwargs):
        """
        Utility function:
        Creates a new urlpath with an article and a new revision for the
        article

        :returns: A new URLPath instance
        """
        if not site:
            site = Site.objects.get_current()
        article = Article(**article_kwargs)
        article.add_revision(ArticleRevision(title=title, **revision_kwargs),
                             save=True)
        article.save()
        newpath = cls.objects.create(
            site=site,
            parent=parent,
            slug=slug,
            article=article)
        article.add_object_relation(newpath)
        return newpath
Ejemplo n.º 6
0
def on_article_delete(instance, *args, **kwargs):
    # If an article is deleted, then throw out its URLPaths
    # But move all descendants to a lost-and-found node.
    site = get_current_site(get_current_request())

    # Get the Lost-and-found path or create a new one
    try:
        lost_and_found = URLPath.objects.get(slug=settings.LOST_AND_FOUND_SLUG,
                                             parent=URLPath.root(),
                                             site=site)
    except URLPath.DoesNotExist:
        article = Article(group_read = True,
                          group_write = False,
                          other_read = False,
                          other_write = False)
        article.add_revision(ArticleRevision(
                 content=_(u'Articles who lost their parents\n'
                            '===============================\n\n'
                            'The children of this article have had their parents deleted. You should probably find a new home for them.'),
                 title=_(u"Lost and found")))
        lost_and_found = URLPath.objects.create(slug=settings.LOST_AND_FOUND_SLUG,
                                                parent=URLPath.root(),
                                                site=site,
                                                article=article)
        article.add_object_relation(lost_and_found)

    
    for urlpath in URLPath.objects.filter(articles__article=instance, site=site):
        # Delete the children
        for child in urlpath.get_children():
            child.move_to(lost_and_found)
        # ...and finally delete the path itself
        # TODO: This should be unnecessary because of URLPath.article(...ondelete=models.CASCADE)
        urlpath.delete()
Ejemplo n.º 7
0
 def create_article(cls, parent, slug, site=None, title="Root", article_kwargs={}, **kwargs):
     """Utility function:
     Create a new urlpath with an article and a new revision for the article"""
     if not site:
         site = Site.objects.get_current()
     article = Article(**article_kwargs)
     article.add_revision(ArticleRevision(title=title, **kwargs), save=True)
     article.save()
     newpath = cls.objects.create(site=site, parent=parent, slug=slug, article=article)
     article.add_object_relation(newpath)
     return newpath
Ejemplo n.º 8
0
 def create_article(cls, parent, slug, site=None, title="Root", article_kwargs={}, **kwargs):
     """Utility function:
     Create a new urlpath with an article and a new revision for the article"""
     if not site: site = get_current_site(get_current_request())
     article = Article(**article_kwargs)
     article.add_revision(ArticleRevision(title=title, **kwargs),
                          save=True)
     article.save()
     newpath = cls.objects.create(site=site, parent=parent, slug=slug, article=article)
     article.add_object_relation(newpath)
     return newpath
Ejemplo n.º 9
0
 def create_root(cls, site=None, title="Root", **kwargs):
     if not site:
         site = Site.objects.get_current()
     root_nodes = cls.objects.root_nodes().filter(site=site)
     if not root_nodes:
         # (get_or_create does not work for MPTT models??)
         article = Article()
         article.add_revision(ArticleRevision(title=title, **kwargs), save=True)
         article.save()
         root = cls.objects.create(site=site, article=article)
         article.add_object_relation(root)
     else:
         root = root_nodes[0]
     return root
Ejemplo n.º 10
0
 def create_root(cls, site=None, title="Root", **kwargs):
     if not site: site = Site.objects.get_current()
     root_nodes = cls.objects.root_nodes().filter(site=site)
     if not root_nodes:
         # (get_or_create does not work for MPTT models??)
         article = Article()
         article.add_revision(ArticleRevision(title=title, **kwargs),
                              save=True)
         article.save()
         root = cls.objects.create(site=site, article=article)
         article.add_object_relation(root)
     else:
         root = root_nodes[0]
     return root
Ejemplo n.º 11
0
 def create_root(cls, site=None, title="Root", request=None, **kwargs):
     if not site:
         site = Site.objects.get_current()
     root_nodes = cls.objects.root_nodes().filter(site=site)
     if not root_nodes:
         article = Article()
         revision = ArticleRevision(title=title, **kwargs)
         if request:
             revision.set_from_request(request)
         article.add_revision(revision, save=True)
         article.save()
         root = cls.objects.create(site=site, article=article)
         article.add_object_relation(root)
     else:
         root = root_nodes[0]
     return root
Ejemplo n.º 12
0
 def create_root(cls, site=None, title="Root", request=None, **kwargs):
     if not site:
         site = Site.objects.get_current()
     root_nodes = cls.objects.root_nodes().filter(site=site)
     if not root_nodes:
         article = Article()
         revision = ArticleRevision(title=title, **kwargs)
         if request:
             revision.set_from_request(request)
         article.add_revision(revision, save=True)
         article.save()
         root = cls.objects.create(site=site, article=article)
         article.add_object_relation(root)
     else:
         root = root_nodes[0]
     return root
Ejemplo n.º 13
0
    def import_page(self, api, site, page, current_site, url_root,
                    user_matching, replace_existing):

        import pypandoc

        # Filter titles, to avoid stranges charaters.
        title = only_printable(page.title)
        urltitle = slugify(only_printable(urllib.unquote(page.urltitle))[:50])

        added = 1

        while urltitle in self.articles_worked_on:
            title = only_printable(page.title) + " " + str(added)
            urltitle = only_printable(
                slugify((urllib.unquote(page.urltitle))[:47] + " " +
                        str(added)))
            added += 1

        self.articles_worked_on.append(urltitle)

        print("Working on %s (%s)" % (title, urltitle))

        # Check if the URL path already exists
        try:
            urlp = URLPath.objects.get(slug=urltitle)

            self.matching_old_link_new_link[
                page.title] = urlp.article.get_absolute_url()

            if not replace_existing:
                print("\tAlready existing, skipping...")
                return

            print("\tDestorying old version of the article")
            urlp.article.delete()

        except URLPath.DoesNotExist:
            pass

        # Create article
        article = Article()

        for history_page in page.getHistory()[-2:][::-1]:

            try:
                if history_page['user'] in user_matching:
                    user = get_user_model().objects.get(
                        pk=user_matching[history_page['user']])
                else:
                    user = get_user_model().objects.get(
                        username=history_page['user'])
            except get_user_model().DoesNotExist:
                print(
                    "\tCannot found user with username=%s. Use --user-matching \"%s:<user_pk>\" to manualy set it"
                    % (
                        history_page['user'],
                        history_page['user'],
                    ))
                user = None

            article_revision = ArticleRevision()
            article_revision.content = pypandoc.convert(
                history_page['*'], 'md', 'mediawiki')
            article_revision.title = title
            article_revision.user = user
            article_revision.owner = user

            article.add_revision(article_revision, save=True)

            article_revision.created = history_page['timestamp']
            article_revision.save()

        # Updated lastest content WITH expended templates
        # TODO ? Do that for history as well ?
        article_revision.content = pypandoc.convert(
            striptags(page.getWikiText(True, True).decode('utf-8')).replace(
                '__NOEDITSECTION__', '').replace('__NOTOC__', ''), 'md',
            'mediawiki')
        article_revision.save()

        article.save()

        upath = URLPath.objects.create(site=current_site,
                                       parent=url_root,
                                       slug=urltitle,
                                       article=article)
        article.add_object_relation(upath)

        self.matching_old_link_new_link[
            page.title] = upath.article.get_absolute_url()

        self.articles_imported.append((article, article_revision))
Ejemplo n.º 14
0
    def import_page(self, api, site, page, current_site, url_root, user_matching, replace_existing):

        import pypandoc

        # Filter titles, to avoid stranges charaters.
        title = only_printable(page.title)
        urltitle = slugify(only_printable(urllib.unquote(page.urltitle))[:50])

        added = 1

        while urltitle in self.articles_worked_on:
            title = only_printable(page.title) + " " + str(added)
            urltitle = only_printable(slugify((urllib.unquote(page.urltitle))[:47] + " " + str(added)))
            added += 1

        self.articles_worked_on.append(urltitle)

        print "Working on %s (%s)" % (title, urltitle)

        # Check if the URL path already exists
        try:
            urlp = URLPath.objects.get(slug=urltitle)

            self.matching_old_link_new_link[page.title] = urlp.article.get_absolute_url()

            if not replace_existing:
                print "\tAlready existing, skipping..."
                return

            print "\tDestorying old version of the article"
            urlp.article.delete()

        except URLPath.DoesNotExist:
            pass

        # Create article
        article = Article()

        for history_page in page.getHistory()[-2:][::-1]:

            try:
                if history_page['user'] in user_matching:
                    user = get_user_model().objects.get(pk=user_matching[history_page['user']])
                else:
                    user = get_user_model().objects.get(username=history_page['user'])
            except get_user_model().DoesNotExist:
                print "\tCannot found user with username=%s. Use --user-matching \"%s:<user_pk>\" to manualy set it" % (history_page['user'], history_page['user'], )
                user = None

            article_revision = ArticleRevision()
            article_revision.content = pypandoc.convert(history_page['*'], 'md', 'mediawiki')
            article_revision.title = title
            article_revision.user = user
            article_revision.owner = user

            article.add_revision(article_revision, save=True)

            article_revision.created = history_page['timestamp']
            article_revision.save()

        # Updated lastest content WITH expended templates
        # TODO ? Do that for history as well ?
        article_revision.content = pypandoc.convert(striptags(page.getWikiText(True, True).decode('utf-8')).replace('__NOEDITSECTION__', '').replace('__NOTOC__', ''), 'md', 'mediawiki')
        article_revision.save()

        article.save()

        upath = URLPath.objects.create(site=current_site, parent=url_root, slug=urltitle, article=article)
        article.add_object_relation(upath)

        self.matching_old_link_new_link[page.title] = upath.article.get_absolute_url()

        self.articles_imported.append((article, article_revision))
Ejemplo n.º 15
0
    def import_page(
        self,
        api,
        site,
        page,
        current_site,
        url_root,
        user_matching,
        replace_existing,
    ):

        from wikitools.pagelist import listFromQuery

        # Filter titles, to avoid stranges charaters.
        title = page.title
        urltitle = title
        urltitle = urltitle.replace("ø", "o")
        urltitle = urltitle.replace("æ", "ae")
        urltitle = urltitle.replace("å", "a")
        urltitle = urltitle.replace("Ø", "O")
        urltitle = urltitle.replace("Æ", "AE")
        urltitle = urltitle.replace("Å", "A")
        urltitle = only_printable(urltitle)
        urltitle = slugify(only_printable(urllib.parse.unquote(urltitle))[:50])

        added = 1

        while urltitle in self.articles_worked_on:
            title = only_printable("{} {}".format(page.title, added))
            urltitle = slugify("{} {}".format(
                only_printable(urllib.parse.unquote(page.urltitle))[:47],
                added))

            added += 1

        self.articles_worked_on.append(urltitle)

        print("Working on {} ({})".format(title, urltitle))
        print(url_root)
        print(urltitle)
        print()
        # Check if the URL path already exists
        try:
            urlp = URLPath.objects.get(slug=urltitle)

            self.matching_old_link_new_link[
                page.title] = urlp.article.get_absolute_url()

            if not replace_existing:
                print("\tAlready existing, skipping...")
                return

            print("\tDestorying old version of the article")
            urlp.article.delete()

        except URLPath.DoesNotExist:
            pass

        # Create article
        article = Article()

        history_page = page.getHistory()[0]

        try:
            if history_page['user'] in user_matching:
                user = get_user_model().objects.get(
                    pk=user_matching[history_page['user']])
            else:
                user = get_user_model().objects.get(
                    username=history_page['user'])
        except get_user_model().DoesNotExist:
            user = None
        except Exception:
            print("Couldn't find user. Something is weird.")

        article_revision = ArticleRevision()
        article_revision.content = refactor(page.getWikiText())
        article_revision.title = title
        article_revision.user = user
        article_revision.owner = user
        article_revision.content = re.sub("\[\[.*(Category|Kategori).*\]\]\n",
                                          "", article_revision.content)

        article.add_revision(article_revision, save=True)

        article_revision.created = history_page['timestamp']
        article_revision.save()

        # Updated lastest content WITH expended templates
        # TODO ? Do that for history as well ?

        article_revision.save()

        article.save()

        upath = URLPath.objects.create(site=current_site,
                                       parent=url_root,
                                       slug=urltitle,
                                       article=article)
        article.add_object_relation(upath)

        self.matching_old_link_new_link[
            page.title] = upath.article.get_absolute_url()

        self.articles_imported.append((article, article_revision))
Ejemplo n.º 16
0
    def import_page(
            self,
            api,
            site,
            page,
            current_site,
            url_root,
            user_matching,
            replace_existing,
            ):

        from wikitools.pagelist import listFromQuery



        # Filter titles, to avoid stranges charaters.
        title = page.title
        urltitle = title
        urltitle = urltitle.replace("ø", "o") 
        urltitle = urltitle.replace("æ", "ae") 
        urltitle = urltitle.replace("å", "a") 
        urltitle = urltitle.replace("Ø", "O") 
        urltitle = urltitle.replace("Æ", "AE") 
        urltitle = urltitle.replace("Å", "A") 
        urltitle = only_printable(urltitle)
        urltitle = slugify(only_printable(urllib.parse.unquote(urltitle))[:50])


        added = 1

        while urltitle in self.articles_worked_on:
            title = only_printable("{} {}".format(page.title, added))
            urltitle = slugify(
                "{} {}".format(only_printable(urllib.parse.unquote(page.urltitle))[:47], added)
            )

            added += 1

        self.articles_worked_on.append(urltitle)

        print("Working on {} ({})".format(title, urltitle))
        print(url_root)
        print(urltitle)
        print()
        # Check if the URL path already exists
        try:
            urlp = URLPath.objects.get(slug=urltitle)

            self.matching_old_link_new_link[
                page.title] = urlp.article.get_absolute_url()

            if not replace_existing:
                print("\tAlready existing, skipping...")
                return

            print("\tDestorying old version of the article")
            urlp.article.delete()

        except URLPath.DoesNotExist:
            pass

        # Create article
        article = Article()

        history_page = page.getHistory()[0]

        try:
            if history_page['user'] in user_matching:
                user = get_user_model().objects.get(
                    pk=user_matching[
                        history_page['user']])
            else:
                user = get_user_model().objects.get(
                    username=history_page['user'])
        except get_user_model().DoesNotExist:
            user = None
        except Exception:
            print("Couldn't find user. Something is weird.")

        article_revision = ArticleRevision()
        article_revision.content = refactor(page.getWikiText())
        article_revision.title = title
        article_revision.user = user
        article_revision.owner = user
        article_revision.content = re.sub("\[\[.*(Category|Kategori).*\]\]\n", "", article_revision.content)

        article.add_revision(article_revision, save=True)

        article_revision.created = history_page['timestamp']
        article_revision.save()

        # Updated lastest content WITH expended templates
        # TODO ? Do that for history as well ?
        
        article_revision.save()

        article.save()

        upath = URLPath.objects.create(
            site=current_site,
            parent=url_root,
            slug=urltitle,
            article=article)
        article.add_object_relation(upath)

        self.matching_old_link_new_link[
            page.title] = upath.article.get_absolute_url()

        self.articles_imported.append((article, article_revision))