Exemple #1
0
    def queryArticlesByCategory(self,
                                catname,
                                count=10,
                                offset=0,
                                user_id=None):
        try:
            category = self.session.query(Category).filter_by(
                name=catname).first()
            query = self.session.query(Article).filter_by(
                category_id=category.id).all()
            for row in query:
                if user_id is not None and self.isFaved(article_id=row.id,
                                                        user_id=user_id):
                    row.faved = True
                else:
                    row.faved = False

            for index, row in enumerate(query):
                row.rowid = index + 1
                row.shortURL = formatURL(row.URL)

            if len(query) > offset:
                query = query[offset - 1:(count + offset + 1)]
            else:
                query = query[:count]
            return query
        except:
            self.session.rollback()
 def queryFavedArticles(self, username):
     try:
         q_user = self.session.query(User).filter_by(name=username).first()
         if q_user:
             user_id = q_user.id
         q_article = self.session.query(Favorite).filter_by(user_id = user_id).order_by(Favorite.timestamp.desc()).all()
         for index, row in enumerate(q_article):
             row.rowid = index + 1
             row.article.shortURL = formatURL(row.article.URL)
         return q_article
     except:
         self.session.rollback()
Exemple #3
0
 def queryFavedArticles(self, username):
     try:
         q_user = self.session.query(User).filter_by(name=username).first()
         if q_user:
             user_id = q_user.id
         q_article = self.session.query(Favorite).filter_by(
             user_id=user_id).order_by(Favorite.timestamp.desc()).all()
         for index, row in enumerate(q_article):
             row.rowid = index + 1
             row.article.shortURL = formatURL(row.article.URL)
         return q_article
     except:
         self.session.rollback()
Exemple #4
0
    def queryArticlesByHot(self, pagesize=32, page=1, user_id=None):
        try:
            query = self.session.query(Article).all()
            now = time.time()
            for row in query:
                if user_id is not None and self.isFaved(article_id=row.id,
                                                        user_id=user_id):
                    row.faved = True
                else:
                    row.faved = False
                delta_hours = int(
                    (decimal.Decimal(now) - row.timestamp) / 3600)
                hot = calculate_score(row.score, delta_hours)
                row.hot = hot
            query = sorted(query, reverse=True)

            for index, row in enumerate(query):
                row.rowid = index + 1
                row.shortURL = formatURL(row.URL)

            totalpage = int(math.ceil(len(query) / float(pagesize)))
            nextpagenum = 1
            privouspagenum = 1
            if totalpage > 1:
                if page >= totalpage:
                    hasnextpage = False
                    hasprivouspage = True
                    privouspagenum = totalpage - 1
                elif page <= 1:
                    hasnextpage = True
                    hasprivouspage = False
                    nextpagenum = 2
                else:
                    hasnextpage = True
                    hasprivouspage = True
                    privouspagenum = page - 1
                    nextpagenum = page + 1
            else:
                hasnextpage = False
                hasprivouspage = False

            query = query[pagesize * (page - 1):pagesize * page]

            return query, hasnextpage, nextpagenum, hasprivouspage, privouspagenum
        except:
            self.session.rollback()
 def queryArticlesByHot(self, pagesize = 32, page = 1, user_id = None):
     try:
         query = self.session.query(Article).all()
         now = time.time()
         for row in query:
             if user_id is not None and self.isFaved(article_id = row.id, user_id = user_id):
                 row.faved = True
             else:
                 row.faved = False
             delta_hours = int((decimal.Decimal(now)-row.timestamp)/3600)
             hot = calculate_score(row.score, delta_hours)
             row.hot = hot
         query = sorted(query, reverse=True)       
         
         for index, row in enumerate(query):
             row.rowid = index + 1
             row.shortURL = formatURL(row.URL)           
         
         totalpage = int(math.ceil(len(query)/float(pagesize)))
         nextpagenum = 1
         privouspagenum = 1
         if totalpage > 1:
             if page >= totalpage:
                 hasnextpage = False
                 hasprivouspage = True
                 privouspagenum = totalpage - 1  
             elif page <= 1:
                 hasnextpage = True
                 hasprivouspage = False
                 nextpagenum = 2 
             else:
                 hasnextpage = True
                 hasprivouspage = True
                 privouspagenum = page - 1  
                 nextpagenum = page + 1  
         else:
             hasnextpage = False
             hasprivouspage = False
             
         
         query = query[pagesize*(page-1):pagesize*page]
         
         return query, hasnextpage, nextpagenum, hasprivouspage, privouspagenum
     except:
         self.session.rollback()
Exemple #6
0
    def queryArticlesByLatest(self, pagesize=32, page=1, user_id=None):
        try:
            query = self.session.query(Article).order_by(
                Article.timestamp.desc()).all()

            for row in query:
                if user_id is not None and self.isFaved(article_id=row.id,
                                                        user_id=user_id):
                    row.faved = True
                else:
                    row.faved = False

            for index, row in enumerate(query):
                row.rowid = index + 1
                row.shortURL = formatURL(row.URL)

            totalpage = int(math.ceil(len(query) / float(pagesize)))
            nextpagenum = 1
            privouspagenum = 1
            if totalpage > 1:
                if page >= totalpage:
                    hasnextpage = False
                    hasprivouspage = True
                    privouspagenum = totalpage - 1
                elif page <= 1:
                    hasnextpage = True
                    hasprivouspage = False
                    nextpagenum = 2
                else:
                    hasnextpage = True
                    hasprivouspage = True
                    privouspagenum = page - 1
                    nextpagenum = page + 1
            else:
                hasnextpage = False
                hasprivouspage = False

            query = query[pagesize * (page - 1):pagesize * page]

            return query, hasnextpage, nextpagenum, hasprivouspage, privouspagenum
        except:
            self.session.rollback()
 def queryArticlesByLatest(self, pagesize = 32, page = 1, user_id = None):
     try:
         query = self.session.query(Article).order_by(Article.timestamp.desc()).all()
                 
         for row in query:
             if user_id is not None and self.isFaved(article_id=row.id, user_id=user_id):
                 row.faved = True
             else:
                 row.faved = False
         
         for index, row in enumerate(query):
             row.rowid = index + 1
             row.shortURL = formatURL(row.URL)
             
         totalpage = int(math.ceil(len(query)/float(pagesize)))
         nextpagenum = 1
         privouspagenum = 1
         if totalpage > 1:
             if page >= totalpage:
                 hasnextpage = False
                 hasprivouspage = True
                 privouspagenum = totalpage - 1  
             elif page <= 1:
                 hasnextpage = True
                 hasprivouspage = False
                 nextpagenum = 2 
             else:
                 hasnextpage = True
                 hasprivouspage = True
                 privouspagenum = page - 1  
                 nextpagenum = page + 1  
         else:
             hasnextpage = False
             hasprivouspage = False
             
         
         query = query[pagesize*(page-1):pagesize*page]
         
         return query, hasnextpage, nextpagenum, hasprivouspage, privouspagenum
     except:
         self.session.rollback()
 def queryArticlesByCategory(self, catname, count = 10, offset = 0, user_id = None):
     try:
         category = self.session.query(Category).filter_by(name=catname).first()
         query = self.session.query(Article).filter_by(category_id=category.id).all()
         for row in query:
             if user_id is not None and self.isFaved(article_id=row.id, user_id=user_id):
                 row.faved = True
             else:
                 row.faved = False
         
         for index, row in enumerate(query):
             row.rowid = index + 1
             row.shortURL = formatURL(row.URL)
             
         if len(query) > offset:
             query = query[offset-1:(count+offset+1)]
         else:
             query = query[:count]
         return query
     except:
         self.session.rollback()