Skip to content

Commit

Permalink
ElectionDay: Adds additional date metadata to subscribers
Browse files Browse the repository at this point in the history
TYPE: Feature
LINK: OGC-1882
  • Loading branch information
Daverball authored Dec 10, 2024
1 parent d9f0e02 commit 1770d45
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 24 deletions.
15 changes: 14 additions & 1 deletion src/onegov/election_day/collections/subscribers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from onegov.election_day.models import EmailSubscriber
from onegov.election_day.models import SmsSubscriber
from onegov.election_day.models import Subscriber
from sedate import utcnow
from sqlalchemy import func


Expand Down Expand Up @@ -190,7 +191,9 @@ def export(self) -> list[dict[str, Any]]:
'domain': subscriber.domain,
'domain_segment': subscriber.domain_segment,
'locale': subscriber.locale,
'active': subscriber.active
'active_since': subscriber.active_since,
'inactive_since': subscriber.inactive_since,
'active': subscriber.active,
}
for subscriber in self.query()
]
Expand Down Expand Up @@ -298,6 +301,9 @@ def confirm_subscription(

subscriber = self.by_address(address, domain, domain_segment)
if subscriber:
if not subscriber.active:
subscriber.active_since = utcnow()
subscriber.inactive_since = None
subscriber.active = True
subscriber.locale = locale
return True
Expand Down Expand Up @@ -359,6 +365,8 @@ def confirm_unsubscription(

subscriber = self.by_address(address, domain, domain_segment)
if subscriber:
if subscriber.active:
subscriber.inactive_since = utcnow()
subscriber.active = False
return True
return False
Expand All @@ -384,6 +392,9 @@ def handle_subscription(

if not subscriber.active or subscriber.locale != request.locale:
assert request.locale is not None
if not subscriber.active:
subscriber.active_since = utcnow()
subscriber.inactive_since = None
subscriber.locale = request.locale
subscriber.active = True
content = request.translate(_(
Expand All @@ -401,4 +412,6 @@ def handle_unsubscription(
subscribers.
"""
if subscriber.active:
subscriber.inactive_since = utcnow()
subscriber.active = False
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-08-08 12:33+0200\n"
"POT-Creation-Date: 2024-12-10 16:58+0100\n"
"PO-Revision-Date: 2022-03-24 15:35+0100\n"
"Last-Translator: Marc Sommerhalder <[email protected]>\n"
"Language-Team: German\n"
Expand Down Expand Up @@ -1428,6 +1428,12 @@ msgstr "Noch keine Abonnenten."
msgid "Locale"
msgstr "Gebietsschema"

msgid "Subscribed"
msgstr "Abonnierung"

msgid "Unsubscribed"
msgstr "Abbestellung"

msgid "Active"
msgstr "Aktiv"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-08-08 12:33+0200\n"
"POT-Creation-Date: 2024-12-10 16:58+0100\n"
"PO-Revision-Date: 2022-03-22 07:59+0100\n"
"Last-Translator: Marc Sommerhalder <[email protected]>\n"
"Language-Team: \n"
Expand Down Expand Up @@ -1434,6 +1434,12 @@ msgstr "Pas encore d’abonnés."
msgid "Locale"
msgstr "Locaux"

msgid "Subscribed"
msgstr "Abonnement"

msgid "Unsubscribed"
msgstr "Annulation"

msgid "Active"
msgstr "Actif"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-08-08 12:33+0200\n"
"POT-Creation-Date: 2024-12-10 16:58+0100\n"
"PO-Revision-Date: 2022-03-22 08:00+0100\n"
"Last-Translator: Marc Sommerhalder <[email protected]>\n"
"Language-Team: \n"
Expand Down Expand Up @@ -1422,6 +1422,12 @@ msgstr "Non ci sono ancora degli iscritti."
msgid "Locale"
msgstr "Località"

msgid "Subscribed"
msgstr "Abbonamento"

msgid "Unsubscribed"
msgstr "Annullamento"

msgid "Active"
msgstr "Attivo"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-08-08 12:33+0200\n"
"POT-Creation-Date: 2024-12-10 16:58+0100\n"
"PO-Revision-Date: 2022-03-22 08:01+0100\n"
"Last-Translator: Marc Sommerhalder <[email protected]>\n"
"Language-Team: \n"
Expand Down Expand Up @@ -1441,6 +1441,12 @@ msgstr "Anc nagins abunents."
msgid "Locale"
msgstr "Regiun linguistica"

msgid "Subscribed"
msgstr "Abunament"

msgid "Unsubscribed"
msgstr "Annullaziun"

msgid "Active"
msgstr "Activ"

Expand Down
14 changes: 14 additions & 0 deletions src/onegov/election_day/models/subscriber.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from onegov.core.orm import Base
from onegov.core.orm.mixins import TimestampMixin
from onegov.core.orm.types import UTCDateTime
from onegov.core.orm.types import UUID
from sqlalchemy import Boolean
from sqlalchemy import Column
Expand All @@ -10,6 +11,7 @@
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import uuid
from datetime import datetime


class Subscriber(Base, TimestampMixin):
Expand Down Expand Up @@ -55,6 +57,18 @@ class Subscriber(Base, TimestampMixin):
#: The domain segment of the election compound part.
domain_segment: 'Column[str | None]' = Column(Text, nullable=True)

#: When has this subscriber last been (explicitly) activated.
active_since: 'Column[datetime | None]' = Column(
UTCDateTime,
nullable=True
)

#: When has this subscriber last been (explicitly) deactivated.
inactive_since: 'Column[datetime | None]' = Column(
UTCDateTime,
nullable=True
)


class SmsSubscriber(Subscriber):

Expand Down
4 changes: 4 additions & 0 deletions src/onegov/election_day/templates/manage/subscribers.pt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
<th>${address_title}</th>
<th tal:condition="request.app.principal.segmented_notifications" i18n:translate>Domain</th>
<th i18n:translate>Locale</th>
<th i18n:translate>Subscribed</th>
<th i18n:translate>Unsubscribed</th>
<th i18n:translate>Active</th>
<th i18n:translate class="row-actions right-aligned">Actions</th>
</tr>
Expand All @@ -70,6 +72,8 @@
<tal:block tal:condition="subscriber.domain_segment" i18n:translate>${subscriber.domain_segment}</tal:block>
</td>
<td>${subscriber.locale}</td>
<td>${layout.format_date(subscriber.active_since, 'datetime')}</td>
<td>${layout.format_date(subscriber.inactive_since, 'datetime')}</td>
<td tal:condition="subscriber.active">✔︎</td>
<td tal:condition="not subscriber.active">✘︎</td>
<td class="right-aligned">
Expand Down
23 changes: 23 additions & 0 deletions src/onegov/election_day/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,26 @@ def add_short_title(context: UpgradeContext) -> None:
table,
Column('short_title_translations', HSTORE, nullable=True)
)


@upgrade_task('Add active/inactive since columns to subscribers')
def add_active_inactive_since_columns(context: UpgradeContext) -> None:
if not context.has_column('subscribers', 'active_since'):
assert not context.has_column('subscribers', 'inactive_since')
context.operations.add_column(
'subscribers', Column('active_since', UTCDateTime, default=None)
)
context.operations.add_column(
'subscribers', Column('inactive_since', UTCDateTime, default=None)
)
# pre-fill the dates where we can make an educated guess
context.operations.execute("""
UPDATE subscribers SET inactive_since = modified
WHERE modified >= TIMESTAMP '2022-02-07'
AND active IS FALSE
""")
context.operations.execute("""
UPDATE subscribers SET active_since = created
WHERE modified IS NOT NULL
AND type = 'sms'
""")
Loading

0 comments on commit 1770d45

Please sign in to comment.