Esempio n. 1
0
def user_info(user_id):
    if g.user_id:
        if not user_id:
            user_id = g.user_id

        user = User.objects(id=user_id).first()
        user_id = str(user.id)
        user_data = {
                    'user_id':str(user.id),
                    'email':user.email,
                    'nickname':user.nickname,
                    'avatar':user.avatar,
                    'pins_count':user.pins_count,
                    'followers_count':user.followers_count,
                    'fans_count':user.fans_count,
                    }

        if user_id != g.user_id:
            I = User.objects(id=g.user_id).first()
            user_data['isfollowed'] = 1 if user in I.followers else 0

        response = make_response(json.dumps(user_data))
        #response.headers
        response.headers['Version'] = '1'
        return response
    else:
        return ('session expired', 400)
Esempio n. 2
0
def user_info_post():
    if g.user_id:
        user = User.objects(id=g.user_id).first()
        
        if request.form['nickname']:
            user.nickname = request.form['nickname']
            
        if request.form['avatar']:
            user.avatar = request.form['avatar']
        
        user.save()
        
        user_id = str(user.id)
        user_data = {
                    'user_id':str(user.id),
                    'email':user.email,
                    'nickname':user.nickname,
                    'avatar':user.avatar,
                    'pins_count':user.pins_count,
                    'followers_count':user.followers_count,
                    'fans_count':user.fans_count,
                    }
        response = make_response(json.dumps(user_data))
        #response.headers
        response.headers['Version'] = '1'
        return response
    else:
        return ('session expired', 400)
Esempio n. 3
0
def show_pins():
    if g.user_id:
        user = User.objects(id=g.user_id).first()
        pins = Pin.objects(owner=g.user_id)[:5].order_by('-create_at')
        res_data = pins_pack(pins, user)
        return (json.dumps(res_data), 200)
    return ('show_pins session timeout', 400)
Esempio n. 4
0
def geotag_post():
    if g.user_id:

        # lat = request.form['lat'] if ('lat' in request.form) and request.form['lat'] else 1
        # long = request.form['long'] if 'long' in request.form else ''
        # pic = request.form['pic'] if 'pic' in request.form else ''
        # stamp = request.form['stamp'] if ('stamp' in request.form) and request.form['stamp'] else 1
        # price = float(request.form['price']) if 'price' in request.form else 1.0
        # currency = request.form['currency'] if ('currency' in request.form) and request.form['currency'] else 'CNY'

        lat = float(request.form['lat']) if 'lat' in request.form else None
        long = float(request.form['long']) if 'long' in request.form else None
        title = request.form['title'] if 'title' in request.form else ''
        owner = User.objects(id=g.user_id).first()

        geotag = Geotag(title=title,
                        owner=owner,
                        loc=[lat,long])
        geotag.save()

        geotag_item = {
            'geotag_id':str(geotag.id),
            'loc':[geotag.lat, geotag.long],
            'create_at':pin.create_at.strftime('%Y-%m-%d %H:%M:%S'),
            }

        return (json.dumps(geotag_item), 200)
    return ('post geotag session timeout', 400)
Esempio n. 5
0
def comment_pin(pin_id):
    if g.user_id:
        content = request.form['content']
        user = User.objects(id=g.user_id).first()
        pin = Pin.objects(id=pin_id).first()
        comment = Comment(content=content,
                          author=user,
                          pin=pin,
                          create_at=datetime.utcnow())
        comment.save()
        
        if pin.comments_count == 0:
            pin.first_comment = content
            pin.first_comment_user = user
            pin.first_comment_create_at = datetime.utcnow()
            pin.save()
        elif pin.comments_count >= 1:
            pin.last_comment = content
            pin.last_comment_user = user
            pin.last_comment_create_at = datetime.utcnow()
            pin.save()

        pin.update(inc__comments_count=1)

        comment_item = {}
        comment_item['comment_id'] = str(comment.id)
        comment_item['content'] = comment.content
        comment_item['author_id'] = str(comment.author.id)
        comment_item['author_name'] = comment.author.nickname
        comment_item['avatar'] = comment.author.avatar
        comment_item['create_at'] = comment.create_at.strftime('%Y-%m-%d %H:%M:%S')

        return (json.dumps(comment_item), 200)
    return ('comment pin session timeout', 400)
Esempio n. 6
0
def login_post():
    email = request.form['email']
    user = User.objects(email=email).first()

    if not user:
        return ('email or password error', 401)

    if bcrypt.check_password_hash(user.password, request.form['password']):
        session['user_id'] = str(user.id)
        
        # 此参数当写入session时配置
        session.permanent = True

        user_data = {
                    'user_id':str(user.id),
                    'email':user.email,
                    'nickname':user.nickname,
                    'avatar':user.avatar,
                    'pins_count':user.pins_count,
                    'followers_count':user.followers_count,
                    'fans_count':user.fans_count,
                    }
        return (json.dumps(user_data), 200)
    else:
        return ('email or password error', 401)
Esempio n. 7
0
def show_timeline():
    if g.user_id:
        user = User.objects(id=g.user_id).first()
        timelines = Timeline.objects(owner=g.user_id)[:5].order_by('-create_at')
        res_data = timeline_pack(timelines, user)
        return (json.dumps(res_data), 200)
    return ('timeline session timeout', 400)
Esempio n. 8
0
def reg_user_post():
    """
        用户注册表单接收
        ~~~~~~~~~~~~~~~~

    """

    err_msg = None
    if not request.form['email']:
        return ('no email', 400)
    if not request.form['nickname']:
        return ('no nickname', 400)
    if not request.form['password']:
        return ('no password', 400)

    email = request.form['email']
    nickname = request.form['nickname']
    password = bcrypt.generate_password_hash(request.form['password'], 8)
    
    #avatar
    avatar = ''
    if 'avatar' in request.form:
        avatar = request.form['avatar']

    user = User(email=email,
                nickname=nickname,
                password=password,
                avatar=avatar,
                register_at=datetime.utcnow())

    user.save()
    
    user_id = str(user.id)
    
    session['user_id'] = user_id
    session.permanent = True
    
    user_data = {
                'user_id':user_id,
                'email':user.email,
                'nickname':user.nickname,
                'avatar':user.avatar,
                }
    response = make_response(json.dumps(user_data))
    #response.headers
    response.headers['Version'] = '1'
    return response
Esempio n. 9
0
def show_timeline_before(timeline_id):
    if g.user_id:
        time_tag = Timeline.objects(id=timeline_id).first().create_at
        user = User.objects(id=g.user_id).first()
        timelines = Timeline.objects(Q(create_at__lt=time_tag)&Q(owner=g.user_id))[:5].order_by('-create_at')
        res_data = timeline_pack(timelines, user)
        return (json.dumps(res_data), 200)
    return ('timeline_before session timeout', 400)
Esempio n. 10
0
def show_pins_before(pin_id):
    if g.user_id:
        time_tag = Pin.objects(id=pin_id).first().create_at
        user = User.objects(id=g.user_id).first()
        pins = Pin.objects(Q(create_at__lt=time_tag)&Q(owner=g.user_id))[:5].order_by('-create_at')
        res_data = pins_pack(pins, user)
        return (json.dumps(res_data), 200)
    return ('show_pins_before session timeout', 400)
Esempio n. 11
0
def relation_followers(page_num):
    if g.user_id:
        limit = 5 
        offset = (page_num - 1) * limit
        user = User.objects(id=g.user_id).fields(slice__followers=[offset, limit]).first()
        followers = user.followers
        res_data = followers_pack(followers, user)
        return (json.dumps(res_data), 200)
    return ('followers list session timeout', 400)
Esempio n. 12
0
def relation_user_fans(user_id, page_num):
    if g.user_id:
        limit = 5 
        offset = (page_num - 1) * limit
        user = User.objects(id=user_id).fields(slice__fans=[offset, limit]).first()
        fans = user.fans
        res_data = fans_pack(fans, user)
        return (json.dumps(res_data), 200)
    return ('fans list session timeout', 400)
Esempio n. 13
0
def like_pin(pin_id):
    if g.user_id:
        user = User.objects(id=g.user_id).first()
        pin = Pin.objects(id=pin_id, likes__nin=[user]).first()
        if pin:
            pin.update(push__likes=user, inc__likes_count=1)
            return ('like success', 200)
        return ('already liked', 400)
    return ('like pin session timeout', 400)
Esempio n. 14
0
def unlike_pin(pin_id):
    if g.user_id:
        user = User.objects(id=g.user_id).first()
        pin = Pin.objects(id=pin_id).first()
        if user in pin.likes:
            pin.update(pull__likes=user, inc__likes_count=-1)
            return ('unlike success', 200)
        return ('not like this pin', 400)
    return ('unlike pin session timeout', 400)
Esempio n. 15
0
def pin_post():
    if g.user_id:

        pin_type = request.form['type'] if ('type' in request.form) and request.form['type'] else 1
        content = request.form['content'] if 'content' in request.form else ''
        pic = request.form['pic'] if 'pic' in request.form else ''
        stamp = request.form['stamp'] if ('stamp' in request.form) and request.form['stamp'] else 1
        price = float(request.form['price']) if 'price' in request.form else 1.0
        currency = request.form['currency'] if ('currency' in request.form) and request.form['currency'] else 'CNY'

        # ex_rate = request.form['ex_rate'] if 'ex_rate' in request.form else 1.0

        owner = User.objects(id=g.user_id).first()
        pin = Pin(type=pin_type,
                  content=content,
                  pic=pic,
                  stamp=stamp,
                  price=price,
                  currency=currency,
                  owner=owner,
                  create_at=datetime.utcnow(),
                  avatar=owner.avatar)
        pin.save()

        timeline = Timeline(pin=pin,
                            owner=owner,
                            create_at=datetime.utcnow())
        timeline.save()

        if owner.fans:
            for fan in owner.fans:
                fan_timeline = Timeline(pin=pin,
                                        owner=fan,
                                        create_at=datetime.utcnow())
                fan_timeline.save()

        owner.update(inc__pins_count=1)

        res_data = {
            'pin_id':str(pin.id),
            'type':pin.type,
            'content':pin.content,
            'avatar':pin.avatar,
            'pic':pin.pic,
            'stamp':pin.stamp,
            'currency':pin.currency,
            'price':pin.price,
            'owner_id':str(pin.owner.id),
            'create_at':pin.create_at.strftime('%Y-%m-%d %H:%M:%S'),
        }
        response = make_response(json.dumps(res_data))
        #response.headers
        #response.headers['Version'] = '1'
        return response
    else:
        return ('pin post session timeout', 400)
Esempio n. 16
0
def admin_user(page):
    if g.admin:
        perpage_num = 10
        offset = (page - 1) * perpage_num
        limit = page * perpage_num
        users = User.objects()[offset:limit].order_by('-register_at')
        return render_template('admin/user.html', 
                                users=users,
                                page=page)
    return redirect(url_for('admin.admin_login'))
Esempio n. 17
0
def pin_detail(pin_id):
    if g.user_id:
        pin = Pin.objects(id=pin_id).first()

        user = User.objects(id=g.user_id).first()
        isliked = 1 if user in pin.likes else 0

        pin_item = {}
        pin_item['pin_id'] = str(pin.id)
        pin_item['type'] = pin.type
        pin_item['content'] = pin.content
        pin_item['pic'] = pin.pic
        pin_item['avatar'] = pin.avatar
        pin_item['price'] = pin.price
        pin_item['currency'] = pin.currency
        pin_item['stamp'] = pin.stamp
        pin_item['comments_count'] = pin.comments_count
        pin_item['isliked'] = 1 if user in pin.likes else 0

        if pin.comments_count == 0:
            pin_item['comments'] = []
        elif pin.comments_count == 1:
            first_comment = {
                'content':pin.first_comment,
                'author_id':str(pin.first_comment_user.id),
                'nickname':pin.first_comment_user.nickname,
                'avatar':pin.first_comment_user.avatar,
                'create_at':pin.first_comment_create_at.strftime('%Y-%m-%d %H:%M:%S'),
            }
            pin_item['comments'] = [first_comment]
        elif pin.comments_count >= 2:
            first_comment = {
                'content':pin.first_comment,
                'author_id':str(pin.first_comment_user.id),
                'nickname':pin.first_comment_user.nickname,
                'avatar':pin.first_comment_user.avatar,
                'create_at':pin.first_comment_create_at.strftime('%Y-%m-%d %H:%M:%S'),
            }
            last_comment = {
                'content':pin.last_comment,
                'author_id':str(pin.last_comment_user.id),
                'nickname':pin.last_comment_user.nickname,
                'avatar':pin.last_comment_user.avatar,
                'create_at':pin.last_comment_create_at.strftime('%Y-%m-%d %H:%M:%S'),
            }
            pin_item['comments'] = [first_comment, last_comment]


        pin_item['likes_count'] = pin.likes_count
        pin_item['create_at'] = pin.create_at.strftime('%Y-%m-%d %H:%M:%S')


        return (json.dumps(pin_item), 200)
    return ('pin detail timeout', 400)
Esempio n. 18
0
def fav_pin(pin_id):
    if g.user_id:
        already_fav = Fav.objects(owner=g.user_id, pin=pin_id).first()
        if not already_fav:
            pin = Pin.objects(id=pin_id).first()
            user = User.objects(id=g.user_id).first()
            fav = Fav(owner=user,
                      pin=pin,
                      create_at=datetime.utcnow())
            fav.save()
            return ('fav success', 200)
        return ('already fav', 400)
    return ('fav pin session timeout', 400)
Esempio n. 19
0
def admin_del_user(user_id):
    if g.admin:
        user = User.objects(id=user_id).first()
        user.delete()
        pins = Pin.objects(owner=user_id)
        for pin in pins:
            pin.delete()
        timelines = Timeline.objects(owner=user_id)
        for timeline in timelines:
            timeline.delete()
        return redirect(url_for('admin.admin_user'))
    return redirect(url_for('admin.admin_login'))

    
Esempio n. 20
0
def avata_url(email):
    if '@' not in email:
        return ('email error', 400)


    
    user = User.objects(email=email).first()

    res_data = {
                'avatar':user.avatar,
               }
    response = make_response(json.dumps(res_data))
    #response.headers
    response.headers['Version'] = '1'
    return response
Esempio n. 21
0
def relation_unfollow(user_id):
    if g.user_id:
        follower = User.objects(id=user_id).first()
        user = User.objects(id=g.user_id).first()
        #TODO
        # user.update(***)
        User.objects(id=g.user_id).update_one(pull__followers=follower, inc__followers_count=-1)
        User.objects(id=user_id).update_one(pull__fans=user, inc__fans_count=-1)
        return ('unfollow success', 200)
    return ('unfollow session timeout', 400)
Esempio n. 22
0
def relation_follow(user_id):
    if g.user_id:

        follower = User.objects(id=user_id).first()
        user = User.objects(id=g.user_id, followers__nin=[follower]).first()
        if user:
        
            #TODO need optim
            # user.update(***)
            User.objects(id=g.user_id).update_one(push__followers=follower, inc__followers_count=1)
            User.objects(id=user_id).update_one(push__fans=user, inc__fans_count=1)

            return ('follow success', 200)
        return ('already followed', 400)
    return ('relation follow session timeout', 401)
Esempio n. 23
0
def pin_likes(pin_id, page_num):
    if g.user_id:
        limit = 5 
        offset = (page_num - 1) * limit
        pin = Pin.objects(id=pin_id).fields(slice__likes=[offset, limit]).first()
        user = User.objects(id=g.user_id).first()

        like_list = []
        for like in pin.likes:
            like_item = {}
            like_item['isfollowed'] = 1 if user in like.fans else 0
            like_item['user_id'] = str(like.id)
            like_item['avatar'] = like.avatar
            like_item['nickname'] = like.nickname
            like_list.append(like_item)
        res_data = {
            'total':len(like_list),
            'items':like_list,
        }
        return (json.dumps(res_data), 200)
    return ('likes list session timeout', 400)
Esempio n. 24
0
def web_set_info():
    if g.user_id:
        user = User.objects(id=g.user_id).first()
        return render_template('set_info.html', user=user)
    return redirect(url_for('.web_login'))