def new_list(): #import pdb; pdb.set_trace() list_name = request.form['list_name'] if list_name is not None and len(list_name) == 0: list_name = None if 'access_token' in session: access_token = session.get('access_token', None) db = connect_db () try: user_info = auth.authenticate_using_access_token(access_token, db) user_id = user_info['id'] except AccessTokenExpiredError: flash("Access Token Expired. Login again") return redirect(url_for('login')) except InvalidAccessTokenError: flash("In Valid Access Token. Login again") return redirect(url_for('login')) if list_name == None: flash('Enter the name of the new list') return redirect(url_for('users')) else: dm.create_new_list(list_name, user_id, db) return redirect(url_for('users')) else: return redirect(url_for('login'))
def users(): if 'access_token' in session: access_token = session.get('access_token', None) db = connect_db () try: user_info = auth.authenticate_using_access_token(access_token, db) user_id = user_info['id'] list_of_lists = dm.list_of_list(user_id, db) except AccessTokenExpiredError: flash("Access Token Expired. Login again") return render_template(url_for('login')) except InvalidAccessTokenError: flash("In Valid Access Token. Login again") return render_template(url_for('login')) except NoListFoundError: flash("No List Created yet.") return render_template(url_for('users', user_id = user_id )) #need to check this. as it seems it go in infinite iteration when no list is added by user. print type(user_info) if request.method == 'GET': print user_info response = make_response(render_template('user_home.html', user_info = user_info, lists = list_of_lists, access_token = access_token)) return no_cache(response) else: return redirect(url_for('login'))
def delete_item(list_id): if 'access_token' in session: print 'list_id: %s, access_token: %s' % (list_id, request.args.get('access_token')) access_token = session.get('access_token', None) db = connect_db () try: user_info = auth.authenticate_using_access_token(access_token, db) user_id = user_info['id'] list_info = dm.fetch_list(list_id, user_id, db) except AccessTokenExpiredError: flash("Access Token Expired. Login again") return redirect(url_for('login')) except InvalidAccessTokenError: flash("In Valid Access Token. Login again") return redirect(url_for('login')) except NoListFoundError: flash("No List found with this id:%d" %(list_id)) return redirect(url_for('users')) else: return redirect(url_for('login')) try: item_id = request.form['item_id'] dm.delete_item(user_id, list_id, item_id, db) list_info = dm.fetch_list(list_id, user_id, db) except NoItemFoundError: flash("No item found") return redirect(url_for('users')) except NoListFoundError: flash("No List found with this id:%d" %(list_id)) return redirect(url_for('users')) response = make_response(render_template('list_info.html', list_info=list_info)) return redirect(url_for('lists', list_id = list_id))
def logout(): query_access_token = request.args.get('access_token') db = connect_db () try: user_info = auth.authenticate_using_access_token(query_access_token, db) user_id = user_info['id'] except AccessTokenExpiredError: response_data = { "meta" : {}, "data" : { "users" : [{ "authentication" : 'Expired', "message" : "Access token Expired" }] } } status = 401 body = json.dumps(response_data) headers = { 'Content-Type' : 'application/json' } return (body, status, headers) except InvalidAccessTokenError: response_data = { "meta" : {}, "data" : { "users" : [ { "message" : "Invalid access token : %s" % (query_access_token) } ] } } status = 400 body = json.dumps(response_data) headers = { 'Content-Type' : 'application/json' } return (body, status, headers) #Logout user after authentication via access token dm.logout_user(query_access_token, db) response_data = { "meta" : {}, "data" : { "users" : [ { "status" : "user logged out" } ] } } status = 200 body = json.dumps(response_data) headers = { 'Content-Type' : 'application/json' } return (body, status, headers)
def logout(): # import pdb; pdb.set_trace() if 'access_token' in session: access_token = session.get('access_token', None) db = connect_db () try: user_info = auth.authenticate_using_access_token(access_token, db) user_id = user_info['id'] except AccessTokenExpiredError: flash("Access Token Expired. Login again") return redirect(url_for('login')) except InvalidAccessTokenError: flash("In Valid Access Token. Login again") return redirect(url_for('login')) dm.logout_user(access_token, db) session.pop('access_token', None) return redirect(url_for('home')) else: return redirect(url_for('login'))
def lists(list_id): if 'access_token' in session: print 'list_id: %s, access_token: %s' % (list_id, request.args.get('access_token')) access_token = session.get('access_token', None) db = connect_db () try: user_info = auth.authenticate_using_access_token(access_token, db) user_id = user_info['id'] list_info = dm.fetch_list(list_id, user_id, db) except AccessTokenExpiredError: flash("Access Token Expired. Login again") return redirect(url_for('login')) except InvalidAccessTokenError: flash("In Valid Access Token. Login again") return redirect(url_for('login')) except NoListFoundError: flash("No List found with this id:%d" %(list_id)) return redirect(url_for('users')) else: return redirect(url_for('login')) if request.method == 'GET': response = make_response(render_template('list_info.html', list_info=list_info)) return no_cache(response) else: # import pdb;pdb.set_trace() #input_values = map(lambda x:x(0), request.form) print request.form id = request.form.getlist('item_id') non_empty_id = filter(lambda x:x != "", id) print non_empty_id clean_input = zip( request.form.getlist('item_id'), request.form.getlist('status'), request.form.getlist('fields'), request.form.getlist('quantity') ) print clean_input for x in clean_input: id = x[0] status = x[1] name = x[2] quantity = x[3] if id in non_empty_id: try: print id list_item_info = dm.update_item(user_id, list_id, id, name, status, quantity, db) except NoItemFoundError: flash("No item found") return redirect(url_for('users')) except NoListFoundError: flash("No List found with this id:%d" %(list_id)) return redirect(url_for('users')) else: try: list_item_info = dm.add_item_to_list(list_id, user_id, name, status, quantity, db) except NoListFoundError: flash("No list found with list_id :%s" % list_id) return redirect(url_for('users')) list_info = dm.fetch_list(list_id, user_id, db) response = make_response(render_template('list_info.html', list_info=list_info)) return no_cache(response)
def Item_modification(list_id, item_id): query_access_token = request.args.get('access_token') db = connect_db () try: user_info = auth.authenticate_using_access_token(query_access_token, db) user_id = user_info['id'] except AccessTokenExpiredError: response_data = { "meta" : {}, "data" : { "users" : [{ "authentication" : 'Expired', "message" : "Access token Expired" }] } } status = 401 body = json.dumps(response_data) headers = { 'Content-Type' : 'application/json' } return (body, status, headers) except InvalidAccessTokenError: response_data = { "meta" : {}, "data" : { "users" : [ { "message" : "Invalid access token : %s" % (query_access_token) } ] } } status = 400 body = json.dumps(response_data) headers = { 'Content-Type' : 'application/json' } return (body, status, headers) #Change requests for name and status of an item if request.method == 'PUT': data = request.json['data']['lists'][0]['items'][0] print data if 'name' in data: try: list_item_info = dm.change_item_name(user_id, list_id, item_id, data['name'], db) except NoItemFoundError: response_data = { "meta" : {}, "data" : { "lists" : [{ "message" : "No Item found of this list" }] } } body = json.dumps(response_data) status = 400 headers = { "Content-Type" : 'application/json' } return (body,status,headers) except NoListFoundError: response_data = { "meta" : {}, "data" : { "lists" : [{ "message" : "No List found of this user" }] } } body = json.dumps(response_data) status = 400 headers = { "Content-Type" : 'application/json' } return (body,status,headers) response_data = { "meta" : {}, "data" : { "lists" : [ list_item_info ] } } status = 400 body = json.dumps(response_data) headers = { "Content-Type" : 'application/json' } return (body,status,headers) else: try: list_item_info = dm.change_item_status(user_id, list_id, item_id, data['status'], db) except NoItemFoundError: response_data = { "meta" : {}, "data" : { "lists" : [{ "message" : "No Item found of this list" }] } } body = json.dumps(response_data) status = 400 headers = { "Content-Type" : 'application/json' } return (body,status,headers) except NoListFoundError: response_data = { "meta" : {}, "data" : { "lists" : [{ "message" : "No List found of this user" }] } } body = json.dumps(response_data) status = 400 headers = { "Content-Type" : 'application/json' } return (body,status,headers) response_data = { "meta" : {}, "data" : { "lists" : [ list_item_info ] } } status = 400 body = json.dumps(response_data) headers = { "Content-Type" : 'application/json' } return (body,status,headers) else: try: dm.delete_item(user_id, list_id, item_id, db) except NoListFoundError: response_data = { "meta" : {}, "data" : { "lists" : [{ "message" : "No List found of this user" }] } } body = json.dumps(response_data) status = 400 headers = { "Content-Type" : 'application/json' } return (body,status,headers) except NoItemFoundError: response_data = { "meta" : {}, "data" : { "lists" : [{ "message" : "No Item found of this list" }] } } body = json.dumps(response_data) status = 400 headers = { "Content-Type" : 'application/json' } return (body,status,headers) response_data = { "meta" : {}, "data" : { "lists" : [ { "items": { "message" : "Item deleted from the list" } } ] } } status = 400 body = json.dumps(response_data) headers = { "Content-Type" : 'application/json' } return (body,status,headers)
def add_item(list_id): query_access_token = request.args.get('access_token') db = connect_db () try: user_info = auth.authenticate_using_access_token(query_access_token, db) user_id = user_info['id'] except AccessTokenExpiredError: response_data = { "meta" : {}, "data" : { "users" : [{ "authentication" : 'Expired', "message" : "Access token Expired" }] } } status = 401 body = json.dumps(response_data) headers = { 'Content-Type' : 'application/json' } return (body, status, headers) except InvalidAccessTokenError: response_data = { "meta" : {}, "data" : { "users" : [ { "message" : "Invalid access token : %s" % (query_access_token) } ] } } status = 400 body = json.dumps(response_data) headers = { 'Content-Type' : 'application/json' } return (body, status, headers) try: data = request.json['data'] name = data['lists'][0]['items'][0]['name'] status = data['lists'][0]['items'][0]['status'] quantity = data['lists'][0]['items'][0]['quantity'] list_item_info = dm.add_item_to_list(list_id, user_id, name, status, quantity, db) except NoListFoundError: response_data = { "meta" : {}, "data" : { "lists" : [{ "message" : "No List found of this user" }] } } body = json.dumps(response_data) status = 400 headers = { "Content-Type" : 'application/json' } return (body,status,headers) response_data = { "meta" : {}, "data" : { "lists" : [ list_item_info ] } } status = 400 body = json.dumps(response_data) headers = { "Content-Type" : 'application/json' } return (body,status,headers)
def change_list_name(list_id): query_access_token = request.args.get('access_token') db = connect_db () try: user_info = auth.authenticate_using_access_token(query_access_token, db) user_id = user_info['id'] except AccessTokenExpiredError: response_data = { "meta" : {}, "data" : { "users" : [{ "authentication" : 'Expired', "message" : "Access token Expired" }] } } status = 401 body = json.dumps(response_data) headers = { 'Content-Type' : 'application/json' } return (body, status, headers) except InvalidAccessTokenError: response_data = { "meta" : {}, "data" : { "users" : [ { "message" : "Invalid access token : %s" % (query_access_token) } ] } } status = 400 body = json.dumps(response_data) headers = { 'Content-Type' : 'application/json' } return (body, status, headers) if request.method == 'PUT': print request.json data = request.json['data'] new_name = data['lists'][0]['name'] try: list_info = dm.change_list_name(list_id, new_name, user_id, db) except NoListFoundError: response_data = { "meta" : {}, "data" : { "lists" : [{ "message" : "No List found of this user" }] } } body = json.dumps(response_data) status = 400 headers = { "Content-Type" : 'application/json' } return (body,status,headers) response_data = { "meta" : {}, "data" : { "lists" : list_info } } status = 200 elif request.method == 'GET': #fetching list info as per the list id try: list_info = dm.fetch_list(list_id, user_id, db) except NoListFoundError: response_data = { "meta" : {}, "data" : { "lists" : [{ "message" : "No List found of this user" }] } } body = json.dumps(response_data) status = 400 headers = { "Content-Type" : 'application/json' } return (body,status,headers) response_data = { "meta" : {}, "data" : { "lists" : list_info } } status = 200 else: # deleting entire list from the user's list try: dm.delete_list(list_id, user_id, db) except NoListFoundError: response_data = { "meta" : {}, "data" : { "lists" : [{ "message" : "No List found of this user" }] } } body = json.dumps(response_data) status = 400 headers = { "Content-Type" : 'application/json' } return (body,status,headers) response_data = { "meta" : {}, "data" : { "lists" : [{ "message" : "List deleted from the user's list" }] } } status = 200 headers = { 'Content-Type' : 'application/json' } body = json.dumps(response_data) return (body,status,headers)
def lists(): query_access_token = request.args.get('access_token') db = connect_db () try: user_info = auth.authenticate_using_access_token(query_access_token, db) user_id = user_info['id'] except AccessTokenExpiredError: response_data = { "meta" : {}, "data" : { "users" : [{ "authentication" : 'Expired', "message" : "Access token Expired" }] } } status = 401 body = json.dumps(response_data) headers = { 'Content-Type' : 'application/json' } return (body, status, headers) except InvalidAccessTokenError: response_data = { "meta" : {}, "data" : { "users" : [ { "message" : "Invalid access token : %s" % (query_access_token) } ] } } status = 400 body = json.dumps(response_data) headers = { 'Content-Type' : 'application/json' } return (body, status, headers) #All list under that user if request.method == 'GET': try: list_of_lists = dm.list_of_list(user_id, db) print list_of_lists except NoListFoundError: response_data = { "meta" : {}, "data" : { "lists" : [{ "message" : "No List found of this user" }] } } body = json.dumps(response_data) status = 200 headers = { "Content-Type" : 'application/json' } return (body,status,headers) response_data = { "meta" : {}, "data" : { "users" : user_info, "lists" : list_of_lists } } body = json.dumps(response_data) status = 200 headers = { "Content-Type" : 'application/json' } return (body,status,headers) else: # Add new list(how to handle request data) data = request.json['data'] list_name = data['lists'][0]['name'] list_info = list(dm.create_new_list(list_name, user_id, db)) response_data = { "meta" : {}, "data" : { "lists" : list_info } } body = json.dumps(response_data) status = 200 headers = { "Content-Type" : 'application/json' } return (body,status,headers)
def user_info(user_id): query_access_token = request.args.get('access_token') db = connect_db () try: user_info = auth.authenticate_using_access_token(query_access_token, db) except AccessTokenExpiredError: response_data = { "meta" : {}, "data" : { "users" : [{ "authentication" : 'Expired', "message" : "Access token Expired" }] } } status = 401 body = json.dumps(response_data) headers = { 'Content-Type' : 'application/json' } return (body, status, headers) except InvalidAccessTokenError: response_data = { "meta" : {}, "data" : { "users" : [ { "message" : "Invalid access token : %s" % (query_access_token) } ] } } status = 400 body = json.dumps(response_data) headers = { 'Content-Type' : 'application/json' } return (body, status, headers) print type(user_info) if request.method == 'GET': print user_info response_data = { "meta":{}, "data":{ "users":[ { "authentication" : 'Sucess', "name" : user_info['name'], "id" : user_info['id'], "email" : user_info['email'], } ] } } status = 200 else: dm.deactive_user(user_info['id'], query_access_token, db) response_data = { "meta":{}, "data":{ "users":[ { "authentication" : "Success", "name" : user_info['name'], "id" : user_info['id'], "name" : user_info['name'], "delete" : "success" } ] } } status = 200 body = json.dumps(response_data) headers = { 'Content-Type' : 'application/json' } return (body, status, headers)