-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Break CRUD routes #703
base: master
Are you sure you want to change the base?
Break CRUD routes #703
Changes from 2 commits
11e0d60
32b0538
cb2cb9c
e7061ac
cb5c06b
bd4ecdd
25d54aa
03d0122
e55fb6a
ea39663
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1126,10 +1126,20 @@ class Meeting(models.Model): | |
|
||
section = models.ForeignKey( | ||
Section, | ||
null=True, | ||
on_delete=models.CASCADE, | ||
related_name="meetings", | ||
help_text="The Section object to which this class meeting belongs.", | ||
) | ||
|
||
associated_break = models.ForeignKey( | ||
"plan.Break", | ||
null=True, | ||
on_delete=models.CASCADE, | ||
related_name="meetings", | ||
help_text="The Section object to which this class meeting belongs.", | ||
) | ||
|
||
day = models.CharField( | ||
max_length=1, | ||
help_text="The single day on which the meeting takes place (one of M, T, W, R, or F).", | ||
|
@@ -1180,6 +1190,19 @@ class Meeting(models.Model): | |
), | ||
) | ||
|
||
def clean(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using Django's built in CONSTRAINT libraries. It is probably more natural to SQL |
||
super().clean() | ||
if (self.section is None and self.associated_break is None) or ( | ||
self.section is not None and self.associated_break is not None | ||
): | ||
raise ValidationError( | ||
"Either the section field of associated_break field must be populated, but not both" | ||
) | ||
|
||
def save(self, *args, **kwargs): | ||
self.clean() | ||
super().save(*args, **kwargs) | ||
|
||
class Meta: | ||
unique_together = (("section", "day", "start", "end", "room"),) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,12 @@ | |
|
||
from courses.models import Course, Meeting, Section | ||
from courses.serializers import CourseListSerializer | ||
from courses.util import get_course_and_section, get_current_semester, normalize_semester | ||
from courses.util import ( | ||
get_course_and_section, | ||
get_current_semester, | ||
normalize_semester, | ||
set_meetings, | ||
) | ||
from courses.views import get_accepted_friends | ||
from PennCourses.docs_settings import PcxAutoSchema | ||
from PennCourses.settings.base import PATH_REGISTRATION_SCHEDULE_NAME | ||
|
@@ -31,8 +36,8 @@ | |
vectorize_user, | ||
vectorize_user_by_courses, | ||
) | ||
from plan.models import PrimarySchedule, Schedule | ||
from plan.serializers import PrimaryScheduleSerializer, ScheduleSerializer | ||
from plan.models import Break, PrimarySchedule, Schedule | ||
from plan.serializers import BreakSerializer, PrimaryScheduleSerializer, ScheduleSerializer | ||
|
||
|
||
@api_view(["POST"]) | ||
|
@@ -618,6 +623,118 @@ def get_queryset(self, semester=None): | |
return queryset | ||
|
||
|
||
class BreakViewSet(AutoPrefetchViewSetMixin, viewsets.ModelViewSet): | ||
|
||
serializer_class = BreakSerializer | ||
permission_classes = [IsAuthenticated] | ||
|
||
def get_serializer_context(self): | ||
context = super().get_serializer_context() | ||
include_location_str = "False" | ||
# TODO: figure out how we want to do locations. | ||
context.update({"include_location": eval(include_location_str)}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets try not to use eval, if locations are not considered in this v1 version, we can comment out? |
||
return context | ||
|
||
def update(self, request): | ||
break_id = request.data.get("id") | ||
if not break_id: | ||
return Response( | ||
{"detail": "Break id is required for update."}, status=status.HTTP_400_BAD_REQUEST | ||
) | ||
|
||
try: | ||
current_break = self.get_queryset().get(id=break_id) | ||
except Break.DoesNotExist: | ||
return Response({"detail": "Break not found."}, status=status.HTTP_404_NOT_FOUND) | ||
except Exception as e: | ||
return Response( | ||
{"detail": "Error retrieving break: " + str(e)}, status=status.HTTP_400_BAD_REQUEST | ||
) | ||
|
||
name = request.data.get("name") | ||
if not name: | ||
return Response( | ||
{"detail": "Break name is required."}, status=status.HTTP_400_BAD_REQUEST | ||
) | ||
location_string = request.data.get("location_string") | ||
|
||
current_break.name = name | ||
current_break.location_string = location_string | ||
|
||
meetings = request.data.get("meetings") | ||
try: | ||
set_meetings(current_break, meetings) | ||
except Exception as e: | ||
return Response( | ||
{"detail": "Error setting meetings: " + str(e)}, status=status.HTTP_400_BAD_REQUEST | ||
) | ||
|
||
try: | ||
current_break.save() | ||
except Exception as e: | ||
return Response( | ||
{"detail": "Error saving break: " + str(e)}, status=status.HTTP_400_BAD_REQUEST | ||
) | ||
|
||
return Response({"message": "success", "id": current_break.id}, status=status.HTTP_200_OK) | ||
|
||
def create(self, request, *args, **kwargs): | ||
break_id = request.data.get("id") | ||
if break_id and Break.objects.filter(id=break_id).exists(): | ||
return self.update(request) | ||
|
||
name = request.data.get("name") | ||
if not name: | ||
return Response( | ||
{"detail": "Break name is required."}, status=status.HTTP_400_BAD_REQUEST | ||
) | ||
location_string = request.data.get("location_string") | ||
|
||
try: | ||
if break_id: | ||
new_break = self.get_queryset().create( | ||
person=request.user, | ||
name=name, | ||
location_string=location_string, | ||
id=break_id, | ||
) | ||
else: | ||
new_break = self.get_queryset().create( | ||
person=request.user, | ||
name=name, | ||
location_string=location_string, | ||
) | ||
except IntegrityError as e: | ||
return Response( | ||
{ | ||
"detail": "IntegrityError encountered while trying to create: " | ||
+ str(e.__cause__) | ||
}, | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
except Exception as e: | ||
return Response( | ||
{"detail": "Error creating break: " + str(e)}, | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
|
||
meetings = request.data.get("meetings") | ||
try: | ||
set_meetings(new_break, meetings) | ||
except Exception as e: | ||
return Response( | ||
{"detail": "Error setting meetings: " + str(e)}, status=status.HTTP_400_BAD_REQUEST | ||
) | ||
|
||
return Response({"message": "success", "id": new_break.id}, status=status.HTTP_201_CREATED) | ||
|
||
def get_queryset(self): | ||
return Break.objects.filter(person=self.request.user).prefetch_related( | ||
"meetings", # Prefetch the related meetings | ||
"meetings__room", # Prefetch the related rooms for each meeting | ||
) | ||
|
||
|
||
class CalendarAPIView(APIView): | ||
schema = PcxAutoSchema( | ||
custom_path_parameter_desc={ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Meeting object"