From a8328e5b4ba523900495b1a0e47c4fda972cc6b2 Mon Sep 17 00:00:00 2001 From: Zakki Date: Tue, 1 Aug 2023 13:46:15 +0700 Subject: [PATCH 1/4] Add migrations and change lambda default to function --- ...ter_sitepreferences_api_config_and_more.py | 49 ++++++++++++++++ django_project/core/models/preferences.py | 54 +++++++++++------ django_project/core/settings/base.py | 1 + ...lter_batchreview_processed_ids_and_more.py | 58 +++++++++++++++++++ .../dashboard/models/batch_review.py | 4 +- .../dashboard/models/entities_user_config.py | 2 +- .../dashboard/models/entity_upload.py | 2 +- .../dashboard/models/layer_config.py | 4 +- django_project/dashboard/models/layer_file.py | 4 +- .../dashboard/models/notification.py | 2 +- 10 files changed, 152 insertions(+), 28 deletions(-) create mode 100644 django_project/core/migrations/0021_alter_sitepreferences_api_config_and_more.py create mode 100644 django_project/dashboard/migrations/0060_alter_batchreview_processed_ids_and_more.py diff --git a/django_project/core/migrations/0021_alter_sitepreferences_api_config_and_more.py b/django_project/core/migrations/0021_alter_sitepreferences_api_config_and_more.py new file mode 100644 index 00000000..902effa7 --- /dev/null +++ b/django_project/core/migrations/0021_alter_sitepreferences_api_config_and_more.py @@ -0,0 +1,49 @@ +# Generated by Django 4.0.7 on 2023-08-01 06:39 + +import core.models.preferences +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0020_sitepreferences_base_url_help_page'), + ] + + operations = [ + migrations.AlterField( + model_name='sitepreferences', + name='api_config', + field=models.JSONField(blank=True, default=core.models.preferences.default_api_config), + ), + migrations.AlterField( + model_name='sitepreferences', + name='default_admin_emails', + field=models.JSONField(blank=True, default=list), + ), + migrations.AlterField( + model_name='sitepreferences', + name='default_geometry_checker_params', + field=models.JSONField(blank=True, default=core.models.preferences.default_geometry_checker_params), + ), + migrations.AlterField( + model_name='sitepreferences', + name='default_public_groups', + field=models.JSONField(blank=True, default=core.models.preferences.default_public_groups), + ), + migrations.AlterField( + model_name='sitepreferences', + name='level_names_template', + field=models.JSONField(blank=True, default=list), + ), + migrations.AlterField( + model_name='sitepreferences', + name='metadata_xml_config', + field=models.JSONField(blank=True, default=core.models.preferences.default_metadata_xml_config), + ), + migrations.AlterField( + model_name='sitepreferences', + name='tile_configs_template', + field=models.JSONField(blank=True, default=list), + ), + ] diff --git a/django_project/core/models/preferences.py b/django_project/core/models/preferences.py index e4fdeff3..e8bfb2cb 100644 --- a/django_project/core/models/preferences.py +++ b/django_project/core/models/preferences.py @@ -5,6 +5,34 @@ from core.models.singleton import SingletonModel +def default_api_config(): + return {'default_page_size': 50, 'max_page_size': 50} + + +def default_metadata_xml_config(): + return { + 'ContactName': 'GeoRepo', + 'ContactOrg': 'Unicef', + 'ContactPosition': 'Administrator', + 'License': ( + 'Not Specified: The original author ' + 'did not specify a license.' + ) + } + + +def default_public_groups(): + return ['UNICEF'] + + +def default_geometry_checker_params(): + return { + 'tolerance': 1e-4, + 'overlaps_threshold': 0.01, + 'gaps_threshold': 0.01, + } + + class SitePreferences(SingletonModel): """Preference settings specifically for website. @@ -123,7 +151,7 @@ class SitePreferences(SingletonModel): # JSON template for vector tiling config # ----------------------------------------------- tile_configs_template = models.JSONField( - default=[], + default=list, blank=True ) # ----------------------------------------------- @@ -141,14 +169,14 @@ class SitePreferences(SingletonModel): # JSON template for admin level names # ----------------------------------------------- level_names_template = models.JSONField( - default=[], + default=list, blank=True ) # ----------------------------------------------- # API pagination setting # ----------------------------------------------- api_config = models.JSONField( - default={'default_page_size': 50, 'max_page_size': 50}, + default=default_api_config, blank=True ) # ----------------------------------------------- @@ -166,15 +194,7 @@ class SitePreferences(SingletonModel): # METADATA XML Config # ----------------------------------------------- metadata_xml_config = models.JSONField( - default={ - 'ContactName': 'GeoRepo', - 'ContactOrg': 'Unicef', - 'ContactPosition': 'Administrator', - 'License': ( - 'Not Specified: The original author ' - 'did not specify a license.' - ) - }, + default=default_metadata_xml_config, blank=True ) # ----------------------------------------------- @@ -182,7 +202,7 @@ class SitePreferences(SingletonModel): # new user will be added to this groups # ----------------------------------------------- default_public_groups = models.JSONField( - default=['UNICEF'], + default=default_public_groups, blank=True ) # ----------------------------------------------- @@ -199,11 +219,7 @@ class SitePreferences(SingletonModel): # Default Geometry Checker Parameters # ----------------------------------------------- default_geometry_checker_params = models.JSONField( - default={ - 'tolerance': 1e-4, - 'overlaps_threshold': 0.01, - 'gaps_threshold': 0.01, - }, + default=default_geometry_checker_params, blank=True ) # ----------------------------------------------- @@ -211,7 +227,7 @@ class SitePreferences(SingletonModel): # send email notification from SignUp and Access Request # ----------------------------------------------- default_admin_emails = models.JSONField( - default=[], + default=list, blank=True ) # ----------------------------------------------- diff --git a/django_project/core/settings/base.py b/django_project/core/settings/base.py index 7bd685ca..5601baf8 100644 --- a/django_project/core/settings/base.py +++ b/django_project/core/settings/base.py @@ -107,6 +107,7 @@ 'django.template.context_processors.tz', 'django.contrib.messages.context_processors.messages', 'core.context_processors.global_context.global_context', + 'django.template.context_processors.request' ], }, }, diff --git a/django_project/dashboard/migrations/0060_alter_batchreview_processed_ids_and_more.py b/django_project/dashboard/migrations/0060_alter_batchreview_processed_ids_and_more.py new file mode 100644 index 00000000..13170f3f --- /dev/null +++ b/django_project/dashboard/migrations/0060_alter_batchreview_processed_ids_and_more.py @@ -0,0 +1,58 @@ +# Generated by Django 4.0.7 on 2023-08-01 06:39 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('dashboard', '0059_alter_notification_type'), + ] + + operations = [ + migrations.AlterField( + model_name='batchreview', + name='processed_ids', + field=models.JSONField(blank=True, default=list, help_text='List of entity uploads that has been processed', null=True), + ), + migrations.AlterField( + model_name='batchreview', + name='upload_ids', + field=models.JSONField(blank=True, default=list, help_text='List of entity uploads', null=True), + ), + migrations.AlterField( + model_name='entitiesuserconfig', + name='filters', + field=models.JSONField(blank=True, default=dict), + ), + migrations.AlterField( + model_name='entityuploadstatus', + name='admin_level_names', + field=models.JSONField(blank=True, default=dict, help_text='Name of admin levels', null=True), + ), + migrations.AlterField( + model_name='layerconfig', + name='id_fields', + field=models.JSONField(blank=True, default=list), + ), + migrations.AlterField( + model_name='layerconfig', + name='name_fields', + field=models.JSONField(blank=True, default=list), + ), + migrations.AlterField( + model_name='layerfile', + name='id_fields', + field=models.JSONField(blank=True, default=list), + ), + migrations.AlterField( + model_name='layerfile', + name='name_fields', + field=models.JSONField(blank=True, default=list), + ), + migrations.AlterField( + model_name='notification', + name='payload', + field=models.JSONField(blank=True, default=dict), + ), + ] diff --git a/django_project/dashboard/models/batch_review.py b/django_project/dashboard/models/batch_review.py index 959a773d..ebb63fbb 100644 --- a/django_project/dashboard/models/batch_review.py +++ b/django_project/dashboard/models/batch_review.py @@ -50,14 +50,14 @@ class BatchReview(models.Model): upload_ids = models.JSONField( help_text='List of entity uploads', - default=[], + default=list, null=True, blank=True ) processed_ids = models.JSONField( help_text='List of entity uploads that has been processed', - default=[], + default=list, null=True, blank=True ) diff --git a/django_project/dashboard/models/entities_user_config.py b/django_project/dashboard/models/entities_user_config.py index 2d82d746..e8a694d9 100644 --- a/django_project/dashboard/models/entities_user_config.py +++ b/django_project/dashboard/models/entities_user_config.py @@ -35,7 +35,7 @@ class EntitiesUserConfig(models.Model): ) filters = models.JSONField( - default={}, + default=dict, blank=True ) diff --git a/django_project/dashboard/models/entity_upload.py b/django_project/dashboard/models/entity_upload.py index 750cb19d..99cbd367 100644 --- a/django_project/dashboard/models/entity_upload.py +++ b/django_project/dashboard/models/entity_upload.py @@ -117,7 +117,7 @@ class EntityUploadStatus(models.Model): admin_level_names = models.JSONField( help_text='Name of admin levels', - default={}, + default=dict, null=True, blank=True ) diff --git a/django_project/dashboard/models/layer_config.py b/django_project/dashboard/models/layer_config.py index 0a6a67b6..43e782b3 100644 --- a/django_project/dashboard/models/layer_config.py +++ b/django_project/dashboard/models/layer_config.py @@ -54,12 +54,12 @@ class LayerConfig(models.Model): ) id_fields = models.JSONField( - default=[], + default=list, blank=True ) name_fields = models.JSONField( - default=[], + default=list, blank=True ) diff --git a/django_project/dashboard/models/layer_file.py b/django_project/dashboard/models/layer_file.py index b77bfecb..870060df 100644 --- a/django_project/dashboard/models/layer_file.py +++ b/django_project/dashboard/models/layer_file.py @@ -84,12 +84,12 @@ class LayerFile(models.Model): ) id_fields = models.JSONField( - default=[], + default=list, blank=True ) name_fields = models.JSONField( - default=[], + default=list, blank=True ) diff --git a/django_project/dashboard/models/notification.py b/django_project/dashboard/models/notification.py index 2014de9b..26f7b81d 100644 --- a/django_project/dashboard/models/notification.py +++ b/django_project/dashboard/models/notification.py @@ -40,7 +40,7 @@ class Notification(models.Model): ) payload = models.JSONField( - default={}, + default=dict, blank=True ) From 94727e7988c5d4c9c4a6af4568eae902cad54c75 Mon Sep 17 00:00:00 2001 From: Zakki Date: Tue, 1 Aug 2023 14:02:46 +0700 Subject: [PATCH 2/4] Fix flake8 test --- django_project/dashboard/api_views/dataset.py | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/django_project/dashboard/api_views/dataset.py b/django_project/dashboard/api_views/dataset.py index b1abfa40..8ced9f0b 100644 --- a/django_project/dashboard/api_views/dataset.py +++ b/django_project/dashboard/api_views/dataset.py @@ -115,29 +115,21 @@ def get_sort_attribute(self, sort_by, sort_direction): return None, None if sort_direction not in ['asc', 'desc']: return None, None - match sort_by: - case 'id': - return 'gg.id', sort_direction - case 'country': - return 'parent_0.label', sort_direction - case 'level': - return 'gg.level', sort_direction - case 'type': - return 'ge.label', sort_direction - case 'name': - return 'gg.label', sort_direction - case 'default_code': - return 'gg.internal_code', sort_direction - case 'code': - return 'gg.unique_code', sort_direction - case 'cucode': - return 'gg.concept_ucode', sort_direction - case 'updated': - return 'gg.start_date', sort_direction - case 'rev': - return 'gg.revision_number', sort_direction - case 'status': - return 'gg.is_approved', sort_direction + + field_mapping = { + 'id': 'gg.id', + 'country': 'parent_0.label', + 'level': 'gg.level', + 'type': 'gg.type', + 'name': 'gg.label', + 'default_code': 'gg.internal_code', + 'code': 'gg.unique_code', + 'cucode': 'gg.concept_code', + 'updated': 'gg.start_date', + 'rev': 'gg.revision_number', + 'status': 'gg.is_approved' + } + return field_mapping[sort_by], sort_direction def do_run_query( self, From c123b2cf93df096a2fb5e7880ea112c9dd86f7a9 Mon Sep 17 00:00:00 2001 From: Zakki Date: Wed, 2 Aug 2023 10:36:48 +0700 Subject: [PATCH 3/4] Add some comments --- django_project/core/models/preferences.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/django_project/core/models/preferences.py b/django_project/core/models/preferences.py index e8bfb2cb..4e045f89 100644 --- a/django_project/core/models/preferences.py +++ b/django_project/core/models/preferences.py @@ -6,10 +6,16 @@ def default_api_config(): + """ + Default value for Preference's api_config. + """ return {'default_page_size': 50, 'max_page_size': 50} def default_metadata_xml_config(): + """ + Default value for Preference's metadata_xml_config. + """ return { 'ContactName': 'GeoRepo', 'ContactOrg': 'Unicef', @@ -22,10 +28,16 @@ def default_metadata_xml_config(): def default_public_groups(): + """ + Default value for Preference's public_group. + """ return ['UNICEF'] def default_geometry_checker_params(): + """ + Default value for Preference's geometry_checker_params. + """ return { 'tolerance': 1e-4, 'overlaps_threshold': 0.01, From 9372d5d4b00b58d3615bf1bac21db1d426a253c7 Mon Sep 17 00:00:00 2001 From: Zakki Date: Wed, 2 Aug 2023 10:41:49 +0700 Subject: [PATCH 4/4] Use version 6.0.0 in flake8 --- deployment/docker/requirements-dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deployment/docker/requirements-dev.txt b/deployment/docker/requirements-dev.txt index 0dba7b10..0817083f 100644 --- a/deployment/docker/requirements-dev.txt +++ b/deployment/docker/requirements-dev.txt @@ -2,7 +2,7 @@ django-nose coverage pep8 pylint -flake8 +flake8==6.0.0 factory_boy django-devserver