Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion app/core/feed/cruds_feed.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections.abc import Sequence
from uuid import UUID

from sqlalchemy import select, update
from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession

from app.core.feed import models_feed, schemas_feed
Expand Down Expand Up @@ -62,6 +62,29 @@ async def change_news_status(
)


async def update_news_module_and_object_id(
module: str,
module_object_id: UUID,
new_module: str,
new_module_object_id: UUID,
db: AsyncSession,
) -> None:
"""
Change the module and module_object_id of a news in the feed
"""
await db.execute(
update(models_feed.News)
.where(
models_feed.News.module == module,
models_feed.News.module_object_id == module_object_id,
)
.values(
module=new_module,
module_object_id=new_module_object_id,
),
)


async def change_news_status_by_module_object_id(
module: str,
module_object_id: UUID,
Expand Down Expand Up @@ -106,3 +129,16 @@ async def get_news_by_module_object_id(
),
)
return result.scalars().first()


async def delete_news_by_module_object_id(
module: str,
module_object_id: UUID,
db: AsyncSession,
) -> None:
await db.execute(
delete(models_feed.News).where(
models_feed.News.module == module,
models_feed.News.module_object_id == module_object_id,
),
)
38 changes: 38 additions & 0 deletions app/core/feed/utils_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,44 @@ async def edit_feed_news(
)


async def update_news_module_and_object_id(
module: str,
module_object_id: uuid.UUID,
new_module: str,
new_module_object_id: uuid.UUID,
db: AsyncSession,
):
"""
Change the module and module_object_id of a news in the feed
"""
await cruds_feed.update_news_module_and_object_id(
module=module,
module_object_id=module_object_id,
new_module=new_module,
new_module_object_id=new_module_object_id,
db=db,
)


async def delete_feed_news(
module: str,
module_object_id: uuid.UUID,
db: AsyncSession,
):
"""
Delete a news in the feed

module: identifier of the module that created the news, may be used to open the right page in the app
module_object_id: identifier of the object that is linked to the news in the module, may be used to open the right page in the app
"""

await cruds_feed.delete_news_by_module_object_id(
module=module,
module_object_id=module_object_id,
db=db,
)


async def check_if_module_object_id_is_linked_to_feed(
module: str,
module_object_id: uuid.UUID,
Expand Down
94 changes: 72 additions & 22 deletions app/modules/calendar/endpoints_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sqlalchemy.ext.asyncio import AsyncSession

from app.core.associations import cruds_associations
from app.core.feed import cruds_feed
from app.core.feed import cruds_feed, utils_feed
from app.core.feed.types_feed import NewsStatus
from app.core.groups.groups_type import AccountType
from app.core.notification.schemas_notification import Message
Expand All @@ -33,6 +33,7 @@
)
from app.modules.calendar.factory_calendar import CalendarFactory
from app.modules.calendar.types_calendar import Decision
from app.modules.calendar.utils_calendar import delete_event_feed_news
from app.types.content_type import ContentType
from app.types.exceptions import NewlyAddedObjectInDbNotFoundError
from app.types.module import Module
Expand Down Expand Up @@ -417,7 +418,7 @@ async def add_event(
"/calendar/events/{event_id}",
status_code=204,
)
async def edit_envent(
async def edit_event(
event_id: uuid.UUID,
event_edit: schemas_calendar.EventEdit,
db: AsyncSession = Depends(get_db),
Expand All @@ -431,23 +432,44 @@ async def edit_envent(

**Only usable by admins or members of the event's association**
"""
event = await cruds_calendar.get_event(db=db, event_id=event_id)
old_event = await cruds_calendar.get_event(db=db, event_id=event_id)

if event is None:
raise HTTPException(status_code=404)
if old_event is None:
raise HTTPException(status_code=404, detail="Event not found")

if event_edit.ticket_url_opening and not event_edit.ticket_url:
if not event.ticket_url_opening:
raise HTTPException(
status_code=400,
detail="Ticket URL and opening time must be provided together",
)
if event_edit.ticket_url and not event_edit.ticket_url_opening:
if not event.ticket_url:
previous_feed_module = (
"tickets" if old_event.ticket_event_id else utils_calendar.root
)
previous_feed_module_object_id = old_event.ticket_event_id or event_id

if event_edit.ticket_event_id:
if event_edit.ticket_url or event_edit.ticket_url_opening:
raise HTTPException(
status_code=400,
detail="Ticket URL and opening time must be provided together",
detail="ticket_url and ticket_url_opening should not be provided when ticket_event_id is provided",
)
ticket_event = await cruds_tickets.get_event_simple_by_id(
event_id=event_edit.ticket_event_id,
db=db,
)
if ticket_event is None:
raise HTTPException(status_code=404, detail="Ticket event not found")
event_edit.ticket_url_opening = ticket_event.open_datetime

elif (event_edit.ticket_url_opening and not event_edit.ticket_url) or (
event_edit.ticket_url and not event_edit.ticket_url_opening
):
raise HTTPException(
status_code=400,
detail="Ticket URL and opening time must be provided together",
)

if event_edit.ticket_event_id:
# We want to set the ticket_url to None when a ticket_event_id is provided
event_edit.ticket_url = None
elif event_edit.ticket_url:
# We want to force the ticket_event_id to None when a ticket_url is provided
event_edit.ticket_event_id = None

has_user_calendar_admin_access = await has_user_permission(
user,
Expand All @@ -458,7 +480,7 @@ async def edit_envent(
if (
not is_user_member_of_an_association(
user=user,
association=event.association,
association=old_event.association,
)
and not has_user_calendar_admin_access
):
Expand All @@ -467,8 +489,8 @@ async def edit_envent(
detail="You are not allowed to edit this event",
)

new_decision = event.decision
if event.decision != Decision.pending and not has_user_calendar_admin_access:
new_decision = old_event.decision
if old_event.decision != Decision.pending and not has_user_calendar_admin_access:
# If the event is not pending and the user is not a member of the group BDE, we will change the decision back to pending
new_decision = Decision.pending

Expand All @@ -481,11 +503,39 @@ async def edit_envent(
event_db = await cruds_calendar.get_event(db=db, event_id=event_id)
if event_db is None:
raise NewlyAddedObjectInDbNotFoundError("event")
await utils_calendar.edit_event_feed_news(
event=event_db,
db=db,
notification_tool=notification_tool,
)

if old_event.decision != Decision.pending and not has_user_calendar_admin_access:
# We want to remove the feed related news if the event was previously approved
await delete_event_feed_news(
module=previous_feed_module,
module_object_id=previous_feed_module_object_id,
db=db,
)
else:
# If we approve the event directly, we want to update the feed news

new_feed_module = "tickets" if event_db.ticket_event_id else utils_calendar.root
new_feed_module_object_id = event_db.ticket_event_id or event_id

if (
new_feed_module != previous_feed_module
or new_feed_module_object_id != previous_feed_module_object_id
):
await utils_feed.update_news_module_and_object_id(
module=previous_feed_module,
module_object_id=previous_feed_module_object_id,
new_module=new_feed_module,
new_module_object_id=new_feed_module_object_id,
db=db,
)

await utils_calendar.edit_event_feed_news(
event=event_db,
module=new_feed_module,
module_object_id=new_feed_module_object_id,
db=db,
notification_tool=notification_tool,
)


@module.router.patch(
Expand Down
25 changes: 21 additions & 4 deletions app/modules/calendar/utils_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sqlalchemy.ext.asyncio import AsyncSession

from app.core.feed import schemas_feed
from app.core.feed.utils_feed import create_feed_news, edit_feed_news
from app.core.feed.utils_feed import create_feed_news, delete_feed_news, edit_feed_news
from app.core.utils.config import Settings
from app.modules.calendar import models_calendar
from app.modules.calendar.types_calendar import Decision
Expand Down Expand Up @@ -38,7 +38,7 @@ async def add_event_to_feed(
action_start=event.ticket_url_opening,
module=module_value,
module_object_id=module_object_id_value,
image_directory="event",
image_directory=root,
image_id=event.id,
require_feed_admin_approval=False,
db=db,
Expand All @@ -48,12 +48,14 @@ async def add_event_to_feed(

async def edit_event_feed_news(
event: models_calendar.Event,
module: str,
module_object_id: UUID,
db: AsyncSession,
notification_tool: NotificationTool,
):
await edit_feed_news(
module=root,
module_object_id=event.id,
module=module,
module_object_id=module_object_id,
news_edit=schemas_feed.NewsEdit(
title=event.name,
start=event.start,
Expand All @@ -68,6 +70,21 @@ async def edit_event_feed_news(
)


async def delete_event_feed_news(
module: str,
module_object_id: UUID,
db: AsyncSession,
):
"""
module: could be "event" or "ticket"
Comment thread
maximeroucher marked this conversation as resolved.
"""
await delete_feed_news(
module=module,
module_object_id=module_object_id,
db=db,
)


async def create_icalendar_file(
all_events: Sequence[models_calendar.Event],
settings: Settings,
Expand Down
Loading
Loading