def edit_profile(): if request.method == "POST": password = request.form['pass'] confirm_pass = request.form['pass-check'] email = request.form['email'] username = current_user.id if(password != ""): ''' storing data to our database ''' dataManager = UserDataManager() #if the confirm passwords are wrong if password != confirm_pass: flash("Passwords are not the same !!") return redirect(url_for("display_edit")) data = dataManager.data data[username]["password"] = password; data[username]["email"] = email; print(data) dataManager.update_data(data) flash("Saved! Back to profile now") return redirect(url_for("display_edit")) else: flash("Not a valid new password !!") return redirect(url_for("display_edit"))
def generate(): if request.method == "POST": form = request.form; #print(form) #if user did pick a week, dont play with database; otherwise display profile page again if 'start-day' in form.keys(): timetable_date_key = form['start-day']; date = "2019-04-23"; ts_type = form['transport-type']; #create temp key week data structure dt = datetime.strptime(timetable_date_key, '%Y-%m-%d') start = dt - timedelta(days=dt.weekday()) end = start + timedelta(days=6) curr = start allDates = [] while curr <= end: allDates.append(curr) curr = curr + timedelta(days=1) week_data = { "Mon": {'date':str(allDates[0])[0:10]}, "Tue": {'date':str(allDates[1])[0:10]}, "Wed": {'date':str(allDates[2])[0:10]}, "Thu": {'date':str(allDates[3])[0:10]}, "Fri": {'date':str(allDates[4])[0:10]}, "Sat": {'date':str(allDates[5])[0:10]}, "Sun": {'date':str(allDates[6])[0:10]} } #store all the events in our datebase for key,value in form.items(): if key == 'start-day' or key == 'transport-type': continue; #get mon-sun day key, and time #get place name, address wk_day, time = key.split('-',2); place_name, address = value.split('==',2); week_data[wk_day][time]={ 'name': place_name, 'address':address } #check if is a valid timetable dataManager = UserDataManager() data = dataManager.data if(valid_table(ts_type, week_data)): #everytime delete the data of the week first, then update. since we can double click in front-end to close some events #delete data of the week del data[current_user.id]['timetable'][timetable_date_key]; data[current_user.id]['timetable'][timetable_date_key] = week_data; dataManager.update_data(data); flash("Valid timetable! Have a good time!!") else: pass return render_template('profile.html', info=data[current_user.id], key=timetable_date_key, default_date=date); return redirect(url_for('display_profile'))
def add_to_bank(place_name): ''' For google matrix api to calculate distance. Basic usage: https://maps.googleapis.com/maps/api/distancematrix/json?parameters for parameters: - required parameters: - origins - need : place id or address - destinations - same as above # we need to store place id or address in our database when we press button "add to timetable" - apikey - optinal parameters(default model is driving ) - language=en - mode - transit - driving - walking - if mode is transit, we can select transit mode - transit_mode = bus, train, tram, rail, subway ''' if request.method == 'POST': #get place name and address place_name = request.form.get('add_bank'); address = request.form.get('address'); place_id = request.form.get('place_id') event = request.form.get('type'); #store into our database username = current_user.id; dataManager = UserDataManager(); data = dataManager.data; data[username]['bank'][place_name] = address; dataManager.update_data(data) #update most_visited && we dont want event in most vistied. bug if(event != 'event'): most_visited = []; most_visited = read_from_json_file("database/most_visited.json"); if place_id in most_visited.keys(): most_visited[place_id]["count"] += 1 else: most_visited[place_id] = { "count":1 } write_to_json_file("database/most_visited.json",most_visited) userinfo = data[username]; date = "2019-04-23" return render_template("profile.html", info = userinfo, key = 'none', default_date = date)
def get_date(): start_date = request.form['start_date'] dt = datetime.strptime(start_date, '%Y-%m-%d') start = dt - timedelta(days=dt.weekday()) end = start + timedelta(days=6) curr = start allDates = [] while curr <= end: allDates.append(curr) curr = curr + timedelta(days=1) #print(allDates) key = str(allDates[0])[0:10] #start day date = str(dt)[0:10] dataManager = UserDataManager() data = dataManager.data userinfo = data[current_user.id] if key not in userinfo['timetable'].keys(): userinfo['timetable'][key] = { "Mon": {'date':str(allDates[0])[0:10]}, "Tue": {'date':str(allDates[1])[0:10]}, "Wed": {'date':str(allDates[2])[0:10]}, "Thu": {'date':str(allDates[3])[0:10]}, "Fri": {'date':str(allDates[4])[0:10]}, "Sat": {'date':str(allDates[5])[0:10]}, "Sun": {'date':str(allDates[6])[0:10]} } dataManager.update_data(data) userinfo = UserDataManager().data[current_user.id] #print("This is the converted date: " + str(allDates[0])[0:10]) #print(start) #print(end) #print(userinfo['timetable'][key]['Mon']) #later on, we want to show the timetable, if there are data store in that week FIXME return render_template("profile.html", info = userinfo, key = key, default_date = date)
def delete_bank(): if request.method == 'POST': place_name = request.form.get('delete_place'); #store into our database username = current_user.id; dataManager = UserDataManager(); data = dataManager.data; #delete the place in database try: del data[username]['bank'][place_name]; except: print("Not such a key") dataManager.update_data(data) userinfo = data[username]; date = "2019-04-23" return render_template("profile.html", info = userinfo, key='none', default_date = date)
def register(): if(request.method == 'POST'): username = request.form['un'] password = request.form['pass'] confirm_pass = request.form['pass-check'] email = request.form['email'] #check if input a valid username and password if(username != "" and password != ""): ''' storing data to our database ''' dataManager = UserDataManager() #if it is not signed up, we add data if dataManager.is_existed(username) == False: #if the confirm passwords are wrong if password != confirm_pass: flash("Passwords are not the same !!") return redirect(url_for("display_register")) data = dataManager.data data[username] = { "name": username, "password":password, "email":email, "bank":{}, "timetable":{} } dataManager.update_data(data) else: flash("User already signed up !!") print("User already existed!") return redirect(url_for("display_register")) else: flash("Not a valid username/password !!") print("Pleas enter a valid username and password") return redirect(url_for("display_register")) flash("Sign up successfully !!") return redirect(url_for("display_login")) return render_template("register.html")
def login(): if request.method == 'POST': username = request.form['un'] password = request.form['pass'] #FIXME #put global variable for if logged in #getting user detail from database dataManager = UserDataManager() #converting json into dictionary #data = json.loads(data) #check username and password if dataManager.authentication(username, password): user = dataManager.get_user(username) login_user(user, remember=True) return redirect(url_for('home')) else: #show a error message that username or password is wron flash("Wrong usernmae or password!!!") return render_template("login.html") return render_template("login.html")
def load_user(username): userdata = UserDataManager() return userdata.get_user(username)
def display_profile(): dataManager = UserDataManager() data = dataManager.data userinfo = data[current_user.id] date = "2019-04-23" return render_template("profile.html", info = userinfo, key = 'none', default_date = date)