def post_createjob(self): p = web.input(name=None, nws=None, nc=None) name = p.name if not name: raise web.badrequest('job name is required') try: jobconfigs.create_job(name, nworksets=web.intget(p.nws), ncrawlers=web.intget(p.nc)) r = dict(p, success=1) except Exception, ex: r = dict(p, success=0, error=str(ex))
def process(self): self.content.seek(0, 0) env = { 'REMOTE_ADDR': self.client.host, 'REQUEST_METHOD': self.method, 'PATH_INFO': self.path, 'CONTENT_LENGTH': web.intget(self.getHeader('content-length'), 0), 'wsgi.input': self.content } if '?' in self.uri: env['QUERY_STRING'] = self.uri.split('?', 1)[1] for k, v in self.received_headers.iteritems(): env['HTTP_' + k.upper()] = v if self.path.startswith('/static/'): f = web.lstrips(self.path, '/static/') assert '/' not in f #@@@ big security hole self.write(file('static/' + f).read()) return self.finish() web.webapi._load(env) web.ctx.trequest = self result = self.actualfunc() self.setResponseCode(int(web.ctx.status.split()[0])) for (h, v) in web.ctx.headers: self.setHeader(h, v) self.write(web.ctx.output) if not web.ctx.get('persist'): self.finish()
def process(self): self.content.seek(0, 0) env = { "REMOTE_ADDR": self.client.host, "REQUEST_METHOD": self.method, "PATH_INFO": self.path, "CONTENT_LENGTH": web.intget(self.getHeader("content-length"), 0), "wsgi.input": self.content, } if "?" in self.uri: env["QUERY_STRING"] = self.uri.split("?", 1)[1] for k, v in self.received_headers.iteritems(): env["HTTP_" + k.upper()] = v if self.path.startswith("/static/"): f = web.lstrips(self.path, "/static/") assert "/" not in f # @@@ big security hole self.write(open("static/" + f).read()) return self.finish() web.webapi._load(env) web.ctx.trequest = self result = self.actualfunc() self.setResponseCode(int(web.ctx.status.split()[0])) for (h, v) in web.ctx.headers: self.setHeader(h, v) self.write(web.ctx.output) if not web.ctx.get("persist"): self.finish()
def GET(self, order_by='title', page_number='1'): if not order_by: order_by = '' page_number = web.intget(page_number, 0) m, has_next = modules.get_all_modules(order_by, offset=page_number * 50) next_page_url = '' if has_next: if not order_by: next_page_url = '/modules/%s/' % (page_number + 1) else: next_page_url = '/modules/by-%s/%s/' % (order_by, page_number + 1) sub_title = 'Page %s' % (page_number + 1) return render.layout(view.all_modules(m, modules.get_count(), order_by, next_page_url, sub_title=sub_title), title='All modules - %s - Google Modules' % sub_title, mode='modeAllModules')
def POST(self): i = web.input(context='', id=[], score=[]) score = web.intget(i.score[0] or i.score[1], '') if i.id and score: # applicants.rate(i.id, score, session.get_user_id()) votes.add(i.id, score, session.get_user_id()) raise web.seeother(web.ctx.environ['HTTP_REFERER'])
def enum_valueof(s, restrict): """ usage: enum : invalid (restrict is necessary) enum(4) : integer which value >= 0 and value <= 4 enum(1-4) : integer which value >= 1 and value <= 4 """ p, q = restrict.split('-', 2) if '-' in restrict else ('0', restrict) start_num = web.intget(p, '-1') range_to_num = web.intget(q, '-1') if start_num < 0 or range_to_num < 0: return None, {"code": 1, "message": "inner setting error"} value = web.intget(s, None) if value is None: return None, {"code": 1, "message": "wrong format"} if not start_num <= value <= range_to_num: return None, {"code": 1, "message": "wrong range"} return value, None
def load(): outdb = {} done = set() with db.transaction(): db.delete('earmark_sponsor', '1=1') db.delete('earmark', '1=1') for e in earmarks.parse_file(earmarks.EARMARK_FILE): de = dict(e) de['id'] = web.intget(de['id']) if not de['id'] or de['id'] in done: continue # missing the ID? come on! if isinstance(de['house_request'], basestring): continue # CLASSIFIED for k in de: de[k] = cleanrow(de[k]) for x in ['house_member', 'house_state', 'house_party', 'senate_member', 'senate_state', 'senate_party', 'district']: de.pop(x) de['recipient_stem'] = tools.stemcorpname(de['intended_recipient']) try: db.insert('earmark', seqname=False, **de) except: pprint(de) raise done.add(de['id']) reps_not_found = set() for e in earmarks.parse_file(earmarks.EARMARK_FILE): for rawRequest, chamber in zip([e.house_request, e.senate_request],[e.house_member, e.senate_member]): for rep in chamber: if rep.lower() not in lastname2rep: #@@ should work on improving quality reps_not_found.add(rep) else: rep = lastname2rep[rep.lower()] if e.id in done: try: db.insert('earmark_sponsor', seqname=False, earmark_id=e.id, politician_id=rep) except: print "Couldn't add %s as sponsor to earmark %d" %(rep, e.id) outdb.setdefault(rep, { 'amt_earmark_requested': 0, 'n_earmark_requested': 0, 'n_earmark_received': 0, 'amt_earmark_received': 0 }) outdb[rep]['n_earmark_requested'] += 1 requested = rawRequest or e.final_amt if not isinstance(requested, float): requested = e.final_amt if requested: outdb[rep]['amt_earmark_requested'] += requested if isinstance(e.final_amt, float) and e.final_amt: outdb[rep]['n_earmark_received'] += 1 outdb[rep]['amt_earmark_received'] += e.final_amt print "Did not find",len(reps_not_found),"reps:", pformat(reps_not_found) for rep, d in outdb.iteritems(): db.update('politician', where='id=$rep', vars=locals(), **d)
def __str_valueof(s, restrict=''): """ usage: s : no restrict s(10) : string which length <= 10 s(7-11) : string which length >= 7 and length <= 10 """ if not restrict: return s, None p, q = restrict.split('-', 2) if '-' in restrict else ('0', restrict) start_num = web.intget(p, '-1') range_to_num = web.intget(q, '-1') if start_num < 0 or range_to_num < 0: return None, {"code": 1, "message": "inner setting error"} if len(s) > range_to_num: return None, {"code": 1, "message": "too long"} if len(s) < start_num: return None, {"code": 1, "message": "too short"} return s, None
def do_feed(self, job): p = web.input(n=5, name=None) # TODO: name will be a string name = web.intget(p.name, -1) if name < 0: return [] count = max(web.intget(p.n, 0), 0) start = time.time() # return an JSON array of objects with properties: # uri, path, via, context and data r = hq.get_job(job).feed(name, count) t = time.time() - start if t > 1.0 and len(r) > 0: logging.warn("slow feed %s:%s %s, %.4fs", job, name, len(r), t) else: logging.debug("feed %s %s:%s, %.4fs", job, name, len(r), t) return r
def GET(self): i = web.input(page=1, display=None, problem=None, author=None) page = min(1, web.intget(i.page, 1)) offset = (page-1) * REPORTS_PER_PAGE limit = REPORTS_PER_PAGE where = self.prepare_where(i) rowcount = db.select('reports', what='count(*) as count', where=where, vars=i)[0].count npages = int(rowcount/REPORTS_PER_PAGE) + 1 # approx rows = db.select('reports', where=where, limit=limit, offset=offset, order="time desc", vars=i) rows = [self.process_row(row) for row in rows] status = any(not row.resolved for row in rows) return render.index(rows, status, page, npages, queryparams=web.input())
def GET(self, order_by='name', page_number= 0): if not order_by: order_by = '' page_number = web.intget(page_number, 0) m, has_next = functions.getAllFunctions(order_by, limit=config.listLimit,offset=page_number*config.listLimit) next_page_url = '' if has_next: if not order_by: next_page_url = '/list/page/%s/' % (page_number + 1) else: next_page_url = '/list/by-%s/%s/' % (order_by, page_number + 1) sub_title = 'Page %s' % (page_number + 1) return render.layout( view.all_functions(m, functions.getCount(), order_by, next_page_url, sub_title=sub_title), title = ('All functions - %s - '+projectName) % sub_title, mode = 'modeAllModules')
def GET(self, order_by='title', page_number='1'): if not order_by: order_by = '' page_number = web.intget(page_number, 0) m, has_next = modules.get_all_modules(order_by, offset=page_number*50) next_page_url = '' if has_next: if not order_by: next_page_url = '/modules/%s/' % (page_number + 1) else: next_page_url = '/modules/by-%s/%s/' % (order_by, page_number + 1) sub_title = 'Page %s' % (page_number + 1) return render.layout( view.all_modules(m, modules.get_count(), order_by, next_page_url, sub_title=sub_title), title = 'All modules - %s - Google Modules' % sub_title, mode = 'modeAllModules')
def new_category(model, name, slug, parent_id=0, description=''): category_setting = get_category_setting(model) if category_setting is None: return False if category_setting['hierarchical'] is False: parent_id = 0 parent_id = web.intget(parent_id, 0) values = { 'name': name, 'slug': slug, 'parent_id': parent_id, 'model': model, 'description': description, 'count': 0 } return db.insert('categories', **values)
def POST(self, id): form = post_form() data = web.input(category_ids=[]) if 'slug' in data and data['slug'].strip(): data['slug'] = data['slug'].strip() form.slug.validators = (validator.unique('posts', 'slug'), ) if 'category_ids' in data: data['category_ids'] = [web.intget(category_id, 0) for category_id in data['category_ids']] if not form.validates(data): return render.post_edit(form) data['user_id'] = session.get_user_id() if post_model.update_post(id=id, **data): session.set_flash('编辑文章成功', 'success') else: session.set_flash('编辑文章失败', 'error') raise web.seeother('/post/edit/%d', id)
def update_category(model, id=None, where=None, vars=None, **values): category_setting = get_category_setting(model) if category_setting is None: return False fields = ['name', 'slug', 'parent_id', 'descrption'] keys = values.keys() for field in keys: if field not in fields: del values[field] continue if field == 'parent_id': values['parent_id'] = web.intget(values['parent_id'], 0) if id is not None and id == values['parent_id'] or category_setting['hierarchical'] is False: values['parent_id'] = 0 if id is not None: where = 'id=$id' vars = {'id': id} return db.update('categories', where=where, vars=vars, **values)
def work(self): self.output = "a" * web.intget(self.n, 0) or " "
def POST(self, module_id): i = web.input(vote='').vote success = votes.add(module_id, web.intget(i, 0), utils.get_user_ip()) return render.layout(view.submitted_form(success, module_id, 'vote'), title='Submit Vote - Google Modules')
def work(self): self.output = ('a' * web.intget(self.n, 0) or ' ')
'email': ( "format error", isEmail, ), 's': ( "is empty", lambda x: x, ), 'n': ( "value error", lambda x: x>=0, ), 'restMethod': ( "value error", (lambda x: x in ['GET', 'POST', 'DELETE']), ), } mapping = { 'n': (lambda x: web.intget(x, None)), } def mapInput(value, tipe): if not value or tipe not in mapping: return value return mapping[tipe](value) def checkInput(value, tipe, isMust): if value is None or value == '': if isMust: return 'is empty' return None for msg, checkFunc in web.utils.group(inputParam[tipe], 2):
def postv_int_valueof(s): n = web.intget(s, 0) return (n, None) if n > 0 else (None, {"code": 1, "message": "wrong format"})
def new_page(name, content, scroll_pos=0, caret_pos=0): site = jt.site scroll_pos, caret_pos = web.intget(scroll_pos, 0), web.intget(caret_pos, 0) page_id = web.insert('pages', site_id=site.id, name=name, scroll_pos=scroll_pos, caret_pos=caret_pos) changes = '<em>Created page</em>' new_revision(page_id, 1, content, changes)
def load(): outdb = {} done = set() with db.transaction(): db.delete('earmark_sponsor', '1=1') db.delete('earmark', '1=1') for e in earmarks.parse_file(earmarks.EARMARK_FILE): de = dict(e) de['id'] = web.intget(de['id']) if not de['id'] or de['id'] in done: continue # missing the ID? come on! if isinstance(de['house_request'], basestring): continue # CLASSIFIED for k in de: de[k] = cleanrow(de[k]) for x in [ 'house_member', 'house_state', 'house_party', 'senate_member', 'senate_state', 'senate_party', 'district' ]: de.pop(x) de['recipient_stem'] = tools.stemcorpname(de['intended_recipient']) try: db.insert('earmark', seqname=False, **de) except: pprint(de) raise done.add(de['id']) reps_not_found = set() for e in earmarks.parse_file(earmarks.EARMARK_FILE): for rawRequest, chamber in zip([e.house_request, e.senate_request], [e.house_member, e.senate_member]): for rep in chamber: if rep.lower() not in lastname2rep: #@@ should work on improving quality reps_not_found.add(rep) else: rep = lastname2rep[rep.lower()] if e.id in done: try: db.insert('earmark_sponsor', seqname=False, earmark_id=e.id, politician_id=rep) except: print "Couldn't add %s as sponsor to earmark %d" % ( rep, e.id) outdb.setdefault( rep, { 'amt_earmark_requested': 0, 'n_earmark_requested': 0, 'n_earmark_received': 0, 'amt_earmark_received': 0 }) outdb[rep]['n_earmark_requested'] += 1 requested = rawRequest or e.final_amt if not isinstance(requested, float): requested = e.final_amt if requested: outdb[rep]['amt_earmark_requested'] += requested if isinstance(e.final_amt, float) and e.final_amt: outdb[rep]['n_earmark_received'] += 1 outdb[rep]['amt_earmark_received'] += e.final_amt print "Did not find", len(reps_not_found), "reps:", pformat(reps_not_found) for rep, d in outdb.iteritems(): db.update('politician', where='id=$rep', vars=locals(), **d)
def set_caret_pos(page_id, caret_pos, scroll_pos): scroll_pos, caret_pos = web.intget(scroll_pos, 0), web.intget(caret_pos, 0) web.update('pages', where='id=$page_id', caret_pos=caret_pos, scroll_pos=scroll_pos, vars=locals())
def GET(self, page=1): page = web.intget(page, 1) search = '' posts, total = post_model.get_posts_all(page=page, limit=4) return render.post_index(posts, page, total, per_num=4, search=search)