pallets-eco / flask-admin

Simple and extensible administrative interface framework for Flask
BSD 3-Clause "New" or "Revised" License
5.69k stars 1.56k forks source link

mongodb form_subdocument overwrite stopped working after updates to current flask/ flask-admin/ flask-mongoenine #2402

Open Bastian-Kuhn opened 6 months ago

Bastian-Kuhn commented 6 months ago

I had to Update the Flask etc. Modules in my Open-Source Project. One Problem was that I had to replace flask-mongoengine with: https://github.com/idoshr/flask-mongoengine

So when switching from old to new and reverse, I can reproduce it to work and stop working. Sadly, I was unable to find out, which project's fault is that nor to fix it. Even checked the last commit's every and found nothing which could have done it.

Working Versions: Flask 2.1.2 Flask-Admin: 1.6.0 flask-mongoengine 1.0.0 wtforms 3.0.1

Not working: Flask 3.0.0 Flask-Admin 1.6.1 flask-mongoengine forkgit@master wtforms 3.1.1

The Problem is, that the form_widget_args, form_overwrite etc. for the subdocuments are not applied, I can even add random field names etc. without error, only on the first level I get error messages when the field is wrong.

That's one example from my Code:

form_subdocuments = {
        'conditions': {
            'form_subdocuments' : {
                None: {
                    'form_widget_args': {
                        'hostname_match': {'style': 'background-color: #2EFE9A;' },
                        'hostname': { 'style': 'background-color: #2EFE9A;' },
                        'tag_match': { 'style': 'background-color: #81DAF5;' },
                        'tag': { 'style': 'background-color: #81DAF5;' },
                        'value_match': { 'style': 'background-color: #81DAF5;' },
                        'value': { 'style': 'background-color: #81DAF5;'},
                    },
                    'form_overrides' : {
                        'hostname': StringField,
                        'tag': StringField,
                        'value': StringField,
                    },
                    'form_rules' : [
                        rules.FieldSet(('match_type',), "Condition Match Type"),
                        rules.HTML(divider % "Match on Host"),
                        rules.FieldSet(
                            ('hostname_match', 'hostname', 'hostname_match_negate'), "Host Match"),
                        rules.HTML(divider % "Match on Attribute"),
                        rules.FieldSet(
                            (
                                'tag_match', 'tag', 'tag_match_negate',
                                'value_match', 'value', 'value_match_negate',
                            ), "Attribute Match"),
                    ]
                }
            }
        },
        'outcomes' : {
            'form_subdocuments' : {
                None: {
                    'form_widget_args': {
                        'rule_template' : {"rows": 10},
                    },
                }
            }
        }
    }

And that's the Model for it:

#   .-- Checkmk BI Aggregations

class BiAggregationOutcome(db.EmbeddedDocument):
    """
    BI Aggregation
    """
    description = db.StringField()
    rule_template = db.StringField()

class CheckmkBiAggregation(db.Document):
    """
    BI Aggregation
    """
    name = db.StringField(required=True, unique=True)

    condition_typ = db.StringField(choices=rule_types)
    conditions = db.ListField(field=db.EmbeddedDocumentField(document_type="FullCondition"))
    render_full_conditions = db.StringField() # Helper for Preview

    outcomes = db.ListField(field=db.EmbeddedDocumentField(document_type="BiAggregationOutcome"))
    render_cmk_bi_rule = db.StringField()
    last_match = db.BooleanField(default=False)
    enabled = db.BooleanField()
    meta = {
        'strict': False
    }
#.

Because of the flask-mongoengine version switch, I had to change ListFields to have the field= param, and the EmbeddedDocuments to have the document_type. Maybe this is related.

Thanks for any help.

Bastian-Kuhn commented 6 months ago

For others with the same Problem, found a solution/ workaround. You must replace None with '' to make it work again.