def activity_on_course_up_view(request, slug): """ Increase by 1 point the rank of the activity on the course. .. caution:: Changing the order or activities in a course requires the **change_course** permission. .. important:: The activity to change is given as a POST parameter called **activity** and contains the resource primary key. :param request: django request object :type request: django.http.HttpRequest :param slug: course slug :type slug: str :return: the course detail view """ if request.method == "POST": # Retrieve objects : course, activity, the current course activity course = get_object_or_404(Course, slug=slug) if course.user_can_change(request.user): activity_pk_form = ActivityPKForm(request.POST or None) if activity_pk_form.is_valid(): activity = get_object_or_404( Activity, pk=activity_pk_form.cleaned_data.get('activity')) course_activity = get_object_or_404(CourseActivity, course=course, activity=activity) previous_course_activity = get_object_or_404( CourseActivity, course=course, rank=course_activity.rank - 1) # Switch course activity ranks course_activity.rank = previous_course_activity.rank previous_course_activity.rank += 1 # Save objects course_activity.save() previous_course_activity.save() messages.success( request, _("Activity “%(activity)s” was repositioned and is now the activity n°%(rank)d on this course." ) % { 'activity': activity, 'rank': course_activity.rank }) return redirect("learning:course/detail", slug=course.slug) return HttpResponseNotFound( activity_pk_form.errors.get('activity')) # User cannot change the course messages.error( request, _("You do not have the required permission to change activities of this course." )) raise PermissionDenied() # Method used is not POST, so it’s not allowed return HttpResponseNotAllowed(['POST'])
def activity_on_course_delete_view(request, slug): """ Delete an activity which is currently attached to a course. The link between the activity and the course will be removed, as well as the activity itself. .. caution:: Changing the activities in a course requires the **change_course** permission. .. important:: The activity to change is given as a POST parameter called **activity** and contains the resource primary key. :param request: django request object :type request: django.http.HttpRequest :param slug: course slug :type slug: str :return: the course detail view """ if request.method == "POST": course = get_object_or_404(Course, slug=slug) activity_pk_form = ActivityPKForm(request.POST or None) if activity_pk_form.is_valid(): activity = get_object_or_404( Activity, pk=activity_pk_form.cleaned_data.get('activity')) if course.user_can_change( request.user) and activity.user_can_delete(request.user): try: course.remove_activity(activity) activity.delete() messages.success( request, _("The activity “%(activity)s” has been removed from this course. " "The activity has also been removed.") % {'activity': activity}) except ChangeActivityOnCourseError as ex: messages.error(request, ex) return redirect("learning:course/detail", slug=course.slug) # Not permission to change delete the activity messages.error( request, _("You do not have the required permissions to delete this activity." )) raise PermissionDenied() return HttpResponseNotFound(activity_pk_form.errors.get('activity')) # Method is not allowed return HttpResponseNotAllowed(['POST'])
def activity_on_course_unlink_view(request, slug): """ Unlink an activity from a course. This means the activity will no longer be attached to the course. The activity is not removed. .. caution:: Changing the activities in a course requires the **change_course** permission. .. important:: The activity to change is given as a POST parameter called **activity** and contains the resource primary key. :param request: django request object :type request: django.http.HttpRequest :param slug: course slug :type slug: str :return: the course detail view """ if request.method == "POST": course = get_object_or_404(Course, slug=slug) if course.user_can_change(request.user): # noinspection PyCallByClass activity_pk_form = ActivityPKForm(request.POST or None) if activity_pk_form.is_valid(): activity = get_object_or_404( Activity, pk=activity_pk_form.cleaned_data.get('activity')) try: course.remove_activity(activity) messages.success( request, _("The activity “%(activity)s” has been removed from this course. " "The activity itself has not been removed.") % {'activity': activity}) except ChangeActivityOnCourseError as ex: messages.error(request, ex) return redirect("learning:course/detail", slug=course.slug) return HttpResponseNotFound( activity_pk_form.errors.get('activity')) # Not permission to change the course messages.error( request, _("You do not have the required permissions to unlink this activity from this course." )) raise PermissionDenied() # Method is not allowed return HttpResponseNotAllowed(['POST'])
def post(self, request, *args, **kwargs): activity_pk_form = ActivityPKForm(self.request.POST or None) if activity_pk_form.is_valid(): activity = get_object_or_404( Activity, pk=activity_pk_form.cleaned_data.get('activity')) try: self.object.add_activity(activity) messages.success( self.request, _('Activity “%(activity)s” added to the course “%(course)s”' ) % { 'activity': activity, 'course': self.object }) except LearningError as ex: messages.error(self.request, ex) return redirect("learning:course/detail/activity/attach", slug=self.object.slug) return HttpResponseNotFound(activity_pk_form.errors.get('activity'))