How to use _basic_response method in autotest

Best Python code snippet using autotest_python

views.py

Source:views.py Github

copy

Full Screen

...16except ImproperlyConfigured:17 notification = None18ALLOW_USER_COURSE_CREATION = getattr(settings, 'ALLOW_USER_COURSE_CREATION', True)19ALLOW_TEACHER_PERMISSION_CASCADE = getattr(settings, 'ALLOW_TEACHER_PERMISSION_CASCADE', True)20def _basic_response(user, ajax=False, message="Success!", redirect="/"):21 if ajax == 'json':22 return JSONResponse({'result': message}, is_iterable=False)23 elif ajax == 'xml':24 return XMLResponse("<result>%s</result>" % message, is_iterable=False)25 else:26 user.message_set.create(message=message)27 return HttpResponseRedirect(redirect)28### Course-related methods ###29def courses(request):30 course_list = get_list_or_404(Course)31 return render_to_response("courses/courses/list.html", {32 "courses": course_list33 }, context_instance=RequestContext(request))34def course_detail(request, course_slug):35 course = get_object_or_404(Course, slug=course_slug)36 if not (course.activated or request.user in course.active_teachers()):37 if request.user:38 request.user.message_set.create(message="The course you tried to access \39 is currently inactive so can only be seen by course teachers. \40 If you are a teacher of this course, please log in to view it.")41 else:42 pass #should set a session message43 return HttpResponseRedirect(reverse("course_list"))44 45 return render_to_response('courses/courses/course.html', {46 'course': course,47 'lesson': course.lesson_set.all(), 48 'is_teacher': request.user in course.active_teachers(),49 'is_student': request.user in course.active_students()50 }, context_instance=RequestContext(request))51@login_required52def course(request, course_slug=None):53 if not ALLOW_USER_COURSE_CREATION:54 request.user.message_set.create(message="Course creation has been \55 disabled")56 return HttpResponseRedirect(reverse("course_list"))57 if course_slug:58 course = get_object_or_404(Course, slug=course_slug)59 template = 'courses/courses/edit.html'60 else:61 course = None62 template = 'courses/courses/create.html'63 if request.method == 'POST':64 form = CourseForm(request.POST, instance=course)65 if form.is_valid():66 if course:67 form.save()68 request.user.message_set.create(message="Course changes have \69 been saved")70 else:71 if form.cleaned_data['privacy'] == u'E':72 form.cleaned_data['moderated'] = True 73 course = form.save()74 t = Teachership(course=course, teacher=request.user, is_owner=True)75 t.save()76 request.user.message_set.create(message="Your course has been \77 created. It will not be visible to other users until you \78 activate it.") 79 return HttpResponseRedirect(course.get_absolute_url())80 81 form = CourseForm(instance=course)82 return render_to_response(template, {83 'form': form,84 'course': course85 }, context_instance=RequestContext(request)) 86@login_required87def course_actions(request, course_slug, action, ajax=False):88 course = get_object_or_404(Course, slug=course_slug) 89 if not request.user in course.active_teachers():90 request.user.mesage_set.create(message="The course you tried to edit may only \91 be edited by its teachers. If you are a teacher of this course, \92 please log in to edit it.")93 return HttpResponseRedirect(reverse("acct_login"))94 if request.method == "POST":95 if action == "activate":96 course.activated = datetime.now()97 course.save()98 message = "This course has now been activated and so may be seen by users"99 elif action == "deactivate":100 course.activated = None101 course.save()102 message = "This course has been deactivated and so may no longer be seen by users"103 elif action == "reorder":104 positions = request.POST.getlist(u"lesson[]")105 for lesson in course.lesson_set.all():106 lesson.position = int(positions[lesson.position - 1])107 lesson.save()108 message = "Course lessons have been reordered"109 return _basic_response(user=request.user, ajax=ajax, message=message, 110 redirect=request.META.get('HTTP_REFERER', course.get_absolute_url()))111 else:112 return HttpResponseForbidden("This URI accepts the POST method only")113@login_required114def enrollment(request, course_slug, action, ajax=False):115 course = get_object_or_404(Course, slug=course_slug)116 if request.user in course.active_teachers():117 return _basic_response(user=request.user, ajax=ajax, 118 message="You may not enroll in a course which you teach", 119 redirect=request.META.get('HTTP_REFERER', course.get_absolute_url())) 120 if request.method == "POST":121 if action == "enroll": 122 if course.moderated:123 er = EnrollmentRequest(requestor=request.user, course=course, status="R")124 er.save()125 if notification:126 notification.send(course.active_teachers(), "course_student_request",127 {'creator': request.user,128 'course': course,129 'uuid': er.uuid,})130 message = "Your enrollment request has been sent" 131 else:132 course.enroll(request.user)133 message = "You are now enrolled in this course"134 elif action == "unenroll":135 course.unenroll(request.user)136 message = "You have been unenrolled from this course"137 else:138 return HttpResponseForbidden("This URI accepts the POST method only")139 return _basic_response(user=request.user, ajax=ajax, message=message, 140 redirect=request.META.get('HTTP_REFERER', reverse("course_list")))141@login_required142def enrollment_requests(request):143 # TODO really shouldn't need two SELECTs and a list comprehension to do this!144 er_list = EnrollmentRequest.objects.filter(145 course__in=[t.course for t in 146 Teachership.objects.filter(teacher=request.user)]147 )148 return render_to_response("courses/requests/list.html", {149 'enrollment_requests': er_list150 }, context_instance=RequestContext(request)) 151@login_required152def enrollment_response(request, enrollment_request_uuid, action, ajax=False):153 #TODO should require POST method 154 er = get_object_or_404(EnrollmentRequest, 155 uuid=enrollment_request_uuid, 156 status="R")157 if not request.user in er.course.active_teachers():158 return _basic_response(user=request.user, ajax=ajax, 159 message="Only teachers of the \"%s\" course may moderate its \160 enrollment. If you are a teacher please log in to continue." % \161 er.course, 162 redirect=reverse("acct_login"))163 if action == "accept": 164 er.course.enroll(er.requestor)165 er.status = "A"166 er.save()167 notice_type = "course_student_acceptance"168 message = "%(student)s is now enrolled in the \"%(course)s\" course" % \169 {'student': er.requestor, 'course': er.course}170 elif action == "decline":171 er.status = "D"172 notice_type = "course_student_rejection"173 message = "%(student)s's request to join the \"%(course)s\" course \174 has been declined" % {'student': er.requestor, 'course': er.course} 175 176 er.save()177 if notification:178 notification.send([er.requestor], notice_type, {"course": er.course})179 #TODO what happens if there is no HTTP_REFERER or notifications?180 return _basic_response(user=request.user, ajax=ajax, message=message, 181 redirect=request.META.get('HTTP_REFERER', reverse("notification_notices")))182@login_required183def teachership(request, course_slug, action, ajax=False):184 course = get_object_or_404(Course, slug=course_slug)185 if not request.user in course.active_teachers():186 return _basic_response(user=request.user, ajax=ajax, 187 message="That action may only be performed by teachers of the \"%s\" \188 course. If you are a teacher, please log in." % course, 189 redirect=reverse("acct_login"))190 if action == "remove":191 if request.method == 'POST':192 if len(course.active_teachers()) <= 1:193 message = "You may not remove yourself as a teacher as you are \194 the only one left!" % course195 else:196 course.unappoint_teacher(request.user)197 message="You have been removed as a teacher of the \"%s\" course" % course198 return _basic_response(user=request.user, ajax=ajax, message=message, 199 redirect=request.META.get('HTTP_REFERER', course.get_absolute_url()))200 else:201 return HttpResponseForbidden("This URI accepts the POST method only") 202 elif action == "invite":203 if not (request.user in course.owners()) and not ALLOW_TEACHER_PERMISSION_CASCADE:204 return _basic_response(user=request.user, ajax=ajax, 205 message="Only course owners may invite other teachers", 206 redirect=request.META.get('HTTP_REFERER', course.get_absolute_url()))207 if request.method == "POST":208 teachers = User.objects.filter(pk__in=request.POST.getlist(u'teachers'))209 for teacher in teachers: 210 i = TeachingInvitation(invitor=request.user, invitee=teacher, course=course, 211 status="I")212 i.save()213 if notification:214 notification.send(teachers, "course_teacher_invitation", {215 'creator': request.user,216 'course': course,217 'uuid': i.uuid218 })219 return _basic_response(user=request.user, ajax=ajax, 220 message="Your invitation has been sent", 221 redirect=request.META.get('HTTP_REFERER', course.get_absolute_url()))222 else:223 return render_to_response("courses/courses/invite_teacher.html", {224 'course': course,225 'friends': [friend for friend in 226 friend_set_for(request.user) if not 227 friend in course.active_teachers()]228 }, context_instance=RequestContext(request))229@login_required230def teachership_response(request, teachership_invitation_uuid, action, ajax=False):231 #TODO should require POST method232 ti = get_object_or_404(TeachingInvitation, uuid=teachership_invitation_uuid, invitee=request.user)233 if action == "accept":234 ti.course.appoint_teacher(request.user)235 ti.course.unenroll(request.user)236 ti.status = "A"237 notice_type = "course_teacher_acceptance"238 message = "You are now a teacher of the \"%s\" course" % ti.course239 redirect = request.META.get('HTTP_REFERER', ti.course.get_absolute_url())240 elif action == "decline":241 ti.status = "D"242 notice_type = "course_teacher_rejection"243 message = "You have rejected an offer to teach the \"%s\" course" % ti.course244 redirect = request.META.get('HTTP_REFERER', reverse("notification_notices"))245 246 ti.save()247 if notification:248 notification.send([ti.invitor], notice_type, {249 "invitee": request.user,250 "course": ti.course251 })252 return _basic_response(user=request.user, ajax=ajax, message=message, redirect=redirect)253### Lesson-related methods ###254def lesson_detail(request, course_slug, lesson_slug):255 course = get_object_or_404(Course, slug=course_slug)256 is_teacher = request.user in course.active_teachers()257 is_student = request.user in course.active_students()258 259 ACCESSIBLE = course.privacy == "P" or \260 (course.privacy == "R" and request.user) or \261 (course.privacy == "E" and is_student)262 263 if (course.activated and ACCESSIBLE) or is_teacher: 264 lesson = get_object_or_404(Lesson, course=course, slug=lesson_slug)265 if not lesson.activated and not is_teacher:266 if request.user:267 request.user.message_set.create(message="This lesson has been deactivated \268 by a teacher, probably for maintenance. Please check back later.")269 else: 270 pass #should set a session message271 return HttpResponseRedirect(reverse("course_list"))272 return render_to_response("courses/lessons/lesson.html", {273 "lesson": lesson,274 "is_teacher": is_teacher,275 "is_student": is_student276 }, context_instance=RequestContext(request))277 else:278 if not course.activated:279 request.user.message_set.create(message="This course is not yet active so can \280 only be seen by course teachers. If you are a teacher of this \281 course, please log in to view it.")282 return HttpResponseRedirect(reverse("acct_login"))283 elif not ACCESSIBLE:284 # TODO can check if user to make these messages more releveant and redirects more useful285 MESSAGE = {286 "R": "You must be a registered user to view the \"%s\" \287 course. If you are registered, please log in to view it." % 288 course,289 "E": "You must be logged in and enrolled in the \"%s\" \290 course in order to view it. If you are enrolled, please \291 log in. "% course}292 if request.user:293 request.user.message_set.create(message=MESSAGE[course.privacy])294 else: 295 pass #should set a session message 296 return HttpResponseRedirect(course.get_absolute_url())297@login_required298def lesson(request, course_slug, lesson_slug=None):299 course = get_object_or_404(Course, slug=course_slug)300 if not request.user in course.active_teachers():301 request.user.message_set.create(message="That action may only be performed by \302 teachers of the \"%s\" course. If you are a teacher please log in." % 303 course)304 return HttpResponseRedirect(reverse("acct_login"))305 if lesson_slug:306 lesson = get_object_or_404(Lesson, course=course, slug=lesson_slug)307 template = "courses/lessons/edit.html"308 else:309 lesson = Lesson(course=course)310 template = "courses/lessons/create.html"311 if request.method == 'POST':312 form = LessonForm(request.POST, instance=lesson)313 if form.is_valid():314 if lesson_slug:315 form.save()316 request.user.message_set.create(message="Lesson changes have \317 been saved")318 else:319 lesson = form.save()320 request.user.message_set.create(message="Lesson has been added") 321 return HttpResponseRedirect(lesson.get_absolute_url())322 323 form = LessonForm(instance=lesson)324 return render_to_response(template, {325 'form': form,326 'course': course,327 'lesson': lesson328 }, context_instance=RequestContext(request))329@login_required330def lesson_actions(request, course_slug, lesson_slug, action, ajax=False):331 #TODO should be single query332 course = get_object_or_404(Course, slug=course_slug)333 lesson = get_object_or_404(Lesson, course=course, slug=lesson_slug)334 if not request.user in course.active_teachers():335 return _basic_response(user=request.user, ajax=ajax, 336 message="This lesson may only be modified by teachers of the \"%s\" \337 course. If you are a teacher please log in." % course, 338 redirect=reverse("acct_login"))339 if request.method == "POST":340 if action == "activate":341 lesson.activated = datetime.now()342 message="This lesson has now been activated and so may be seen by users"343 elif action == "deactivate":344 lesson.activated = None345 message="This lesson has now been deactivated and so may no longer \346 be seen by users"347 lesson.save()348 return _basic_response(user=request.user, ajax=ajax, message=message, 349 redirect=request.META.get('HTTP_REFERER', lesson.get_absolute_url()))350 else:...

Full Screen

Full Screen

resources.py

Source:resources.py Github

copy

Full Screen

...37 result = self.link()38 result.update({39 'test_results': TestResultCollection(self._request).link(),40 })...

Full Screen

Full Screen

conftest.py

Source:conftest.py Github

copy

Full Screen

1import pytest2from sanic import Sanic, response3from sanic_testing import TestManager4def _basic_response(request):5 return response.text("foo")6@pytest.fixture7def app():8 sanic_app = Sanic(__name__)9 TestManager(sanic_app)10 sanic_app.route(11 "/", methods=["GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS"]12 )(_basic_response)13 return sanic_app14@pytest.fixture15def manager():16 sanic_app = Sanic(__name__)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful