added read all button

This commit is contained in:
Kostiantyn Stoliarskyi 2023-06-12 17:40:10 +03:00
parent 75a248aac3
commit d1e561b842
7 changed files with 152717 additions and 17 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -10,6 +10,7 @@
<div class="p-5 flex border-b-2 border-gray-200 border-solid dark:border-gray-700 text-gray-900 dark:text-white dark:divide-gray-700">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-8 h-8"> <path stroke-linecap="round" stroke-linejoin="round" d="M14.857 17.082a23.848 23.848 0 005.454-1.31A8.967 8.967 0 0118 9.75v-.7V9A6 6 0 006 9v.75a8.967 8.967 0 01-2.312 6.022c1.733.64 3.56 1.085 5.455 1.31m5.714 0a24.255 24.255 0 01-5.714 0m5.714 0a3 3 0 11-5.714 0M3.124 7.5A8.969 8.969 0 015.292 3m13.416 0a8.969 8.969 0 012.168 4.5" /> </svg>
<h1 class="text-2xl font-extrabold dark:text-white ml-4">Notifications</h1>
<a href="{{url_for('notifications.mark_all_as_read')}}" type="button" class="{% if not current_user.active_notifications %}disabled{% endif %} ml-auto text-green-700 hover:text-white border border-green-700 hover:bg-green-800 focus:ring-4 focus:outline-none focus:ring-green-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center mr-2 mb-2 dark:border-green-500 dark:text-green-500 dark:hover:text-white dark:hover:bg-green-600 dark:focus:ring-green-800">Mark all notifications as READ</a>
</div>
{% if not current_user.notifications %}
<p
@ -19,7 +20,7 @@
{% endif %} {% for notification in current_user.notifications %}
<!-- prettier-ignore -->
<dl class="bg-white dark:bg-gray-900 max-w-full p-5 text-gray-900 divide-y divide-gray-200 dark:text-white dark:divide-gray-700 m-3 border-2 border-gray-200 border-solid rounded-lg dark:border-gray-700">
<dt class="mb-2"> <a class="flex flex-col pb-4 {% if not notification.is_read %} font-bold {% endif %}" href="{{url_for('notifications.mark_as_read',notification_id=notification.id)}}">{{notification.text}}</a> </dt>
<dt class="mb-2"> <a class="flex flex-col pb-4 text-gray-500 {% if not notification.is_read %} text-blue-950 dark:text-white font-bold {% endif %}" href="{{url_for('notifications.mark_as_read',notification_id=notification.id)}}">{{notification.text}}</a> </dt>
<dd class="flex flex-col md:flex-row text-lg font-semibold text-gray-500 md:text-lg dark:text-gray-400">
<p> Created at {{notification.created_at.strftime('%B %d, %Y')}}</p>
</dd>

View File

@ -1,4 +1,4 @@
from flask import Blueprint, render_template, redirect
from flask import Blueprint, render_template, redirect, url_for
from flask_login import login_required, current_user
@ -41,3 +41,13 @@ def mark_as_read(notification_id: int):
notification.is_read = True
notification.save()
return redirect(notification.link)
@bp.route("/mark_all_as_read", methods=["GET"])
@login_required
def mark_all_as_read():
for notification in current_user.notifications:
notification.is_read = True
notification.save(False)
db.session.commit()
return redirect(url_for("notifications.get_all"))

View File

@ -10,29 +10,33 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '8f9233babba4'
down_revision = '96995454b90d'
revision = "8f9233babba4"
down_revision = "a41f004cad1a"
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('notifications',
sa.Column('link', sa.String(length=256), nullable=False),
sa.Column('text', sa.String(length=256), nullable=False),
sa.Column('is_read', sa.Boolean(), nullable=True),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('is_deleted', sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
op.create_table(
"notifications",
sa.Column("link", sa.String(length=256), nullable=False),
sa.Column("text", sa.String(length=256), nullable=False),
sa.Column("is_read", sa.Boolean(), nullable=True),
sa.Column("user_id", sa.Integer(), nullable=True),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("is_deleted", sa.Boolean(), nullable=True),
sa.ForeignKeyConstraint(
["user_id"],
["users.id"],
),
sa.PrimaryKeyConstraint("id"),
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('notifications')
op.drop_table("notifications")
# ### end Alembic commands ###

View File

@ -37,3 +37,7 @@
.mt-135 {
margin-top: 135px;
}
.disabled {
pointer-events: none;
cursor: default;
}

View File

@ -177,3 +177,10 @@ def test_notifications(client: FlaskClient):
).first()
assert notification
assert notification.user_id == user_2.id
response: Response = client.get(
"/notifications/mark_all_as_read",
follow_redirects=True,
)
assert response.status_code == 200
assert len(user_2.active_notifications) == 0