def POST(self): http_input = web.input(tournament_id=None) tournament_id = http_input.tournament_id if tournament_id is None: raise web.notfound() tournament = Tournament.get(int(tournament_id), joined_attrs=["results"]) if tournament is None: raise web.notfound() results_grid = tournament_forms.EditResultsGrid().bind(tournament.results, data=http_input) if results_grid.validate(): # If the form was properly filled, updates the model and returns the standard table # Besides, we sort the results "from the outside" since they are directly returned (commit & no load) results_grid.sync() tournament.sort_results() results = config.views.results(tournament) statistics = config.views.statistics(tournament) else: # Otherwise, returns the editing grid results = config.views.results_admin(tournament, results_grid) statistics = None # Returns the dictionary return dict(results=results, statistics=statistics)
def GET(self, courseid, taskid, path): """ GET request """ if self.user_manager.session_logged_in(): try: course = self.course_factory.get_course(courseid) if not self.user_manager.course_is_open_to_user(course): return self.template_helper.get_renderer().course_unavailable() task = course.get_task(taskid) if not self.user_manager.task_is_visible_by_user(task): return self.template_helper.get_renderer().task_unavailable() path_norm = posixpath.normpath(urllib.unquote(path)) public_folder_path = os.path.normpath(os.path.realpath(os.path.join(task.get_directory_path(), "public"))) file_path = os.path.normpath(os.path.realpath(os.path.join(public_folder_path, path_norm))) # Verify that we are still inside the public directory if os.path.normpath(os.path.commonprefix([public_folder_path, file_path])) != public_folder_path: raise web.notfound() if os.path.isfile(file_path): mimetypes.init() mime_type = mimetypes.guess_type(file_path) web.header('Content-Type', mime_type[0]) with open(file_path) as static_file: return static_file.read() else: raise web.notfound() except: if web.config.debug: raise else: raise web.notfound() else: return self.template_helper.get_renderer().index(self.user_manager.get_auth_methods_fields(), False)
def _PUT(self, *param, **params): user_id = param[0] if not validates_param_id(self, user_id): self.logger.debug("Failed to update account. the value of parameter is invalid.") return web.notfound(self.view.alert) if not validates_user(self): self.logger.debug("Failed to update account. the value of input is invalid.") return web.badrequest(self.view.alert) user = findby1(self.orm, user_id) if not user: self.logger.debug("Failed to update account. No such account - id=%s" % user_id) return web.notfound() cmp_user = findby1email(self.orm, self.input.email) if not cmp_user is None: if int(user_id) != cmp_user.id: self.logger.debug("Failed to update account. The same mail address '%s' already exist - user='******'" % (self.input.email, cmp_user.nickname)) return web.conflict(web.ctx.path) user.nickname = self.input.nickname user.email = self.input.email user.languages = self.input.languages if not is_empty(self.input.new_password): if compare_password(self, user) == False: return web.badrequest(self.view.alert) (password, salt) = sha1encrypt(self.input.new_password) user.password = password user.salt = salt update(self.orm, user) return web.seeother(web.ctx.path)
def POST(self, name, glyphid): if not is_loggedin(): return web.notfound() master = models.Master.get(fontname=name) if not master: return web.notfound() x = web.input(pointnr='', source='', x='', y='') query = models.GlyphOutline.filter(idmaster=master.idmaster, fontsource=x.source.upper(), glyphname=glyphid, pointnr=x.pointnr) query.update(dict(x=x.x, y=x.y)) web.ctx.orm.commit() writeGlyphlist(master, glyphid) glyphA = models.Glyph.get(idmaster=master.idmaster, fontsource='A', name=glyphid) glyphB = models.Glyph.get(idmaster=master.idmaster, fontsource='A', name=glyphid) import xmltomf xmltomf.xmltomf1(master, glyphA, glyphB) makefont(working_dir(), master) M_glyphjson = get_edges_json(u'%s.log' % master.fontname, glyphid) if x.source.upper() == 'A': glyphjson = get_edges_json(u'%sA.log' % master.fontname, glyphid) else: glyphjson = get_edges_json(u'%sB.log' % master.fontname, glyphid) return simplejson.dumps({'M': M_glyphjson, 'R': glyphjson})
def GET(self, name, glyphid): """ View single post """ if not is_loggedin(): raise seeother('/login') master = models.Master.get(fontname=name) if not master: return web.notfound() if not models.GlyphOutline.exists(idmaster=master.idmaster, glyphname=glyphid): return web.notfound() A_glyphjson = get_edges_json(u'%sA.log' % master.fontname, glyphid) B_glyphjson = get_edges_json(u'%sB.log' % master.fontname, glyphid) M_glyphjson = get_edges_json(u'%s.log' % master.fontname, glyphid) localparametersA = models.LocalParam.get(idlocal=master.idlocala) localparametersB = models.LocalParam.get(idlocal=master.idlocalb) globalparams = models.GlobalParam.get(idglobal=master.idglobal) a_original_glyphjson = get_edges_json_from_db(master, glyphid, 'A') b_original_glyphjson = get_edges_json_from_db(master, glyphid, 'B') return render.view(master, glyphid, A_glyphjson, B_glyphjson, M_glyphjson, localparametersA, localparametersB, globalparams, origins={'a': a_original_glyphjson, 'b': b_original_glyphjson})
def _DELETE(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if is_int(param[2]) is False: return web.notfound() nic_id = int(param[2]) model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname)[0] guest = MergeGuest(model, virt) nic_info = virt.get_interface_info()[nic_id] finally: kvc.close() mac = nic_info["mac"]["address"] self.logger.debug('spinning off delete_nic_job dom=%s, mac=%s' % (domname, mac)) if delete_nic_job(self,model,domname,mac) is True: return web.accepted() else: return False
def yzImage ( imageargs, dbcfg ): """Return an xz plane fileobj.read()""" restargs = imageargs.split('/') if len ( restargs ) == 5: [ resstr, xstr, ydimstr, zdimstr, rest ] = restargs globalcoords = False elif len ( restargs ) == 6: [ resstr, xstr, ydimstr, zdimstr, rest, other ] = restargs globalcoords = True else: return web.badrequest() # expecting an argument of the form /resolution/x/y1,y2/z1,z2/ # Check that the arguments are well formatted if not re.match ('[0-9]+$', xstr) or\ not re.match ('[0-9]+,[0-9]+$', ydimstr) or\ not re.match ('[0-9]+,[0-9]+$', zdimstr) or\ not re.match ('[0-9]+$', resstr ): return web.badrequest() y1s,y2s = ydimstr.split(',') z1s,z2s = zdimstr.split(',') x = int(xstr) y1i = int(y1s) y2i = int(y2s) z1i = int(z1s) z2i = int(z2s) resolution = int(resstr) # Convert to local coordinates if global specified if ( globalcoords ): x = int ( float(x) / float( 2**(resolution-dbcfg.baseres))) y1i = int ( float(y1i) / float( 2**(resolution-dbcfg.baseres))) y2i = int ( float(y2i) / float( 2**(resolution-dbcfg.baseres))) #RBTODO need to make a dbconfig object # Check arguments for legal values if not dbcfg.checkCube ( resolution, x, x, y1i, y2i, z1i, z2i )\ or x >= dbcfg.imagesz[resolution][0]: return web.notfound() corner=[x,y1i,z1i-dbcfg.slicerange[0]] dim=[1,y2i-y1i,z2i-z1i ] try: annodb = anndb.AnnotateDB ( dbcfg ) cb = annodb.cutout ( corner, dim, resolution ) fileobj = StringIO.StringIO ( ) cb.yzSlice ( dbcfg.zscale[resolution], fileobj ) except: return web.notfound() web.header('Content-type', 'image/png') fileobj.seek(0) return fileobj.read()
def POST(self): x = web.input(master_id='', project_id='', task_id='') project = models.Project.get(id=x.project_id) if not project: raise web.notfound() master = models.Master.get(id=x.master_id) if not master: return web.notfound() if x.task_id: from celery.result import AsyncResult from metapolator.config import celery res = AsyncResult(x.task_id, backend=celery.backend) if res.ready(): master.task_completed = True web.ctx.orm.commit() return ujson.dumps({'done': True}) else: master.task_updated = datetime.datetime.now() web.ctx.orm.commit() return ujson.dumps({'done': False, 'task_id': x.task_id}) master.task_completed = True web.ctx.orm.commit() return ujson.dumps({'done': True})
def GET(self, name): if not is_coordinator(): raise web.notfound() if name == "contacts.xls": return self.download_contacts() raise web.notfound()
def get(self, uuid): """ ### Request `GET /birds/{id}` Empty body. ### Response Valid status codes: - `200 OK` if the bird exists - `404 Not found` if the bird does not exist A `404 Not found` is expected when the bird does not exist, but not for birds with `visible` set to `false`. The response body for a `200 OK` request can be found in `get-birds-id-response.json`. """ try: bird = Bird.objects.get(id=uuid) return self.format_bird(bird) except mongoengine.DoesNotExist: raise web.notfound() except mongoengine.ValidationError: raise web.notfound()
def _DELETE(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() device = param[1] if device is None: return web.notfound() cmdname = u"Delete Bonding Setting" cmd = BONDING_COMMAND_DELETE options = {} options['dev'] = device options["succession"] = None _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], cmd), options) _jobgroup = JobGroup(cmdname, karesansui.sheconf['env.uniqkey']) _job = Job('%s command' % cmdname, 0, _cmd) _jobgroup.jobs.append(_job) host = findbyhost1(self.orm, host_id) _machine2jobgroup = m2j_new(machine=host, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=self.me, modified_user=self.me, ) save_job_collaboration(self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup, ) return web.accepted()
def GET(self, name=None): """ Get the static content of relative path "name" """ if not name: # If simply /index.py/ is specified, load the main front-end page: #name = 'softchordweb.html' # Modified version of PyJamas-generated softchordweb.html that has # the title changed to "softChord Web": name = 'softchordweb-custom.html' ext = name.split(".")[-1] # Figure out what is the type of this static file: cType = { "js":"application/x-javascript", "txt":"text/plain", "css":"text/css", "html":"text/html", "png":"images/png", "jpg":"image/jpeg", "gif":"image/gif", "ico":"image/x-icon" } # If this file is in static/output, then it's a PyJamas-generated file: if name in os.listdir('static/output'): path = 'static/output/%s' % name #return "ATTEMPTING TO READ: %s" % path web.header("Content-Type", cType[ext]) return open(path, "rb").read() else: # If the static contents was not found web.notfound() return "NOT FOUND: %s" % name
def _locate(self, id): """ See what we know about this uri... uri is in fact a SHA-1 hash of the uri """ def ithas(key, cache): if key in cache: return cache[key] return None unknown = get_unknown() known = get_known() u = ithas(id, unknown) if u: web.notfound() return None u = ithas(id, known) if not u: u = get_manager().load(id) if not u: unknown[id] = True web.notfound() return None known[id] = u return u
def delete(self, uuid): """ ### Request `DELETE /birds/{id}` Empty body ### Response Valid status codes: - `200 OK` if the bird has been removed - `404 Not found` if the bird didn't exist Empty body expected. """ try: bird = Bird.objects.get(id=uuid) except mongoengine.DoesNotExist: raise web.notfound() except mongoengine.ValidationError: raise web.notfound() bird.delete()
def GET(self, catrel_name, arch_name, osrel_name, catalogname): """Get a srv4 reference by catalog ane catalogname.""" configuration.SetUpSqlobjectConnection() try: sqo_osrel, sqo_arch, sqo_catrel = pkgdb.GetSqoTriad( osrel_name, arch_name, catrel_name) except sqlobject.main.SQLObjectNotFound: raise web.notfound() join = [ sqlbuilder.INNERJOINOn(None, models.Srv4FileInCatalog, models.Srv4FileInCatalog.q.srv4file==models.Srv4FileStats.q.id), ] res = models.Srv4FileStats.select( sqlobject.AND( models.Srv4FileInCatalog.q.osrel==sqo_osrel, models.Srv4FileInCatalog.q.arch==sqo_arch, models.Srv4FileInCatalog.q.catrel==sqo_catrel, models.Srv4FileStats.q.catalogname==catalogname, models.Srv4FileStats.q.use_to_generate_catalogs==True), join=join, ) try: srv4 = res.getOne() mimetype, data = srv4.GetRestRepr() web.header('Content-type', mimetype) web.header('Access-Control-Allow-Origin', '*') return cjson.encode(data) except sqlobject.main.SQLObjectNotFound: raise web.notfound() except sqlobject.dberrors.OperationalError, e: raise web.internalerror(e)
def _GET(self, *param, **params): _prep_console() (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() dom = kvc.search_guests(domname)[0] document = XMLParse(dom.XMLDesc(1)) self.view.graphics_port = XMLXpath(document, '/domain/devices/graphics/@port') self.view.xenname = XMLXpath(document, '/domain/name/text()') finally: kvc.close() h_model = findbyhost1(self.orm, host_id) try: from karesansui.lib.utils import get_ifconfig_info device = KVM_BRIDGE_PREFIX + "0" self.view.host_ipaddr = get_ifconfig_info(device)[device]["ipaddr"] except: try: self.view.host_ipaddr = h_model.hostname.split(":")[0].strip() except: self.view.host_ipaddr = socket.gethostbyname(socket.gethostname()) return True
def GET(self, uri): dirs = uri.split('/') if dirs[0] in ['', 'js', 'css', 'img', 'favicon.ico']: filename = uri if dirs[0] == '': filename = 'index.html' try: with open(filename) as staticfile: filecontent = staticfile.read() files[filename] = filecontent return filecontent except IOError, e: if e.errno == 2: raise web.notfound() if files.has_key(filename): return files[filename] else: try: with open(filename) as staticfile: filecontent = staticfile.read() files[filename] = filecontent return filecontent except IOError, e: if e.errno == 2: raise web.notfound()
def GET(self, catrel_name, arch_name, osrel_name, catalogname): """Get a srv4 reference by catalog ane catalogname.""" web.header('Access-Control-Allow-Origin', '*') try: sqo_osrel, sqo_arch, sqo_catrel = models.GetSqoTriad( osrel_name, arch_name, catrel_name) except sqlobject.main.SQLObjectNotFound: raise web.notfound( cjson.encode({'message': 'catalog %s %s %s not found' % (osrel_name, arch_name, catrel_name)})) join = [ sqlbuilder.INNERJOINOn(None, models.Srv4FileInCatalog, models.Srv4FileInCatalog.q.srv4file==models.Srv4FileStats.q.id), ] res = models.Srv4FileStats.select( sqlobject.AND( models.Srv4FileInCatalog.q.osrel==sqo_osrel, models.Srv4FileInCatalog.q.arch==sqo_arch, models.Srv4FileInCatalog.q.catrel==sqo_catrel, models.Srv4FileStats.q.catalogname==catalogname, models.Srv4FileStats.q.use_to_generate_catalogs==True), join=join, ) try: srv4 = res.getOne() mimetype, data = srv4.GetRestRepr(quick=True) web.header('Content-type', mimetype) return cjson.encode(data) except sqlobject.main.SQLObjectNotFound: data = {'message': 'no %s %s %s %s packages found' % (catrel_name, arch_name, osrel_name, catalogname)} raise web.notfound(cjson.encode(data)) except sqlobject.dberrors.OperationalError as exc: raise web.internalerror(exc)
def _GET(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if is_int(param[2]) is False: return web.badrequest() disk_id = int(param[2]) model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() virt = kvc.search_kvg_guests(domname)[0] guest = MergeGuest(model, virt) self.view.guest = guest self.view.disk_info = virt.get_disk_info()[disk_id] finally: kvc.close() return True
def get_context(self, repo, sha): d = self.get_base_context(repo, sha, "") try: commit = self.repo.get_object(sha) except KeyError: raise web.notfound("No such commit") if commit.__class__.__name__ != "Commit": raise web.notfound("Not a valid commit") files = [] for change in git.get_changes(self.repo, commit): if change.type == 'delete': files.append((ACTION_ICONS.get('delete'), change.old.path, commit.parents[0], 'Deleted')) else: diff = git.unified_diff(self.repo, change.old, change.new) html = renderer.render_diff(diff) files.append((ACTION_ICONS.get(change.type, 'fire'), change.new.path, commit.id, html)) d['inline_style'] = renderer.get_style() d['files'] = files d['branch'] = commit.id d['commit'] = commit d['branch_name'] = commit.id[:10] return d
def GET(self): x = web.input(project=0) project = models.Project.get(id=x.project) if not project: raise web.notfound() if x.get('glyph'): if not models.Glyph.exists(name=x.get('glyph'), project_id=project.id): raise web.notfound() project.currentglyph = x.glyph web.ctx.orm.commit() masters = project.get_ordered_masters() masters_list = [] metapost = Metapost(project) for i, master in enumerate(masters): prepare_master_environment(master) glyphs = master.get_glyphs() glyphs = glyphs.filter(models.Glyph.name == project.currentglyph) if not metapost.execute_single(master, glyphs.first()): return web.badrequest() master_instancelog = project.get_instancelog(master.version, 'a') glyphsdata = get_glyph_info_from_log(master_instancelog, master=master) metalabel = get_metapolation_label(chr(models.LABELS[i])) masters_list.append({'glyphs': glyphsdata, 'label': chr(models.LABELS[i]), 'metapolation': metalabel, 'master_id': master.id}) glyphs = masters[0].get_glyphs() glyphs = glyphs.filter(models.Glyph.name == project.currentglyph) if not metapost.execute_interpolated_single(glyphs.first()): return web.badrequest() instancelog = project.get_instancelog(masters[0].version) metaglyphs = get_glyph_info_from_log(instancelog) import operator masters = map(operator.attrgetter('id', 'version'), models.Master.filter(project_id=project.id)) return ujson.dumps({'masters': masters_list, 'versions': project.get_versions(), 'metaglyphs': metaglyphs, 'mode': project.mfparser, 'project_id': project.id})
def get_course_and_check_rights(self, courseid, taskid=None, allow_all_staff=True): """ Returns the course with id ```courseid``` and the task with id ```taskid```, and verify the rights of the user. Raise web.notfound() when there is no such course of if the users has not enough rights. :param courseid: the course on which to check rights :param taskid: If not None, returns also the task with id ```taskid``` :param allow_all_staff: allow admins AND tutors to see the page. If false, all only admins. :returns (Course, Task) """ try: if self.user_manager.session_logged_in(): course = self.course_factory.get_course(courseid) if allow_all_staff: if not self.user_manager.has_staff_rights_on_course(course): raise web.notfound() else: if not self.user_manager.has_admin_rights_on_course(course): raise web.notfound() if taskid is None: return (course, None) else: return (course, course.get_task(taskid)) else: raise web.notfound() except: raise web.notfound()
def GET(self, deviceId, notificationId): """ Get single notification :: GET /_notifications/{deviceId}/{notificationId} Returns: :: { "uid": "e78599c4-758b-4c6e-87b1-daabaccff798", "timestamp": "2014-02-07 16:26:44", "metric": "e5511f295a474037a75bc966d02b67d2", "acknowledged": 0, "seen": 0, "windowsize": 3600, "device": "9a90eaf2-6374-4230-aa96-0830c0a737fe" } :param uid: Notification ID :type uid: str :param timestamp: Notification timestamp :type timestamp: timestamp :param metric: Metric that triggered notification :type metric: str :param acknowledged: Acknowledged status :type acknowledged: int :param seen: Seen status :type seen: int :param windowsize: Notification window in seconds during which no other notifications for a given instance should be sent to a given device :type windowsize: int :param device: Device ID :type device: str """ # Update the last_timestamp for the notification device. try: with web.ctx.connFactory() as conn: repository.updateNotificationDeviceTimestamp(conn, deviceId) except ObjectNotFoundError: return web.notfound("Notification device not found: %s" % deviceId) try: with web.ctx.connFactory() as conn: notificationRow = repository.getNotification(conn, notificationId, NotificationHandler.fields) notificationDict = dict([(col, notificationRow[col]) for col in NotificationHandler.fields]) if notificationRow["device"] != deviceId: return web.notfound("Device not found: %s" % deviceId) self.addStandardHeaders() return utils.jsonEncode(notificationDict) except ObjectNotFoundError: return web.notfound("Notification not found: %s" % notificationId)
def _GET(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() virt = kvc.search_kvg_guests(domname)[0] vcpus_info = virt.get_vcpus_info() self.view.max_vcpus_limit = kvc.get_max_vcpus() self.view.max_vcpus = vcpus_info['bootup_vcpus'] self.view.vcpus_limit = vcpus_info['max_vcpus'] self.view.vcpus = vcpus_info['vcpus'] self.view.cpuTime = virt.get_info()['cpuTime'] self.view.hypervisor = virt.get_info()['hypervisor'] self.view.guest = model finally: kvc.close() return True
def deleteModel(metricId): try: with web.ctx.connFactory() as conn: metricRow = repository.getMetric(conn, metricId) except app_exceptions.ObjectNotFoundError: raise web.notfound("ObjectNotFoundError Metric not found: Metric ID: %s" % metricId) if metricRow.datasource == "autostack": raise NotAllowedResponse( {"result": ("Not a standalone model=%s; datasource=%s. Unable" " to DELETE from this endpoint") % (metricId, metricRow.datasource,) }) log.debug("Deleting model for %s metric=%s", metricRow.datasource, metricId) with web.ctx.connFactory() as conn: repository.deleteModel(conn, metricId) # NOTE: this is the new way using datasource adapters try: createDatasourceAdapter(metricRow.datasource).unmonitorMetric(metricId) except app_exceptions.ObjectNotFoundError: raise web.notfound( "ObjectNotFoundError Metric not found: Metric ID: %s" % (metricId,)) return utils.jsonEncode({'result': 'success'})
def POST(self): import datetime from dateutil import relativedelta table = 'cloud_host' data = _web.input() try: uuid = data.uuid duration = data.duration except AttributeError: raise _web.internalerror() if duration: try: duration = int(duration) except: raise _web.notfound() else: raise _web.notfound() try: old_duration = db.select('cloud_host',where="`uuid`='%s'" % uuid).list()[0]['expired_time'] #如果已经存在duration if old_duration: old_year,old_month,old_day = map(int,(old_duration[:4],old_duration[4:6],old_duration[6:8])) old_date = datetime.date(year=old_year,month=old_month,day=old_day) next_month = old_date + relativedelta.relativedelta(months=duration) next_month = next_month.strftime("%Y%m%d") db.update(table,where="`uuid`='%s'" % uuid,enable=1,expired_time = next_month) return {'message':'success'} #如果无duration,设置enable=1,和expired_time else: nextmonth = datetime.date.today() + relativedelta.relativedelta(months=duration) nextmonth = nextmonth.strftime("%Y%m%d") db.update(table,where="`uuid`='%s'" % uuid,enable=1,expired_time = nextmonth) return {'message':'success'} except Exception,e: raise _web.notfound(message=e)
def GET(self, courseid, taskid, path): """ GET request """ if User.is_logged_in(): try: course = FrontendCourse(courseid) if not course.is_open_to_user(User.get_username()): return renderer.course_unavailable() task = course.get_task(taskid) if not task.is_visible_by_user(User.get_username()): return renderer.task_unavailable() path_norm = posixpath.normpath(urllib.unquote(path)) public_folder_path = os.path.normpath(os.path.realpath(os.path.join(INGIniousConfiguration["tasks_directory"], courseid, taskid, "public"))) file_path = os.path.normpath(os.path.realpath(os.path.join(public_folder_path, path_norm))) # Verify that we are still inside the public directory if os.path.normpath(os.path.commonprefix([public_folder_path, file_path])) != public_folder_path: raise web.notfound() if os.path.isfile(file_path): mimetypes.init() mime_type = mimetypes.guess_type(file_path) web.header('Content-Type', mime_type[0]) with open(file_path) as static_file: return static_file.read() else: raise web.notfound() except: if web.config.debug: raise else: raise web.notfound() else: return renderer.index(False)
def POST(self, courseid): """ GET request """ course, _ = self.get_course_and_check_rights(courseid) user_input = web.input(tasks=[], classrooms=[], users=[]) if "filter_type" not in user_input or "type" not in user_input or "format" not in user_input or user_input.format not in self.valid_formats: raise web.notfound() tasks = course.get_tasks().keys() for i in user_input.tasks: if i not in tasks: raise web.notfound() if user_input.filter_type == "users": self._validate_list(user_input.users) classrooms = list(self.database.classrooms.find({"courseid": courseid, "students": {"$in": user_input.users}})) else: self._validate_list(user_input.classrooms) classrooms = list(self.database.classrooms.find({"_id": {"$in": [ObjectId(cid) for cid in user_input.classrooms]}})) classrooms = dict([(username, classroom) for classroom in classrooms for username in classroom["students"]]) submissions = list(self.database.submissions.find({"username": {"$in": classrooms.keys()}, "taskid": {"$in": user_input.tasks}, "courseid": course.get_id(), "status": {"$in": ["done", "error"]}})) if user_input.type == "single": submissions = self.submission_manager.keep_best_submission(submissions) web.header('Content-Type', 'application/x-gzip', unique=True) web.header('Content-Disposition', 'attachment; filename="submissions.tgz"', unique=True) return self.submission_manager.get_submission_archive(submissions, list(reversed(user_input.format.split('/'))), classrooms)
def GET(self, table, what): try: table = table_map[table] except KeyError: raise web.notfound() if not r_safeproperty.match(what): raise web.notfound() #if `what` is not there in the `table` (provide available options rather than 404???) try: maxnum = float(db.select(table, what='max(%s) as m' % what, vars=locals())[0].m) except: raise web.notfound() items = db.select(table, what="*, 100*(%s/$maxnum) as pct" % what, order='%s desc' % what, where='%s is not null' % what, vars=locals()).list() for item in items: if table == 'district': item.id = 'd' + item.name item.path = '/us/' + item.name.lower() elif table == 'politician': state = '-'+item.district_id.split('-')[0] if item.district_id else '' item.name = '%s %s (%s%s)' % (item.firstname, item.lastname, (item.party or 'I')[0], state) item.path = '/p/' + item.id return render.dproperty(items, what, namesmap().get(what))
def _GET(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() network_name = param[1] if not (network_name and host_id): return web.badrequest() kvc = KaresansuiVirtConnection() try: try: network = kvc.search_kvn_networks(network_name)[0] # throws KaresansuiVirtException info = network.get_info() except KaresansuiVirtException, e: # network not found self.logger.debug("Network not found. name=%s" % network_name) return web.notfound() finally: kvc.close() cidr = '%s/%s' % (info['ip']['address'], info['ip']['netmask']) network = dict(name=info['name'], cidr=cidr, dhcp_start=info['dhcp']['start'], dhcp_end=info['dhcp']['end'], forward_dev=info['forward']['dev'], forward_mode=info['forward']['mode'], bridge=info['bridge']['name'], ) self.view.info = info self.view.network = network return True
def GET(self): try: f = open("../www/index.html") return f.read() except IOError: web.notfound()
def POST(self, autostackId, data=None): # pylint: disable=C0103,R0201 """ Create one or more Autostack Metric(s) :: POST /_autostacks/{autostackId}/metrics [ { "namespace": "AWS/EC2", "metric": "CPUUtilization" }, ... ] Request body is a list of items, each of which are a subset of the standard cloudwatch native metric, specifying only: :param namespace: AWS Namespace :type namespace: str :param metric: AWS Metric name :type str: `datasource`, `region`, and `dimensions` normally required when creating models are not necessary. """ try: self.addStandardHeaders() with web.ctx.connFactory() as conn: autostackRow = repository.getAutostack(conn, autostackId) data = data or utils.jsonDecode(web.data()) for nativeMetric in data: try: if nativeMetric["namespace"] == "Autostacks": slaveDatasource = "autostack" else: slaveDatasource = "cloudwatch" # only support cloudwatch for now modelParams = {} if "min" and "max" in nativeMetric: modelParams["min"] = nativeMetric["min"] modelParams["max"] = nativeMetric["max"] modelSpec = { "datasource": "autostack", "metricSpec": { "autostackId": autostackRow.uid, "slaveDatasource": slaveDatasource, "slaveMetric": nativeMetric }, "modelParams": modelParams } metricId = (createAutostackDatasourceAdapter(). monitorMetric(modelSpec)) with web.ctx.connFactory() as conn: metricRow = repository.getMetric(conn, metricId) metricDict = convertMetricRowToMetricDict(metricRow) except KeyError: raise web.badrequest("Missing details in request") except ValueError: response = {"result": "failure"} raise web.badrequest(utils.jsonEncode(response)) response = {"result": "success", "metric": metricDict} raise web.created(utils.jsonEncode(response)) except ObjectNotFoundError: raise web.notfound("Autostack not found: Autostack ID: %s" % autostackId) except (web.HTTPError) as ex: if bool(re.match(r"([45][0-9][0-9])\s?", web.ctx.status)): # Log 400-599 status codes as errors, ignoring 200-399 log.error(str(ex) or repr(ex)) raise except Exception as ex: log.exception("POST Failed") raise web.internalerror(str(ex) or repr(ex))
def GET(self, autostackId=None): # pylint: disable=C0103 """ Get instances for known Autostack: :: GET /_autostacks/{autostackId}/instances Preview Autostack instances: :: GET /_autostacks/preview_instances?region={region}&filters={filters} :param region: AWS Region Name :type region: str :param filters: AWS Tag value pattern :type value: str (JSON object) Example query params: :: region=us-west-2&filters={"tag:Name":["jenkins-master"]} :return: List of instance details. See AutostackInstancesHandler.formatInstance() for implementation. Example return value: :: [ { "instanceID": "i-12345678", "state": "stopped", "regionName": "us-west-2", "instanceType": "m1.medium", "launchTime": "2013-09-24T02:02:48Z", "tags": { "Type": "Jenkins", "Description": "Jenkins Master", "Name": "jenkins-master" } }, { "instanceID": "i-12345678", "state": "running", "regionName": "us-west-2", "instanceType": "m1.large", "launchTime": "2013-12-19T12:02:31Z", "tags": { "Type": "Jenkins", "Name": "jenkins-master", "Description": "Jenkin Master(Python 2.7)" } } ] """ self.addStandardHeaders() aggSpec = { "datasource": "cloudwatch", # only support EC2 for now "region": None, # to be filled below "resourceType": "AWS::EC2::Instance", # only support EC2 for now "filters": None # to be filled below } adapter = createCloudwatchDatasourceAdapter() if autostackId is not None: try: with web.ctx.connFactory() as conn: autostackRow = repository.getAutostack(conn, autostackId) except ObjectNotFoundError: raise web.notfound("Autostack not found: Autostack ID: %s" % autostackId) except web.HTTPError as ex: if bool(re.match(r"([45][0-9][0-9])\s?", web.ctx.status)): # Log 400-599 status codes as errors, ignoring 200-399 log.error(str(ex) or repr(ex)) raise except Exception as ex: raise web.internalerror(str(ex) or repr(ex)) aggSpec["region"] = autostackRow.region aggSpec["filters"] = autostackRow.filters result = adapter.getMatchingResources(aggSpec) else: data = web.input(region=None, filters=None) if not data.region: raise InvalidRequestResponse({"result": "Invalid region"}) if not data.filters: raise InvalidRequestResponse({"result": "Invalid filters"}) try: aggSpec["region"] = data.region aggSpec["filters"] = utils.jsonDecode(data.filters) result = adapter.getMatchingResources(aggSpec) except boto.exception.EC2ResponseError as responseError: raise InvalidRequestResponse({"result": responseError.message}) if result: return utils.jsonEncode( [self.formatInstance(instance) for instance in result]) return utils.jsonEncode([])
def get_app(config): """ :param config: the configuration dict :return: A new app """ # First, disable debug. It will be enabled in the configuration, later. web.config.debug = False config = _put_configuration_defaults(config) mongo_client = MongoClient(host=config.get('mongo_opt', {}).get('host', 'localhost')) database = mongo_client[config.get('mongo_opt', {}).get('database', 'INGInious')] gridfs = GridFS(database) # Init database if needed db_version = database.db_version.find_one({}) if db_version is None: database.submissions.ensure_index([("username", pymongo.ASCENDING)]) database.submissions.ensure_index([("courseid", pymongo.ASCENDING)]) database.submissions.ensure_index([("courseid", pymongo.ASCENDING), ("taskid", pymongo.ASCENDING)]) database.submissions.ensure_index([("submitted_on", pymongo.DESCENDING)]) # sort speed database.user_tasks.ensure_index( [("username", pymongo.ASCENDING), ("courseid", pymongo.ASCENDING), ("taskid", pymongo.ASCENDING)], unique=True) database.user_tasks.ensure_index([("username", pymongo.ASCENDING), ("courseid", pymongo.ASCENDING)]) database.user_tasks.ensure_index([("courseid", pymongo.ASCENDING), ("taskid", pymongo.ASCENDING)]) database.user_tasks.ensure_index([("courseid", pymongo.ASCENDING)]) database.user_tasks.ensure_index([("username", pymongo.ASCENDING)]) appli = CookieLessCompatibleApplication(MongoStore(database, 'sessions')) # Init gettext available_translations = { "fr": "Français", "es": "Español" } available_languages = {"en": "English"} available_languages.update(available_translations) appli.add_translation("en", gettext.NullTranslations()) # English does not need translation ;-) for lang in available_translations.keys(): appli.add_translation(lang, gettext.translation('messages', get_root_path() + '/frontend/i18n', [lang])) builtins.__dict__['_'] = appli.gettext if config.get("maintenance", False): template_helper = TemplateHelper(PluginManager(), None, 'frontend/templates', 'frontend/templates/layout', 'frontend/templates/layout_lti', config.get('use_minified_js', True)) template_helper.add_to_template_globals("get_homepath", appli.get_homepath) template_helper.add_to_template_globals("_", _) appli.template_helper = template_helper appli.init_mapping(urls_maintenance) return appli.wsgifunc(), appli.stop default_allowed_file_extensions = config['allowed_file_extensions'] default_max_file_size = config['max_file_size'] zmq_context, __ = start_asyncio_and_zmq(config.get('debug_asyncio', False)) # Init the different parts of the app plugin_manager = PluginManager() # Create the FS provider if "fs" in config: fs_provider = filesystem_from_config_dict(config["fs"]) else: task_directory = config["tasks_directory"] fs_provider = LocalFSProvider(task_directory) default_problem_types = { problem_type.get_type(): problem_type for problem_type in [DisplayableCodeProblem, DisplayableCodeSingleLineProblem, DisplayableFileProblem, DisplayableMultipleChoiceProblem, DisplayableMatchProblem] } course_factory, task_factory = create_factories(fs_provider, default_problem_types, plugin_manager, WebAppCourse, WebAppTask) user_manager = UserManager(appli.get_session(), database, config.get('superadmins', [])) update_pending_jobs(database) client = create_arch(config, fs_provider, zmq_context, course_factory) lti_outcome_manager = LTIOutcomeManager(database, user_manager, course_factory) submission_manager = WebAppSubmissionManager(client, user_manager, database, gridfs, plugin_manager, lti_outcome_manager) template_helper = TemplateHelper(plugin_manager, user_manager, 'frontend/templates', 'frontend/templates/layout', 'frontend/templates/layout_lti', config.get('use_minified_js', True)) # Init web mail smtp_conf = config.get('smtp', None) if smtp_conf is not None: web.config.smtp_server = smtp_conf["host"] web.config.smtp_port = int(smtp_conf["port"]) web.config.smtp_starttls = bool(smtp_conf.get("starttls", False)) web.config.smtp_username = smtp_conf.get("username", "") web.config.smtp_password = smtp_conf.get("password", "") web.config.smtp_sendername = smtp_conf.get("sendername", "*****@*****.**") # Add some helpers for the templates template_helper.add_to_template_globals("_", _) template_helper.add_to_template_globals("str", str) template_helper.add_to_template_globals("available_languages", available_languages) template_helper.add_to_template_globals("get_homepath", appli.get_homepath) template_helper.add_to_template_globals("allow_registration", config.get("allow_registration", True)) template_helper.add_to_template_globals("sentry_io_url", config.get("sentry_io_url")) template_helper.add_to_template_globals("user_manager", user_manager) template_helper.add_to_template_globals("default_allowed_file_extensions", default_allowed_file_extensions) template_helper.add_to_template_globals("default_max_file_size", default_max_file_size) template_helper.add_other("course_admin_menu", lambda course, current: course_admin_utils.get_menu(course, current, template_helper.get_renderer(False), plugin_manager, user_manager)) template_helper.add_other("preferences_menu", lambda current: preferences_utils.get_menu(appli, current, template_helper.get_renderer(False), plugin_manager, user_manager)) # Not found page appli.notfound = lambda: web.notfound(template_helper.get_renderer().notfound('Page not found')) # Enable stacktrace display if needed web_debug = config.get('web_debug', False) appli.internalerror = internalerror_generator(template_helper.get_renderer(False)) if web_debug is True: web.config.debug = True appli.internalerror = debugerror elif isinstance(web_debug, str): web.config.debug = False appli.internalerror = emailerrors(web_debug, appli.internalerror) # Insert the needed singletons into the application, to allow pages to call them appli.plugin_manager = plugin_manager appli.course_factory = course_factory appli.task_factory = task_factory appli.submission_manager = submission_manager appli.user_manager = user_manager appli.template_helper = template_helper appli.database = database appli.gridfs = gridfs appli.client = client appli.default_allowed_file_extensions = default_allowed_file_extensions appli.default_max_file_size = default_max_file_size appli.backup_dir = config.get("backup_directory", './backup') appli.webterm_link = config.get("webterm", None) appli.lti_outcome_manager = lti_outcome_manager appli.allow_registration = config.get("allow_registration", True) appli.allow_deletion = config.get("allow_deletion", True) appli.available_languages = available_languages appli.welcome_page = config.get("welcome_page", None) appli.static_directory = config.get("static_directory", "./static") appli.webdav_host = config.get("webdav_host", None) # Init the mapping of the app appli.init_mapping(urls) # Loads plugins plugin_manager.load(client, appli, course_factory, task_factory, database, user_manager, submission_manager, config.get("plugins", [])) # Start the inginious.backend client.start() return appli.wsgifunc(), lambda: _close_app(appli, mongo_client, client)
def default_error(msg=''): raise web.notfound()
def GET(self, username): account = accounts.find(username=username) if not account: raise web.notfound() else: return render_template("admin/people/edits", account)
def GET(self, sitename, path): if path == '_query': return self.GET_query(sitename) else: raise web.notfound("")
def POST(self, key): """Called when the user wants to borrow the edition""" i = web.input(action='borrow', format=None, ol_host=None, _autoReadAloud=None, q="") if i.ol_host: ol_host = i.ol_host else: ol_host = 'openlibrary.org' edition = web.ctx.site.get(key) if not edition: raise web.notfound() # Make a call to availability v2 update the subjects according # to result if `open`, redirect to bookreader response = lending.get_availability_of_ocaid(edition.ocaid) availability = response[edition.ocaid] if response else {} archive_url = 'https://archive.org/stream/' + edition.ocaid + '?ref=ol' if i._autoReadAloud is not None: archive_url += '&_autoReadAloud=show' if i.q: _q = urllib.parse.quote(i.q, safe='') archive_url += "#page/-/mode/2up/search/%s" % _q if availability and availability['status'] == 'open': raise web.seeother(archive_url) error_redirect = (archive_url) user = accounts.get_current_user() if user: account = OpenLibraryAccount.get_by_email(user.email) ia_itemname = account.itemname if account else None if not user or not ia_itemname: web.setcookie(config.login_cookie_name, "", expires=-1) redirect_url = "/account/login?redirect=%s/borrow?action=%s" % ( edition.url(), i.action) if i._autoReadAloud is not None: redirect_url += '&_autoReadAloud=' + i._autoReadAloud raise web.seeother(redirect_url) action = i.action # Intercept a 'borrow' action if the user has already # borrowed the book and convert to a 'read' action. # Added so that direct bookreader links being routed through # here can use a single action of 'borrow', regardless of # whether the book has been checked out or not. if action == 'borrow' and user.has_borrowed(edition): action = 'read' bookPath = '/stream/' + edition.ocaid if i._autoReadAloud is not None: bookPath += '?_autoReadAloud=show' if action == 'borrow': resource_type = i.format or 'bookreader' if resource_type not in ['epub', 'pdf', 'bookreader']: raise web.seeother(error_redirect) user_meets_borrow_criteria = user_can_borrow_edition( user, edition, resource_type) if user_meets_borrow_criteria: # This must be called before the loan is initiated, # otherwise the user's waitlist status will be cleared # upon loan creation track_loan = False if is_users_turn_to_borrow( user, edition) else True loan = lending.create_loan(identifier=edition.ocaid, resource_type=resource_type, user_key=ia_itemname, book_key=key) if loan: loan_link = loan['loan_link'] if resource_type == 'bookreader': if track_loan: # As of 2017-12-14, Petabox will be # responsible for tracking borrows which # are the result of waitlist redemptions, # so we don't want to track them here to # avoid double accounting. When a reader # is at the head of a waitlist and goes to # claim their loan, Petabox now checks # whether the waitlist was initiated from # OL, and if it was, petabox tracks # ol.loans.bookreader accordingly via # lending.create_loan. stats.increment('ol.loans.bookreader') raise web.seeother( make_bookreader_auth_link(loan.get_key(), edition.ocaid, bookPath, ol_host, ia_userid=ia_itemname)) elif resource_type == 'pdf': stats.increment('ol.loans.pdf') raise web.seeother(loan_link) elif resource_type == 'epub': stats.increment('ol.loans.epub') raise web.seeother(loan_link) else: raise web.seeother(error_redirect) else: raise web.seeother(error_redirect) elif action == 'return': # Check that this user has the loan user.update_loan_status() loans = get_loans(user) # We pick the first loan that the user has for this book that is returnable. # Assumes a user can't borrow multiple formats (resource_type) of the same book. user_loan = None for loan in loans: # Handle the case of multiple edition records for the same # ocaid and the user borrowed from one and returning from another has_loan = (loan['book'] == edition.key or loan['ocaid'] == edition.ocaid) if has_loan: user_loan = loan break if not user_loan: # $$$ add error message raise web.seeother(error_redirect) user_loan.return_loan() # Show the page with "you've returned this". Use a dummy slug. # $$$ this would do better in a session variable that can be cleared # after the message is shown once raise web.seeother(edition.url()) elif action == 'read': # Look for loans for this book user.update_loan_status() loans = get_loans(user) for loan in loans: if loan['book'] == edition.key: raise web.seeother( make_bookreader_auth_link(loan['_key'], edition.ocaid, bookPath, ol_host, ia_userid=ia_itemname)) elif action == 'join-waitinglist': return self.POST_join_waitinglist(edition, user) elif action == 'leave-waitinglist': return self.POST_leave_waitinglist(edition, user, i) # Action not recognized raise web.seeother(error_redirect)
def GET(self): raise web.notfound()
def notfound(): username = session.username return web.notfound(render.head(username,render.error()))
def POST(self, uri): if uri == 'api': try: print web.data() json_data = json.loads(web.data()) except Exception, e: traceback.print_exc() raise web.badrequest() try: return json.dumps(API().serve(json_data)) except Exception, e: traceback.print_exc() raise web.internalerror(e.message) else: raise web.notfound() def GET(self, uri): dirs = uri.split('/') if dirs[0] in [ '', 'js', 'css', 'img', 'donate', 'index2.html', 'favicon.ico' ]: filename = uri if dirs[0] == '': filename = 'index.html' try: with open(filename) as staticfile: filecontent = staticfile.read() files[filename] = filecontent return filecontent except IOError, e:
def GET_AUTH(self, courseid, scoreboardid): # pylint: disable=arguments-differ """ GET request """ course = self.course_factory.get_course(courseid) scoreboards = course.get_descriptor().get('scoreboard', []) try: scoreboardid = int(scoreboardid) scoreboard_name = scoreboards[scoreboardid]["name"] scoreboard_content = scoreboards[scoreboardid]["content"] scoreboard_reverse = bool(scoreboards[scoreboardid].get( 'reverse', False)) except: raise web.notfound() # Convert scoreboard_content if isinstance(scoreboard_content, str): scoreboard_content = OrderedDict([(scoreboard_content, 1)]) if isinstance(scoreboard_content, list): scoreboard_content = OrderedDict([(entry, 1) for entry in scoreboard_content]) if not isinstance(scoreboard_content, OrderedDict): scoreboard_content = OrderedDict(iter(scoreboard_content.items())) # Get task names task_names = {} for taskid in scoreboard_content: try: task_names[taskid] = course.get_task(taskid).get_name() except: raise web.notfound("Unknown task id " + taskid) # Get all submissions results = self.database.submissions.find( { "courseid": courseid, "taskid": { "$in": list(scoreboard_content.keys()) }, "custom.score": { "$exists": True }, "result": "success" }, ["taskid", "username", "custom.score"]) # Get best results per users(/group) result_per_user = {} users = set() for submission in results: # Be sure we have a list if not isinstance(submission["username"], list): submission["username"] = [submission["username"]] submission["username"] = tuple(submission["username"]) if submission["username"] not in result_per_user: result_per_user[submission["username"]] = {} if submission["taskid"] not in result_per_user[ submission["username"]]: result_per_user[submission["username"]][ submission["taskid"]] = submission["custom"]["score"] else: # keep best score current_score = result_per_user[submission["username"]][ submission["taskid"]] new_score = submission["custom"]["score"] task_reversed = scoreboard_reverse != ( scoreboard_content[submission["taskid"]] < 0) if task_reversed and current_score > new_score: result_per_user[submission["username"]][ submission["taskid"]] = new_score elif not task_reversed and current_score < new_score: result_per_user[submission["username"]][ submission["taskid"]] = new_score for user in submission["username"]: users.add(user) # Get user names users_realname = {} for username, userinfo in self.user_manager.get_users_info( users).items(): users_realname[username] = userinfo[0] if userinfo else username # Compute overall result per user, and sort them overall_result_per_user = {} for key, val in result_per_user.items(): total = 0 solved = 0 for taskid, coef in scoreboard_content.items(): if taskid in val: total += val[taskid] * coef solved += 1 overall_result_per_user[key] = {"total": total, "solved": solved} sorted_users = list(overall_result_per_user.keys()) sorted_users = sorted(sorted_users, key=sort_func(overall_result_per_user, scoreboard_reverse)) # Compute table table = [] # Header if len(scoreboard_content) == 1: header = ["", "Student(s)", "Score"] emphasized_columns = [2] else: header = ["", "Student(s)", "Solved", "Total score"] + [ task_names[taskid] for taskid in list(scoreboard_content.keys()) ] emphasized_columns = [2, 3] # Lines old_score = () rank = 0 for user in sorted_users: # Increment rank if needed, and display it line = [] if old_score != (overall_result_per_user[user]["solved"], overall_result_per_user[user]["total"]): rank += 1 old_score = (overall_result_per_user[user]["solved"], overall_result_per_user[user]["total"]) line.append(rank) else: line.append("") # Users line.append(",".join(sorted([users_realname[u] for u in user]))) if len(scoreboard_content) == 1: line.append(overall_result_per_user[user]["total"]) else: line.append(overall_result_per_user[user]["solved"]) line.append(overall_result_per_user[user]["total"]) for taskid in scoreboard_content: line.append(result_per_user[user].get(taskid, "")) table.append(line) renderer = self.template_helper.get_custom_renderer( 'frontend/webapp/plugins/scoreboard') return renderer.scoreboard(course, scoreboardid, scoreboard_name, header, table, emphasized_columns)
def page(self, course, username, task, submissionid): """ Get all data and display the page """ submission = self.submission_manager.get_submission( submissionid, False) if not submission or username not in submission["username"] or submission["courseid"] != course.get_id() or submission["taskid"] != \ task.get_id(): raise web.notfound() submission = self.submission_manager.get_input_from_submission( submission) submission = self.submission_manager.get_feedback_from_submission( submission, show_everything=True) to_display = [] for problem in task.get_problems(): if problem.get_id( ) in submission["input"]: # present in input and in task data = { "id": problem.get_id(), "name": problem.get_name(), "defined": True, "present": True, "context": problem.get_header(), "content": None, "language": "plain", "feedback": submission.get("problems", {}).get(problem.get_id(), None), "base64": None, "mime": "text/plain" } if isinstance(problem, DisplayableCodeFileProblem): extension = path.splitext( submission["input"][problem.get_id()]["filename"])[1] try: if extension in [".zip", ".pdf", ".tgz"]: data["language"] = extension[1:] data["mime"] = "application/" + extension[1:] data["content"] = base64.b64decode(submission["input"][ problem.get_id()]["value"]).encode('utf8') except: data["content"] = None data["base64"] = submission["input"][ problem.get_id()]["value"] elif isinstance(problem, DisplayableMultipleChoiceProblem): data["content"] = "Multiple choice question: \n" chosen = submission["input"][problem.get_id()] if not isinstance(chosen, list): chosen = [chosen] for c in chosen: choice = problem.get_choice_with_index(int(c)) if choice is None: t = "unknown" m = "" else: t = "valid" if choice.get("valid", False) else "invalid" m = choice["text"] data["content"] += "\t- %s (%s): \n\t%s\n" % (c, t, m) data["base64"] = base64.b64encode( str(submission["input"][problem.get_id()]).encode( 'utf-8')).decode('utf-8') elif isinstance(submission["input"][problem.get_id()], str): data["content"] = submission["input"][problem.get_id()] data["base64"] = base64.b64encode( str(submission["input"][problem.get_id()]).encode( 'utf-8')).decode('utf-8') try: data["language"] = problem.get_original_content( )["language"] except: pass to_display.append(data) else: # not present in input, but present in task data = { "id": problem.get_id(), "name": problem.get_name(), "defined": True, "present": False, "context": problem.get_header(), "content": None, "language": "plain", "feedback": submission.get("problems", {}).get(problem.get_id(), None), "base64": None, "mime": "text/plain" } to_display.append(data) done_id = [d["id"] for d in to_display] for pid in submission["input"]: if pid not in done_id: data = { "id": pid, "name": pid, "defined": False, "present": True, "context": None, "content": None, "language": "plain", "feedback": submission.get("problems", {}).get(pid, None), "base64": None, "mime": "text/plain" } if isinstance(submission["input"][pid], dict): # file extension = path.splitext( submission["input"][pid]["filename"])[1] try: if extension in [".zip", ".pdf", ".tgz"]: data["language"] = extension[1:] data["mime"] = "application/" + extension[1:] data["content"] = base64.b64decode( submission["input"][pid]["value"]).decode('utf-8') except: data["content"] = None data["base64"] = submission["input"][pid]["value"] elif isinstance(submission["input"][pid], str): data["content"] = submission["input"][pid] data["base64"] = base64.b64encode( str(submission["input"][pid]).encode('utf-8')).decode( 'utf-8') to_display.append(data) return self.template_helper.get_renderer().course_admin.submission( course, username, task, submissionid, submission, to_display)
def notfound(): return web.notfound(render.notfound())
def GET(self, did=None): '''@params: did: (str)device id sampling: (datetime''' try: tanggal = to_date(web.input().get('sampling')) except: tanggal = datetime.datetime.today() conn = pg.connect(dbname="bsolo3", user="******", password="******") cursor = conn.cursor() if did: sql = "SELECT content FROM raw WHERE content->>'device' LIKE %s ORDER BY id DESC LIMIT 35" cursor.execute(sql, ('%/' + did + '/%', )) ''' regx = re.compile('.*'+did+'/', re.IGNORECASE) rst = [r for r in db.sensors.find({"device": regx}, {"_id": 0}).sort( "_id", -1).limit(25)] ''' rst = [r[0] for r in cursor.fetchall()] if not rst: return web.notfound() if web.input().get('sampling', None): #try: sampling = to_date(web.input().get('sampling')) _dari = time.mktime(sampling.timetuple()) _hingga = _dari + 86400 # satu hari = 86400 ms ''' rst = [r for r in db.sensors.find( {"$and": [{"device": regx}, {"sampling": {"$gte": _dari}}, {"sampling": {"$lt": _hingga}}]}, {_id: 0})] ''' sql = "SELECT content FROM raw WHERE content->>'device' LIKE %s AND (content->>'sampling')::int >= %s AND (content->>'sampling')::int <= %s" cursor.execute(sql, ('%/' + did + '/%', _dari, _hingga)) rst = [r[0] for r in cursor.fetchall()] if not rst: return "Tidak Ada Data Pada Tanggal " + web.input().get( 'sampling') rst.reverse() #except Exception as e: # print e out = {} if web.input().get('raw'): out['periodic'] = rst else: out['periodic'] = [map_periodic(r) for r in rst] out["bsolo_logger"] = BSOLO_LOGGER.get(did) else: out = [] sql = "SELECT DISTINCT(content->>'device') FROM raw" cursor.execute(sql) out = [r[0] for r in cursor.fetchall()] cursor.close() conn.close() #testing from asnan (data untuk kategori grafik) tinggal return untuk melihat data = [] kategori = [] battery = [] signal_quality = [] tick = [] are_tick = False distance = [] are_distance = False wl_scale = [] are_wl_scale = False sun_radiation_scale = [] wind_dir_scale = [] temperature_ambien_scale = [] humidity_ambien_scale = [] wind_speed_scale = [] are_klimatologi = False r = out["periodic"] r.reverse() for j in r: if "distance" in j: are_distance = True distance.append(j.get("distance")) if "wl_scale" in j: are_wl_scale = True wl_scale.append(j.get("wl_scale")) if "tick" in j: if "sun_radiation_scale" and "wind_dir_scale" and "temperature_ambien_scale" and "humidity_ambien_scale" and "wind_speed_scale" in j: are_klimatologi = True sun_radiation_scale.append(j.get("sun_radiation_scale")) wind_dir_scale.append(j.get("wind_dir_scale")) temperature_ambien_scale.append( j.get("temperature_ambien_scale")) humidity_ambien_scale.append( j.get("humidity_ambien_scale")) wind_speed_scale.append(j.get("wind_speed_scale")) tick.append(j.get("tick")) else: are_tick = True tick.append(j.get("tick")) kategori.append(j.get("sampling")) battery.append(j.get("battery")) signal_quality.append(j.get("signal_quality")) #end data.append({'name': 'signal_quality', 'data': signal_quality}) data.append({'name': 'battery', 'data': battery}) if are_distance == True: data.append({'name': 'distance', 'data': distance}) jenis_prima = "SONAR" if are_wl_scale == True: data.append({'name': 'wl_scale', 'data': wl_scale}) jenis_prima = "PRESSURE" if are_tick == True: data.append({'name': 'tick', 'data': tick}) jenis_prima = "ARR" if are_klimatologi == True: data.append({'name': 'tick', 'data': tick}) data.append({ 'name': 'sun_radiation_scale', 'data': sun_radiation_scale }) data.append({'name': 'wind_dir_scale', 'data': wind_dir_scale}) data.append({ 'name': 'temperature_ambien_scale', 'data': temperature_ambien_scale }) data.append({ 'name': 'humidity_ambien_scale', 'data': humidity_ambien_scale }) data.append({'name': 'wind_speed_scale', 'data': wind_speed_scale}) jenis_prima = "KLIMATOLOGI" conn = Agent._connection sql = "SELECT cname from agent where prima_id = %s" % ('"' + did + '"') result = conn.queryAll(sql) if result: pname = result[0][0] else: pname = "--" #print result return render.sensor.sensor_graph({ 'data': str(data), 'kategori': str(kategori), 'did': did, 'jenis_prima': jenis_prima, 'pname': pname, 'tanggal': tanggal })
def GET(self, feedid): feed = model.get_feed(feedid) if not feed: return web.notfound() changes = model.get_changes(feed['id']) return render_raw.rss(feed, changes)
def notfound(): """404请求处理器""" import xtemplate raise web.notfound(xtemplate.render("notfound.html", show_aside=False))
def notfound(): return web.notfound("{'error': 'Page not found'}")
def GET(self, key, name, fmt): lst = web.ctx.site.get(key) if lst is None: raise web.notfound() text = getattr(self, 'GET_' + name + '_' + fmt)(lst) return delegate.RawText(text)
def GET(self, op, id=None): if id is None: id = xutils.get_argument("id", "") name = xutils.get_argument("name", "") page = xutils.get_argument("page", 1, type=int) pagesize = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int) show_menu = xutils.get_argument("show_menu", "true") != "false" show_search = xutils.get_argument("show_search", "true") != "false" orderby = xutils.get_argument("orderby", None) is_iframe = xutils.get_argument("is_iframe", "false") token = xutils.get_argument("token", "") user_name = xauth.current_name() kw = Storage() kw.show_left = False kw.show_groups = False kw.show_aside = True kw.groups = [] kw.files = [] kw.op = op kw.user_name = user_name kw.page = page kw.orderby = orderby kw.pagesize = pagesize kw.recommended_notes = [] kw.show_add_file = False kw.template_name = "note/page/view.html" kw.search_action = "/note/timeline" kw.search_placeholder = "搜索笔记" if id == "0": raise web.found("/") # 回收站的笔记也能看到 file = find_note_for_view(token, id, name) if file is None: raise web.notfound() if token == "": check_auth(file, user_name) pathlist = NOTE_DAO.list_path(file) can_edit = (file.creator == user_name) or (user_name == "admin") role = xauth.get_current_role() # 定义一些变量 show_mdate = False recent_created = [] show_recommend = False next_note = None prev_note = None event_ctx = Storage(id=file.id, user_name=user_name) xmanager.fire("note.view", event_ctx) view_func = VIEW_FUNC_DICT.get(file.type, view_md_func) view_func(file, kw) if show_recommend and user_name is not None: # 推荐系统 handle_note_recommend(kw, file, user_name) if op == "edit": kw.show_aside = False kw.show_search = False kw.show_comment = False if is_iframe == "true": kw.show_menu = False kw.show_search = False template_name = kw['template_name'] del kw['template_name'] # 如果是页面,需要查出上级目录列表 handle_left_dir(kw, user_name, file, op) return xtemplate.render_by_ua( template_name, html_title=file.name, file=file, note_id=id, show_mdate=show_mdate, can_edit=can_edit, pathlist=pathlist, page_url="/note/view?id=%s&orderby=%s&page=" % (id, orderby), recent_created=recent_created, CREATE_BTN_TEXT_DICT=CREATE_BTN_TEXT_DICT, is_iframe=is_iframe, **kw)
def GET(self, name): if name in content_mapping: return render.blog_post(content_mapping[name]) else: raise web.notfound()
def GET(self, key): doc = web.ctx.site.get(key) if doc is None or doc.type.key != '/type/list': raise web.notfound() return render_template("type/list/embed", doc)
def GET_AUTH(self, courseid): # pylint: disable=arguments-differ course = self.course_factory.get_course(courseid) contest_data = get_contest_data(course) if not contest_data['enabled']: raise web.notfound() start = datetime.strptime(contest_data['start'], "%Y-%m-%d %H:%M:%S") end = datetime.strptime(contest_data['end'], "%Y-%m-%d %H:%M:%S") blackout = end - timedelta(hours=contest_data['blackout']) users = self.user_manager.get_course_registered_users(course) tasks = list(course.get_tasks().keys()) db_results = self.database.submissions.find( { "username": { "$in": users }, "courseid": courseid, "submitted_on": { "$gte": start, "$lt": blackout }, "status": "done" }, { "username": True, "_id": False, "taskid": True, "result": True, "submitted_on": True }).sort([("submitted_on", pymongo.ASCENDING)]) task_status = { taskid: { "status": "NA", "tries": 0 } for taskid in tasks } results = { username: { "name": self.user_manager.get_user_realname(username), "tasks": copy.deepcopy(task_status) } for username in users } activity = [] # Compute stats for each submission task_succeeded = {taskid: False for taskid in tasks} for submission in db_results: for username in submission["username"]: if submission['taskid'] not in tasks: continue if username not in users: continue status = results[username]["tasks"][submission['taskid']] if status["status"] == "AC" or status["status"] == "ACF": continue else: if submission['result'] == "success": if not task_succeeded[submission['taskid']]: status["status"] = "ACF" task_succeeded[submission['taskid']] = True else: status["status"] = "AC" status["tries"] += 1 status["time"] = submission['submitted_on'] status["score"] = ( submission['submitted_on'] + timedelta(minutes=contest_data["penalty"] * (status["tries"] - 1)) - start).total_seconds() / 60 elif submission['result'] == "failed" or submission[ 'result'] == "killed": status["status"] = "WA" status["tries"] += 1 elif submission['result'] == "timeout": status["status"] = "TLE" status["tries"] += 1 else: # other internal error continue activity.append({ "user": results[username]["name"], "when": submission['submitted_on'], "result": (status["status"] == 'AC' or status["status"] == 'ACF'), "taskid": submission['taskid'] }) activity.reverse() # Compute current score for user in results: score = [0, 0] for data in list(results[user]["tasks"].values()): if "score" in data: score[0] += 1 score[1] += data["score"] results[user]["score"] = tuple(score) # Sort everybody results = OrderedDict( sorted(list(results.items()), key=lambda t: (-t[1]["score"][0], t[1]["score"][1]))) # Compute ranking old = None current_rank = 0 for cid, user in enumerate(results.keys()): if results[user]["score"] != old: old = results[user]["score"] current_rank = cid + 1 results[user]["rank"] = current_rank results[user]["displayed_rank"] = str(current_rank) else: results[user]["rank"] = current_rank results[user]["displayed_rank"] = "" return self.template_helper.get_custom_renderer('frontend/webapp/plugins/contests').\ scoreboard(course, start, end, blackout, tasks, results, activity)
def notfound(): web.ctx.status = '404 Not Found' return web.notfound(str(render.__404()))
def POST(self, sitename, path): if path == '_save_many': return self.POST_save_many(sitename) else: raise web.notfound("")
def GET(self, changeid): change = model.get_change(changeid) if not change: return web.notfound() feed = model.get_feed(change['feedid']) return render.change(feed, change)
def _validate_list(self, usernames): """ Prevent MongoDB injections by verifying arrays sent to it """ for i in usernames: if not id_checker(i): raise web.notfound()
def GET(self, date): item = "ol_cdump_" + date if item not in get_ol_dumps(): raise web.notfound() else: raise web.found(download_url(item, item + ".txt.gz"))
def _parse_lti_data(self, courseid, taskid): """ Verify and parse the data for the LTI basic launch """ post_input = web.webapi.rawinput("POST") self.logger.debug('_parse_lti_data:' + str(post_input)) try: course = self.course_factory.get_course(courseid) except exceptions.CourseNotFoundException as ex: raise web.notfound(str(ex)) try: test = LTIWebPyToolProvider.from_webpy_request() validator = LTIValidator(self.database.nonce, course.lti_keys()) if test.launch_url.startswith('http:'): test.launch_url = 'https:' + test.launch_url[5:] verified = test.is_valid_request(validator) except Exception: self.logger.exception("...") self.logger.info("Error while validating LTI request for %s", str(post_input)) raise web.forbidden(_("Error while validating LTI request")) if verified: self.logger.debug('parse_lit_data for %s', str(post_input)) user_id = post_input["user_id"] roles = post_input.get("roles", "Student").split(",") realname = self._find_realname(post_input) email = post_input.get("lis_person_contact_email_primary", "") lis_outcome_service_url = post_input.get("lis_outcome_service_url", None) outcome_result_id = post_input.get("lis_result_sourcedid", None) consumer_key = post_input["oauth_consumer_key"] if course.lti_send_back_grade(): if lis_outcome_service_url is None or outcome_result_id is None: self.logger.info( 'Error: lis_outcome_service_url is None but lti_send_back_grade is True' ) raise web.forbidden( _("In order to send grade back to the TC, INGInious needs the parameters lis_outcome_service_url and " "lis_outcome_result_id in the LTI basic-launch-request. Please contact your administrator." )) else: lis_outcome_service_url = None outcome_result_id = None tool_name = post_input.get('tool_consumer_instance_name', 'N/A') tool_desc = post_input.get('tool_consumer_instance_description', 'N/A') tool_url = post_input.get('tool_consumer_instance_url', 'N/A') context_title = post_input.get('context_title', 'N/A') context_label = post_input.get('context_label', 'N/A') session_id = self.user_manager.create_lti_session( user_id, roles, realname, email, courseid, taskid, consumer_key, lis_outcome_service_url, outcome_result_id, tool_name, tool_desc, tool_url, context_title, context_label) loggedin = self.user_manager.attempt_lti_login() return session_id, loggedin else: self.logger.info("Couldn't validate LTI request") raise web.forbidden(_("Couldn't validate LTI request"))
def GET(self, path): data = fs.get(path) if data: sys.stdout.write(data) else: web.notfound()