示例#1
0
def save_new_user(request):
    login = request.POST['login']
    email = request.POST['email']

    if len(login) == 0 or len(email) == 0:
        return Response('Bad input', content_type='text/plain', status_int=500)

    try:
        exists = DBSession.query(User).filter(or_(User.login == login,
                                                  User.email == email)).count()
        if exists > 0:
            existing_user = DBSession.query(User).filter(User.login == login).first()
            return HTTPFound(location=route_url('show_user', request, id=existing_user.id))

        user = User(login, email)
        user.first_name = request.POST['first_name']
        user.last_name = request.POST['last_name']
        user.user_name = request.POST['user_name']
        user.password = request.POST['password']
        user.gender = request.POST['gender']
        user.created_date = datetime.datetime.now()
        user.last_login_date = datetime.datetime.now()

        DBSession.add(user)
        DBSession.flush()
        return HTTPFound(location=route_url('show_user', request, id=user.id))
    except DBAPIError:
        return Response(conn_err_msg, content_type='text/plain', status_int=500)
示例#2
0
def add_review(request):
    try:
        review = Review()
        review.title = request.POST['title']
        review.description = request.POST['description']
        review.rating = int(request.POST['rating'])
        review.review_type = request.POST['review_type']
        review.created_date = datetime.datetime.now()

        # filename = request.POST['img'].filename
        input_file = request.POST['img'].file
        dir_path = os.path.join('..', 'images', 'reviews', '%s' % uuid.uuid1())

        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        file_path = os.path.join(dir_path, '%s.png' % uuid.uuid4())

        temp_file_path = file_path + '~'
        output_file = open(temp_file_path, 'wb')

        input_file.seek(0)

        while True:
            data = input_file.read(2 << 16)
            if not data:
                break
            output_file.write(data)

        output_file.close()

        os.rename(temp_file_path, file_path)

        DBSession.add(review)
        DBSession.flush()
        return HTTPFound(location=route_url('show_review', request, id=review.id))
    except DBAPIError:
        return Response(conn_err_msg, content_type='text/plain', status_int=500)