def register(self, request): if request.method == 'POST': username = request.form['username'] email = request.form['email'] password = request.form['password'] confirm_password = request.form['confirm_password'] datab = db.get_db() error = None userExist = model.get(datab, 'users', None, username) if not username: error = 'Username is required.' elif not email: error = 'Email is required.' elif not password: error = 'Password is required.' elif password != confirm_password: error = 'password confirmation is different' elif userExist is not None: error = 'User {} is already registered.'.format(username) if error is None: hashed_Password = generate_password_hash(password) model.put(datab, 'users', username, email, hashed_Password) return "ok" flash(error) return error
def login(self, request): if request.method == 'POST': username = request.form['username'] password = request.form['password'] datab = db.get_db() error = None user = model.get(datab, 'users', None, username) if user is None: error = 'Incorrect username.' elif not check_password_hash(user[3], password): error = 'Incorrect password.' if error is None: session.clear() session['logged_in'] = True session['userId'] = user[0] session['username'] = username session['email'] = user[2] return "login" flash(error) return error
def crud(self, request, id=""): datab = db.get_db() # name= request.form['username'] # email= request.form['email'] # password=request.form['password'] if request.method == 'GET': user = model.get(datab, 'users', id) return jsonify(user) elif request.method == 'PUT': name = request.form['username'] email = request.form['email'] password = request.form['password'] model.put(datab, 'users', name, email, password) response = 'The User #' + str(id) + 'is added' return response elif request.method == 'DELETE': model.delete(datab, 'users', id) response = 'The User #' + str(id) + 'is deleted now' print("********************") print(response) return response elif request.method == 'POST': new_name = request.form['username'] new_email = request.form['email'] new_password = request.form['password'] actual = model.get(datab, 'users', id) if new_name == "": new_name = actual[1] if new_email == "": new_email = actual[2] if new_password == "": new_password = actual[3] model.update(datab, 'users', id, new_name, new_email, new_password) response = 'The User #' + str(id) + 'is updated' return response
def crud(self,request,id=""): datab = db.get_db() if request.method == 'GET': articles = model.get(datab,'articles',id) return jsonify(articles) elif request.method == 'PUT': title= request.form['title'] body= request.form['body'] userId= session['userId'] model.put(datab,'articles',title,body,userId) response = 'your article is added, thanks!' return response elif request.method =='DELETE': model.delete(datab,'articles', id) response = 'The User #'+str(id)+'is deleted now' return response elif request.method == 'POST': new_title= request.form['title'] new_body= request.form['body'] new_userId=session['userId'] actual = model.get(datab, 'articles', id) if new_title == "": new_title = actual[1] if new_body == "": new_body= actual[2] if new_userId == "": new_userId = actual[4] model.update(datab,'articles',id, new_title, new_body, new_userId) response = 'The article #'+str(id)+'is updated' return response
def getAll(self): datab = db.get_db() users = model.get(datab, 'users') return jsonify(users)
def returnArt(self,id=""):#added d2 ex2 datab = db.get_db() return model.get(datab,'articles',id)
def getAll(self): datab = db.get_db() articles = model.get(datab,'articles') print(articles) return jsonify(articles)