Best Python code snippet using tox_python
views.py
Source:views.py
...50def ajax_content_response(request, course_key, content):51 user_info = cc.User.from_django_user(request.user).to_dict()52 annotated_content_info = get_annotated_content_info(course_key, content, request.user, user_info)53 return JsonResponse({54 'content': prepare_content(content, course_key),55 'annotated_content_info': annotated_content_info,56 })57@require_POST58@login_required59@permitted60def create_thread(request, course_id, commentable_id):61 """62 Given a course and commentble ID, create the thread63 """64 log.debug("Creating new thread in %r, id %r", course_id, commentable_id)65 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)66 course = get_course_with_access(request.user, 'load', course_key)67 post = request.POST68 if course.allow_anonymous:69 anonymous = post.get('anonymous', 'false').lower() == 'true'70 else:71 anonymous = False72 if course.allow_anonymous_to_peers:73 anonymous_to_peers = post.get('anonymous_to_peers', 'false').lower() == 'true'74 else:75 anonymous_to_peers = False76 if 'title' not in post or not post['title'].strip():77 return JsonError(_("Title can't be empty"))78 if 'body' not in post or not post['body'].strip():79 return JsonError(_("Body can't be empty"))80 thread = cc.Thread(81 anonymous=anonymous,82 anonymous_to_peers=anonymous_to_peers,83 commentable_id=commentable_id,84 course_id=course_key.to_deprecated_string(),85 user_id=request.user.id,86 thread_type=post["thread_type"],87 body=post["body"],88 title=post["title"]89 )90 # Cohort the thread if required91 try:92 group_id = get_group_id_for_comments_service(request, course_key, commentable_id)93 except ValueError:94 return HttpResponseBadRequest("Invalid cohort id")95 if group_id is not None:96 thread.group_id = group_id97 thread.save()98 # patch for backward compatibility to comments service99 if 'pinned' not in thread.attributes:100 thread['pinned'] = False101 if post.get('auto_subscribe', 'false').lower() == 'true':102 user = cc.User.from_django_user(request.user)103 user.follow(thread)104 data = thread.to_dict()105 add_courseware_context([data], course)106 if request.is_ajax():107 return ajax_content_response(request, course_key, data)108 else:109 return JsonResponse(prepare_content(data, course_key))110@require_POST111@login_required112@permitted113def update_thread(request, course_id, thread_id):114 """115 Given a course id and thread id, update a existing thread, used for both static and ajax submissions116 """117 if 'title' not in request.POST or not request.POST['title'].strip():118 return JsonError(_("Title can't be empty"))119 if 'body' not in request.POST or not request.POST['body'].strip():120 return JsonError(_("Body can't be empty"))121 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)122 thread = cc.Thread.find(thread_id)123 thread.body = request.POST["body"]124 thread.title = request.POST["title"]125 # The following checks should avoid issues we've seen during deploys, where end users are hitting an updated server126 # while their browser still has the old client code. This will avoid erasing present values in those cases.127 if "thread_type" in request.POST:128 thread.thread_type = request.POST["thread_type"]129 if "commentable_id" in request.POST:130 course = get_course_with_access(request.user, 'load', course_key)131 commentable_ids = get_discussion_categories_ids(course)132 if request.POST.get("commentable_id") in commentable_ids:133 thread.commentable_id = request.POST["commentable_id"]134 else:135 return JsonError(_("Topic doesn't exist"))136 thread.save()137 if request.is_ajax():138 return ajax_content_response(request, course_key, thread.to_dict())139 else:140 return JsonResponse(prepare_content(thread.to_dict(), course_key))141def _create_comment(request, course_key, thread_id=None, parent_id=None):142 """143 given a course_key, thread_id, and parent_id, create a comment,144 called from create_comment to do the actual creation145 """146 assert isinstance(course_key, CourseKey)147 post = request.POST148 if 'body' not in post or not post['body'].strip():149 return JsonError(_("Body can't be empty"))150 course = get_course_with_access(request.user, 'load', course_key)151 if course.allow_anonymous:152 anonymous = post.get('anonymous', 'false').lower() == 'true'153 else:154 anonymous = False155 if course.allow_anonymous_to_peers:156 anonymous_to_peers = post.get('anonymous_to_peers', 'false').lower() == 'true'157 else:158 anonymous_to_peers = False159 comment = cc.Comment(160 anonymous=anonymous,161 anonymous_to_peers=anonymous_to_peers,162 user_id=request.user.id,163 course_id=course_key.to_deprecated_string(),164 thread_id=thread_id,165 parent_id=parent_id,166 body=post["body"]167 )168 comment.save()169 if post.get('auto_subscribe', 'false').lower() == 'true':170 user = cc.User.from_django_user(request.user)171 user.follow(comment.thread)172 if request.is_ajax():173 return ajax_content_response(request, course_key, comment.to_dict())174 else:175 return JsonResponse(prepare_content(comment.to_dict(), course.id))176@require_POST177@login_required178@permitted179def create_comment(request, course_id, thread_id):180 """181 given a course_id and thread_id, test for comment depth. if not too deep,182 call _create_comment to create the actual comment.183 """184 if cc_settings.MAX_COMMENT_DEPTH is not None:185 if cc_settings.MAX_COMMENT_DEPTH < 0:186 return JsonError(_("Comment level too deep"))187 return _create_comment(request, SlashSeparatedCourseKey.from_deprecated_string(course_id), thread_id=thread_id)188@require_POST189@login_required190@permitted191def delete_thread(request, course_id, thread_id):192 """193 given a course_id and thread_id, delete this thread194 this is ajax only195 """196 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)197 thread = cc.Thread.find(thread_id)198 thread.delete()199 return JsonResponse(prepare_content(thread.to_dict(), course_key))200@require_POST201@login_required202@permitted203def update_comment(request, course_id, comment_id):204 """205 given a course_id and comment_id, update the comment with payload attributes206 handles static and ajax submissions207 """208 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)209 comment = cc.Comment.find(comment_id)210 if 'body' not in request.POST or not request.POST['body'].strip():211 return JsonError(_("Body can't be empty"))212 comment.body = request.POST["body"]213 comment.save()214 if request.is_ajax():215 return ajax_content_response(request, course_key, comment.to_dict())216 else:217 return JsonResponse(prepare_content(comment.to_dict(), course_key))218@require_POST219@login_required220@permitted221def endorse_comment(request, course_id, comment_id):222 """223 given a course_id and comment_id, toggle the endorsement of this comment,224 ajax only225 """226 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)227 comment = cc.Comment.find(comment_id)228 comment.endorsed = request.POST.get('endorsed', 'false').lower() == 'true'229 comment.endorsement_user_id = request.user.id230 comment.save()231 return JsonResponse(prepare_content(comment.to_dict(), course_key))232@require_POST233@login_required234@permitted235def openclose_thread(request, course_id, thread_id):236 """237 given a course_id and thread_id, toggle the status of this thread238 ajax only239 """240 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)241 thread = cc.Thread.find(thread_id)242 thread.closed = request.POST.get('closed', 'false').lower() == 'true'243 thread.save()244 return JsonResponse({245 'content': prepare_content(thread.to_dict(), course_key),246 'ability': get_ability(course_key, thread.to_dict(), request.user),247 })248@require_POST249@login_required250@permitted251def create_sub_comment(request, course_id, comment_id):252 """253 given a course_id and comment_id, create a response to a comment254 after checking the max depth allowed, if allowed255 """256 if cc_settings.MAX_COMMENT_DEPTH is not None:257 if cc_settings.MAX_COMMENT_DEPTH <= cc.Comment.find(comment_id).depth:258 return JsonError(_("Comment level too deep"))259 return _create_comment(request, SlashSeparatedCourseKey.from_deprecated_string(course_id), parent_id=comment_id)260@require_POST261@login_required262@permitted263def delete_comment(request, course_id, comment_id):264 """265 given a course_id and comment_id delete this comment266 ajax only267 """268 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)269 comment = cc.Comment.find(comment_id)270 comment.delete()271 return JsonResponse(prepare_content(comment.to_dict(), course_key))272@require_POST273@login_required274@permitted275def vote_for_comment(request, course_id, comment_id, value):276 """277 given a course_id and comment_id,278 """279 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)280 user = cc.User.from_django_user(request.user)281 comment = cc.Comment.find(comment_id)282 user.vote(comment, value)283 return JsonResponse(prepare_content(comment.to_dict(), course_key))284@require_POST285@login_required286@permitted287def undo_vote_for_comment(request, course_id, comment_id):288 """289 given a course id and comment id, remove vote290 ajax only291 """292 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)293 user = cc.User.from_django_user(request.user)294 comment = cc.Comment.find(comment_id)295 user.unvote(comment)296 return JsonResponse(prepare_content(comment.to_dict(), course_key))297@require_POST298@login_required299@permitted300def vote_for_thread(request, course_id, thread_id, value):301 """302 given a course id and thread id vote for this thread303 ajax only304 """305 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)306 user = cc.User.from_django_user(request.user)307 thread = cc.Thread.find(thread_id)308 user.vote(thread, value)309 return JsonResponse(prepare_content(thread.to_dict(), course_key))310@require_POST311@login_required312@permitted313def flag_abuse_for_thread(request, course_id, thread_id):314 """315 given a course_id and thread_id flag this thread for abuse316 ajax only317 """318 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)319 user = cc.User.from_django_user(request.user)320 thread = cc.Thread.find(thread_id)321 thread.flagAbuse(user, thread)322 return JsonResponse(prepare_content(thread.to_dict(), course_key))323@require_POST324@login_required325@permitted326def un_flag_abuse_for_thread(request, course_id, thread_id):327 """328 given a course id and thread id, remove abuse flag for this thread329 ajax only330 """331 user = cc.User.from_django_user(request.user)332 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)333 course = get_course_by_id(course_key)334 thread = cc.Thread.find(thread_id)335 remove_all = cached_has_permission(request.user, 'openclose_thread', course_key) or has_access(request.user, 'staff', course)336 thread.unFlagAbuse(user, thread, remove_all)337 return JsonResponse(prepare_content(thread.to_dict(), course_key))338@require_POST339@login_required340@permitted341def flag_abuse_for_comment(request, course_id, comment_id):342 """343 given a course and comment id, flag comment for abuse344 ajax only345 """346 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)347 user = cc.User.from_django_user(request.user)348 comment = cc.Comment.find(comment_id)349 comment.flagAbuse(user, comment)350 return JsonResponse(prepare_content(comment.to_dict(), course_key))351@require_POST352@login_required353@permitted354def un_flag_abuse_for_comment(request, course_id, comment_id):355 """356 given a course_id and comment id, unflag comment for abuse357 ajax only358 """359 user = cc.User.from_django_user(request.user)360 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)361 course = get_course_by_id(course_key)362 remove_all = cached_has_permission(request.user, 'openclose_thread', course_key) or has_access(request.user, 'staff', course)363 comment = cc.Comment.find(comment_id)364 comment.unFlagAbuse(user, comment, remove_all)365 return JsonResponse(prepare_content(comment.to_dict(), course_key))366@require_POST367@login_required368@permitted369def undo_vote_for_thread(request, course_id, thread_id):370 """371 given a course id and thread id, remove users vote for thread372 ajax only373 """374 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)375 user = cc.User.from_django_user(request.user)376 thread = cc.Thread.find(thread_id)377 user.unvote(thread)378 return JsonResponse(prepare_content(thread.to_dict(), course_key))379@require_POST380@login_required381@permitted382def pin_thread(request, course_id, thread_id):383 """384 given a course id and thread id, pin this thread385 ajax only386 """387 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)388 user = cc.User.from_django_user(request.user)389 thread = cc.Thread.find(thread_id)390 thread.pin(user, thread_id)391 return JsonResponse(prepare_content(thread.to_dict(), course_key))392@require_POST393@login_required394@permitted395def un_pin_thread(request, course_id, thread_id):396 """397 given a course id and thread id, remove pin from this thread398 ajax only399 """400 course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)401 user = cc.User.from_django_user(request.user)402 thread = cc.Thread.find(thread_id)403 thread.un_pin(user, thread_id)404 return JsonResponse(prepare_content(thread.to_dict(), course_key))405@require_POST406@login_required407@permitted408def follow_thread(request, course_id, thread_id):409 user = cc.User.from_django_user(request.user)410 thread = cc.Thread.find(thread_id)411 user.follow(thread)412 return JsonResponse({})413@require_POST414@login_required415@permitted416def follow_commentable(request, course_id, commentable_id):417 """418 given a course_id and commentable id, follow this commentable...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!