def import_dir(self, dir_path, params={}): """ Imports a directory of CSV files. https://canvas.instructure.com/doc/api/sis_imports.html#method.sis_imports_api.create """ archive = self._build_archive(dir_path) f = open(archive, "r") try: body = f.read() finally: f.close() params["import_type"] = SISImportModel.CSV_IMPORT_TYPE url = "/api/v1/accounts/%s/sis_imports.json%s" % ( settings.RESTCLIENTS_CANVAS_ACCOUNT_ID, self._params(params)) headers = { "Accept": "application/json", "Content-Type": "application/zip" } response = Canvas_DAO().postURL(url, headers, body) if not (response.status == 200 or response.status == 204): raise DataFailureException(url, response.status, response.data) return self._sis_import_from_json(json.loads(response.data))
def _post_resource(self, url, body): """ Canvas POST method. """ headers = { "Content-Type": "application/json", "Accept": "application/json" } response = Canvas_DAO().postURL(url, headers, json.dumps(body)) if not (response.status == 200 or response.status == 204): raise DataFailureException(url, response.status, response.data) return json.loads(response.data)
def delete_admin(self, account_id, user_id, role): """ Remove an account admin role from a user. https://canvas.instructure.com/doc/api/admins.html#method.admins.destroy """ url = "/api/v1/accounts/%s/admins/%s?role=%s" % (account_id, user_id, quote(role)) response = Canvas_DAO().deleteURL(url, {"Accept": "application/json"}) if not (response.status == 200 or response.status == 204): raise DataFailureException(url, response.status, response.data) return True
def delete_report(self, report): """ Deletes a generated report instance. https://canvas.instructure.com/doc/api/account_reports.html#method.account_reports.destroy """ url = "/api/v1/accounts/%s/reports/%s/%s" % ( report.account_id, report.type, report.report_id) response = Canvas_DAO().deleteURL(url, {"Accept": "application/json"}) if response.status != 200: raise DataFailureException(url, response.status, response.data) return self._report_from_json(report.account_id, json.loads(response.data))
def import_str(self, csv, params={}): """ Imports a CSV string. https://canvas.instructure.com/doc/api/sis_imports.html#method.sis_imports_api.create """ params["import_type"] = SISImportModel.CSV_IMPORT_TYPE url = "/api/v1/accounts/%s/sis_imports.json%s" % ( settings.RESTCLIENTS_CANVAS_ACCOUNT_ID, self._params(params)) headers = {"Accept": "application/json", "Content-Type": "text/csv"} response = Canvas_DAO().postURL(url, headers, csv) if not (response.status == 200 or response.status == 204): raise DataFailureException(url, response.status, response.data) return self._sis_import_from_json(json.loads(response.data))
def _get_resource_url(self, url, auto_page, data_key): """ Canvas GET method on a full url. Return representation of the requested resource, chasing pagination links to coalesce resources if indicated. """ response = Canvas_DAO().getURL(url, {"Accept": "application/json"}) if response.status != 200: raise DataFailureException(url, response.status, response.data) data = json.loads(response.data) self.next_page_url = self._next_page(response) if auto_page and self.next_page_url: if isinstance(data, list): data.extend( self._get_resource_url(self.next_page_url, True, data_key)) elif isinstance(data, dict) and data_key is not None: data[data_key].extend( self._get_resource_url(self.next_page_url, True, data_key)[data_key]) return data
def proxy(request, service, url): if not hasattr(settings, "RESTCLIENTS_ADMIN_GROUP"): print "You must have a group defined as your admin group." print 'Configure that using RESTCLIENTS_ADMIN_GROUP="u_foo_bar"' raise Exception("Missing RESTCLIENTS_ADMIN_GROUP in settings") user_service = UserService() actual_user = user_service.get_original_user() g = Group() is_admin = g.is_member_of_group(actual_user, settings.RESTCLIENTS_ADMIN_GROUP) if not is_admin: return HttpResponseRedirect("/") use_pre = False headers = {} if service == "sws": dao = SWS_DAO() headers["X-UW-Act-as"] = actual_user elif service == "pws": dao = PWS_DAO() elif service == "gws": dao = GWS_DAO() elif service == "nws": dao = NWS_DAO() elif service == "hfs": dao = Hfs_DAO() elif service == "book": dao = Book_DAO() elif service == "canvas": dao = Canvas_DAO() elif service == "grad": dao = Grad_DAO() elif service == "uwnetid": dao = Uwnetid_DAO() elif service == "libraries": dao = MyLibInfo_DAO() elif service == "libcurrics": dao = LibCurrics_DAO() elif service == "myplan": dao = MyPlan_DAO() elif service == "iasystem": dao = IASYSTEM_DAO() headers = {"Accept": "application/vnd.collection+json"} subdomain = None if url.endswith('/evaluation'): if url.startswith('uwb/') or url.startswith('uwt/'): subdomain = url[:3] url = url[4:] else: subdomain = url[:2] url = url[3:] elif service == "calendar": dao = TrumbaCalendar_DAO() use_pre = True else: return HttpResponseNotFound("Unknown service: %s" % service) url = "/%s" % quote(url) if request.GET: try: url = "%s?%s" % (url, urlencode(request.GET)) except UnicodeEncodeError: err = "Bad URL param given to the restclients browser" return HttpResponse(err) start = time() try: if service == "iasystem" and subdomain is not None: response = dao.getURL(url, headers, subdomain) else: response = dao.getURL(url, headers) except Exception as ex: response = MockHTTP() response.status = 500 response.data = str(ex) end = time() # Assume json, and try to format it. try: if not use_pre: content = format_json(service, response.data) json_data = response.data else: content = response.data json_data = None except Exception as e: content = format_html(service, response.data) json_data = None context = { "url": unquote(url), "content": content, "json_data": json_data, "response_code": response.status, "time_taken": "%f seconds" % (end - start), "headers": response.headers, "override_user": user_service.get_override_user(), "use_pre": use_pre, } try: loader.get_template("restclients/extra_info.html") context["has_extra_template"] = True context["extra_template"] = "restclients/extra_info.html" except TemplateDoesNotExist: pass try: loader.get_template("restclients/proxy_wrapper.html") context["wrapper_template"] = "restclients/proxy_wrapper.html" except TemplateDoesNotExist: context["wrapper_template"] = "proxy_wrapper.html" try: search_template_path = re.sub(r"\..*$", "", url) search_template = "proxy/%s%s.html" % (service, search_template_path) loader.get_template(search_template) context["search_template"] = search_template context["search"] = format_search_params(url) except TemplateDoesNotExist: context["search_template"] = None return render_to_response("proxy.html", context, context_instance=RequestContext(request))