Ejemplo n.º 1
0
def upload_with_filter():
    content = request.get_json()
    # save remote image to local
    image_url = content.get('imageURL', '')
    filename = image_url[image_url.index('name') + 5:-4]
    filepath = utils.getTmpPath(filename)
    if not utils.fileExist(filepath):
        return jsonify(status=0, msg='filter image not exist')
    new_image_path = utils.getUploadPath(filename)
    utils.convertImageTo(filepath, new_image_path)
    diary = Diary(uid=g.uid,
                  diary_type=1,
                  image=filename,
                  additional_actions=json.dumps(content['actions']),
                  location=content['location'],
                  create_time=utils.getSecondTime(),
                  last_modified_time='',
                  is_delete=0)

    db.session.add(diary)
    db.session.commit()
    # create image diary with additional_actions
    actions = content['actions']
    compositeImage(filename, actions)
    # create thumbnail image for it
    utils.createMiniImage(filename)
    return jsonify(imgUrl=url_for('app.getImage', name=filename))
Ejemplo n.º 2
0
def diary():
    if request.method == 'POST':
        content = request.get_json()
        if content.get('id', '') != '':
            diary = Diary.query.get(int(content['id']))
            diary.title = content.get('title', '')
            diary.content = content.get('text', '')
            diary.weather = content.get('weather', '')
            diary.has_image = content.get('has_image', '')
            diary.last_modified_time = utils.getSecondTime()
            try:
                db.session.commit()
            except Exception as e:
                print(e)
        else:
            diary = Diary(uid=g.uid,
                          diary_type=0,
                          title=content.get('title', ''),
                          content=content.get('text', ''),
                          has_image=content.get('has_image', ''),
                          location=content.get('location', ''),
                          weather=content.get('weather', ''),
                          create_time=utils.getSecondTime(),
                          last_modified_time=utils.getSecondTime(),
                          is_delete=0)
            db.session.add(diary)
            db.session.commit()

        return jsonify(status='1', id=diary.id)
    else:
        did = request.args.get('id', 0)
        print(did)
        if int(did) != 0:
            data = getDiaryById(did)
        else:
            data = getDiary(request.args.get("end_time", ''),
                            max_diary_count=MAX_DIARY_COUNT)
        return jsonify(diary=data)
Ejemplo n.º 3
0
def login():
    content = request.get_json()
    code = str(content['code'])
    sessionKey, openid = getUserInfo(code)
    res = User.query.filter_by(openid=openid).first()
    # 如果没有查询到用户id,则注册一个新的用户
    if res is None:
        u = User(openid=openid,
                 session_key=sessionKey,
                 token="",
                 expiration_time=utils.getSecondTime() + TOKEN_TIMEOUT)
        db.session.add(u)
        db.session.commit()

    # 为用户生成token
    token = utils.calculateHashCodeForString(openid +
                                             str(utils.getMilliTime()))
    res = User.query.filter_by(openid=openid).first()
    if res is not None:
        res.token = token
        res.expiration_time = utils.getSecondTime() + TOKEN_TIMEOUT
        db.session.commit()

    return jsonify(token=token)
Ejemplo n.º 4
0
def upload():
    image = request.files['image']
    if image and utils.allowedUploadFileType(image.filename):
        # check if the image belong to the text diary
        diaryID = request.form.get('id', '')
        if diaryID != '':
            res = addTextDiaryImage(image, diaryID)
            if res == 0:
                return jsonify(status=1)
            else:
                return jsonify(status=0, msg=res)
        filehash = utils.calculateHashCodeForString(image.filename, 'sha1')
        # type = image.filename.rsplit('.', 1)[1]
        filename = filehash
        filepath = os.path.join(UPLOAD_FOLDER, filename)
        # if utils.fileExist(filepath):
        #     return jsonify(status=0,msg='diary has been created!')
        image.save(filepath)

    diary = Diary(uid=g.uid,
                  diary_type=1,
                  image=filename,
                  additional_actions=request.form.get('actions', ''),
                  location=request.form.get('location', ''),
                  create_time=utils.getSecondTime(),
                  last_modified_time='',
                  is_delete=0)

    db.session.add(diary)
    # create image diary with additional_actions
    actions = json.loads(request.form['actions'])
    compositeImage(filename, actions)
    # create thumbnail image for it
    utils.createMiniImage(filename)
    db.session.commit()
    return jsonify(imgUrl=url_for('app.getImage', name=filename))
Ejemplo n.º 5
0
def getDiary(get_end_time, max_diary_count=10):
    data = []
    day_count = 0
    diary_tpl = {'diary': {'text': [], 'image': []}, 'date': []}
    reference_time = ''
    end_time = utils.getSecondTime()
    if get_end_time != '':
        end_time = int(get_end_time)
    diarys = Diary.query.filter_by(uid=g.uid) \
                        .filter(Diary.create_time < end_time) \
                        .filter_by(is_delete=0) \
                        .order_by(Diary.create_time.desc()) \
                        .limit(max_diary_count) \
                        .all()
    print('total {} diary be select !!!!!!!'.format(len(diarys)))
    if len(diarys) != 0:
        reference_time = utils.getZeroTime(diarys[0].create_time)
        for diary in diarys:
            while True:
                if diary.create_time < reference_time:
                    if diary_tpl['date'] != []:
                        data.append(copy.deepcopy(diary_tpl))
                        diary_tpl['diary']['text'] = []
                        diary_tpl['diary']['image'] = []
                        diary_tpl['date'] = []
                    reference_time -= utils.DAY
                    day_count += 1
                else:
                    break

            createTime = utils.getTimeFromTimestamp(diary.create_time)
            day = utils.getDayName(diary.create_time)
            if diary.diary_type == 0:  # text diary
                images = ''
                diaryImages = diary.images.all()
                if len(diaryImages) != 0:
                    images = [{
                        'url':
                        utils.createImageUrl(i.image) +
                        '&type=text_diary_image'
                    } for i in diaryImages]
                lastModifiedTime = utils.getTimeFromTimestamp(
                    diary.last_modified_time)
                d = {
                    'main': {
                        'id': diary.id,
                        'title': diary.title,
                        'text': diary.content,
                        'images': images,
                    },
                    'extra': {},
                    'system': {
                        'createdTime': [
                            createTime.year, createTime.month, createTime.day,
                            createTime.hour, createTime.minute,
                            createTime.second, day
                        ],
                        'lastModifiedTime':
                        diary.last_modified_time,
                        'weather':
                        diary.weather
                    }
                }
                diary_tpl['diary']['text'].append(d)
            else:
                actions = json.loads(diary.additional_actions)
                imageText = actions[1].get('text', 'NULL')
                d = {
                    'id':
                    diary.id,
                    'imageURL':
                    utils.createImageUrl(diary.image),
                    'text':
                    imageText,
                    'createtime': [
                        createTime.year, createTime.month, createTime.day,
                        createTime.hour, createTime.minute, createTime.second,
                        day
                    ]
                }
                diary_tpl['diary']['image'].append(d)
            diary_tpl['date'] = [
                createTime.year, createTime.month, createTime.day, day
            ]

        if day_count == 0:
            data.append(diary_tpl)
        data.reverse()
    return data