From 3bd381692b44d7460befc09e8d6b287b89bae49b Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 9 Jul 2018 14:32:38 +0200 Subject: [PATCH] Send a notification when a follow action happens --- calsocial/__init__.py | 9 ++++++++- calsocial/models.py | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/calsocial/__init__.py b/calsocial/__init__.py index 1f860d3..8af9ebf 100644 --- a/calsocial/__init__.py +++ b/calsocial/__init__.py @@ -272,7 +272,7 @@ class CalendarSocialApp(Flask): """View for following a user """ - from .models import db, Profile, User, UserFollow + from .models import db, Profile, User, UserFollow, Notification, NotificationAction profile = current_user.profile @@ -284,6 +284,13 @@ class CalendarSocialApp(Flask): followed_id=profile, accepted_at=datetime.utcnow()) db.session.add(follow) + + notification = Notification(profile=profile, + actor=current_user.profile, + item=profile, + action=NotificationAction.follow) + db.session.add(notification) + db.session.commit() return redirect(url_for('display_profile', username=username)) diff --git a/calsocial/models.py b/calsocial/models.py index d8a6708..b241dde 100644 --- a/calsocial/models.py +++ b/calsocial/models.py @@ -24,6 +24,7 @@ from warnings import warn from flask_babelex import lazy_gettext from flask_security import UserMixin, RoleMixin from flask_sqlalchemy import SQLAlchemy +from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.orm.exc import NoResultFound from sqlalchemy_utils.types.choice import ChoiceType @@ -519,6 +520,11 @@ class Notification(db.Model): return item_class.query.get(self.item_id) + @item.setter + def item(self, value): + self.item_type = value.__class__.__name__ + self.item_id = value.id + @property def message(self): """Get the translated message for ``key``