Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
 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)
Пример #4
0
 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)
Пример #5
0
    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)
Пример #6
0
    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)
Пример #7
0
 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)
Пример #8
0
    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)
        """
        i = web.input(pid=None, sort="popular")

        if not session().logged:
            raise web.seeother('/register')

        if i.pid:
            i.pid = int(i.pid)
            u = Academic(session()['uname'])
            p = Paper(i.pid)
            if canvote(u, i.pid):
                try:
                    # move set_vote to paper api
                    p.votes += 1
                    p.save()
                    record_vote(u['username'], p.submitter, i.pid)
                except IndexError:
                    return "No such items exists to vote on"
        raise web.seeother('/?sort=%s' % i.sort)
Пример #9
0
def items():
    """A function which generates all rss items for this website"""
    papers = Paper.getall()
    items = []
    desc = "%s - %s votes, submitted by: %s @ %s" 
    for paper in filter(lambda x: x['enabled'], newest(papers)):
        description = desc % (paper['url'], paper['votes'],
                              paper['submitter'], paper['time'])
        items.append({'title': paper['title'],
                      'link': 'https://hackerlist.net:1443/item?pid=%s' % \
                          paper['pid'],
                      'description': description,
                      'date': paper['time'],
                      'guid': paper['pid']})
    return items
Пример #10
0
def items():
    """A function which generates all rss items for this website"""
    papers = Paper.getall()
    items = []
    desc = "%s - %s votes, submitted by: %s @ %s"
    for paper in filter(lambda x: x['enabled'], newest(papers)):
        description = desc % (paper['url'], paper['votes'], paper['submitter'],
                              paper['time'])
        items.append({'title': paper['title'],
                      'link': 'https://hackerlist.net:1443/item?pid=%s' % \
                          paper['pid'],
                      'description': description,
                      'date': paper['time'],
                      'guid': paper['pid']})
    return items
Пример #11
0
    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)
        """
        i = web.input(pid=None, sort="popular")
        
        if not session().logged:
            raise web.seeother('/register')

        if i.pid:
            i.pid = int(i.pid)
            u = Academic(session()['uname'])
            p = Paper(i.pid)
            if canvote(u, i.pid):
                try:
                    # move set_vote to paper api
                    p.votes += 1
                    p.save()                    
                    record_vote(u['username'], p.submitter, i.pid)
                except IndexError:
                    return "No such items exists to vote on"
        raise web.seeother('/?sort=%s' % i.sort)
Пример #12
0
 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)
Пример #13
0
 def clear(pid):
     """Clear comments for an item"""
     paper = Paper(pid)
     paper.comments = []
     return paper.save()
Пример #14
0
 def clear(pid):
     """Clear comments for an item"""
     paper = Paper(pid)
     paper.comments = []
     return paper.save()