Skip to content

Commit

Permalink
[#3558] Add migration to update existing instances (WIP), lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Nov 22, 2023
1 parent 96ff75d commit c894a1e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Generated by Django 3.2.21 on 2023-11-22 17:27

from django.db import migrations
from django.db.migrations.state import StateApps
from django.db.backends.base.schema import BaseDatabaseSchemaEditor

from openforms.analytics_tools.constants import AnalyticsTools

SITEIMPROVE_VALUES = [
"https://siteimproveanalytics.com",
"https://siteimproveanalytics.com",
"https://*.siteimproveanalytics.io",
]
GA_VALUES = ["https://www.googleanalytics.com", "https://www.googletagmanager.com"]

FIELD_TO_IDENTIFIER = {
"matomo_url": AnalyticsTools.matomo,
"piwik_pro_url": AnalyticsTools.piwik_pro,
"piwik_url": AnalyticsTools.piwik,
}


def set_identifier(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
"""Set the corresponding analytic tool as the ``CSPSetting.identifier`` field.
Depending on the analytic tool used, the ``CSPSetting.value`` field can be fixed
(e.g. with GA or Siteimprove) or configured by the user. The latter requires more work,
as we need to iterate over the ``AnalyticsToolsConfiguration`` fields to find the right match.
"""

AnalyticsToolsConfiguration = apps.get_model(
"analytics_tools", "AnalyticsToolsConfiguration"
)
analytics_conf = AnalyticsToolsConfiguration.get_solo()
CSPSetting = apps.get_model("config", "CSPSetting")

for csp_setting in CSPSetting.objects.filter(identifier="").iterator():
if csp_setting.value in SITEIMPROVE_VALUES:
csp_setting.identifier = AnalyticsTools.siteimprove
elif csp_setting.value in GA_VALUES:
csp_setting.identifier = AnalyticsTools.google_analytics
else:
for field, identifier in FIELD_TO_IDENTIFIER.items():
if getattr(analytics_conf, field) == csp_setting.value:
csp_setting.identifier = identifier

csp_setting.save()


class Migration(migrations.Migration):

dependencies = [
("analytics_tools", "0002_auto_20230119_1500"),
]

operations = [
migrations.RunPython(set_identifier, migrations.RunPython.noop),
]
59 changes: 49 additions & 10 deletions src/openforms/config/migrations/0063_auto_20231122_1816.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,62 @@
class Migration(migrations.Migration):

dependencies = [
('config', '0062_cspsetting_identifier'),
("config", "0062_cspsetting_identifier"),
]

operations = [
migrations.AlterField(
model_name='cspsetting',
name='directive',
field=models.CharField(choices=[('default-src', 'default-src'), ('script-src', 'script-src'), ('script-src-attr', 'script-src-attr'), ('script-src-elem', 'script-src-elem'), ('img-src', 'img-src'), ('object-src', 'object-src'), ('prefetch-src', 'prefetch-src'), ('media-src', 'media-src'), ('frame-src', 'frame-src'), ('font-src', 'font-src'), ('connect-src', 'connect-src'), ('style-src', 'style-src'), ('style-src-attr', 'style-src-attr'), ('style-src-elem', 'style-src-elem'), ('base-uri', 'base-uri'), ('child-src', 'child-src'), ('frame-ancestors', 'frame-ancestors'), ('navigate-to', 'navigate-to'), ('form-action', 'form-action'), ('sandbox', 'sandbox'), ('report-uri', 'report-uri'), ('report-to', 'report-to'), ('manifest-src', 'manifest-src'), ('worker-src', 'worker-src'), ('plugin-types', 'plugin-types'), ('require-sri-for', 'require-sri-for')], help_text='CSP header directive.', max_length=64, verbose_name='directive'),
model_name="cspsetting",
name="directive",
field=models.CharField(
choices=[
("default-src", "default-src"),
("script-src", "script-src"),
("script-src-attr", "script-src-attr"),
("script-src-elem", "script-src-elem"),
("img-src", "img-src"),
("object-src", "object-src"),
("prefetch-src", "prefetch-src"),
("media-src", "media-src"),
("frame-src", "frame-src"),
("font-src", "font-src"),
("connect-src", "connect-src"),
("style-src", "style-src"),
("style-src-attr", "style-src-attr"),
("style-src-elem", "style-src-elem"),
("base-uri", "base-uri"),
("child-src", "child-src"),
("frame-ancestors", "frame-ancestors"),
("navigate-to", "navigate-to"),
("form-action", "form-action"),
("sandbox", "sandbox"),
("report-uri", "report-uri"),
("report-to", "report-to"),
("manifest-src", "manifest-src"),
("worker-src", "worker-src"),
("plugin-types", "plugin-types"),
("require-sri-for", "require-sri-for"),
],
help_text="CSP header directive.",
max_length=64,
verbose_name="directive",
),
),
migrations.AlterField(
model_name='cspsetting',
name='identifier',
field=models.CharField(blank=True, help_text='An extra tag for this CSP entry, to identify the exact source.', max_length=64, verbose_name='identifier'),
model_name="cspsetting",
name="identifier",
field=models.CharField(
blank=True,
help_text="An extra tag for this CSP entry, to identify the exact source.",
max_length=64,
verbose_name="identifier",
),
),
migrations.AlterField(
model_name='cspsetting',
name='value',
field=models.CharField(help_text='CSP header value.', max_length=255, verbose_name='value'),
model_name="cspsetting",
name="value",
field=models.CharField(
help_text="CSP header value.", max_length=255, verbose_name="value"
),
),
]

0 comments on commit c894a1e

Please sign in to comment.