def post(self, roomName): room = Room.query.filter(Room.name == roomName).first() if not room: return "Room {} not found.".format(roomName), 404 events = request.get_json() eventSchema = EventSchema() Event.begin() for evt in events: user = User.query.filter(User.uid == evt['uid']).first() if (user == None): #Lucio 20190516: Add unknown users User.begin() user = User("UNKNOWN USER", "*****@*****.**", evt['uid'], UserTypesEnum.STUDENT) user.add(user) User.commit() result = eventSchema.load(evt) if (len(result.errors) > 0): return 'Error parsing content:{}.'.format(result.errors), 400 event = Event() event.roomId = room.id event.userId = user.id event.eventType = EventTypesEnum[result.data['eventType']] event.dateTime = result.data['dateTime'] event.add(event) Event.commit() return "All events updated", 200
def signup(): # Output message if something goes wrong... msg = '' if request.method == 'POST' and 'name' in request.form and 'surname' in request.form and 'password' in request.form and 'email' in request.form: # Create variables for easy access name = request.form['name'] name = Markup.escape(name) surname = request.form['surname'] surname = Markup.escape(surname) password = request.form['password'] password = Markup.escape(password) email = request.form['email'] email = Markup.escape(email) id_card = request.files['file'] if id_card and allowed_file(id_card.filename): # Check if account exists using MySQL #cursor = db.connection.cursor() # HARCODED SQL # query = "'SELECT * FROM accounts WHERE email = " + email # cursor.execute(query) # SECURE VERSION #cursor.execute('SELECT * FROM accounts WHERE email = %s', [email]) #account = cursor.fetchone() account = User.query.filter_by(email=email).first() # If account exists show error and validation checks if account: msg = 'Account already exists!' elif not re.match(r'[^@]+@[^@]+\.[^@]+', email): msg = 'Invalid email address!' elif not name or not surname or not password or not email: msg = 'Please fill out the form!' else: filename = secure_filename(id_card.filename) id_card.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename)) print(id_card.filename) # Account doesnt exists and the form data is valid, now insert new account into accounts table #cursor.execute("INSERT INTO accounts (name, surname, email, filename, password, amount) VALUES" # "(%s, %s, %s, %s, %s, 0)", (name, surname, email, id_card.filename, password,)) #db.connection.commit() new_user = User(name, surname, email, password, filename) User.add(new_user) msg = 'You have successfully registered!' else: msg = 'Only PNG/JPG/JPEG file are allowed' elif request.method == 'POST': # Form is empty... (no POST data) msg = 'Please fill out the form!' # Show registration form with message (if any) return render_template('signup.html', msg=msg)
def prepare_models(): from app.models.user import User from app.models.flower import Flower from app.models.song import Song user_owner = User.add('*****@*****.**', 'mapix', 'mapix') user_guardian = User.add('*****@*****.**', 'imapix', 'imapix') Flower.add(user_owner, user_guardian) Song.add('Hello,I Love You 2014', '小普') Song.add('欢迎你来大工厂demo', '邵小毛') Song.add('Dear Mama Remix', '马戏团小丑') Song.add('四月挽歌', '周云蓬')
def verify_moble(phone, code): form = UserPhoneForm().validate_for_api() is_login = cloud.verify_sms_code(form.account.data, form.secret.data) if not is_login: raise AuthFailed() user = User.query.filter_by(phone_number=str(phone)).first() if not user: with db.auto_commit(): user = User() user.phone_number = phone user.add() return dict(uid=user.id, scope=user.scope)
def register(): form = RegisterForm(request.form) if request.method == 'POST' and form.validate(): user = User().set_attrs(form.data) with db.auto_commit(): user.add() send_email(user.email, '激活你的账户', 'email/confirm.html', token=user.generate_token()) flash('Email has been sent!Please confirm the login message') return redirect(url_for('web.auth:login')) return render_template('auth/register.html', form=form)
def install_step1(form): if form.validate_on_submit(): create_tables(db) from app.models.site import SiteMeta from app.models.user import User metas = { "name": form.name.data, "description": "这是一个书籍站点", "about": "这个地方可以用来介绍您自己,或者您的网站。" } SiteMeta.add(metas) User.add(form.username.data, form.password.data) current_app.start = True set_site(current_app) return render_template("admin/start/install-step1.html", username=form.username.data) return render_template("admin/start/install.html", form=form)
def create_user(): payload = request.get_json() new_user = User(first_name=payload.get("firstName", None), last_name=payload.get('lastName', None), email=payload.get('email', None), phone=payload.get('phone', None), group_id=payload.get('groupId', None), company_id=payload.get('companyId', None), account_id=payload.get('accountId', None)) #TODO: Add validation of request before hitting the database created_user = User.add(db.session, new_user) return jsonify(created_user.serialize)
def post(self): args = get_args() if not args: return response("Invalid parameters", 422) user = (User.query.filter(User.active == False).filter( User.email == args["email"]).first()) if user: return self.reactive(user, args) else: try: user = User(name=args["name"], email=args["email"], uid=args["uid"], userType=args["userType"]) user.add(user) except SQLAlchemyError as e: return rollback(e, db), 406 else: return schema.dump(user).data, 201
def signup(): # Output message if something goes wrong... msg = '' if request.method == 'POST' and 'name' in request.form and 'surname' in request.form and 'password' in request.form and 'email' in request.form: # Create variables for easy access name = request.form['name'] surname = request.form['surname'] password = hashlib.sha256( request.form['password'].encode("utf8")).hexdigest() email = request.form['email'] id_card = request.files['file'] if id_card and allowed_file(id_card.filename): account = User.query.filter_by(email=email).first() # If account exists show error and validation checks if account: msg = 'Account already exists!' elif not re.match(r'[^@]+@[^@]+\.[^@]+', email): msg = 'Invalid email address!' elif not name or not surname or not password or not email: msg = 'Please fill out the form!' else: filename = secure_filename(id_card.filename) id_card.save( os.path.join(current_app.config['UPLOAD_FOLDER'], filename)) new_user = User(name, surname, email, password, filename) User.add(new_user) msg = 'You have successfully registered!' else: msg = 'Only PNG/JPG/JPEG file are allowed' elif request.method == 'POST': # Form is empty... (no POST data) msg = 'Please fill out the form!' # Show registration form with message (if any) return render_template('signup.html', msg=msg)
def register(): form = RegisterForm() if form.validate_on_submit(): if form.password.data == form.password2.data: user = User.add(form.username.data,form.password.data) if user: return redirect(url_for("auth.login")) else: flash("username has exsist!") else: flash("two password must be same!") return render_template("register.html",form=form)
print("RoomsList: {} ...\n".format(len(rooms))) Room.query.delete() events = Event.query.all() print("EventsList: {} ...\n".format(len(events))) Event.query.delete() users = User.query.all() print("UsersList: {} ...\n".format(len(users))) User.query.delete() user = User(name="uName", email="*****@*****.**", uid="000000000", userType=UserTypesEnum.PROFESSOR) user.add(user) print("User ID {}\n".format(user.id)) room = Room() room.name = "E003" roomUser = RoomUser() roomUser.user = user room.users.append(roomUser) room.active = True room.add(room) print("Room ID {}\n".format(room.id)) schedule = Schedule() schedule.room = room
def save_user(data): return User.add(data)
def post(self): #get the json data sent over post as a dictionary try: #check if it was json data that was sent if request.is_json: data = request.get_json() else: response = { "message": "Please supply json data", "status": "failure" } return make_response(jsonify(response)), 400 except Exception as e: response = { "message": "An error occured: Here are the details - " + str(e), "status": "failure" } return make_response(jsonify(response)), 500 #ensure that username, email and password keys are provided try: username = data['username'] password = data['password'] email = data['email'] except KeyError as missing_key: response = { "message": "Please supply a " + str(missing_key), "status": "failure" } return make_response(jsonify(response)), 400 #check if username, password or email is empty if not (username) or not (password) or not ( email): # using not instead response = { "message": "Please supply a value for username, email and password", "status": "failure" } return make_response(jsonify(response)), 400 # check if what was got from json for username or password is not a string if not isinstance(username, str) or not isinstance( password, str) or not isinstance(email, str): response = { 'message': 'Please supply string values for username, email and password', "status": "failure" } return make_response(jsonify(response)), 401 #check if email is not in the right format if re.search(r'[\w\.-]+@[\w\.-]+', email) is None: response = { "message": "Please supply a valid email address", "status": "failure" } return make_response(jsonify(response)), 400 # Check to see if the user already exists user = User.query.filter( or_(User.username == data['username'], User.email == data['email'])).first() if user is not None: # There is an existing user. We don't want to register users twice # Return a message to the user telling them that they they already exist response = { "message": 'User already exists. Please login.', "status": "failure" } return make_response(jsonify(response)), 401 try: # Register the user username = data['username'] password = generate_password_hash(data['password']) email = data['email'] new_user = User(username=username, email=email, password=password) new_user.add() response = { "message": 'You registered successfully. Please log in.', "status": "success" } # return a response notifying the user that they registered successfully return make_response(jsonify(response)), 201 except Exception as e: # An error occured, therefore return a string message containing the error response = { "message": "An error occurred, these are the details: " + str(e), "status": "failure" } return make_response(jsonify(response)), 500