def GET(self): i = web.input(pid=None, cid=None, opt="") option = i.pop('opt') try: # getting the requested paper i.pid = int(i.pid) paper = Paper(i.pid) if not paper.enabled: raise except (TypeError, IndexError): raise web.notfound() try: # getting the specified comment i.cid = int(i.cid) comment = paper.comments[i.cid] if not comment['enabled']: raise IndexError except (TypeError, IndexError): return render().item(paper) if option and comment['username'] == session()['uname'] \ and session()['logged']: # TODO: or session()['admin'] if option == "delete": paper.activate_comment(i.cid, state=False) return render().item(paper) if option == "edit": return render().edit(i.pid, i.cid, comment) return render().comment(i.pid, i.cid, comment)
def GET(self): ## TODO: Enforce https #redirect2https(web.ctx, '/login') i = web.input(msg="", redir="", err="") if session().logged: return render().auth.login(resp=ALREADY_LOGGED) return render().auth.login()
def GET(self): if session().logged: return render().account.index() else: return render().generic("Permission Denied", "Can't let you do that, " \ "Starfox. Please " \ "<a href='/login'>Login</a> " \ "first.")
def POST(self): """TODO: handle redir""" i = web.input(username='', passwd='', redir='/') if not Academic.validates(i.username, i.passwd): return render().auth.login(err=AUTH_ERR['malformed_creds']) if login(i.username, i.passwd): raise web.seeother(i.redir) return render().auth.login(err=AUTH_ERR['wrong_creds'])
def GET(self): i = web.input(pid=None, comment=None) if i.pid: try: papers = db.get('papers') paper = papers[int(i.pid)] if i.comment: comment = paper['comments'][int(i.comment)] return render().comment(i.pid, comment) return render().item(i.pid, paper) except IndexError: return "No such item exists, id out of range" raise web.seeother('/')
def GET(self, uid=None): i = web.input(page=0, limit=10) if getattr(session(), 'passwd', None) and uid: mail = Mailbox() if uid: return render().email(uid, email=mail.read(uid)) raise web.seeother('/login')
def POST(self): i = web.input(username='', email='', password='', redir='/') i.email = i.email.lower() u = User.register(i.username, i.password, email=i.email) if User.authenticate(i.username, i.password, u.salt, u.uhash): # Logic to populate session() with user vars: session().logged = True session().username = i.username # migrate elsewhere, maybe utils redir if i.redir: if not i.redir[0] == "/": i.redir = "/" + i.redir elif i.redir[0] == "/": i.redir = "/" raise web.seeother(web.ctx.homedomain + i.redir) raise web.seeother(web.ctx.homedomain + "/account") return render().login(msg=ERROR_LOGIN_PASSWD['key']) session().logged = True session().username = i.username raise web.seeother('/account')
def GET(self): """Handles HTTP GET requests to the / route. The waltz.render() object is pre-configured to address any file within the project's templates/ directory as if it were an instance method. Calling waltz.render().filename() will automatically render the corresponding templates/filename.html file and inject it within the templates/base.html template. If using the templates/base.html template is undesirable, an alternative render called slender is provided to compile/render html files standalone as independent entities. For example: waltz.slender().index() will render and return templates/index.html without first injecting it into templates/base.html If you would like to create your own custom render object, you can do so by accessing the web module via waltz.web and by following the web.py documentation. See: http://webpy.org/docs/0.3/templetor """ if waltz.session()['logged']: return waltz.render().index() raise waltz.web.seeother('/login')
def POST(self): """Organize/sort the comments according to votes, author, time, etc (heuristic) """ i = web.input(pid=None, time=datetime.utcnow().ctime(), comment="", username=session()['uname'], votes=0, enabled=True) if i.pid: i.pid = int(i.pid) i.cid = 0 #sets default cid val if first comment if not session().logged: raise web.seeother('/login?redir=/item=?pid=%s' % i.pid) try: db = Db('db/openjournal') papers = db.get('papers') paper = papers[i.pid] #XXX get by key 'pid' instead if paper['comments']: i.cid = paper['comments'][-1]['cid'] + 1 papers[i.pid]['comments'].append(dict(i)) db.put('papers', papers) record_comment(i.username, i.pid, i.cid) return render().item(i.pid, paper) except IndexError: return "No such item exists, id out of range" raise web.seeother('/')
def GET(self): posts = [] filenames = os.listdir(os.path.join(os.getcwd(),"blog","posts")) for filename in filenames: with open(os.path.join(os.getcwd(),"blog","posts",filename)) as f: posts.append((filename, markdown.markdown(f.read()))) return render().index(posts)
def GET(self, username=""): if username: try: user = User.get(username, safe=True) return render().profiles.index(user) except: pass raise web.seeother('/')
def GET(self, username=""): if username: try: user = Academic.get(username, safe=True) return render().profiles.index(user) except: pass raise web.seeother('/')
def GET(self): i = web.input(sort="popular", limit=30) db = Db('db/openjournal') papers = db.get('papers') try: papers = globals()[i.sort](papers) except: papers = popular(papers) return render().index(papers[:i.limit])
def GET(self): i = web.input(search="") results = [] if i.search: papers = Paper.getall() matches = S().match(i.search) pids = [int(x['pid']) for x in matches] results = [p for p in papers if p['pid'] in pids] return render().search(i.search, results)
def GET(self): i = web.input(redir='') session().logged = False session().username= '' session().kill() if i.redir: raise web.seeother(i.redir) #raise redir2login(redir='/login') return render().auth.login(resp=LOGGED_OUT)
def GET(self): i = web.input(search="") results = [] if i.search: papers = Paper.get() matches = S().match(i.search) pids = [int(x['pid']) for x in matches] results = [p for p in papers if p['pid'] in pids] return render().search(i.search, results)
def GET(self): i = web.input(page=0, limit=10) if getattr(session(), 'passwd', None): mail = Mailbox() page, limit = int(i.page), int(i.limit) offset = page * limit emails = mail.newest(limit=limit, offset=offset) return render().ui(emails=emails, page=page, limit=limit) raise web.seeother('/login')
def POST(self): """Organize/sort the comments according to votes, author, time, etc (heuristic) POST route to add a comment to a paper side effects: - handles votes / karma """ i = web.input(pid=None, cid=None, comment="", opt="", enabled=True) option = i.pop('opt') try: # getting the requested paper i.pid = int(i.pid) paper = Paper(i.pid) except (TypeError, IndexError): # TODO: Log # IndexEror("No such paper") or # TypeError("int() arg must be str or number, not 'NoneType'") raise web.notfound() if not i.comment: return render().item(paper) if not session().logged: raise web.seeother('/login?redir=/item?pid=%s' % i.pid) else: i.username = session()['uname'] if option == "edit": try: paper.edit_comment(i.cid, content=i.comment, enabled=i.enabled) except (TypeError, ValueError) as e: # XXX Log error e return render().item(paper) else: i.cid = paper.add_comment(i.cid, session()['uname'], content=i.comment, votes=paper.votes, enabled=i.enabled) record_comment(session()['uname'], i.pid, i.cid) return render().item(paper)
def POST(self): i = web.input(username='', passwd='', redir='/') if session().logged: raise web.seeother(i.redir) if not Academic.validates(i.username, i.passwd): return render().auth.login(err=AUTH_ERR['malformed_creds']) # attempting login first if login(i.username, i.passwd): raise web.seeother(i.redir) try: u = Academic.register(i.username, i.passwd, **Academic.defaults()) u.login() raise web.seeother(i.redir) except Academic.RegistrationException as e: err = AUTH_ERR[str(e.message)] return render().auth.register(err=err)
def GET(self): i = web.input(pid=None, cid=None) if i.pid: i.pid = int(i.pid) try: db = Db('db/openjournal') papers = db.get('papers') paper = papers[i.pid] if i.cid: i.cid = int(i.cid) # XXX Revise data storage scheme s.t. comment can # be retrieved by cid pkey, not i.cid'th index (below) # reason: cnnt can be deleted and lead to consistency # errors (id would then reference wrong entity) comment = paper['comments'][i.cid] return render().comment(i.pid, i.cid, comment) return render().item(i.pid, paper) except IndexError: return "No such item exists, id out of range" raise web.seeother('/')
def GET(self): i = web.input(sort="popular", limit=30, page=0) papers = Paper.getall() limit = int(i.limit) page = int(i.page) pages = int(ceil(float(len(papers)) / limit)) try: papers = globals()[i.sort](papers) except: papers = popular(papers) start = page * limit end = start + limit return render().index(papers[start:end], pages, i.page, sort=i.sort, limit=limit)
def POST(self): """TODO: handle redir""" i = web.input(username='', passwd='', redir='') if i.username and i.passwd: try: u = User.get(i.username) if User.easyauth(u, i.passwd): loadsession(u) raise web.seeother('/') except: raise err = "Incorrect username or password" else: err = "Please provide all required fields" return render().auth.login(err=err)
def POST(self): """Organize/sort the comments according to votes, author, time, etc (heuristic) """ i = web.input(pid=None, time=datetime.utcnow().ctime(), comment="", user="******", votes=0) if i.pid: try: papers = db.get('papers') paper = papers[int(i.pid)] papers[int(i.pid)]['comments'].append(dict(i)) db.put('papers', papers) return render().item(i.pid, paper) except IndexError: return "No such item exists, id out of range" raise web.seeother('/')
def GET(self): i = web.input(op=None, ref=None, qty=1) if i.ref: pid = long(i.ref) if i.op == "coupon": session().cart.coupon = Coupon(1, "1010", percent_off=10, value_off="200.00") if i.op == "add" and pid: # XXX Replace Product with some product retrieved from listing # XXX Must sanitize + normalize pid, assert not None in treasury p = Product(pid, "Example Product", price="1.00") session().cart.add(p) if i.op == "remove" and pid: session().cart.remove(pid) if i.op == "reset": session().cart.empty() return render().cart.index()
def POST(self): i = web.input(q="", p='0', rows=50, page=1) serp = "<p>No Results Found</p>" zeroclick="" if i.p == '0' and 'http://' in i.q: zeroclick = '<img style="height: 50px;" src="http://archive.org/images/wayback.gif"/><span style="position: relative; top: -14px; margin-left: 10px;">Go Wayback in time! <a style="margin-left: 10px;" href="http://web.archive.org/web/*/%s">View older versions of %s</a><span>' % (i.q, i.q) #raise web.seeother('http://web.archive.org/web/*/%s' % i.q) # Fallback to seach all mediatypes if i.q: r = requests.post('http://archive.org/advancedsearch.php', data={'q': i.q.replace('http://', ''), 'rows': i.rows, 'page': i.page, 'output': 'json'}) serp = r.json return render().serp(query=i.q, p=i.p, serp=serp, page=i.page, rows=i.rows, zeroclick=zeroclick)
def POST(self): i = web.input(q="", p='0', rows=50, page=1) serp = "<p>No Results Found</p>" zeroclick="" if i.p == '0' and 'http://' in i.q: zeroclick = '<img style="height: 50px;" src="http://archive.org/images/wayback.gif"/><span style="position: relative; top: -14px; margin-left: 10px;"><a style="margin-left: 10px;" href="http://web.archive.org/web/*/%s">View older versions o1f %s</a><span>' % (i.q, i.q) raise web.seeother('http://web.archive.org/web/*/%s' % i.q) # Fallback to seach all mediatypes if i.q: r = requests.post('http://archive.org/advancedsearch.php', data={'q': i.q.replace('http://', ''), 'rows': i.rows, 'page': i.page, 'output': 'json'}) serp = r.json return render().serp(query=i.q, p=i.p, serp=serp, page=i.page, rows=i.rows, zeroclick=zeroclick)
def GET(self): """Research http://news.ycombinator.com/item?id=1781013 how hacker news voting works and emulate """ msg = None i = web.input(pid=None) db = Db('db/openjournal') if i.pid: ps = db.get('papers') if not session().logged: msg = JS("Must be logged in to vote") else: try: ps[int(i.pid)]['votes'] += 1 db.put('papers', ps) except IndexError: return "No such items exists to vote on" return render().index(ps, msg=msg)
def POST(self): i = web.input(q='', cat='', p='0', rows=50, page=1) serp = "<p>No Results Found</p>" zeroclick = [] if i.p == '0': if i.q: urls = findurls(i.q) if urls: zeroclick += [(timeline(url), url) for url in urls] # Fallback to seach all mediatypes if i.q: r = requests.post('http://archive.org/advancedsearch.php', data={'q': i.q.replace('http://', ''), 'rows': i.rows, 'page': i.page, 'output': 'json'}) serp = r.json() return render().serp(query=i.q, p=i.p, serp=serp, page=i.page, rows=i.rows, zeroclick=zeroclick)
def POST(self): def defusr(): return {'karma': 0, 'comments': [], 'votes': [], 'posts': [], 'created': datetime.datetime.utcnow(), 'bio': '', 'email': ''} i = web.input(username='', passwd='', redir='') if i.username and i.passwd: if re.match(username_regex, i.username): if re.match(passwd_regex, i.passwd): try: # treat as login if creds are right u = User.get(i.username) if User.easyauth(u, i.passwd): loadsession(u) raise web.seeother('/') except: pass try: u = User.register(i.username, i.passwd, **defusr()) loadsession(u) raise web.seeother('/') except: err = "Username unavailable" else: err = passwd_err else: err = username_err else: err = "Please enter all required fields" return render().auth.register(err=err)
def GET(self): return render().auth.login()
def GET(self, uid=None): if getattr(session(), 'passwd', None): return render().compose() raise web.seeother('/login')
def GET(self): i = web.input(p='0', q='', rows=50, page=1) if i.q: return self.POST() return render().index(p=int(i.p))
def GET(self): return render().terms()
def GET(self): if not session().logged: raise web.seeother('/register') return render().submit()
def GET(self): if session().logged: raise web.seeother('/') return render().auth.register()
def GET(self): i = web.input(p='0', cat='', q='', rows=50, page=1) if i.q: return self.POST() return render().index(p=int(i.p))