From a58142030df3f6a4c99bfd33bd7530fc5f341558 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Fri, 13 Oct 2023 12:34:20 +0800 Subject: [PATCH] Commit migrations and fixed pruning crontab command not setting student status to False. Also fixed Student Status subject field in admin --- .../management/commands/clean_old_entries.py | 1 + stude/student_status/admin.py | 15 ++++++++++++--- .../0002_alter_studygroup_timestamp.py | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 stude/study_groups/migrations/0002_alter_studygroup_timestamp.py diff --git a/stude/accounts/management/commands/clean_old_entries.py b/stude/accounts/management/commands/clean_old_entries.py index c7f4f80..2ed9397 100644 --- a/stude/accounts/management/commands/clean_old_entries.py +++ b/stude/accounts/management/commands/clean_old_entries.py @@ -35,6 +35,7 @@ class Command(BaseCommand): # Set the fields of the old statuses to the required values for status in old_statuses: logger.info(f'Resetting StudentStatus: {status}') + status.active = False status.location = Point(0, 0) status.subject = None status.landmark = None diff --git a/stude/student_status/admin.py b/stude/student_status/admin.py index 78241be..ca2ccc2 100644 --- a/stude/student_status/admin.py +++ b/stude/student_status/admin.py @@ -4,8 +4,10 @@ from leaflet.admin import LeafletGeoAdmin from django import forms from subjects.models import SubjectInstance, Subject from accounts.models import CustomUser +from django.contrib.gis.geos import Point +# Unused. To reimplement down the line class CustomStudentStatusForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(CustomStudentStatusForm, self).__init__(*args, **kwargs) @@ -23,9 +25,8 @@ class CustomStudentStatusForm(forms.ModelForm): # Filter the Subject objects by these names subjects = Subject.objects.filter(name__in=subject_instance_names) self.fields['subject'].queryset = subjects - # To fix: study group is still empty despite student already joined one subject = forms.ModelChoiceField( - queryset=Subject.objects.none(), required=False) + queryset=Subject.objects.all(), required=False) class Meta: model = StudentStatus @@ -34,12 +35,20 @@ class CustomStudentStatusForm(forms.ModelForm): class StudentStatusAdmin(LeafletGeoAdmin): model = StudentStatus - form = CustomStudentStatusForm # define which fields are required + def save_model(self, request, obj, form, change): + if obj.active is False: + obj.location = Point(0, 0) + obj.subject = None + obj.landmark = None + obj.study_group = None + super().save_model(request, obj, form, change) + def get_form(self, request, obj=None, **kwargs): form = super(StudentStatusAdmin, self).get_form(request, obj, **kwargs) form.base_fields['landmark'].required = False + form.base_fields['study_group'].required = False return form diff --git a/stude/study_groups/migrations/0002_alter_studygroup_timestamp.py b/stude/study_groups/migrations/0002_alter_studygroup_timestamp.py new file mode 100644 index 0000000..1a78af3 --- /dev/null +++ b/stude/study_groups/migrations/0002_alter_studygroup_timestamp.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.5 on 2023-10-13 04:23 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('study_groups', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='studygroup', + name='timestamp', + field=models.DateTimeField(auto_now_add=True), + ), + ]