def handle_login(): fail = json.dumps({"status": "failure"}) success = make_response(json.dumps({"status": "success"})) request_json = request.json if not request.json: return fail email = request_json.get("email") password = request_json.get("password") if not email: return json.dumps({"status": "failure", "issue": "no email"}) if not password: return json.dumps({"status": "failure", "issue": "no password"}) account = Account.validate(email, password) Account.dump_table() if not account: return json.dumps({ "status": "failure", "issue": "invalid login / no account" }) user_id = account.get_uuid() token_table = TokenTable() token_id = token_table.create(user_id) success.set_cookie(TOKEN_NAME, token_id) print("login success") return success
def create_post_view(): user_id = get_userid() if not user_id: print("USER ID WAS BAD") return render_template("login.html") # token_conn = TokenTable() # user_id = token_conn.get_uuid(cookie) acc = Account.init_from_uuid(user_id) res = request.args print(res) print(request.form) if not res: return render_template("edit_post.html") res = dict(res) res['is_request'] = True res['length'] = res.get('duration') res['date'] = res.get('start_date') res['skill_set'] = res.get('skillset', []) post = acc.create_post(**res) # print("POSTS CREATE NEW") post = post.to_dict() # print(post) return redirect("/posts")
def create_new_post(): user_id = get_userid() if not user_id: print("USER ID WAS BAD") return json.dumps({"status": "failure"}) # token_conn = TokenTable() # user_id = token_conn.get_uuid(cookie) acc = Account.init_from_uuid(user_id) res = request.json res = dict(res) print(res) res['is_request'] = True res['length'] = res.get('duration') res['date'] = res.get('start_date') res['skill_set'] = res.get('skillset', []) res['tags'] = res.get('tags', "").split(',') post = acc.create_post(**res) print("POSTS CREATE NEW!") post = post.to_dict() print(post) post['status'] = 'success' return json.dumps(post)
def view_post(post_id): """ we are going to have some filtering going on... """ # get post id from request, create post object, add a volunteer to the post object, update post = Post.init_from_uid(post_id) # Post.dump_table() if not post: print("bad post") return render_template("post.html") print("/posts/postid") post_d = post.to_dict() img_file = "meeting.jpg" user_id = post.user_id user_account = Account.init_from_uuid(user_id) if user_account and user_account.get_name(): post_d["user_name"] = user_account.get_name() else: post_d["user_name"] = "No Name" if post.get_tags() and "technology" in post.get_tags(): img_file = "code.jpg" if post.get_tags() and "handy" in post.get_tags(): img_file = "handy.jpg" if post.get_tags() and "plumber" in post.get_tags(): img_file = "plumb.jpg" return render_template("post.html", **post_d, img_file=img_file)
def signup(): """ email (string) password (string) **not hashed yet! """ # similar to the login route cookie = request.cookies.get(TOKEN_NAME) # # no cookie if not cookie: return render_template("signup.html") token_conn = TokenTable() user_id = token_conn.get_uuid(cookie) account = Account.init_from_uuid(user_id) # if they are logge din with valid cookie if user_id and cookie and token_conn.validate(user_id, cookie): return render_template("signup.html", token_uuid=get_userid(), logged_in=True, **account.to_dict()) return render_template("signup.html")
def grab_post(): user_id = get_userid() if not user_id: return FAIL_MSG user = Account.init_from_uuid(user_id) if not user: return FAIL_MSG post_id = request.json["post_id"] post = Post.init_from_uid(post_id) if not post.add_volunteer(user_id): return FAIL_MSG print("!!!!!!!!!!!!!!!!!!!!!!!!!") post.update_in_db() return json.dumps({"status": "success"})
def posts(): """ we are going to have some filtering going on... """ # if they are logged in, they are going to have a small thing saying they are logged in filter = {} if request.args and 'type' in request.args: print(request.args) filter['type'] = [request.args['type'].lower()] filtered = Post.get_with_filter(filter) # Add the personal thing in to the dict new_list = [] for post_dict in filtered: uuid = post_dict["user_id"] user = Account.init_from_uuid(uuid) img_file = "meeting.jpg" if "tags" in post_dict and (post_dict["tags"] != None ) and "technology" in post_dict["tags"]: img_file = "code.jpg" if "tags" in post_dict and (post_dict["tags"] != None) and "handy" in post_dict["tags"]: img_file = "handy.jpg" if "tags" in post_dict and (post_dict["tags"] != None) and "plumber" in post_dict["tags"]: img_file = "plumb.jpg" if user: post_dict["personal"] = 1 if user.is_personal else 2 post_dict["img"] = img_file print(post_dict) print("found") tags = post_dict.get("tags", []) new_tags = [] if tags: for t in tags: new_tag = t.capitalize() new_tags.append(new_tag) post_dict["tags"] = new_tags if post_dict["volunteers_required"] > 0: new_list.append(post_dict) else: print("not found", post_dict.items()) return render_template("posts.html", posts=new_list)
def handle_signin(): request_json = request.json fail = json.dumps({"status": "failure"}) success = make_response(json.dumps({"status": "success"})) if not request.json: return fail email = request_json.get("email") password = request_json.get("password") name = request_json.get("name") is_user = request_json.get("is_user") print("is user", is_user) if not email: return json.dumps({"status": "failure", "issue": "no email"}) if not password: return json.dumps({"status": "failure", "issue": "no password"}) if not name: return json.dumps({"status": "failure", "issue": "no name"}) # TODO check to see if email exists in the db, if so, return failure if Account.exists(email): return json.dumps({"status": "failure", "issue": "email exists"}) # TODO change for person vs org ClassToUse = Person if is_user else Organization user = ClassToUse(name, email, password) user.insert_into_db() if isinstance(type(Person), type(ClassToUse)): print("made a person") elif isinstance(type(Organization), type(ClassToUse)): print("made an org") else: print("probs an error when crating an entity") # TODO set password user_id = user.get_uuid() token_table = TokenTable() token_id = token_table.create(user_id) success.set_cookie(TOKEN_NAME, token_id) return success
def login(): """ email (string) password (string) """ # we need to give the user a cookie, if they are not logged in, so that we can figure out if they are validated? # 1. if they are not logged in, then they are prompted to login # after login, they are given the uuid token # 2. if they login with an existing email and, check the password to see if it matches, it if does, they get the cookie with uuid # if the password does not match, they are again prompted to this page # TODO the login route should also have info about the route they were trying to go down before, so tif they are logged in / # when they are logged in, it should send them to the right place cookie = request.cookies.get(TOKEN_NAME) # # no cookie if not cookie: return render_template("login.html") token_conn = TokenTable() user_id = token_conn.get_uuid(cookie) # malformed cookie if not user_id: return render_template("login.html") # # valid user_id expired token -> make a new token? if user_id and cookie and not token_conn.validate(user_id, cookie): return render_template("login.html") # user is logged in if user_id and cookie and token_conn.validate(user_id, cookie): user = Account.init_from_uuid(user_id) d = user.to_dict() if user else {} print("user_id: {}".format(user_id)) return render_template("login.html", logged_in=True, token_uuid=user_id, **d) print("error?") return render_template("login.html")