Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/update viz setting and widgets #1478

Merged
merged 6 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions apps/analysis_framework/migrations/0041_widget_mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Generated by Django 3.2.17 on 2024-02-22 05:02
from django.db import migrations
import copy


def analysis_framework_widgets_mapping(apps, schema_editor):
AnalysisFramework = apps.get_model('analysis_framework', 'AnalysisFramework')
af_qs = AnalysisFramework.objects.filter(properties__isnull=False)
for af in af_qs:
if af.properties == {}:
continue
new_widget_config = af.properties
# Remove super legacy config
new_widget_config.pop('old_stats_config', None)

# For reassurance
new_widget_config = copy.deepcopy(new_widget_config)

# Migrate legacy config to latest
# -- Widget1D
if 'widget_1d' not in new_widget_config['stats_config']:
new_widget_config['widget_1d'] = [
{'pk': new_widget_config['stats_config'][key]['pk']}
for key in [
'widget1d'
]
if key in new_widget_config
]

# -- Widget2D
if 'widget_2d' not in new_widget_config['stats_config']:
new_widget_config['widget_2d'] = [
{'pk': new_widget_config['stats_config'][key]['pk']}
for key in [
'widget2d'
]
if key in new_widget_config
]

# -- Multiselect
if 'multiselect_widgets' not in new_widget_config['stats_config']:
new_widget_config['multiselect_widgets'] = [
{'pk': new_widget_config['stats_config'][key]['pk']}
for key in [
'specific_needs_groups_widget',
'demographic_groups_widget'
]
if key in new_widget_config
]

# -- Organigram
if 'organigram_widget' not in new_widget_config['stats_config']:
new_widget_config['organigram_widget'] = [
{'pk': new_widget_config['stats_config'][key]['pk']}
for key in [
'affected_groups_widget'
]
if key in new_widget_config
]

legacy_widget_keys = [
'affected_groups_widget',
'demographic_groups_widget',
'specific_needs_groups_widget',
]
for widget_key in legacy_widget_keys:
new_widget_config['stats_config'].pop(widget_key, None)

af.properties = new_widget_config
af.save(update_fields=('properties',))


class Migration(migrations.Migration):

dependencies = [
('analysis_framework', '0040_auto_20231109_1208'),
]
operations = [
migrations.RunPython(
analysis_framework_widgets_mapping,
reverse_code=migrations.RunPython.noop
)
]
3 changes: 2 additions & 1 deletion apps/analysis_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ class AnalysisFrameworkPropertiesStatsConfigSerializer(serializers.Serializer):
@staticmethod
def _validate_widget_with_widget_type(data, widget_type, many=False):
if not data:
return
if many:
return []
if many:
ids = [item['pk'] for item in data]
widgets = list(Widget.objects.filter(pk__in=ids))
Expand Down
84 changes: 75 additions & 9 deletions apps/analysis_framework/tests/snapshots/snap_test_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,17 @@
'title': 'Scale',
'version': 1,
'widgetId': 'SCALE'
},
{
'clientId': 'organigram-widget-104-client-id',
'id': '9',
'key': 'organigram-widget-104-key',
'order': 5,
'properties': {
},
'title': 'Organigram',
'version': 1,
'widgetId': 'ORGANIGRAM'
}
],
'title': 'AF (TEST)'
Expand Down Expand Up @@ -780,6 +791,13 @@
'messages': "Different widget type was provided. Required: matrix2dWidget Provided: ['multiselectWidget']",
'objectErrors': None
},
{
'arrayErrors': None,
'clientId': None,
'field': 'multiselectWidgets',
'messages': "Different widget type was provided. Required: multiselectWidget Provided: ['organigramWidget']",
'objectErrors': None
},
{
'arrayErrors': None,
'clientId': None,
Expand Down Expand Up @@ -843,7 +861,7 @@
{
'clientId': 'section-2-text-101-client-id',
'conditional': None,
'id': '9',
'id': '10',
'key': 'section-2-text-101',
'order': 1,
'properties': {
Expand Down Expand Up @@ -1018,7 +1036,11 @@
'pk': '6'
}
],
'organigramWidgets': None,
'organigramWidgets': [
{
'pk': '9'
}
],
'reliabilityWidget': {
'pk': '8'
},
Expand All @@ -1041,7 +1063,7 @@
{
'clientId': 'multi-select-widget-102-client-id',
'conditional': None,
'id': '10',
'id': '11',
'key': 'multi-select-widget-102-key',
'order': 2,
'properties': {
Expand Down Expand Up @@ -1073,6 +1095,18 @@
'title': 'Scale',
'version': 1,
'widgetId': 'SCALE'
},
{
'clientId': 'organigram-widget-104-client-id',
'conditional': None,
'id': '9',
'key': 'organigram-widget-104-key',
'order': 5,
'properties': {
},
'title': 'Organigram',
'version': 1,
'widgetId': 'ORGANIGRAM'
}
],
'title': 'Updated AF (TEST)'
Expand Down Expand Up @@ -1143,7 +1177,7 @@
{
'clientId': 'section-2-text-101-client-id',
'conditional': None,
'id': '12',
'id': '13',
'key': 'section-2-text-101',
'order': 1,
'properties': {
Expand Down Expand Up @@ -1320,7 +1354,11 @@
},
'multiselectWidgets': [
],
'organigramWidgets': None,
'organigramWidgets': [
{
'pk': '9'
}
],
'reliabilityWidget': {
'pk': '8'
},
Expand Down Expand Up @@ -1348,7 +1386,7 @@
'parentWidget': '1',
'parentWidgetType': 'MATRIX1D'
},
'id': '13',
'id': '14',
'key': 'multi-select-widget-102-key',
'order': 2,
'properties': {
Expand Down Expand Up @@ -1380,6 +1418,18 @@
'title': 'Scale',
'version': 1,
'widgetId': 'SCALE'
},
{
'clientId': 'organigram-widget-104-client-id',
'conditional': None,
'id': '9',
'key': 'organigram-widget-104-key',
'order': 5,
'properties': {
},
'title': 'Organigram',
'version': 1,
'widgetId': 'ORGANIGRAM'
}
],
'title': 'Updated AF (TEST)'
Expand Down Expand Up @@ -1412,7 +1462,7 @@
{
'clientId': 'section-2-text-101-client-id',
'conditional': None,
'id': '14',
'id': '15',
'key': 'section-2-text-101',
'order': 1,
'properties': {
Expand Down Expand Up @@ -1589,7 +1639,11 @@
},
'multiselectWidgets': [
],
'organigramWidgets': None,
'organigramWidgets': [
{
'pk': '9'
}
],
'reliabilityWidget': {
'pk': '8'
},
Expand All @@ -1612,7 +1666,7 @@
{
'clientId': 'multi-select-widget-102-client-id',
'conditional': None,
'id': '15',
'id': '16',
'key': 'multi-select-widget-102-key',
'order': 2,
'properties': {
Expand Down Expand Up @@ -1644,6 +1698,18 @@
'title': 'Scale',
'version': 1,
'widgetId': 'SCALE'
},
{
'clientId': 'organigram-widget-104-client-id',
'conditional': None,
'id': '9',
'key': 'organigram-widget-104-key',
'order': 5,
'properties': {
},
'title': 'Organigram',
'version': 1,
'widgetId': 'ORGANIGRAM'
}
],
'title': 'Updated AF (TEST)'
Expand Down
12 changes: 11 additions & 1 deletion apps/analysis_framework/tests/test_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def test_analysis_framework_update(self):
widget2d {
pk
}
multiselectWidgets {
multiselectWidgets {
pk
}
organigramWidgets {
Expand Down Expand Up @@ -492,6 +492,7 @@ def _query_check(id, minput, **kwargs):
variables={'id': id},
**kwargs,
)

# ---------- Without login
valid_minput = copy.deepcopy(self.valid_minput)
new_widgets = [
Expand All @@ -513,6 +514,15 @@ def _query_check(id, minput, **kwargs):
order=4,
properties=dict(),
),
dict(
clientId='organigram-widget-104-client-id',
title='Organigram',
widgetId=self.genum(Widget.WidgetType.ORGANIGRAM),
version=1,
key='organigram-widget-104-key',
order=5,
properties=dict(),
),
]
valid_minput['secondaryTagging'].extend(new_widgets)
_query_check(0, valid_minput, assert_for_error=True)
Expand Down
38 changes: 7 additions & 31 deletions apps/entry/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def _return(properties):
return _return(w_filter.properties if w_filter else None)

properties = widget.properties
if config.get('is_conditional_widget'):
if config.get('is_conditional_widget'): # TODO: Remove this
# TODO: Skipping conditional widget, in new this is not needed
return default
return _return(properties)
Expand Down Expand Up @@ -180,9 +180,9 @@ def get_project_entries_stats(project, skip_geo_data=False):
'reliability_widget': {
'pk': 2683,
},
'organigram_widgets': {
'pk': 2682,
},
'organigram_widgets': [
{'pk': 2682},
]
'multiselect_widgets': [
{'pk': 2681},
{'pk': 8703},
Expand All @@ -193,21 +193,6 @@ def get_project_entries_stats(project, skip_geo_data=False):
af = project.analysis_framework
config = af.properties.get('stats_config')

# TODO: REMOVE THIS
if 'multiselect_widgets' not in config:
config['multiselect_widgets'] = [
{'pk': config[key]['pk']}
for key in [
'specific_needs_groups_widget',
'demographic_groups_widget',
]
if key in config
]
if 'organigram_widgets' not in config and 'affected_groups_widget' in config:
config['organigram_widgets'] = [
config['affected_groups_widget']
]

widgets_pk = [
info['pk']
for _info in config.values()
Expand All @@ -225,24 +210,15 @@ def get_project_entries_stats(project, skip_geo_data=False):
for widget in Widget.objects.filter(pk__in=widgets_pk, analysis_framework=af)
}

# TODO: Remove this later after all data are updated.
config['widget1d'] = config.get('widget1d') or config['widget_1d']
config['widget2d'] = config.get('widget2d') or config['widget_2d']

# Make sure this are array
for key in ['widget1d', 'widget2d']:
if not isinstance(config[key], list):
config[key] = [config[key]]

w_reliability_default = w_severity_default = w_multiselect_widget_default = w_organigram_widget_default = {
'pk': None,
'properties': {
'options': [],
},
}

w1ds = [_get_widget_info(_config, widgets) for _config in config['widget1d']]
w2ds = [_get_widget_info(_config, widgets) for _config in config['widget2d']]
w1ds = [_get_widget_info(_config, widgets) for _config in config['widget_1d'] or []]
w2ds = [_get_widget_info(_config, widgets) for _config in config['widget_2d'] or []]

w_multiselect_widgets = [
_get_widget_info(
Expand All @@ -269,7 +245,7 @@ def get_project_entries_stats(project, skip_geo_data=False):

matrix_widgets = [
{'id': w['pk'], 'type': w_type, 'title': w['_widget'].title}
for widgets, w_type in [[w1ds, 'widget1d'], [w2ds, 'widget2d']]
for widgets, w_type in [[w1ds, 'widget_1d'], [w2ds, 'widget_2d']]
for w in widgets
]

Expand Down
Loading
Loading