Esempio n. 1
0
 def test_get_table_column_numbers(self):
     text = wtp.parse("""{|
     |  column data  ||   second column data
     |}""")
     col_numbers = WikiTableService().get_table_column_numbers(
         text.tables[0])
     expected_col_numbers = 2
     self.assertEqual(col_numbers, expected_col_numbers)
Esempio n. 2
0
    def create_page(self, document_data: dict) -> None:
        """
        Creates a wiki page

        Keyword arguments:
        document_data -- All required data for a project using
                         Organised Editing Guidelines
        """
        mediawiki = MediaWikiService()
        wikitext = WikiTextService()
        token = mediawiki.get_token()

        page_title = self.templates.oeg_page

        overview_page_sections = self.document_to_page_sections(document_data)

        sections_text = wikitext.generate_text_from_dict(
            self.templates.page_template,
            f"=={self.templates.page_initial_section}==",
            overview_page_sections,
        )
        updated_text = WikiTableService().add_table_row(
            page_text=sections_text,
            new_row=self.generate_activities_list_table_row(document_data),
            table_section_title=self.templates.activities_list_section_title,
            table_template=self.templates.table_template,
        )
        if mediawiki.is_existing_page(page_title):
            page_text = MediaWikiService().get_page_text(
                self.templates.oeg_page)
            overview_page_table = (WikiSectionService().get_section_table(
                page_text,
                self.templates.activities_list_section_title).string)
            updated_text = WikiTableService().add_table_row(
                page_text=page_text,
                new_row=self.generate_activities_list_table_row(document_data),
                table_section_title=self.templates.
                activities_list_section_title,
                table_template=overview_page_table,
            )
            mediawiki.edit_page(token, self.templates.oeg_page, updated_text)
        else:
            mediawiki.create_page(token, page_title, updated_text)
Esempio n. 3
0
    def get_project_users(self, table_text: str):
        users_list_table = WikiTableService().get_text_table(table_text)
        users_list_data = users_list_table.data(span=False)
        users_list = []

        for table_row_number, table_row_data in enumerate(users_list_data[1:],
                                                          start=1):
            user_id = users_list_table.cells(
                row=table_row_number,
                column=self.templates.users_list_user_id_column).value

            user_name = users_list_table.cells(
                row=table_row_number,
                column=self.templates.users_list_user_name_column).value
            users_list.append({
                "userId": user_id.strip(),
                "userName": user_name.strip()
            })
        return users_list
Esempio n. 4
0
 def test_get_text_table(self):
     text_table = ("==Table section==\n"
                   "{|class='wikitable sortable'\n"
                   "|-\n"
                   '! scope="col" | First column header\n'
                   "|-\n"
                   "| First column data\n"
                   "|-\n"
                   "|}")
     table = WikiTableService().get_text_table(text_table)
     self.assertIsInstance(table, wtp.Table)
Esempio n. 5
0
 def test_get_new_row_index(self):
     text = wtp.parse("{|class='wikitable sortable'\n"
                      "|-\n"
                      '! scope="col" | First column data\n'
                      "|-\n"
                      "|}\n")
     table_new_row_index = WikiTableService().get_new_row_index(
         text.tables[0])
     header_delimiter_length = len("\n|-\n")
     expected_new_row_index = len(text) - header_delimiter_length
     self.assertEqual(table_new_row_index, expected_new_row_index)
    def get_organisation_projects(self, table_text: str):
        projects_list_table = WikiTableService().get_text_table(table_text)
        projects_list_data = projects_list_table.data(span=False)
        organisation_projects = []

        for table_row_number, table_row_data in enumerate(
                projects_list_data[1:], start=1):
            project_wiki_page_hyperlink = projects_list_table.cells(
                row=table_row_number,
                column=self.templates.projects_list_project_name_column,
            ).value
            project_name = WikiTextService(
            ).get_page_link_and_text_from_wiki_page_hyperlink(
                project_wiki_page_hyperlink)[1]

            project_author = projects_list_table.cells(
                row=table_row_number,
                column=self.templates.projects_list_project_author_column,
            ).value

            project_status = projects_list_table.cells(
                row=table_row_number,
                column=self.templates.projects_list_project_status_column,
            ).value

            organisation_projects.append({
                "name": project_name.strip(),
                "author": project_author.strip(),
                "status": project_status.strip(),
            })

        return organisation_projects
    def create_page(self, document_data: dict):
        """
        Creates a wiki page

        Keyword arguments:
        document_data -- All required data for a project using
                         Organised Editing Guidelines
        """
        mediawiki = MediaWikiService()

        organisation_page_sections = self.document_to_page_sections(
            document_data)

        sections_text = WikiTextService().generate_text_from_dict(
            self.templates.page_template,
            self.templates.page_initial_section,
            organisation_page_sections,
        )
        updated_text = WikiTableService().add_table_row(
            page_text=sections_text,
            new_row=self.generate_projects_list_table_row(document_data),
            table_section_title=self.templates.projects_section,
            table_template=self.templates.table_template,
        )

        page_title = f"{self.templates.oeg_page}/{document_data['organisation']['name'].capitalize()}"
        token = mediawiki.get_token()
        if mediawiki.is_existing_page(page_title):
            page_text = MediaWikiService().get_page_text(page_title)
            organisation_page_table = (WikiSectionService().get_section_table(
                page_text, self.templates.projects_list_section).string)
            updated_text = WikiTableService().add_table_row(
                page_text=page_text,
                new_row=self.generate_projects_list_table_row(document_data),
                table_section_title=self.templates.projects_list_section,
                table_template=organisation_page_table,
            )
            mediawiki.edit_page(token, page_title, updated_text)
        else:
            mediawiki.create_page(token, page_title, updated_text)
Esempio n. 8
0
    def get_overview_page_platforms_and_organisations(
            self, overview_page_table_text: str):
        overview_page_table = WikiTableService().get_text_table(
            overview_page_table_text)
        overview_page_table_data = overview_page_table.data(span=False)

        organisation_list = []
        platform_list = []
        wikitext = WikiTextService()

        for table_row_number, table_row_data in enumerate(
                overview_page_table_data[1:], start=1):
            hyperlinked_organisation_url = overview_page_table.cells(
                row=table_row_number,
                column=self.templates.overview_list_organisation_name_column,
            ).value

            hyperlinked_platform_url = overview_page_table.cells(
                row=table_row_number,
                column=self.templates.overview_list_platform_name_column,
            ).value

            organisation_list.append({
                "name":
                wikitext.get_page_link_and_text_from_wiki_page_hyperlink(
                    hyperlinked_organisation_url)[1]
            })

            (
                platform_url,
                platform_name,
            ) = wikitext.get_page_link_and_text_from_external_hyperlink(
                hyperlinked_platform_url)
            platform_list.append({"name": platform_name, "url": platform_url})

        return platform_list, organisation_list
    def get_organisation_projects_platforms(self, table_text):
        projects_list_table = WikiTableService().get_text_table(table_text)
        projects_list_data = projects_list_table.data(span=False)
        projects_platforms = []

        for table_row_number, table_row_data in enumerate(
                projects_list_data[1:], start=1):
            platform_external_hyperlink = projects_list_table.cells(
                row=table_row_number,
                column=self.templates.projects_list_platform_name_column,
            ).value
            (
                platform_url,
                platform_name,
            ) = WikiTextService(
            ).get_page_link_and_text_from_external_hyperlink(
                platform_external_hyperlink)

            projects_platforms.append({
                "name": platform_name.strip(),
                "url": platform_url.strip(),
            }  # noqa
                                      )
        return projects_platforms
Esempio n. 10
0
    def test_get_table_last_column_data(self):
        first_column_data, second_column_data = (" First column",
                                                 " Second column")
        text = wtp.parse(f"""{{|class='wikitable sortable'
        |-
        ! scope="col" |{first_column_data}
        ! scope="col" |{second_column_data}
        |-
        | First column data
        | Second column data
        |-
        |}}""")

        column_numbers = 2
        table_last_column_data = WikiTableService().get_table_last_column_data(
            text.tables[0], column_numbers)
        self.assertEqual(second_column_data, table_last_column_data)
Esempio n. 11
0
 def edit_page_text(self, update_fields: dict, overview_page_data: dict,
                    document_data: dict):
     page_text = MediaWikiService().get_page_text(self.templates.oeg_page)
     updated_table_fields = self.get_update_table_fields(
         update_fields, overview_page_data)
     if updated_table_fields:
         overview_page_table = WikiSectionService().get_section_table(
             page_text, self.templates.activities_list_section_title)
         project_list_section_title = (
             f"\n=={self.templates.page_initial_section}==\n"
             f"==={self.templates.activities_list_section_title}===\n")
         updated_text = WikiTableService().edit_table(
             overview_page_table.string,
             project_list_section_title,
             updated_table_fields,
         )
         return updated_text
     else:
         return page_text
Esempio n. 12
0
    def edit_page(self, document_data: dict, update_fields: dict,
                  project_page_data: dict):
        """
        Edits a wiki page

        Keyword arguments:
        document_data -- All required data for a project using
                         Organised Editing Guidelines
        """
        mediawiki = MediaWikiService()
        token = mediawiki.get_token()
        page_title = (f"{self.templates.oeg_page}/Projects/"
                      f'{project_page_data["project"]["name"].capitalize()}')

        project_page_sections = self.document_to_page_sections(document_data)

        sections_text = WikiTextService().generate_text_from_dict(
            self.templates.page_template,
            f"=={self.templates.page_initial_section}==",
            project_page_sections,
        )
        updated_text = WikiTableService().add_table_row(
            page_text=sections_text,
            new_row=self.get_project_users_table_rows(document_data),
            table_section_title=self.templates.team_user_section,
            table_template=self.templates.table_template,
        )

        if ("project" in update_fields.keys()
                and "name" in update_fields["project"].keys()
                and update_fields["project"]["name"].capitalize() !=
                project_page_data["project"]["name"].capitalize()):
            new_page = (f"{self.templates.oeg_page}/Projects/"
                        f'{document_data["project"]["name"].capitalize()}')
            mediawiki.move_page(token=token,
                                old_page=page_title,
                                new_page=new_page)
            mediawiki.edit_page(token, new_page, updated_text)
        else:
            mediawiki.edit_page(token, page_title, updated_text)
Esempio n. 13
0
    def create_page(self, document_data: dict) -> None:
        """
        Creates a wiki page

        Keyword arguments:
        document_data -- All required data for a project using
                         Organised Editing Guidelines
        """
        mediawiki = MediaWikiService()

        token = mediawiki.get_token()

        project_page_sections = self.document_to_page_sections(document_data)

        sections_text = WikiTextService().generate_text_from_dict(
            self.templates.page_template,
            f"=={self.templates.page_initial_section}==",
            project_page_sections,
        )
        updated_text = WikiTableService().add_table_row(
            page_text=sections_text,
            new_row=self.get_project_users_table_rows(document_data),
            table_section_title=self.templates.team_user_section,
            table_template=self.templates.table_template,
        )

        page_title = (f"{self.templates.oeg_page}/Projects/"
                      f'{document_data["project"]["name"].capitalize()}')
        if mediawiki.is_existing_page(page_title):
            raise ValueError(
                "Error reporting project "
                f"'{document_data['project']['name'].capitalize()}'."
                " Project already was reported.")
        else:
            mediawiki.create_page(
                token=token,
                page_title=page_title,
                page_text=updated_text,
            )
Esempio n. 14
0
    def test_add_table_row(self):
        page_text = "=Section=\n==Child section==\nSection text\n"
        new_table_row = "\n| First column data\n|-"
        table_section_title = "Table section"
        table_template = ("==Table section==\n"
                          "{|class='wikitable sortable'\n"
                          "|-\n"
                          '! scope="col" | First column header\n'
                          "|-\n"
                          "|}\n")
        text_with_table_row = WikiTableService().add_table_row(
            page_text, new_table_row, table_section_title, table_template)

        expected_table = (f"{page_text}"
                          "==Table section==\n"
                          "{|class='wikitable sortable'\n"
                          "|-\n"
                          '! scope="col" | First column header\n'
                          "|-\n"
                          "| First column data\n"
                          "|-\n"
                          "|}")
        self.assertEqual(expected_table, text_with_table_row)
Esempio n. 15
0
 def test_get_text_table_fails_with_text_missing_table(self):
     missing_table_text = "text"
     with self.assertRaises(ValueError):
         WikiTableService().get_text_table(missing_table_text)
Esempio n. 16
0
    def get_edit_page_text(
        self,
        update_fields: dict,
        current_organisation_page: dict,
        update_organisation_page: dict,
    ) -> str:
        """
        Get the text for a updated organisation page

        Keyword Arguments:
        update_fields -- Fields that are being updated
        current_organisation_page -- Dict with the current organisation page content that
                                     is being updated
        update_organisation_page -- Dict with the organisation page updated content

        Returns:
        updated_text -- Text for the updated organisation page
        """
        # Get text of a organisation page
        page_title = (
            f"{self.templates.oeg_page}/"
            f"{current_organisation_page['organisation']['name'].capitalize()}"
        )
        page_text = MediaWikiService().get_page_text(page_title)

        # Generate updated text for organisation page
        update_current_organisation_page = self.document_to_page_sections(
            update_organisation_page)
        sections_text = WikiTextService().generate_text_from_dict(
            page_text,
            self.templates.page_initial_section,
            update_current_organisation_page,
        )

        updated_table_fields = self.get_update_table_fields(
            update_fields, current_organisation_page)

        # Update organisation page text if none table field needs updated
        project_list_table = WikiSectionService().get_section_table(
            page_text, self.templates.projects_list_section)
        project_list_section_title = (
            f"\n=={self.templates.projects_section}==\n"
            f"==={self.templates.projects_list_section}===\n")
        updated_text = (sections_text + project_list_section_title +
                        project_list_table.string)
        if updated_table_fields:
            # Update organisation page text if any table field needs updated
            forbidden_fields = [
                self.templates.projects_list_project_author_column,
                self.templates.projects_list_project_status_column,
            ]
            project_wiki_page = WikiTextService().hyperlink_wiki_page(
                wiki_page=
                (f"{self.templates.oeg_page}/Projects/"
                 f'{current_organisation_page["project"]["name"].capitalize()}'
                 ),
                text=current_organisation_page["project"]["name"].capitalize(),
            )
            updated_project_list_table = WikiTableService().edit_table(
                project_list_table.string,
                project_list_section_title,
                updated_table_fields,
                project_wiki_page,
                forbidden_fields,
            )
            updated_text = sections_text + updated_project_list_table
        return updated_text