from app import db
from app.models.notification import Notification
# from app.models.users import Member

def notify(user, message, href=None):
    """ Notify a user (by id, name or object reference) with a message.
        An hyperlink can be added to redirect to the notification source """

    # Cuz' duck typing is quite cool
    # TODO: maybe abort if no user is found
    if type(user) == str:
        user = Member.query.filter_by(name=user).first()
    if isinstance(user, Member):
        user = user.id
    if user and Member.query.get(user):
        n = Notification(user, message, href=href)
        db.session.add(n)
        db.session.commit()
    else:
        print("User not found")
