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): 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('/')
class Db(object): """ Database proxy """ def __init__(self, config): self.config = config self._backend = None self._db_name = None self._port = None self._host = None if 'cache_host' in self.config: import redis self._backend = "redis" self._host = self.config.get('cache_host') self._port = self.config.get('cache_port') or 6379 self._db_name = self.config.get('cache_db_name') or 0 self.db = redis.StrictRedis(host=self._host, port=self._port, db=self._db_name) self.set_value = self.db.set self.has_value = self.db.exists else: from lazydb import Db self._backend = "lazydb" self._db_name = self.config.get('cache_db_name') or 'databridge_cache_db' self.db = Db(self._db_name) self.set_value = self.db.put self.has_value = self.db.has def get(self, key): return self.db.get(key) def put(self, key, value): self.set_value(key, value) def has(self, key): return self.has_value(key)
def GET(self): """Research http://news.ycombinator.com/item?id=1781013 how hacker news voting works and emulate XXX Restrict voting to session().logged users + element id must not already exist in user['votes'] set. XXX Requires accounting + record keeping XXX Preserve the web.ctx GET query params to preserve sorting / ordering Algo: 1. Add karma to paper['submitter'] if vote 2. Record vote in user['votes'] set by id - calc unique vote id via some linear combination of paper pid (and or comment id [cid], if it exists) """ msg = None i = web.input(pid=None, sort="popular") if not session().logged: raise web.seeother('/register') db = Db('db/openjournal') ps = db.get('papers') u = User.get(session()['uname']) if i.pid: i.pid = int(i.pid) if canvote(u, i.pid): try: ps[i.pid]['votes'] += 1 db.put('papers', ps) submitter_uname = ps[i.pid]['submitter'] record_vote(u['username'], submitter_uname, i.pid) except IndexError: return "No such items exists to vote on" raise web.seeother('/?sort=%s' % i.sort)
class AuctionsMapping(object): """Mapping for processed auctions""" def __init__(self, config): self.config = config if 'host' in self.config: config = { 'host': self.config.get('host'), 'port': self.config.get('port') or 6379, 'db': self.config.get('name') or 0, 'password': self.config.get('password') or None } self.db = StrictRedis(**config) LOGGER.info('Set redis store "{db}" at {host}:{port} ' 'as auctions mapping'.format(**config)) self._set_value = self.db.set self._has_value = self.db.exists else: db = self.config.get('name', 'auctions_mapping') self.db = LazyDB(db) LOGGER.info('Set lazydb "{}" as auctions mapping'.format(db)) self._set_value = self.db.put self._has_value = self.db.has def get(self, key): return self.db.get(key) def put(self, key, value, **kwargs): LOGGER.info('Save ID {} in cache'.format(key)) self._set_value(key, value, **kwargs) def has(self, key): return self._has_value(key) def delete(self, key): return self.db.delete(key)
def clear(pid): """Clear comments for an item""" db = Db('db/openjournal') papers = db.get('papers') papers[pid]['comments'] = [] return db.put('papers', papers)