Exemplo n.º 1
0
def upload():
    img_url = file.url('default.jpg')  #获取默认头像的地址
    form = UploadPhotos()  #表单类实例化
    if form.validate_on_submit():
        photo = form.photo.data  #获取上传的文件
        suffix = photo.filename.split('.')[-1]  #获取后缀
        #在这个基础上完善  upload下存放图片 以每个人的username为名 存储当前用户的所有图片
        #生成唯一的随机图片名
        while True:
            newName = random_name(suffix)
            path = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],
                                newName)
            if not os.path.exists(path):
                break
        #保存上传图片
        file.save(photo, name=newName)
        #删除之前上传的图片
        if current_user.icon != 'default.jpg':
            os.remove(
                os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],
                             current_user.icon))
            os.remove(
                os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],
                             's_' + current_user.icon))
        img_zoom(path)  #进行缩放
        current_user.icon = newName
        db.session.add(current_user)  #把修改后的图片名 保存到表中
        db.session.commit()
        img_url = file.url(current_user.icon)  #获取上传后的图片地址
    return render_template('owncenter/photo.html', form=form, img_url=img_url)
Exemplo n.º 2
0
def upload():
    form = Uploadedphotos()
    img_url = file.url('default.jpg')
    if form.validate_on_submit():
        photo = form.photo.data
        suffix = photo.filename.split('.')[-1]
        while True:
            newname = random_name(suffix)
            path = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],
                                newname)
            if not os.path.exists(path):
                break
        file.save(photo, name=newname)
        if current_user.icon != 'default.jpg':
            os.remove(
                os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],
                             current_user.icon))
            os.remove(
                os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],
                             's_' + current_user.icon))
        img_zoom(path)
        current_user.icon = newname
        db.session.add(current_user)
        db.session.commit()
        img_url = file.url(current_user.icon)
    return render_template('owncenter/uploadphoto.html',
                           form=form,
                           img_url=img_url)
Exemplo n.º 3
0
def upload_icon():
    form = UploadPhotos()
    if current_user.icon != 'default.jpg':
        img_url = file.url(current_user.icon.split('_')[1])
    else:
        img_url = file.url('default.jpg')
    if form.validate_on_submit():
        photo = form.photo.data
        suffix = photo.filename.split('.')[-1]
        while True:
            newname = random_name(suffix)
            path = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], newname)
            if not os.path.exists(path):
                break
        file.save(photo, name=newname)
        img = Image.open(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], newname))
        if 1.3 < img.size[1]/img.size[0] < 1.5:
            if current_user.icon != 'default.jpg':
                os.remove(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], current_user.icon))
                os.remove(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], current_user.icon.split('_')[1]))
            img_zoom(path)
            current_user.icon = 's_' + newname
            db.session.add(current_user)
            db.session.commit()
            img_url = file.url(current_user.icon.split('_')[1])
            # print(img_url)
            flash('上传成功')
        else:
            os.remove(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], newname))
            flash('图片尺寸比例必须为1.3-1.5')
    ############################################
    c = Course.query.filter_by(pid=0)
    c1 = Course.query.filter(and_(Course.path.endswith(','), Course.pid != 0))
    c2 = Course.query.filter(and_(not_(Course.path.endswith(',')), Course.pid != 0))
    return render_template('owncenter/icon.html', form=form, img_url=img_url,course=c,course1=c1,course2=c2)
Exemplo n.º 4
0
def upload():
    form = UploadPhotos()
    form1 = UploadInfo()
    # form2 = SubmitInfo()
    if form.validate_on_submit():

        print(1111111111111111111111111111111111)
        photo = form.photo.data
        suffix = photo.filename.split('.')[-1]
        while True:
            newname = random_name(suffix)
            path = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],newname)
            if not os.path.exists(path):
                break
        file.save(photo,name=newname)
        if current_user.icon != 'default.jpg':
            os.remove(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],current_user.icon))
        img_zoom(path)
        current_user.icon = newname
        db.session.add(current_user)
        db.session.commit()
        img_url = file.url(current_user.icon)
        # print(img_url)
        flash('上传成功')
        # return render_template('owncenter/center.html', form=form,form1=form1,img_url=img_url)
    # if current_user.icon != 'default.jpg':
    img_url = file.url(current_user.icon)
    # else:
    #     img_url = '#'
    u1 = User.query.filter_by(username=current_user.username).first()
    if form1.validate_on_submit():
        if form1.sex.data == 'm':
            u1.sex = True
            u1.save()
        else:
            u1.sex = False
            u1.save()
        if form1.age.data != None:
            u1.age = form1.age.data
            u1.save()
            flash('修改成功')

    # if form1.sex.data == None:
    #     print(form1.sex.data)
    #     form1.sex.data = 'm'


    if u1.sex == True:
        form1.sex.data = 'm'
    else:
        form1.sex.data = 'w'
    form1.age.data = u1.age



    return render_template('owncenter/center.html',form=form,form1=form1,img_url=img_url)
Exemplo n.º 5
0
def change_password():
    form = Icon()
    if form.validate_on_submit():
        shuffix = os.path.splitext(form.file.data.filename)[-1]
        #生成随机的图片名
        while True:
            newName = new_name(shuffix)
            if not os.path.exists(
                    os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],
                                 newName)):
                break
        file.save(form.file.data, name=newName)
        #判断用户更改头像 原头像是否为默认 不是则将原图片删除
        if current_user.icon != 'default.jpg':
            os.remove(
                os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],
                             current_user.icon))

        #执行缩放
        img = Image.open(
            os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], newName))
        img.thumbnail((300, 300))
        #保存新的图片名称为新的图片的s_newname
        img.save(
            os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],
                         's_' + newName))

        current_user.icon = newName
        db.session.add(current_user)
        flash('头像上传成功')
    img_url = file.url(current_user.icon)
    return render_template('user/change_icon.html', form=form, img_url=img_url)
Exemplo n.º 6
0
def change_icon():
    form = Icon()
    if form.validate_on_submit():
        filename = form.icon.data.filename
        suffix = filename.split('.')[-1]
        #循环确保生成的图片名称唯一性
        while True:
            filename = random_filename(suffix)
            path = os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],filename)
            if not os.path.exists(path):
                break
        file.save(form.icon.data,name=filename) #保存图片
        #删除之前的图片
        #原图一张
        #在首页展示的时候 一张 大小为100  s_
        if current_user.icon != 'default.jpg':
            os.remove(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],current_user.icon))
            os.remove(os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'],'s_'+current_user.icon))
        #执行缩放
        img_zoom(path)

        current_user.icon = filename
        #数据保存到表中
        db.session.add(current_user)
        #获取图片地址
    img_url = file.url(current_user.icon)
    return render_template('user/icon.html',form=form,img_url=img_url)
Exemplo n.º 7
0
def register():
    form = Register()
    if form.validate_on_submit():
        photos = request.files.get('icon')
        suffix = photos.filename.split('.')[-1]
        imgInfo = random_filename(suffix)
        filename = file.save(photos, name=imgInfo[1])
        path = os.path.join(Config.UPLOADED_PHOTOS_DEST, filename)
        img_zoom(path)
        img_url = file.url(filename)
        u = User(username=form.username.data, password=form.userpass.data, email=form.email.data,icon=img_url)
        u.save()
        flash('恭喜注册成功')
        return redirect(url_for('user.login'))
    return render_template('user/register.html',form=form)
Exemplo n.º 8
0
def self_center(page):
    form = UploadInfo()
    img_url = file.url(current_user.icon)
    u = User.query.filter_by(username=current_user.username).first()
    if form.validate_on_submit():
        if form.sex.data == 'm':
            u.sex = True
            u.save()
        else:
            u.sex = False
            u.save()
        if form.age.data != None:
            u.age = form.age.data
            u.save()
            flash('修改成功')
    if u.sex == True:
        form.sex.data = 'm'
    else:
        form.sex.data = 'w'
    form.age.data = u.age
    register_date = u.register_date
    lastlogin_date = u.lastlogin_date
    now = datetime.utcnow() + timedelta(hours=8)
    date = str((now-register_date)).split(' ')
    if len(date) == 1:
        date = 1
    else:
        date = int(date[0])
    print(now)
    print(register_date)
    print(date)
    print("--------------------------------")
    print(u.favorite)
    print("--------------------------------")
    print(User.query.filter_by(confirm=1))
    print(type(User.query.filter_by(confirm=1)))
    print(type(u.favorite))
    listp = []
    for post in u.favorite.all():
        listp.append(post.id)
    print(listp)
    pagination = Posts.query.filter(Posts.id.in_(listp)).paginate(page, current_app.config['EVERY_PAGE_NUM'], False)
    favorite = pagination.items
    #########################################
    c = Course.query.filter_by(pid=0)
    c1 = Course.query.filter(and_(Course.path.endswith(','), Course.pid != 0))
    c2 = Course.query.filter(and_(not_(Course.path.endswith(',')), Course.pid != 0))
    return render_template('owncenter/center.html',form=form,img_url=img_url,date=date,lastlogin_date=lastlogin_date,now=now,pagination=pagination,favorite=favorite,course=c,course1=c1,course2=c2)