def create_item(request): ampy = initAmpy(request) if ampy is None: return HTTPInternalServerError() try: body = request.json_body ampname = body["ampname"] longname = body["longname"] description = body["description"] if request.matched_route.name == "allsites": location = body["location"] elif request.matched_route.name == "allmeshes": public = body["public"] issource = body["issource"] except (ValueError, KeyError): return HTTPBadRequest(body=json.dumps({"error": "missing value"})) if re.search("[^.:/a-zA-Z0-9_-]", ampname) is not None: return HTTPBadRequest( body=json.dumps({"error": "bad characters in ampname"})) if request.matched_route.name == "allsites": result = ampy.add_amp_site(ampname, longname, location, description) url = request.route_url("onesite", name=escapeURIComponent(ampname)) label = "site" elif request.matched_route.name == "allmeshes": result = ampy.add_amp_mesh(ampname, longname, description, public, issource) url = request.route_url("onemesh", mesh=escapeURIComponent(ampname)) label = "mesh" else: return HTTPBadRequest() if result: return HTTPCreated(headers=[("Location", url)], body=json.dumps( {label: { "ampname": ampname, "url": url, }})) return HTTPBadRequest()
def login_view(request): next = request.params.get('next') or request.route_url('home') login = '' did_fail = False if 'submit' in request.POST: login = request.POST.get('login', '') passwd = request.POST.get('passwd', '') user = USERS.get(login, None) logging.debug("Connection attempt from : {}".format(login)) if user and user.check_password(passwd): headers = remember(request, login) return HTTPFound(location=next, headers=headers) did_fail = True return { 'login': login, 'next': next, 'failed_attempt': did_fail, 'users': USERS, }
def generate(self, path, request, **kw): if WIN: path = path.replace("\\", "/") for (url, spec, route_name) in self.registrations: if WIN: spec = spec.replace("\\", "/") if path.startswith(spec): subpath = path[len(spec):] if self.cache_busters: subpath, kw = self._bust_asset_path( request, spec, subpath, kw) if url is None: kw["subpath"] = subpath subdomains = request.registry.settings["subdomains"] return request.route_url( route_name, subdomain=subdomains[hash(subpath) % len(subdomains)], **kw ) else: subpath = urllib.parse.quote(subpath) return urllib.parse.urljoin(url, subpath[1:]) raise ValueError("No static URL definition matching {0!s}".format(path))
def create_schedule(request): ampy = initAmpy(request) if ampy is None: return HTTPInternalServerError() try: body = request.json_body except (ValueError, KeyError): return HTTPBadRequest(body=json.dumps({"error": "invalid body"})) # XXX who should verify that some of the other stuff makes sense? like only # http tests having zero destinations. or will that just not be an issue # as we can schedule tests with no targets perfectly ok. But maybe we want # to ensure all tests meet minumum/maximum number of targets (which amplet # client and ampweb/static/scripts/modals/schedule_modal.js knows) # XXX can we populate that javascript from a python file? for item in SCHEDULE_OPTIONS: if item not in body: return HTTPBadRequest(body=json.dumps( {"error": "Missing option '%s'" % item})) if not validate_args(body["test"], body["args"]): return HTTPBadRequest(body=json.dumps( {"error": "Bad arguments '%s'" % body["args"]})) schedule_id = ampy.schedule_new_amp_test(body) if schedule_id >= 0: url = request.route_url(request.matched_route.name[:-1], name=urllib.parse.unquote(request.matchdict["name"]), schedule_id=schedule_id) return HTTPCreated(headers=[("Location", url)], body=json.dumps({ "schedule": { "schedule_id": schedule_id, "url": url, }})) return HTTPBadRequest()
def photo_list_slash(request): raise HTTPMovedPermanently(request.route_url('photo_list'))