Skip to content

Commit

Permalink
add view to manage cohost
Browse files Browse the repository at this point in the history
The view will be used by ajax request which will be sent from event_home.html.
  • Loading branch information
superryeti committed Mar 10, 2023
1 parent dc1df58 commit f053e4c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions physionet-django/events/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
urlpatterns = [
path('', views.event_home, name='event_home'),
path('create/', views.create_event, name='create_event'),
path('manage_co_hosts/', views.manage_co_hosts, name='manage_co_hosts'),
path('<slug:event_slug>/', views.event_detail, name='event_detail'),
path('<slug:event_slug>/edit_event/', views.update_event, name='update_event'),
path('<slug:event_slug>/details/', views.get_event_details, name='get_event_details'),
Expand Down
47 changes: 47 additions & 0 deletions physionet-django/events/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,50 @@ def event_detail(request, event_slug):
'is_waitlisted': is_waitlisted,
'event_datasets': event_datasets,
})


@login_required
def manage_co_hosts(request):
"""
Manage co-hosts of an event
"""
user = request.user

if request.method == 'POST' and request.is_ajax():
participant_id = request.POST.get('participant_id')

event_slug = request.POST.get('event_slug')
event = get_object_or_404(Event, slug=event_slug)

if not event.host == user:
return JsonResponse({'error': 'You are not the host of this event'}, status=403)

if event.end_date < datetime.now().date():
return JsonResponse({'error': 'You cannot manage co-hosts of an event that has ended'}, status=403)

if not event.participants.filter(id=participant_id).exists():
return JsonResponse({'error': 'User is not a participant of this event'}, status=403)

participant = event.participants.get(id=participant_id)

if 'Remove cohost' in request.POST.get('submit'):
if not participant.is_cohost:
return JsonResponse({'error': 'User is not a cohost of this event'}, status=403)
participant.is_cohost = False
participant.save()

# placeholder for notification to cohost that they have been removed and to host that they removed a cohost

return JsonResponse({'success': 'Cohost removed successfully'})
elif 'Make cohost' in request.POST.get('submit'):
if participant.is_cohost:
return JsonResponse({'error': 'User is already a cohost of this event'}, status=403)
participant.is_cohost = True
participant.save()

# placeholder for notification to cohost that they have been added and to host that they added a cohost

return JsonResponse({'success': 'Cohost added successfully'})

messages.error(request, 'Invalid request')
return redirect(event_home)

0 comments on commit f053e4c

Please sign in to comment.