Skip to content

Commit

Permalink
store notifaction settings in preferences page.
Browse files Browse the repository at this point in the history
  • Loading branch information
thet committed Oct 19, 2023
1 parent c369c30 commit e92449f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
31 changes: 28 additions & 3 deletions src/euphorie/client/browser/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from euphorie.client.model import Account
from euphorie.client.model import AccountChangeRequest
from euphorie.client.model import get_current_account
from euphorie.client.model import NotificationSubscription
from euphorie.client.utils import CreateEmailTo
from euphorie.client.utils import randomString
from plone import api
Expand Down Expand Up @@ -153,9 +154,26 @@ def notification_categories(self):
yield category[1]

@property
def user_notification_categories(self):
user = get_current_account()
return []
def existing_notification_subscriptions(self):
subscriptions = Session.query(NotificationSubscription).filter(
NotificationSubscription.account_id == get_current_account().getId()
)
return {subscription.category: subscription for subscription in subscriptions}

def subscribe_notification(self, category):
if category in self.existing_notification_subscriptions:
return
Session.add(
NotificationSubscription(
account_id=get_current_account().getId(),
category=category,
)
)

def unsubscribe_notification(self, category):
subscription = self.existing_notification_subscriptions.get(category)
if subscription:
Session.delete(subscription)

@button.buttonAndHandler(_("Save"), name="save")
def handleSave(self, action):
Expand All @@ -169,6 +187,13 @@ def handleSave(self, action):
user.first_name = data["first_name"]
user.last_name = data["last_name"]

notifications = self.request.get("notifications", {})
for category in self.notification_categories:
if notifications.get(category.id):
self.subscribe_notification(category.id)
else:
self.unsubscribe_notification(category.id)


class AccountSettings(AutoExtensibleForm, form.Form):
"""View name: @@account-settings."""
Expand Down
2 changes: 1 addition & 1 deletion src/euphorie/client/browser/templates/preferences.pt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<fieldset class="pat-checklist group checkbox">
<tal:loop repeat="category categories">
<label class="">
<input checked="${python:'checked' if category.id in view.user_notification_categories else None}"
<input checked="${python:'checked' if category.id in view.existing_notification_subscriptions else None}"
name="notifications.${category/id}:boolean:record"
type="checkbox"
value="true"
Expand Down

0 comments on commit e92449f

Please sign in to comment.