def process_signout(): cookies = request.cookies print cookies responseWrapper = ResponseWrapper() response = any_response(request) if 'session' in cookies: print "cookie : ",cookies['session'] userid = sessionDAO.get_userid(cookies['session']) # see if user is logged in print "user : "******"User not found"]) response.status_code = 302 else: responseWrapper.set_error(True) responseWrapper.set_data(["User not logged in"]) response.status_code = 302 response.set_cookie("session", value=" ", expires=0) response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response
def update_post(): responseWrapper = ResponseWrapper() response = any_response(request) user = validate_cookie(request) if user != None: post = Post() try: # build post object from form data form_data = request.form['data'] json_data = json.loads(form_data) post.id = json_data['_id'] post.title = json_data['title'] post.link = json_data['link'] post.category = json_data['category'] post.tags = json_data['tags'] post.group = json_data['groups'] post.added_by = user.name except Exception as inst: print "error reading form data" print inst responseWrapper = ResponseWrapper() responseWrapper.set_error(True) responseWrapper.set_data(["error reading form data. Retry posting"]) if post.title != None and post.link != None and post.group != None and post.added_by != None: result = postDAO.update_post(post); responseWrapper = ResponseWrapper() if result != None or result != False: responseWrapper.set_error(False) responseWrapper.set_data([str(result)]) response.status_code = 201 else: responseWrapper.set_error(True) responseWrapper.set_data(["error writing post"]) else: print "error in form data" responseWrapper = ResponseWrapper() responseWrapper.set_error(True) responseWrapper.set_data(["insufficient fields, try again"]) response.status_code = 302 else: responseWrapper.set_error(True) responseWrapper.set_data(["User not logged in. Please Login"]) response.status_code = 302 response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response
def get_userinfo(): responseWrapper = ResponseWrapper() response = any_response(request) user = validate_cookie(request) if user != None: user = userDAO.get_user_info(user.id) responseWrapper.set_data([user]) responseWrapper.set_error(False) else: responseWrapper.set_error(True) responseWrapper.set_data(["User not found. Please Login"]) response.status_code = 302 response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response
def user_signup(): email = None password = None name = None responseWrapper = ResponseWrapper() response = any_response(request) print request.form email = request.form['email'] password = request.form['password'] name = request.form['name'] verify = request.form['verify'] if email != None and password != None and name != None: # set these up in case we have an error case errors = {'username': cgi.escape(name), 'email': cgi.escape(email)} if validate_signup(name, password, verify, email, errors): #create a modelled user temp_user = User(email, password, name) if not userDAO.add_user(temp_user): # this was a duplicate error = ['Username already taken. Please choose a different username'] responseWrapper.set_error(True) responseWrapper.set_data(error) response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" response.status_code = 409 return response session_id = sessionDAO.start_session(email) response.set_cookie("session", value=session_id, domain=".linkur.co.in") responseWrapper.set_error(False) responseWrapper.set_data(["User Signup success!"]) else: print "user did not validate" responseWrapper.set_error(True) responseWrapper.set_data(["User did not validate. Signup failed!"]) else: responseWrapper.set_error(True) responseWrapper.set_data(["Error in form data"]) response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response
def create_user_groups(): user = validate_cookie(request) print user.__str__() responseWrapper = ResponseWrapper() print "hello new group" response = any_response(request) if user != None: group = Group() try: # form_data = request.form['data'] print request.form form_data = request.form['data'] json_data = json.loads(form_data) group.name = json_data['group_name'] print "appending to group user ", user.id group.users.append(user.id) except Exception as inst: print inst print "Error reading form data" responseWrapper.set_error(True) responseWrapper.set_data([inst]) response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response new_group_id = groupDAO.insert_group(group) group.id = new_group_id result = userDAO.append_group(user.id,group) if result != None: responseWrapper.set_error(False) new_group_id = str(new_group_id) responseWrapper.set_data([{"group_id":new_group_id}]) else: responseWrapper.set_error(True) responseWrapper.set_data(["error writing group"]) else: responseWrapper.set_error(True) responseWrapper.set_data(["User not found. Please login again"]) response.status_code = 302 response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response
def insert_catergory(): user = validate_cookie(request) responseWrapper = ResponseWrapper() response = any_response(request) if user != None: category = Category() form_data = request.form['data'] json_data = json.loads(form_data) category.name = json_data['category_name'] result = categoryDAO.insert_category(category) if result != None: responseWrapper.set_error(False) responseWrapper.set_data(result) else: responseWrapper.set_error(True) responseWrapper.set_data(["error writing category"]) else: responseWrapper.set_error(True) responseWrapper.set_data(["User not found. Please login again"]) response.status_code = 302 response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response
def get_categories(): user = validate_cookie(request) responseWrapper = ResponseWrapper() response = any_response(request) if user != None: # process things result = categoryDAO.get_categories() if result != None: responseWrapper.set_error(False) responseWrapper.set_data([result]) else: responseWrapper.set_error(True) responseWrapper.set_data(["error reading categories"]) response.status_code = 302 else: responseWrapper.set_error(True) responseWrapper.set_data(["User not found. Please login again"]) response.status_code = 302 response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response
def delete_post(post_id=None): responseWrapper = ResponseWrapper() response = any_response(request) user = validate_cookie(request) if user != None: if post_id != None: try: result = postDAO.delete_post(post_id) print "result is ", result if result == None: responseWrapper.set_error(True) responseWrapper.set_data(["Error deleting post"]) else: responseWrapper.set_error(False) responseWrapper.set_data(["Success deleting post"]) response.status_code = 200 except Exception as inst: print inst responseWrapper.set_error(True) responseWrapper.set_data(["Error deleting post"]) response.status_code = 500 else: responseWrapper.set_error(True) responseWrapper.set_data(["Post id is null"]) else: responseWrapper.set_error(True) responseWrapper.set_data(["User not logged in. Please Login"]) response.status_code = 302 response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response
def search(): responseWrapper = ResponseWrapper() response = any_response(request) user = validate_cookie(request) print user.__str__() if user != None: queryText = request.args["q"] # print queryText result = postDAO.search(user, queryText) response = any_response(request) responseWrapper.set_data(result) responseWrapper.set_error(False) else: responseWrapper.set_error(True) responseWrapper.set_data(["User not found, Login"]) response.status_code = 302 response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response
def get_recent_posts(): responseWrapper = ResponseWrapper() response = any_response(request) user = validate_cookie(request) if user != None: group_id = None try: group_id = request.args["group_id"] print group_id except Exception as inst: print "please send group_id as part of url parameter" responseWrapper.set_error(True) responseWrapper.set_data(["group_id not supplied as URL param"]) response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response posts = postDAO.get_recent_posts(group_id) json_result = None if posts != None : responseWrapper.set_data(posts) responseWrapper.set_error(False) else: responseWrapper.set_error(True) print(json_result) else: responseWrapper.set_error(True) responseWrapper.set_data(["User not found"]) response.status_code = 302 # redirect("/index.html") response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response
def change_user_password(): responseWrapper = ResponseWrapper() response = any_response(request) user = validate_cookie(request) if user != None: # read form data for old & new password try: old_password = request.form['old_password'] print old_password new_password = request.form['new_password'] print new_password result = userDAO.change_password(user.id, old_password, new_password) if result != None: # update success responseWrapper.set_data(["Success updating password"]) responseWrapper.set_error(False) else: # update FAILURE responseWrapper.set_data(["Error updating password"]) responseWrapper.set_error(True) except Exception as inst: print "An error occurred while updating password" print inst else: responseWrapper.set_error(True) responseWrapper.set_data(["User not found"]) response.status_code = 302 response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response
def get_user_groups(): user = validate_cookie(request) responseWrapper = ResponseWrapper() response = any_response(request) if user != None: groups = userDAO.get_groups(user.id) if groups != None: responseWrapper.set_error(False) responseWrapper.set_data(groups) else: responseWrapper.set_error(True) responseWrapper.set_data(["error reading user groups"]) else: responseWrapper.set_error(True) responseWrapper.set_data(["User not found. Please login again"]) response.status_code = 302 response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response
def accept_group_invite(invite_hash): # check for cookie # check group collection for group id # if exists, append group object to table # else throw error user = validate_cookie(request) responseWrapper = ResponseWrapper() response = any_response(request) if user != None: group_obj = groupDAO.get_group_by_hash(str(invite_hash)) if group_obj != None: # check if group is already part for the user group_exists = userDAO.does_group_exist(user.id,group_obj) print "group exists", group_exists append_group_result = None append_user_result = None if group_exists == False: append_group_result = groupDAO.append_user(group_obj, user.id) group_obj = groupDAO.get_group_by_id(group_obj.id) print " modified grp \n" print group_obj.__str__() append_user_result = userDAO.append_group(user.id,group_obj) else: responseWrapper.set_error(False) responseWrapper.set_data(["group already part of user"]) # check for DB errors if append_user_result != False and append_group_result != False: responseWrapper.set_error(False) responseWrapper.set_data("") elif append_user_result == False: responseWrapper.set_error(True) responseWrapper.set_data(["error adding group to user"]) else: responseWrapper.set_error(True) responseWrapper.set_data(["error adding user to group"]) else: responseWrapper.set_error(True) responseWrapper.set_data(["No such group. Try again"]) else: # TODO redirect to login page responseWrapper.set_error(True) responseWrapper.set_data(["User not found. Please login again"]) response.status_code = 302 response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response
def remove_group_for_user(group_id=None): # check for cookie & get user object # check group collection for group id # if exists, remove user from group collection # then remove group from user collection # else throw error user = validate_cookie(request) responseWrapper = ResponseWrapper() response = any_response(request) if user != None: if group_id != None: group_obj = groupDAO.get_group_by_id(str(group_id)) if group_obj != None: # check if group is already part for the user group_exists = userDAO.does_group_exist(user.id,group_obj) print "group exists", group_exists remove_group_result = None remove_user_result = None if group_exists == True: remove_group_result = groupDAO.remove_user(group_obj, user.id) # TODO if the group contains ZERO number of users, delete group remove_user_result = userDAO.remove_group(user.id, group_obj) else: print "Group not related to user" # check for DB errors if remove_group_result != False and remove_user_result != False: responseWrapper.set_error(False) responseWrapper.set_data(["Success removing group"]) elif remove_user_result == False: responseWrapper.set_error(True) responseWrapper.set_data(["error removing group from user"]) else: responseWrapper.set_error(True) responseWrapper.set_data(["error removing user from group"]) else: responseWrapper.set_error(True) responseWrapper.set_data(["No such group. Try again"]) else: responseWrapper.set_error(True) responseWrapper.set_data(["group id is null"]) else: # TODO redirect to login page responseWrapper.set_error(True) responseWrapper.set_data(["User not found. Please login again"]) response.status_code = 302 response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response
def user_login(): responseWrapper = ResponseWrapper() response = any_response(request) username = None password = None try: username = request.form['email'] print username password = request.form['password'] print password except Exception as inst: print "error reading form data" print inst responseWrapper.set_error(True) responseWrapper.set_data(["Error reading form data. check form data"]) response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response if username != None and password != None: validation_result = userDAO.validate_login(username, password) if validation_result["error"] == True: # error responseWrapper.set_error(True) responseWrapper.set_data(validation_result["data"]) response.status_code = 403 else: # continue processing user_record = validation_result["data"] if user_record: session_id = sessionDAO.start_session(user_record['_id']) if session_id is None: responseWrapper.set_error(True) responseWrapper.set_data(["Session not found. Signin again"]) response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" response.status_code = 403 return response # set_cookie(key, value='', max_age=None, expires=None, path='/', domain=None, secure=None, httponly=False) response.set_cookie("session", value=session_id, expires=None, path="/", httponly=True) responseWrapper.set_error(False) responseWrapper.set_data(["Signin success"]) else: responseWrapper.set_error(True) responseWrapper.set_data(["User not found"]) response.status_code = 403 else: responseWrapper.set_error(True) responseWrapper.set_data(["Username / password blank"]) response.status_code = 403 response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__) response.mimetype = "application/json" return response