2

I am trying to create a simple admin for editing a mongo collection. I have posted the code below. It all works perfectly locally or as a docker container. However when I deploy this in our micro-service architecture the app lives at: SERVER_NAME/TEAM_NAME/APP_NAME/.

Flask routes set with @app.route work correctly. However the urls in the admin templates are not correct and always start directly at SERVER_NAME ignoring team-name and app-name. The actual pages and resources are located at the correct urls but the urls for the static resources are not found. How do I make sure the urls generated within flask-admin also take into account the relative url?

The code:

import os
import flask_admin
from wtforms import form, fields
from flask_admin.contrib.pymongo import ModelView, filters


# User admin
class WordPairsForm(form.Form):
    text = fields.StringField("Text")
    language = fields.SelectField("Language", choices=[("de", "german"), ("en", "english"), ("pl", "polish")])
    label = fields.SelectField("Label", choices=[("badword", "bad word"), ("no_stay", "no overnight stay")])
    active = fields.BooleanField("Active", default="checked")


class WordPairsView(ModelView):
    column_list = ("text", "language", "label", "active")
    column_sortable_list = ("text", "language", "label", "active")
    column_searchable_list = ("text",)
    column_filters = (
        filters.FilterLike("text", "Text"),
        filters.FilterNotLike("text", "Text"),
        filters.FilterEqual("language", "Language", options=[("de", "german"), ("en", "english"), ("pl", "polish")]),
        filters.FilterEqual("label", "Label", options=[("badword", "bad word"), ("no_stay", "no overnight stay")]),
        filters.BooleanEqualFilter("active", "Active")
    )
    form = WordPairsForm

    def create_form(self):
        _form = super(WordPairsView, self).create_form()
        return _form

    def edit_form(self, obj):
        _form = super(WordPairsView, self).edit_form(obj)
        return _form

    def get_list(self, *args, **kwargs):
        count, data = super(WordPairsView, self).get_list(*args, **kwargs)

        return count, data

    def get_url


def add_admin(app):
    admin = flask_admin.Admin(
        app,
        name="CQAS Admin",
        url=os.getenv(
            "F_ADMIN_URL",
            "/admin"
        ),
        static_url_path=os.getenv("F_ADMIN_STATIC_URL", None),
        subdomain=os.getenv("F_ADMIN_SUBDOMAIN", None),
        endpoint=os.getenv("F_ADMIN_ENDPOINT", None)
    )
    admin.add_view(WordPairsView(app.data.data, "WordPairs"))
galinden
  • 610
  • 8
  • 13
  • I'm not certain these are related as I haven't directly tried to do something like this, but the keyword params you're specifying did remind me of an answer I read a few months ago while we were converting to the factory model: https://stackoverflow.com/a/39618659/307542 If this doesn't help, you may get a little more traction if you can boil this down to a MCVE. – abathur Apr 02 '18 at 15:32

0 Answers0