def post(self, request, user_id, timetable_id): BODY_STRUCTURE = [ ("lecture", ParseType.INT, True, []), ] userprofile = request.user.userprofile if userprofile.id != int(user_id): return HttpResponse(status=401) try: timetable = userprofile.timetables.get(id=timetable_id) except Timetable.DoesNotExist: return HttpResponseNotFound() if request.method == "POST": lecture_id, = parse_body(request.body, BODY_STRUCTURE) lecture = Lecture.objects.get(id=lecture_id) if not (lecture.year == timetable.year and lecture.semester == timetable.semester): return HttpResponseBadRequest( "Wrong field 'lecture' in request data") if timetable.lectures.filter(id=lecture_id).exists(): return HttpResponseBadRequest( 'Wrong field \'lecture\' in request data') if timetable.lectures.filter(id=lecture_id).exists(): return HttpResponseBadRequest( 'Wrong field \'lecture\' in request data') timetable.lectures.add(lecture) return JsonResponse(timetable.to_json())
def patch(self, request, review_id): BODY_STRUCTURE = [ ("content", ParseType.STR, False, [lambda content: len(content.strip()) > 0]), ("grade", ParseType.INT, False, [lambda grade: 1 <= grade <= 5]), ("load", ParseType.INT, False, [lambda load: 1 <= load <= 5]), ("speech", ParseType.INT, False, [lambda speech: 1 <= speech <= 5 ]), ] review = get_object_or_404(Review, id=review_id) content, grade, load, speech = parse_body(request.body, BODY_STRUCTURE) user = request.user if user is None or not user.is_authenticated: return HttpResponse(status=401) if not review.writer == user.userprofile: return HttpResponse(status=401) if review.is_deleted: return HttpResponseBadRequest("Target review deleted by admin") patch_object( review, { "content": content, "grade": grade, "load": load, "speech": speech, }, ) return JsonResponse(review.to_json(user=request.user), safe=False)
def post(self, request): BODY_STRUCTURE = [ ("content", ParseType.STR, True, [lambda content: len(content.strip()) > 0]), ("lecture", ParseType.INT, True, []), ("grade", ParseType.INT, True, [lambda grade: 1 <= grade <= 5]), ("load", ParseType.INT, True, [lambda load: 1 <= load <= 5]), ("speech", ParseType.INT, True, [lambda speech: 1 <= speech <= 5]), ] content, lecture_id, grade, load, speech = parse_body( request.body, BODY_STRUCTURE) user = request.user if user is None or not user.is_authenticated: return HttpResponse(status=401) user_profile = user.userprofile lecture = user_profile.review_writable_lectures.get(id=lecture_id) course = lecture.course review = Review.objects.create( course=course, lecture=lecture, content=content, grade=grade, load=load, speech=speech, writer=user_profile, ) return JsonResponse(review.to_json(user=request.user), safe=False)
def post(self, request, user_id, timetable_id): BODY_STRUCTURE = [ ("arrange_order", ParseType.INT, True, []), ] userprofile = request.user.userprofile if userprofile.id != int(user_id): return HttpResponse(status=401) try: timetable = userprofile.timetables.get(id=timetable_id) except Timetable.DoesNotExist: return HttpResponseNotFound() arrange_order, = parse_body(request.body, BODY_STRUCTURE) reorder_timetable(timetable, arrange_order) return JsonResponse(timetable.to_json())
def favorite_departments(request): user = request.user user_profile = user.userprofile if request.method == "POST": BODY_STRUCTURE = [ ("fav_department", ParseType.LIST_INT, True, []), ] fav_department, = parse_body(request.body, BODY_STRUCTURE) user_profile.favorite_departments.clear() for department_id in fav_department: department_obj = Department.objects.get(id=department_id) user_profile.favorite_departments.add(department_obj) return HttpResponse() return HttpResponseBadRequest()
def post(self, request, user_id): BODY_STRUCTURE = [ ("year", ParseType.INT, True, []), ("semester", ParseType.INT, True, []), ("lectures", ParseType.LIST_INT, True, []), ] userprofile = request.user.userprofile if userprofile.id != int(user_id): return HttpResponse(status=401) year, semester, lecture_ids = parse_body(request.body, BODY_STRUCTURE) if not _validate_year_semester(year, semester): return HttpResponseBadRequest( "Wrong fields 'year' and 'semester' in request data") related_timetables = Timetable.get_related_timetables( userprofile, year, semester) if related_timetables.exists(): arrange_order = related_timetables.order_by( "arrange_order").last().arrange_order + 1 else: arrange_order = 0 timetable = Timetable.objects.create(user=userprofile, year=year, semester=semester, arrange_order=arrange_order) for i in lecture_ids: try: lecture = Lecture.objects.get(id=i, year=year, semester=semester) except Lecture.DoesNotExist: return HttpResponseBadRequest( "Wrong field 'lectures' in request data") timetable.lectures.add(lecture) return JsonResponse(timetable.to_json())
def post(self, request): BODY_STRUCTURE = [ ("score", ParseType.INT, True, [lambda score: 1 <= score <= 5]), ] score, = parse_body(request.body, BODY_STRUCTURE) user = request.user if user is None or not user.is_authenticated: return HttpResponse(status=401) current_year = timezone.now().year if Rate.objects.filter(user=user.userprofile, year=current_year).exists(): return HttpResponseBadRequest("You already rated for current year") Rate.objects.create(score=score, user=user.userprofile, year=current_year, version=settings.VERSION) return HttpResponse()
def post(self, request, user_id): BODY_STRUCTURE = [ ("lecture", ParseType.INT, True, []), ] userprofile = request.user.userprofile if userprofile.id != int(user_id): return HttpResponse(status=401) wishlist = Wishlist.objects.get_or_create(user=userprofile)[0] lecture_id, = parse_body(request.body, BODY_STRUCTURE) if not wishlist.lectures.filter(id=lecture_id).exists(): return HttpResponseBadRequest( "Wrong field 'lecture' in request data") lecture = Lecture.objects.get(id=lecture_id) wishlist.lectures.remove(lecture) result = wishlist.to_json() return JsonResponse(result)
def post(self, request, user_id, timetable_id): BODY_STRUCTURE = [ ("lecture", ParseType.INT, True, []), ] userprofile = request.user.userprofile if userprofile.id != int(user_id): return HttpResponse(status=401) try: timetable = userprofile.timetables.get(id=timetable_id) except Timetable.DoesNotExist: return HttpResponseNotFound() lecture_id, = parse_body(request.body, BODY_STRUCTURE) if not timetable.lectures.filter(id=lecture_id).exists(): return HttpResponseBadRequest( "Wrong field 'lecture' in request data") lecture = Lecture.objects.get(id=lecture_id) timetable.lectures.remove(lecture) return JsonResponse(timetable.to_json())