def copy_page_and_children(self, foreign_id, parent_id, depth=0):
        '''
        Recusively copies over pages, their translations, and child pages
        '''
        url = "{}/api/v2/pages/{}/".format(self.base_url, foreign_id)

        self.log(ACTION, "Requesting Data", {"url": url}, depth)
        try:
            # TODO: create a robust wrapper around this functionality
            response = requests.get(url)
            content = json.loads(response.content)
        except Exception as e:
            self.log(ERROR, "Requesting Data - abandoning copy",
                     {"url": url, "exception": e}, depth)
            return None

        parent = Page.objects.get(id=parent_id).specific
        page = None
        try:
            self.log(ACTION, "Create Page", {"url": url}, depth)
            page = self.attach_page(parent, content)
            if page:
                self.log(SUCCESS, "Create Page",
                         {"url": url,
                          "page title": page.title.encode('utf-8')},
                         depth)
        except PageNotImportable as e:
            message = e.message.pop("message")
            self.log(WARNING, message, e.message.pop("message"), depth)
            return None

        if page:
            # create translations
            if content["meta"]["translations"]:
                for translation_obj in content["meta"]["translations"]:
                    _url = "{}/api/v2/pages/{}/".format(self.base_url,
                                                        translation_obj["id"])
                    # TODO: create a robust wrapper around this functionality
                    _response = requests.get(_url)
                    self.log(
                        ACTION,
                        "Getting translated content",
                        {"url": _url}, depth)
                    if _response.content:
                        _content = json.loads(_response.content)

                        if ("locale" in translation_obj and
                                translation_obj["locale"]):
                            self.attach_translated_content(
                                page, _content, translation_obj["locale"])
                        else:
                            self.log(
                                ERROR,
                                "locale is null",
                                {"url": _url, }, depth)
                    else:
                        self.log(
                            ERROR,
                            "Getting translated content",
                            {"url": _url}, depth)

            main_language_child_ids = content["meta"]["main_language_children"]

            # recursively iterate through child nodes
            if main_language_child_ids:
                for main_language_child_id in main_language_child_ids:
                    try:
                        self.copy_page_and_children(
                            foreign_id=main_language_child_id,
                            parent_id=page.id, depth=depth + 1)
                    except Exception as e:
                        self.log(ERROR, "Copying Children",
                                 {"url": url, "exception": e})
def copy_page_and_children ( self , foreign_id , parent_id , depth = 0 ) : url = "{}/api/v2/pages/{}/" . format ( self . base_url , foreign_id ) self . log ( ACTION , "Requesting Data" , { "url" : url } , depth ) try : response = requests . get ( url ) content = json . loads ( response . content ) except Exception as e : self . log ( ERROR , "Requesting Data - abandoning copy" , { "url" : url , "exception" : e } , depth ) return None parent = Page . objects . get ( id = parent_id ) . specific page = None try : self . log ( ACTION , "Create Page" , { "url" : url } , depth ) page = self . attach_page ( parent , content ) if page : self . log ( SUCCESS , "Create Page" , { "url" : url , "page title" : page . title . encode ( 'utf-8' ) } , depth ) except PageNotImportable as e : message = e . message . pop ( "message" ) self . log ( WARNING , message , e . message . pop ( "message" ) , depth ) return None if page : if content [ "meta" ] [ "translations" ] : for translation_obj in content [ "meta" ] [ "translations" ] : _url = "{}/api/v2/pages/{}/" . format ( self . base_url , translation_obj [ "id" ] ) _response = requests . get ( _url ) self . log ( ACTION , "Getting translated content" , { "url" : _url } , depth ) if _response . content : _content = json . loads ( _response . content ) if ( "locale" in translation_obj and translation_obj [ "locale" ] ) : self . attach_translated_content ( page , _content , translation_obj [ "locale" ] ) else : self . log ( ERROR , "locale is null" , { "url" : _url , } , depth ) else : self . log ( ERROR , "Getting translated content" , { "url" : _url } , depth ) main_language_child_ids = content [ "meta" ] [ "main_language_children" ] if main_language_child_ids : for main_language_child_id in main_language_child_ids : try : self . copy_page_and_children ( foreign_id = main_language_child_id , parent_id = page . id , depth = depth + 1 ) except Exception as e : self . log ( ERROR , "Copying Children" , { "url" : url , "exception" : e } )
Recusively copies over pages, their translations, and child pages