def register_token(): # the route to authenticate through the use of a token if is_logged_in(cookies={'Cookie': request.cookies.get('user_cookies')}): flash("You are already logged in!", 'info') return redirect(url_for('main.home')) form = TokenForm() # gets the token form if form.validate_on_submit(): # if valid # sends the necessary get request response = send_http_request( cookies={'Cookie': request.cookies.get('user_cookies')}, url= f'https://elderlift-serverside.ue.r.appspot.com/v1/api/register/{form.token.data}', method='GET', body={}) # flashes the fact that you are registered flash("You have successfully registered!", "success") # redirects to the login page return redirect(url_for('users.login')) # renders the token page return render_template( "register_token.html", title="Authenticate with token", form=form, authenticated=is_logged_in( cookies={'Cookie': request.cookies.get('user_cookies')}))
def account(): if not is_logged_in( cookies={'Cookie': request.cookies.get('user_cookies')}): abort(400, "You are not logged in!") form = AccountForm() if request.method == 'POST': # if it's a post request if form.validate_on_submit(): # if it's valid # creates a body to be sent body = { 'name': form.name.data, 'city': form.city.data, 'country': form.country.data, 'address': form.address.data, 'contact': form.contact.data } # sends a http request response = send_http_request( cookies={'Cookie': request.cookies.get('user_cookies')}, url= "https://elderlift-serverside.ue.r.appspot.com/v1/api/account", method='PUT', body=body) # then shows that it was successful flash(f'{response.content.decode("utf-8")}', 'success') return redirect(url_for('main.home')) # redirects back to the home # sends an http request to get the account info response = send_http_request( cookies={'Cookie': request.cookies.get('user_cookies')}, url="https://elderlift-serverside.ue.r.appspot.com/v1/api/account", method='GET') json_user = json.loads(response.content).get("user") form.name.data = json_user.get("name") form.address.data = json_user.get("address") form.city.data = json_user.get("city") form.country.data = json_user.get("country") form.contact.data = json_user.get("contact") # renders a template with the account page, sends a user that has all the user's info return render_template( "account.html", title="Account", authenticated=is_logged_in( cookies={'Cookie': request.cookies.get('user_cookies')}), user=json_user, form=form)
def elderly_update_task(task_id): # this is to update a task if not is_logged_in( cookies={'Cookie': request.cookies.get('user_cookies') }): # not logged in, aborts it abort(400, "You are not logged in!") # gets the current user if logged in user = json.loads( send_http_request( url="https://elderlift-serverside.ue.r.appspot.com/v1/api/account", method='GET', cookies={ 'Cookie': request.cookies.get('user_cookies') }).content).get("user") # gets the task from http request task = json.loads( send_http_request( url= f"https://elderlift-serverside.ue.r.appspot.com/v1/api/task/{task_id}", method='GET', cookies={ 'Cookie': request.cookies.get('user_cookies') }).content) if user.get("id") != task.get("elderly_id"): abort(401, "You are not allowed to edit someone else's tasks!") form = TaskForm() if form.validate_on_submit( ): # if the form is valid, then it sends a put request body = {"title": form.title.data, "content": form.content.data} response = send_http_request( url= f"https://elderlift-serverside.ue.r.appspot.com/v1/api/elderly/{task_id}", method="PUT", body=body, cookies={'Cookie': request.cookies.get('user_cookies')}) flash(f"{response.content.decode('utf-8')}", "success") return redirect(url_for("tasks.task_by_id", task_id=task_id)) form.title.data = task.get("title") # presets the form to previous values form.content.data = task.get("content") return render_template( "create_task.html", title="Update Task", form=form, authenticated=is_logged_in( cookies={'Cookie': request.cookies.get('user_cookies')}))
def task_by_id(task_id): # gets the task from http request response = send_http_request( url= f"https://elderlift-serverside.ue.r.appspot.com/v1/api/task/{task_id}", method='GET', cookies={'Cookie': request.cookies.get('user_cookies')}) # gets the task as a json task = json.loads(response.content) # the task doer and elderly booleans to determine different cases task_doer = 0 # 0 for user being an elderly, 1 for the task doer not there, 2 for task doer being the user elderly = False # if the user is logged in, can show some extra stuff if is_logged_in(cookies={'Cookie': request.cookies.get('user_cookies')}): # gets the current user user = json.loads( send_http_request( url= "https://elderlift-serverside.ue.r.appspot.com/v1/api/account", method='GET', cookies={ 'Cookie': request.cookies.get('user_cookies') }).content).get("user") # checks if the user is a taskdoer if user.get("user_role") == "Taskdoer": # if there is no task doer, then sets it to 1 if not task.get("task_doer_id"): task_doer = 1 # if the current user is the task doer, sets it to 2 elif task.get("task_doer_id") == user.get("id"): task_doer = 2 # if the task has the elderly id of the current id, then it can show stuff if task.get("elderly_id") == user.get("id"): elderly = True return render_template( "task.html", title=f"Task - {task.get('id')}", task=task, task_doer=task_doer, elderly=elderly, authenticated=is_logged_in( cookies={'Cookie': request.cookies.get('user_cookies')}))
def reset_password(): if is_logged_in(cookies={'Cookie': request.cookies.get('user_cookies')}): abort(400, "You're logged in! Log out to reset password!") form = ResetPasswordForm() if form.validate_on_submit(): body = {"email": form.email.data} response = send_http_request( cookies={'Cookie': request.cookies.get('user_cookies')}, url= "https://elderlift-serverside.ue.r.appspot.com/v1/api/reset_password", method='POST', body=body) flash(response.content.decode('utf-8'), 'success') return redirect(url_for('users.reset_password_token')) return render_template('request_reset_password.html', title='Request Password Reset', form=form, authenticated=False)
def users_tasks(): # get the logged in user's tasks if not is_logged_in( cookies={'Cookie': request.cookies.get('user_cookies') }): # if they're not logged in, shows an error abort(401, "Not logged in!") page = request.args.get('page', 1, type=int) user = json.loads( send_http_request( cookies={ 'Cookie': request.cookies.get('user_cookies') }, url="https://elderlift-serverside.ue.r.appspot.com/v1/api/account", method='GET').content).get("user") tasks = json.loads( send_http_request( cookies={ 'Cookie': request.cookies.get('user_cookies') }, url= f"https://elderlift-serverside.ue.r.appspot.com/v1/api/user/{user.get('id')}/tasks?page={page}", method='GET').content) return render_template("users_tasks.html", title='Your Tasks', tasks=tasks.get("tasks"), authenticated=True, page_num=page, total_pages=tasks.get("pages"))
def user_by_id(user_id): # route for a specific user based on its user id page = request.args.get('page', 1, type=int) # gets the response response = json.loads( send_http_request( cookies={ 'Cookie': request.cookies.get('user_cookies') }, url= f"https://elderlift-serverside.ue.r.appspot.com/v1/api/user/{user_id}", method='GET').content).get("user") tasks = response.get("tasks") total_pages = math.ceil(len(tasks) / 5) tasks = tasks[(page - 1) * 5:page * 5] # loads the response into the template return render_template( "user_by_id.html", title=f"User {response.get('name')}", authenticated=is_logged_in( cookies={'Cookie': request.cookies.get('user_cookies')}), user=response, tasks=tasks, tasks_num=len(response.get("tasks")), total_pages=total_pages, page_num=page)
def create_task(): if is_logged_in(cookies={'Cookie': request.cookies.get('user_cookies')}): # gets the current user if logged in user = json.loads( send_http_request( url= "https://elderlift-serverside.ue.r.appspot.com/v1/api/account", method='GET', cookies={ 'Cookie': request.cookies.get('user_cookies') }).content).get("user") if user.get("user_role") == 'Elderly': # if the user is an elderly, then it creates a form for the tasks form = TaskForm() # if it is validated, then it sends a post request if form.validate_on_submit(): body = {"title": form.title.data, "content": form.content.data} response = send_http_request( url= "https://elderlift-serverside.ue.r.appspot.com/v1/api/elderly/new_task", method='POST', body=body, cookies={ 'Cookie': request.cookies.get('user_cookies') }).content.decode("utf-8") # sends post http request and then flashes a success message flash(f"{response}", "success") return redirect(url_for("main.home")) # redirects to home page return render_template( "create_task.html", title="Create Task", form=form, authenticated=is_logged_in( cookies={'Cookie': request.cookies.get('user_cookies')})) return "You need to log in as an elderly user!", 400 abort(400, "You are not logged in!")
def register(): # the register route if is_logged_in(cookies={'Cookie': request.cookies.get('user_cookies')}): flash("You are already logged in!", 'info') return redirect(url_for('main.home')) form = RegistrationForm() # the form if form.validate_on_submit(): # if it's a post method and is validated # sends in the necessary information body = { 'name': form.name.data, 'email': form.email.data, 'password': form.password.data, 'city': form.city.data, 'country': form.country.data, 'user_role': form.role.data, 'address': form.address.data, 'contact': form.contact.data } # sends a request, and gets the response (response is in either a success message or aborts to error) response = send_http_request( cookies={'Cookie': request.cookies.get('user_cookies')}, url="https://elderlift-serverside.ue.r.appspot.com/v1/api/register", method="POST", body=body) # flashes a success with the response flash(f"{response.content.decode('utf-8')}", "success") # redirects to a token page return redirect(url_for("users.register_token")) # renders a template of the registration page return render_template( "register.html", title="Registration", form=form, authenticated=is_logged_in( cookies={'Cookie': request.cookies.get('user_cookies')}))
def home(country=None, city=None, address=None): page = request.args.get('page', 1, type=int) # gets the page from query parameter url = "https://elderlift-serverside.ue.r.appspot.com/v1/api/tasks" + urllib.parse.quote( ((f"/{country}" + ((f"/{city}" + (f"/{address}" if address else "")) if city else "")) if country else "")) + f'?page={page}' # asks for the url from the endpoint response_tasks = json.loads( send_http_request(url=url, method='GET', body={}, cookies={ 'Cookie': request.cookies.get('user_cookies') }).content) form = AddressForm() # gets the address form if form.validate_on_submit(): # if valid # gets all the information needed country_form = form.country.data city_form = form.city.data address_form = form.address.data if country_form: # shows the search results, flashes what was searched flash( f"Showing the filtered tasks: " + (f"{address_form}, " if address_form else "") + (f"{city_form}, " if city_form else "") + f'{country_form}', "info") # redirects back to the home page, with the endpoint of the country, city and address return redirect( url_for('main.home', country=country_form if country_form else None, city=city_form if city_form else None, address=address_form if address_form else None)) # returns the template of the home page return render_template( "home.html", tasks=response_tasks.get("tasks"), form=form, authenticated=is_logged_in( cookies={'Cookie': request.cookies.get('user_cookies')}), page_num=page, total_pages=response_tasks.get('pages'))
def login(): # the login route if is_logged_in(cookies={'Cookie': request.cookies.get('user_cookies')}): flash("You are already logged in!", 'info') return redirect(url_for('main.home')) form = LoginForm() # the form for the login page if form.validate_on_submit(): # if the form is valid # then sends the information of the user user_info = { "email": form.email.data, "password": form.password.data, } # sends an http request to the login response = send_http_request( cookies={'Cookie': request.cookies.get('user_cookies')}, url="https://elderlift-serverside.ue.r.appspot.com/v1/api/login", method='POST', body=user_info) # flashes a success message - then redirects back to home flash(f"{response.content.decode('utf-8')}", "success") cookie_html = make_response(redirect(url_for('main.home'))) cookie_html.set_cookie('user_cookies', f"session={response.cookies.get('session')}") return cookie_html # renders the login page return render_template( "login.html", title="Login", form=form, authenticated=is_logged_in( cookies={'Cookie': request.cookies.get('user_cookies')}))
def task_doer_remove_task(task_id): if not is_logged_in( cookies={'Cookie': request.cookies.get('user_cookies') }): # if it's not logged in, then it aborts the function abort(400, "You are not logged in!") # gets the current user if logged in user = json.loads( send_http_request( url="https://elderlift-serverside.ue.r.appspot.com/v1/api/account", method='GET', cookies={ 'Cookie': request.cookies.get('user_cookies') }).content).get("user") # gets the task from http request task = json.loads( send_http_request( url= f"https://elderlift-serverside.ue.r.appspot.com/v1/api/task/{task_id}", method='GET', cookies={ 'Cookie': request.cookies.get('user_cookies') }).content) if user.get( "user_role" ) != "Taskdoer": # if it is not a task doer then it aborts the function abort(400, "You are not a taskdoer!") if task.get("task_doer_id") != user.get( "id" ): # if it is already taken, it says you don't have permission to add the task abort(401, "You don't have permission to delete that task!") # sends a request and then flashes a success and redirects to the task response = send_http_request( url= f"https://elderlift-serverside.ue.r.appspot.com/v1/api/task_doer/{task_id}", method="DELETE", cookies={'Cookie': request.cookies.get('user_cookies')}) flash(response.content.decode("utf-8"), "success") return redirect(url_for("tasks.task_by_id", task_id=task_id))
def logout(): # the logout route if not is_logged_in( cookies={'Cookie': request.cookies.get('user_cookies') }): # if they're not logged in abort(400, "You're not logged in!") response = send_http_request( cookies={'Cookie': request.cookies.get('user_cookies')}, url="https://elderlift-serverside.ue.r.appspot.com/v1/api/logout", method='GET') # logs them out cookies_html = make_response(redirect(url_for('main.home'))) cookies_html.set_cookie('user_cookies', '', expires=0) # resets the headers (the cookies) # shows the response with the success message flash(response.content.decode('utf-8'), "success") # redirects them back to home page return cookies_html
def elderly_delete_task(task_id): if is_logged_in(cookies={'Cookie': request.cookies.get('user_cookies')}): # gets the current user if logged in user = json.loads( send_http_request( url= "https://elderlift-serverside.ue.r.appspot.com/v1/api/account", method='GET', cookies={ 'Cookie': request.cookies.get('user_cookies') }).content).get("user") # gets the task from http request task = json.loads( send_http_request( url= f"https://elderlift-serverside.ue.r.appspot.com/v1/api/task/{task_id}", method='GET', cookies={ 'Cookie': request.cookies.get('user_cookies') }).content) # if the elderly of the task is the current user, allow deletions if user.get("id") == task.get("elderly_id"): response = send_http_request( url= f"https://elderlift-serverside.ue.r.appspot.com/v1/api/elderly/{task_id}", method="DELETE", cookies={'Cookie': request.cookies.get('user_cookies')}) flash(f"{response.content.decode('utf-8')}", 'success') return redirect(url_for('main.home')) abort(401, "That's not your task to delete!") abort(400, "You're not logged in!")
def about(): # about endpoint with the about html page return render_template( "about.html", title="About", authenticated=is_logged_in( cookies={'Cookie': request.cookies.get('user_cookies')}))