Skip to content

Commit

Permalink
Cohorts can have active status toggled
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebrownlee committed Apr 7, 2024
1 parent a283612 commit e8a3827
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion LearningAPI/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class ProposalStatusAdmin(admin.ModelAdmin):
@admin.register(Cohort)
class CohortAdmin(admin.ModelAdmin):
"""For managing cohort information"""
list_display = ('name', 'slack_channel', 'start_date', 'end_date',)
list_display = ('name', 'start_date', 'end_date', 'active')

@admin.register(Assessment)
class AssessmentAdmin(admin.ModelAdmin):
Expand Down
17 changes: 17 additions & 0 deletions LearningAPI/migrations/0043_cohort_active.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.10 on 2024-04-07 16:47

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("LearningAPI", "0042_alter_learningrecord_unique_together"),
]

operations = [
migrations.AddField(
model_name="cohort",
name="active",
field=models.BooleanField(default=False),
),
]
1 change: 1 addition & 0 deletions LearningAPI/models/people/cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Cohort(models.Model):
end_date = models.DateField(auto_now=False, auto_now_add=False)
break_start_date = models.DateField(auto_now=False, auto_now_add=False)
break_end_date = models.DateField(auto_now=False, auto_now_add=False)
active = models.BooleanField(default=False)

def __repr__(self) -> str:
return f'{self.name}'
Expand Down
2 changes: 1 addition & 1 deletion LearningAPI/views/cohort_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class CohortSerializer(serializers.ModelSerializer):

class Meta:
model = Cohort
fields = ( 'id', 'name' )
fields = ( 'id', 'name', 'active' )


class CohortInfoSerializer(serializers.ModelSerializer):
Expand Down
18 changes: 13 additions & 5 deletions LearningAPI/views/cohort_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CohortPermission(permissions.BasePermission):
"""Cohort permissions"""

def has_permission(self, request, view):
if view.action in ['create', 'update', 'destroy', 'assign', 'migrate']:
if view.action in ['create', 'update', 'destroy', 'assign', 'migrate', 'active']:
return request.auth.user.is_staff
elif view.action in ['retrieve', 'list']:
return True
Expand Down Expand Up @@ -156,8 +156,7 @@ def list(self, request):
active = self.request.query_params.get('active', None)

if active == 'true':
today = datetime.now().date()
cohorts = cohorts.filter(start_date__lte=today, end_date__gte=today)
cohorts = cohorts.filter(active=True)

if search_terms is not None:
for letter in list(search_terms):
Expand All @@ -171,7 +170,7 @@ def list(self, request):
members__nss_user__user__is_staff=False)),
is_instructor=Count('members', filter=Q(
members__nss_user__user=request.auth.user)),
).all().order_by('-pk')
).all().order_by('pk')

if limit is not None:
cohorts = cohorts.order_by("-start_date")[0:int(limit)]
Expand All @@ -182,6 +181,15 @@ def list(self, request):
except Exception as ex:
return HttpResponseServerError(ex)

@action(methods=['put', ], detail=True)
def active(self, request, pk):
if request.method == "PUT":
cohort = Cohort.objects.get(pk=pk)
cohort.active = request.data.get('active', False)
cohort.save()

return Response(None, status=status.HTTP_204_NO_CONTENT)

@action(methods=['put', ], detail=True)
def migrate(self, request, pk):
"""Migrate all students in a cohort from client side to server side
Expand Down Expand Up @@ -379,5 +387,5 @@ class Meta:
'id', 'name', 'slack_channel', 'start_date', 'end_date',
'coaches', 'break_start_date', 'break_end_date', 'students',
'is_instructor', 'courses', 'info', 'student_organization_url',
'github_classroom_url', 'attendance_sheet_url',
'github_classroom_url', 'attendance_sheet_url', 'active'
)

0 comments on commit e8a3827

Please sign in to comment.