From 92c4b559f361801e697abb0a29744043f443fcdc Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 21 Nov 2019 14:25:02 +0000 Subject: [PATCH] people: Allow to modify if people want to receive promotional updates Signed-off-by: Michael Tremer --- Makefile.am | 4 +++ src/backend/accounts.py | 41 ++++++++++++++++++++++++++ src/backend/countries.py | 32 ++++++++++++++++++++ src/templates/messages/base-promo.html | 2 +- src/templates/people/index.html | 26 ++++++++++++++++ src/templates/people/subscribe.html | 27 +++++++++++++++++ src/templates/people/subscribed.html | 19 ++++++++++++ src/templates/people/unsubscribe.html | 35 ++++++++++++++++++++++ src/templates/people/unsubscribed.html | 27 +++++++++++++++++ src/web/__init__.py | 4 +++ src/web/people.py | 32 ++++++++++++++++++++ 11 files changed, 248 insertions(+), 1 deletion(-) create mode 100644 src/templates/people/subscribe.html create mode 100644 src/templates/people/subscribed.html create mode 100644 src/templates/people/unsubscribe.html create mode 100644 src/templates/people/unsubscribed.html diff --git a/Makefile.am b/Makefile.am index 7c618798..53574dff 100644 --- a/Makefile.am +++ b/Makefile.am @@ -275,6 +275,10 @@ templates_people_DATA = \ src/templates/people/passwd.html \ src/templates/people/search.html \ src/templates/people/sip.html \ + src/templates/people/subscribe.html \ + src/templates/people/subscribed.html \ + src/templates/people/unsubscribe.html \ + src/templates/people/unsubscribed.html \ src/templates/people/user.html \ src/templates/people/user-edit.html \ src/templates/people/users.html diff --git a/src/backend/accounts.py b/src/backend/accounts.py index 2c9c47ec..bc284bff 100644 --- a/src/backend/accounts.py +++ b/src/backend/accounts.py @@ -366,6 +366,10 @@ class Accounts(Object): first_name=res.first_name, last_name=res.last_name, country_code=res.country_code) + # Non-EU users do not need to consent to promo emails + if account.country_code and not account.country_code in countries.EU_COUNTRIES: + account.consents_to_promotional_emails = True + # Invite newly registered users to newsletter self.backend.messages.send_template( "newsletter/subscribe", address="%s <%s>" % (account, account.email)) @@ -1060,6 +1064,25 @@ class Account(LDAPObject): # Delete avatar hash self.memcache.delete("accounts:%s:avatar-hash" % self.dn) + # Consent to promotional emails + + def get_consents_to_promotional_emails(self): + return self.is_member_of_group("promotional-consent") + + def set_contents_to_promotional_emails(self, value): + group = self.backend.groups.get_by_gid("promotional-consent") + assert group, "Could not find group: promotional-consent" + + if value is True: + group.add_member(self) + else: + group.del_member(self) + + consents_to_promotional_emails = property( + get_consents_to_promotional_emails, + set_contents_to_promotional_emails, + ) + class StopForumSpam(Object): def init(self, uid, email, address): @@ -1265,6 +1288,10 @@ class Group(LDAPObject): """ Adds a member to this group """ + # Do nothing if this user is already in the group + if account.is_member_of_group(self.gid): + return + if "posixGroup" in self.objectclasses: self._add_string("memberUid", account.uid) else: @@ -1274,6 +1301,20 @@ class Group(LDAPObject): self.members.append(account) self.members.sort() + def del_member(self, account): + """ + Removes a member from a group + """ + # Do nothing if this user is not in the group + if not account.is_member_of_group(self.gid): + return + + if "posixGroup" in self.objectclasses: + self._delete_string("memberUid", account.uid) + else: + self._delete_string("member", account.dn) + + if __name__ == "__main__": a = Accounts() diff --git a/src/backend/countries.py b/src/backend/countries.py index d5a898fb..782c3fe1 100644 --- a/src/backend/countries.py +++ b/src/backend/countries.py @@ -40,6 +40,38 @@ ZONES = { "PF", "PG", "PN", "PW", "SB", "TK", "TO", "TV", "UM", "VU", "WF", "WS"], } +EU_COUNTRIES = ( + "BE", + "BG", + "CZ", + "DK", + "DE", + "EE", + "IE", + "EL", + "ES", + "FR", + "FR", + "GB", + "HR", + "IT", + "CY", + "LV", + "LT", + "LU", + "HU", + "MT", + "NL", + "AT", + "PL", + "PT", + "RO", + "SI", + "SK", + "FI", + "SE", +) + def get_name(code): try: return iso3166.countries_by_alpha2[code].name diff --git a/src/templates/messages/base-promo.html b/src/templates/messages/base-promo.html index 3c0815a9..2c56f343 100644 --- a/src/templates/messages/base-promo.html +++ b/src/templates/messages/base-promo.html @@ -2,5 +2,5 @@ {% block footer %} {{ _("Don't like these emails?") }} - {{ _("Unsubscribe") }}. + {{ _("Unsubscribe") }}. {% end block %} diff --git a/src/templates/people/index.html b/src/templates/people/index.html index f8173acd..1fb58b56 100644 --- a/src/templates/people/index.html +++ b/src/templates/people/index.html @@ -14,6 +14,32 @@ + {% if not current_user.consents_to_promotional_emails %} +
+
+
{{ _("You are currently not subscribed to important updates from the IPFire Project") }}
+ +
+
+ +
+ +
+

+ {{ _("Subscribe to receive notifications about important security updates of IPFire and other news from inside the project") }} +

+
+ + +
+
+
+ {% end %} + {% if hints %}
diff --git a/src/templates/people/subscribe.html b/src/templates/people/subscribe.html new file mode 100644 index 00000000..864930db --- /dev/null +++ b/src/templates/people/subscribe.html @@ -0,0 +1,27 @@ +{% extends "../base.html" %} + +{% block title %}{{ _("Subscribe") }}{% end block %} + +{% block content %} +
+
+
+
+
{{ _("Subscribe to Receive Import Updates from the IPFire Project") }}
+ +

+ {{ _("Subscribe to receive updates after releases and other important news from the IPFire Project.") }} +

+ +
+ {% raw xsrf_form_html() %} + + +
+
+
+
+
+{% end block %} diff --git a/src/templates/people/subscribed.html b/src/templates/people/subscribed.html new file mode 100644 index 00000000..035b5033 --- /dev/null +++ b/src/templates/people/subscribed.html @@ -0,0 +1,19 @@ +{% extends "../base.html" %} + +{% block title %}{{ _("Thank You") }}{% end block %} + +{% block content %} +
+
+
+
+ + +

+ {{ _("You have been subscribed and will now receive updates from the IPFire Project.") }} +

+
+
+
+
+{% end block %} diff --git a/src/templates/people/unsubscribe.html b/src/templates/people/unsubscribe.html new file mode 100644 index 00000000..e1e90093 --- /dev/null +++ b/src/templates/people/unsubscribe.html @@ -0,0 +1,35 @@ +{% extends "../base.html" %} + +{% block title %}{{ _("Unsubscribe") }}{% end block %} + +{% block content %} +
+
+
+
+
{{ _("Unsubscribe From Updates From The IPFire Project") }}
+ +

+ {{ _("If you unsubscribe, you will no longer receive important emails from the IPFire Project.") }} +

+ +
+ {% raw xsrf_form_html() %} + + +
+ +
+ {% raw xsrf_form_html() %} + + +
+
+
+
+
+{% end block %} diff --git a/src/templates/people/unsubscribed.html b/src/templates/people/unsubscribed.html new file mode 100644 index 00000000..caae8042 --- /dev/null +++ b/src/templates/people/unsubscribed.html @@ -0,0 +1,27 @@ +{% extends "../base.html" %} + +{% block title %}{{ _("Thank You") }}{% end block %} + +{% block content %} +
+
+
+
+ + +

+ {{ _("You have been unsubscribed and will no longer receive important updates from the IPFire Project.") }} +

+
+
+ +
+ {% raw xsrf_form_html() %} + + +
+
+
+{% end block %} diff --git a/src/web/__init__.py b/src/web/__init__.py index 4cead780..41c3f6cd 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -285,6 +285,10 @@ class Application(tornado.web.Application): (r"/users/([a-z_][a-z0-9_-]{0,31})/passwd", people.UserPasswdHandler), (r"/users/([a-z_][a-z0-9_-]{0,31})/sip", people.SIPHandler), + # Promotional Consent Stuff + (r"/subscribe", people.SubscribeHandler), + (r"/unsubscribe", people.UnsubscribeHandler), + # Single-Sign-On for Discourse (r"/sso/discourse", people.SSODiscourse), diff --git a/src/web/people.py b/src/web/people.py index dfc02b55..893a8063 100644 --- a/src/web/people.py +++ b/src/web/people.py @@ -158,6 +158,38 @@ class SearchHandler(auth.CacheMixin, base.BaseHandler): self.render("people/search.html", q=q, accounts=accounts) +class SubscribeHandler(auth.CacheMixin, base.BaseHandler): + @tornado.web.authenticated + def get(self): + if self.current_user.consents_to_promotional_emails: + return self.render("people/subscribed.html") + + self.render("people/subscribe.html") + + @tornado.web.authenticated + def post(self): + # Give consent + self.current_user.consents_to_promotional_emails = True + + self.render("people/subscribed.html") + + +class UnsubscribeHandler(auth.CacheMixin, base.BaseHandler): + @tornado.web.authenticated + def get(self): + if self.current_user.consents_to_promotional_emails: + return self.render("people/unsubscribe.html") + + self.render("people/unsubscribed.html") + + @tornado.web.authenticated + def post(self): + # Withdraw consent + self.current_user.consents_to_promotional_emails = False + + self.render("people/unsubscribed.html") + + class SIPHandler(auth.CacheMixin, base.BaseHandler): @tornado.web.authenticated def get(self, uid): -- 2.47.2