-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve notification framework (#1143)
Co-authored-by: Felix Rindt <[email protected]>
- Loading branch information
1 parent
bf3daf2
commit b58214e
Showing
20 changed files
with
603 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
ephios/core/migrations/0025_remove_notification_failed_notification_processed_by_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Generated by Django 4.2.6 on 2023-11-28 22:10 | ||
|
||
from django.db import migrations, models | ||
|
||
import ephios.extra.json | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("core", "0024_identityprovider_create_missing_groups_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="notification", | ||
name="failed", | ||
), | ||
migrations.AddField( | ||
model_name="notification", | ||
name="processed_by", | ||
field=models.JSONField( | ||
blank=True, | ||
decoder=ephios.extra.json.CustomJSONDecoder, | ||
default=list, | ||
encoder=ephios.extra.json.CustomJSONEncoder, | ||
help_text="List of slugs of notification backends that have processed this notification", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="notification", | ||
name="processing_completed", | ||
field=models.BooleanField( | ||
default=False, | ||
verbose_name="processing completed", | ||
help_text="All enabled notification backends have processed this notification when flag is set", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="notification", | ||
name="read", | ||
field=models.BooleanField(default=False, verbose_name="read"), | ||
), | ||
] |
81 changes: 81 additions & 0 deletions
81
ephios/core/migrations/0026_userprofile_disabled_notifications_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Generated by Django 4.2.7 on 2024-01-06 13:21 | ||
from json import JSONDecodeError | ||
|
||
from django.db import migrations, models | ||
|
||
import ephios.extra.json | ||
from ephios.core.services.notifications.backends import enabled_notification_backends | ||
from ephios.core.services.notifications.types import notification_type_from_slug | ||
from ephios.extra.preferences import JSONSerializer | ||
|
||
|
||
def migrate_disabled_notifications(apps, schema_editor): | ||
UserProfile = apps.get_model("core", "UserProfile") | ||
UserPreferenceModel = apps.get_model("dynamic_preferences_users", "UserPreferenceModel") | ||
db_alias = schema_editor.connection.alias | ||
backends = [backend.slug for backend in enabled_notification_backends()] | ||
for profile in UserProfile.all_objects.using(db_alias).all(): | ||
try: | ||
preferences = JSONSerializer.deserialize( | ||
UserPreferenceModel.objects.using(db_alias) | ||
.get(section="notifications", name="notifications", instance__pk=profile.pk) | ||
.raw_value | ||
) | ||
except (UserPreferenceModel.DoesNotExist, ValueError, JSONDecodeError): | ||
continue | ||
for notification_type, active_backends in preferences.items(): | ||
for backend in set(backends) - set(active_backends): | ||
profile.disabled_notifications.append([backend, notification_type]) | ||
profile.save() | ||
|
||
|
||
def revert_disabled_notifications(apps, schema_editor): | ||
UserProfile = apps.get_model("core", "UserProfile") | ||
UserPreferenceModel = apps.get_model("dynamic_preferences_users", "UserPreferenceModel") | ||
db_alias = schema_editor.connection.alias | ||
backends = [backend.slug for backend in enabled_notification_backends()] | ||
for profile in UserProfile.all_objects.using(db_alias).all(): | ||
try: | ||
preferences_instance = UserPreferenceModel.objects.using(db_alias).get( | ||
section="notifications", name="notifications", instance__pk=profile.pk | ||
) | ||
preferences = JSONSerializer.deserialize(preferences_instance.raw_value) | ||
for disabled_tuple in profile.disabled_notifications: | ||
backend_slug, type_slug = disabled_tuple | ||
try: | ||
notification_type_from_slug(type_slug) | ||
except ValueError: | ||
continue | ||
enabled = preferences.get(type_slug, backends) | ||
if backend_slug in enabled: | ||
enabled.remove(backend_slug) | ||
preferences[type_slug] = enabled | ||
preferences_instance.raw_value = JSONSerializer.serialize(preferences) | ||
preferences_instance.save() | ||
except (UserPreferenceModel.DoesNotExist, JSONDecodeError): | ||
continue | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("core", "0025_remove_notification_failed_notification_processed_by_and_more"), | ||
("dynamic_preferences_users", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="userprofile", | ||
name="disabled_notifications", | ||
field=models.JSONField( | ||
decoder=ephios.extra.json.CustomJSONDecoder, | ||
default=list, | ||
encoder=ephios.extra.json.CustomJSONEncoder, | ||
), | ||
), | ||
migrations.RunPython(migrate_disabled_notifications, revert_disabled_notifications), | ||
migrations.AddField( | ||
model_name="userprofile", | ||
name="email_invalid", | ||
field=models.BooleanField(default=False, verbose_name="Email address invalid"), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.