示例#1
0
def init_tutorials(app, url_prefix):
    session = talisker.requests.get_session()
    tutorials_discourse = Tutorials(
        parser=TutorialParser(
            api=DiscourseAPI(
                base_url="https://discourse.charmhub.io/",
                session=session,
                api_key=DISCOURSE_API_KEY,
                api_username=DISCOURSE_API_USERNAME,
                get_topics_query_id=2,
            ),
            index_topic_id=2628,
            url_prefix=url_prefix,
        ),
        document_template="tutorials/tutorial.html",
        url_prefix=url_prefix,
        blueprint_name="tutorials",
    )

    @app.route(url_prefix)
    def index():
        tutorials_discourse.parser.parse()
        tutorials_discourse.parser.parse_topic(
            tutorials_discourse.parser.index_topic)

        tutorials = tutorials_discourse.parser.tutorials
        topic_list = []

        for item in tutorials:
            if item["categories"] not in topic_list:
                topic_list.append(item["categories"])
            item["categories"] = {
                "slug":
                item["categories"],
                "name":
                " ".join([
                    word.capitalize() for word in item["categories"].split("-")
                ]),
            }

        topic_list.sort()
        topics = []

        for topic in topic_list:
            topics.append({
                "slug":
                topic,
                "name":
                " ".join([word.capitalize() for word in topic.split("-")]),
            })

        return flask.render_template(
            "tutorials/index.html",
            tutorials=tutorials,
            topics=topics,
        )

    tutorials_discourse.init_app(app)
示例#2
0
def init_tutorials(app, url_prefix):
    session = talisker.requests.get_session()
    tutorials_discourse = Tutorials(
        parser=TutorialParser(
            api=DiscourseAPI(
                base_url="https://discourse.charmhub.io/",
                session=session,
                api_key=DISCOURSE_API_KEY,
                api_username=DISCOURSE_API_USERNAME,
                get_topics_query_id=2,
            ),
            index_topic_id=2628,
            url_prefix=url_prefix,
        ),
        document_template="tutorials/tutorial.html",
        url_prefix=url_prefix,
        blueprint_name="tutorials",
    )

    @app.route(url_prefix)
    def index():
        page = flask.request.args.get("page", default=1, type=int)
        topics_request = flask.request.args.get("topic",
                                                default=None,
                                                type=str)
        posts_per_page = 12
        tutorials_discourse.parser.parse()
        tutorials_discourse.parser.parse_topic(
            tutorials_discourse.parser.index_topic)

        if not topics_request:
            tutorials = tutorials_discourse.parser.tutorials
        else:
            topics = topics_request.split(",")
            tutorials = [
                doc for doc in tutorials_discourse.parser.tutorials
                if doc["categories"] in topics
            ]

        total_pages = math.ceil(len(tutorials) / posts_per_page)

        return flask.render_template(
            "tutorials/index.html",
            forum_url=tutorials_discourse.parser.api.base_url,
            tutorials=tutorials,
            page=page,
            posts_per_page=posts_per_page,
            total_pages=total_pages,
            active_section="tutorials",
            topic=topics_request,
        )

    tutorials_discourse.init_app(app)
示例#3
0
def init_tutorials(app, url_prefix):
    session = talisker.requests.get_session()
    tutorials_discourse = Tutorials(
        parser=TutorialParser(
            api=DiscourseAPI(base_url="https://discourse.charmhub.io/",
                             session=session),
            index_topic_id=2628,
            category_id=34,
            url_prefix=url_prefix,
        ),
        document_template="tutorials/tutorial.html",
        url_prefix=url_prefix,
        blueprint_name="tutorials",
    )

    @app.route(url_prefix)
    def index():
        page = flask.request.args.get("page", default=1, type=int)
        topic = flask.request.args.get("topic", default=None, type=str)
        posts_per_page = 12
        tutorials_discourse.parser.parse()

        if not topic:
            metadata = tutorials_discourse.parser.metadata
        else:
            metadata = [
                doc for doc in tutorials_discourse.parser.metadata
                if topic in doc["categories"]
            ]

        total_pages = math.ceil(len(metadata) / posts_per_page)

        return flask.render_template(
            "tutorials/index.html",
            navigation=tutorials_discourse.parser.navigation,
            forum_url=tutorials_discourse.parser.api.base_url,
            metadata=metadata,
            page=page,
            posts_per_page=posts_per_page,
            total_pages=total_pages,
            active_section="tutorials",
            topic=topic,
        )

    tutorials_discourse.init_app(app)
示例#4
0
)

server_docs.init_app(app)

# Allow templates to be queried from discourse.ubuntu.com
app.add_url_rule(
    "/templates/<filename>",
    "templates",
    view_func=show_template,
)

tutorials_path = "/tutorials"
tutorials_docs = Tutorials(
    parser=TutorialParser(
        api=discourse_api,
        category_id=34,
        index_topic_id=13611,
        url_prefix=tutorials_path,
    ),
    document_template="/tutorials/tutorial.html",
    url_prefix=tutorials_path,
    blueprint_name="tutorials",
)
app.add_url_rule(tutorials_path,
                 view_func=build_tutorials_index(session, tutorials_docs))
tutorials_docs.init_app(app)

# Ceph docs
ceph_docs = Docs(
    parser=DocParser(api=discourse_api,
                     index_topic_id=17250,
                     url_prefix="/ceph/docs"),
示例#5
0
    def setUp(self):
        """
        Set up Flask app with Discourse extension for testing
        And set up mocking for discourse.example.com
        """

        # Suppress annoying warnings from HTTPretty
        # See: https://github.com/gabrielfalcao/HTTPretty/issues/368
        warnings.filterwarnings("ignore",
                                category=ResourceWarning,
                                message="unclosed.*")

        # Enable HTTPretty and set up mock URLs
        httpretty.enable()
        register_uris()

        template_folder = f"{this_dir}/fixtures/templates"

        app = flask.Flask("main", template_folder=template_folder)
        app_no_nav = flask.Flask("no-nav", template_folder=template_folder)
        app_no_mappings = flask.Flask("no-mappings",
                                      template_folder=template_folder)
        app_broken_mappings = flask.Flask("broken-mappings",
                                          template_folder=template_folder)
        app_no_category = flask.Flask("no-category",
                                      template_folder=template_folder)
        app_url_prefix = flask.Flask("url-prefix",
                                     template_folder=template_folder)

        app.testing = True
        app_no_nav.testing = True
        app_no_mappings.testing = True
        app_broken_mappings.testing = True
        app_no_category.testing = True
        app_url_prefix.testing = True

        discourse_api = DiscourseAPI(
            base_url="https://discourse.example.com/",
            session=requests.Session(),
        )

        Tutorials(
            parser=TutorialParser(
                api=discourse_api,
                category_id=2,
                index_topic_id=34,
                url_prefix="/",
            ),
            document_template="document.html",
            url_prefix="/",
        ).init_app(app)

        Tutorials(
            parser=TutorialParser(
                api=discourse_api,
                category_id=2,
                index_topic_id=42,
                url_prefix="/",
            ),
            document_template="document.html",
            url_prefix="/",
        ).init_app(app_no_nav)

        Tutorials(
            parser=TutorialParser(
                api=discourse_api,
                category_id=2,
                index_topic_id=35,
                url_prefix="/",
            ),
            document_template="document.html",
            url_prefix="/",
        ).init_app(app_no_mappings)

        Tutorials(
            parser=TutorialParser(
                api=discourse_api,
                category_id=2,
                index_topic_id=36,
                url_prefix="/",
            ),
            document_template="document.html",
            url_prefix="/",
        ).init_app(app_broken_mappings)

        Tutorials(
            parser=TutorialParser(api=discourse_api,
                                  index_topic_id=37,
                                  url_prefix="/"),
            document_template="document.html",
            url_prefix="/",
        ).init_app(app_no_category)

        Tutorials(
            parser=TutorialParser(api=discourse_api,
                                  index_topic_id=38,
                                  url_prefix="/docs"),
            document_template="document.html",
            url_prefix="/tutorials",
        ).init_app(app_url_prefix)

        self.client = app.test_client()
        self.client_no_nav = app_no_nav.test_client()
        self.client_no_mappings = app_no_mappings.test_client()
        self.client_broken_mappings = app_broken_mappings.test_client()
        self.client_no_category = app_no_category.test_client()
        self.client_url_prefix = app_url_prefix.test_client()