Beispiel #1
0
def chnage_photo():
    user = request.user.get()
    data = request.forms
    data['photo'] = request.files.get('photo')
    v = Validator(data)
    if user:
        img = data.get("photo")
        if img is not None:
            path = profile_path.format(id=user.id)
            photo_name = 'photo_'+str(user.id)+"_"+str(uuid.uuid4())+".png"
            thumbnail_name = photo_name.rstrip(".png")+".thumbnail.png"
            if not os.path.exists(path): os.makedirs(path)
            remove_similar(path, photo_name)

            image_path = os.path.join(path, photo_name)
            thumbnail_path = os.path.join(path, thumbnail_name)

            photo = image_thumbnail(img, width=200, height=200, fill='cover')
            photo.save(image_path)

            img.file.seek(0)
            thumbnail = image_thumbnail(img, width=50, height=50, fill='cover')
            thumbnail.save(thumbnail_path)

            user.photo = "/file"+image_path.replace(files_dir, '').replace("\\", '/')
            user.photo_s = "/file"+thumbnail_path.replace(files_dir, '').replace("\\", '/')
            request.db.add(user)
            request.db.commit()
            return {"status": "ok", "photo": user.photo}
    return {"status": "fail",
            "errors": v.errors}
Beispiel #2
0
def registrate():
    data = request.forms
    data['photo'] = request.files.get('photo')
    v = Validator(data)
    v.field("first_name").required()
    v.field("last_name").required()
    v.field("email").required().email()
    v.field("password").required().length(min=5, message="Длина пароля не менее %(min)d символов")
    v.field("photo").image()
    if data.get("password") != data.get("repassword"):
        v.add_error('password', 'Пароли не совпадают', 'wrong_repassword')
    if(v.is_valid()):
        data = v.valid_data
        password_hash = hash_password(data.get('password'))
        user = request.db(User).get_by_email(data['email'])
        if user:
            v.add_error('email', 'Электронный адрес уже используется.', 'email_is_used_already')
        else:
            user = User()
            user.email = data['email']
            user.first_name = data['first_name']
            user.last_name = data['last_name']
            user.password = password_hash
            request.db.add(user)
            request.db.commit()
            img = data.get("photo")
            if img is not None:
                path = profile_path.format(id=user.id)
                photo_name = 'photo_'+str(user.id)+"_"+str(uuid.uuid4())+".png"
                thumbnail_name = photo_name.rstrip(".png")+".thumbnail.png"
                if not os.path.exists(path): os.makedirs(path)
                remove_similar(path, photo_name)

                image_path = os.path.join(path, photo_name)
                thumbnail_path = os.path.join(path, thumbnail_name)

                photo = image_thumbnail(img, width=200, height=200, fill='cover')
                photo.save(image_path)

                img.file.seek(0)
                thumbnail = image_thumbnail(img, width=50, height=50, fill='cover')
                thumbnail.save(thumbnail_path)

                user.photo = "/file"+image_path.replace(files_dir, '').replace("\\", '/')
                user.photo_s = "/file"+thumbnail_path.replace(files_dir, '').replace("\\", '/')
                request.db.commit()
            auth(user)
            return {"status": "ok", "reload": True}
    return {"status": "fail",
            "errors": v.errors}