def api_import(request, course_slug): """ API to allow automated Pages updates """ offering = get_object_or_404(CourseOffering, slug=course_slug) if request.method != 'POST': return HttpError(request, status=405, title="Method not allowed", error="This URL accepts only POST requests", errormsg=None, simple=True) if request.META['CONTENT_TYPE'] != 'application/json': return HttpError(request, status=415, title='Unsupported Media Type', error="Media type of request must be 'application/json'.", simple=True) data = request.read() try: user = _pages_from_json(request, offering, data) #LOG EVENT# l = LogEntry(userid=user.userid, description="API import of pages in %s." % (offering,), related_object=offering) l.save() except ValidationError as e: status = 400 if hasattr(e, 'status'): status = e.status return HttpError(request, status=status, title='Bad request', error=e.messages[0], simple=True) return HttpError(request, status=200, title='Success', error='Page import successful.', simple=True)
def _forbidden_response(request, visible_to): """ A nicer forbidden message that says why, and gently suggests that anonymous users log in. """ error = 'Not allowed to view this page. It is visible only to %s in this course.' % (visible_to,) errormsg_template = '<strong>You are not currently logged in</strong>. You may be able to view this page if you <a href="%s">log in</a>' errormsg = None if not request.user.is_authenticated(): url = conditional_escape(settings.LOGIN_URL + '?next=' + request.get_full_path()) errormsg = mark_safe(errormsg_template % (url)) return HttpError(request, status=403, title="Forbidden", error=error, errormsg=errormsg)
def delete(request, reminder_slug): if request.method != 'POST': return HttpError(request, status=405, title="Method not allowed", error="This URL accepts only POST requests", errormsg=None) reminder, person = _get_reminder_or_404(request, reminder_slug) reminder.person = person reminder.status = 'D' reminder.save() l = LogEntry(userid=request.user.username, description="deleted reminder %s" % (reminder.slug, ), related_object=reminder) l.save() messages.add_message(request, messages.SUCCESS, 'Reminder deleted.') return HttpResponseRedirect(reverse('reminders:index', kwargs={}))
def process_exception(self, request, exception): import traceback exc_info = sys.exc_info() format = traceback.format_exc(exc_info[2]) message = unicode(exception) if (isinstance(exception, IOError) and '_verify(ticket, service)' in format and ('Connection reset by peer' in message or 'Name or service not known' in message or 'Connection timed out' in message or 'EOF occurred in violation of protocol' in message)): # CAS verification timeout return HttpError( request, status=500, title="CAS Error", error= "Could not contact the CAS server to verify your credentials. Please try logging in again." ) elif isinstance( exception, AssertionError ) and "Django CAS middleware requires authentication middleware" in format: # CAS choke return HttpError( request, status=500, title="CAS Error", error= "Could not contact the CAS server to verify your credentials. Please try logging in again." ) elif isinstance( exception, EOFError ) and "return request.POST.get('csrfmiddlewaretoken', '')" in format: # file upload EOF return HttpError( request, status=500, title="Upload Error", error="Upload seems to have not completed properly.") elif OperationalError is not None and isinstance( exception, OperationalError ) and "Lost connection to MySQL server at 'reading initial communication packet'" in format: # lost main DB return HttpError(request, status=500, title="Database Error", error="Unable to connect to database.") elif OperationalError is not None and isinstance( exception, OperationalError) and "MySQL server has gone away" in format: # lost main DB return HttpError(request, status=500, title="Database Error", error="Unable to connect to database.") elif isinstance( exception, AssertionError ) and "The Django CAS middleware requires authentication middleware" in format: # wacky authentication thing that means the database is missing, or something return HttpError(request, status=500, title="Database Error", error="Unable to connect to database.")