I am building a birthday reminder app and am working on the notifications bell in flask. When I open up the notifications it should populate with the user birthdays that the user follows that are coming up in less than one month and/or are today. Doesn't work even though there are clearly birthdays in the database within the right date and timeframe.
@app.context_processor
def base():
def notifbday():
if current_user.is_authenticated:
user_id = flask_login.current_user.id
# Get the current date
today = datetime.now()
# Calculate the date one month earlier
one_month_later = today + relativedelta(months=1)
print(one_month_later)
currentyear = func.extract('year', today)
birthdays_in_last_month = User.query.join(
Follow, User.id == Follow.follower_id
).filter(
func.extract('month', User.birthday) <= one_month_later.month,
func.extract('day', User.birthday) <= one_month_later.day,
func.extract('month', User.birthday) >= today.month,
func.extract('day', User.birthday) >= today.day
).all()
print(birthdays_in_last_month)
return birthdays_in_last_month
return dict(notif=notifbday)
It did work when I did like this only but stopped after adding all four filters.
birthdays_in_last_month = User.query.join(
Follow, User.id == Follow.follower_id
).filter(
func.extract('month', User.birthday) >= today.month,
func.extract('day', User.birthday) >= today.day
).all()
<div class="popup" id="myPopup">
<i id="notif" class="fa fa-bell" style="font-size: 48px; color: red"></i>
<div id="popuptext">
{% if notif() != None %} {% for n in notif() %}
<div class="notifmsgs">
<p>
{{ n.username }}'s birthday is coming up on {{ n.birthday.strftime('%Y-%m-%d') }}
</p>
</div>
{% endfor %}{% endif %}
</div>
</div>
The User query join was reversed. Had to change to User.id == Follow.followed_id
not follower_id
@app.context_processor
def base():
def notifbday():
print('test')
if current_user.is_authenticated:
print('test')
user_id = flask_login.current_user.id
# Get the current date
today = datetime.now()
# Calculate the date one month earlier
one_month_later = today + relativedelta(months=1)
print(one_month_later)
one_month_before = today - relativedelta(months=12)
birthdays_in_last_month = User.query.join(
Follow, User.id == Follow.followed_id
).filter(
(func.extract('month', User.birthday) >= today.month),
(func.extract('month', User.birthday) <= one_month_later.month)).all()
print(birthdays_in_last_month)
return birthdays_in_last_month
else:
print('user is not logged in')
return dict(notif=notifbday)