예제 #1
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)