Пример #1
0
def index_images(page, per_page):
    paginate = Image.query.order_by(db.desc(Image.id)).paginate(
        page=page, per_page=per_page, error_out=False)
    map = {'has_next': paginate.has_next}
    images = []
    for image in paginate.items:
        comments = []
        for i in range(0, min(2, len(image.comments))):
            comment = image.comments[i]
            comments.append({
                'username': comment.user.username,
                'user_id': comment.user_id,
                'content': comment.content
            })
        imgvo = {
            'id': image.id,
            'url': image.url,
            'comment_count': len(image.comments),
            'user_id': image.user_id,
            'head_url': image.user.head_url,
            'created_date': str(image.created_date),
            'comments': comments
        }
        images.append(imgvo)

    map['images'] = images
    return json.dumps(map)
Пример #2
0
def write_2_file(room):
    with open(f, "w+") as file:
        if room not in db.engine.table_names():
            file.write("No chat history")
        else:
            for p in tables[room].query.order_by(db.desc(
                    tables[room].id)).all():
                file.write("[{}] {}: {}\n".format(p.timestamp, p.username,
                                                  p.messages))
Пример #3
0
def profile(user_id):
    user = User.query.get(user_id)
    if user is None:
        redirect('/')
    # 个人详情页中的"更多"应该做成分页形式,
    paginate = Image.query.filter_by(user_id=user_id).order_by(
        db.desc(Image.id)).paginate(page=1, per_page=3, error_out=False)
    return render_template('profile.html',
                           user=user,
                           has_next=paginate.has_next,
                           images=paginate.items)
Пример #4
0
 def all(self, offset=None, limit=None, order_by=None, desc=False):
     query = self.__model__.query
     if order_by is not None:
         if desc:
             query = query.order_by(db.desc(order_by))
         else:
             query = query.order_by(order_by)
     if offset is not None:
         query = query.offset(offset)
     if limit is not None:
         query = query.limit(limit)
     return query.all()
Пример #5
0
 def all(self, offset=None, limit=None, order_by=None, desc=False):
     query = self.session.query(self.__model__)
     if order_by is not None:
         if desc:
             query = query.order_by(db.desc(order_by))
         else:
             query = query.order_by(order_by)
     if offset is not None:
         query = query.offset(offset)
     if limit is not None:
         query = query.limit(limit)
     return query.all()
Пример #6
0
def user_images(user_id, page, per_page):
    # 将所需要的字段以json形式返回
    paginate = Image.query.filter_by(user_id=user_id).order_by(
        db.desc(Image.id)).paginate(page=page, per_page=per_page)
    map = {'has_next': paginate.has_next}
    images = []
    for image in paginate.items:
        imgvo = {
            'id': image.id,
            'url': image.url,
            'comment_count': len(image.comments)
        }
        images.append(imgvo)
    map['images'] = images
    return json.dumps(map)
Пример #7
0
 def find(self,
          offset=None,
          limit=None,
          order_by=None,
          desc=False,
          **kargs):
     query = self.__model__.query.filter_by(**kargs)
     if order_by is not None:
         if desc:
             query = query.order_by(db.desc(order_by))
         else:
             query = query.order_by(order_by)
     if offset is not None:
         query = query.offset(offset)
     if limit is not None:
         query = query.limit(limit)
     return query.all()
Пример #8
0
def api_deploys():
    deploys.session_commit()
    offset = request.args.get("offset", None, type=int)
    limit = request.args.get("limit", None, type=int)
    if g.user.role == g.user.ROLE["ADMIN"]:
        return jsonify(
            dict(rc=0,
                 data=dict(deploys=deploys.all(offset,
                                               limit,
                                               order_by="created_at",
                                               desc=True),
                           count=deploys.count())))
    else:
        return jsonify(
            dict(rc=0,
                 data=dict(deploys=deploys.find(user_id=g.user.id).order_by(
                     db.desc("created_at")).limit(limit).offset(offset).all(),
                           count=deploys.count(user_id=g.user.id))))
Пример #9
0
def api_deploys():
    deploys.session_commit()
    offset = request.args.get("offset", None, type=int)
    limit = request.args.get("limit", None, type=int)
    if g.user.role == g.user.ROLE["ADMIN"]:
        return jsonify(
            dict(rc=0,
                 data=dict(deploys=deploys.all(
                     offset,
                     limit,
                     order_by="created_at",
                     desc=True),
                     count=deploys.count())))
    else:
        return jsonify(
            dict(rc=0,
                 data=dict(deploys=deploys.find(user_id=g.user.id)
                           .order_by(db.desc("created_at")).limit(limit)
                           .offset(offset).all(),
                           count=deploys.count(user_id=g.user.id))))
Пример #10
0
def index():
    images = Image.query.order_by(db.desc(Image.id)).limit(10).all()
    return render_template('index.html', images=images)