def test2(self): user1 = User(user_name = "Saravanan", password="******", email = "*****@*****.**") user1.save() self.event = Event(title = "Some title", description="some description", people_wants_to_meet="saravanan", time_of_event="2012-12-18", user = user1) self.assertIsNone(self.event.full_clean())
def post(self,request): r = request.data new_record = User(first_name = r['first name'],messenger_id = r['messenger user id']) new_record.save() data = User.objects.all() serializer = UserSerializer(data, many=True) return Response(serializer.data)
def add_user(payload, user): #adds user to the db new_user = User(first_name=payload["first_name"], last_name=payload["last_name"], reporter=user) new_user.save() #deserializes new_user and adds to db serialized = UserSerializer(new_user) #to json return serialized.data
def users(username=None): """ The users fuction provide three user's action user action : 1.get user data, 2.regist user, 3.delete user """ if request.method == 'GET': if username is None: return jsonify(result=ResultType.GET_USER_NO_DATA) user = User.query.filter(User.username == username).first() if user: result = {} result['result'] = ResultType.GET_USER_SECCESS result['user'] = user response = make_response(json.dumps(result, default=to_json)) response.headers['Content-Type'] = 'application/json' return response else: return jsonify(result=ResultType.GET_USER_NO_DATA) elif request.method == 'POST': if request.form['username'] is None or request.form['username'] == '': return jsonify(result=ResultType.USERNAME_IS_NONE_ERROR) if request.form['password'] is None or request.form['password'] == '': return jsonify(result=ResultType.PASSWORD_IS_NONE_ERRPR) user = User.query.filter( User.username == request.form['username']).first() if user: return jsonify(result=ResultType.USER_EXIST_ERROR) user = User(request.form['username'], request.form['password']) user.save() return jsonify(result=ResultType.REGISTER_SECCESS) elif request.method == 'DELETE': if username: user = User.query.filter(User.username == username).first() if user: user.delete() return jsonify(status=0) else: return jsonify(status=-1) else: return jsonify(status=-1)
def register(): form = RegisterForm(request.form) if request.method == 'POST' and form.validate(): if re.match("^[0-9a-zA-Z_]+$",form.username.data): user = User(username=form.username.data, password=bcrypt.generate_password_hash(form.password.data), email=form.email.data) user.save() flash('Registration Successful, login please.') return redirect('/login') else: flash('error username') return render_template('register.html', form=form)