def login(): s3 = boto3.client('s3') #default_image_path = os.path.join(current_app.config['APP_ROOT'], 'static/img/default.jpg') # #s3.upload_file(default_image_path, 'ece1779-final', '/tmp/default/default.jpg') # # #target_image_path = os.path.join(current_app.config['APP_ROOT'], 'static/profile_pics') # #shutil.rmtree(target_image_path) # #os.mkdir(os.path.join(current_app.config['APP_ROOT'], 'static/profile_pics')) # # #s3.download_file('ece1779-final', '/tmp/default/default.jpg','/tmp/default.jpg') if 'user' in session: return redirect(url_for('main.home')) form = LoginForm() users_table = Users() if form.validate_on_submit(): email = form.email.data users = users_table.query_email(email) if len(users) == 0: #users空 print("!!!") flash("You should register first", 'danger') else: user = users[0] print(user) if len(user['image_file']) == 0: user[ 'image_file'] = 'https://covid19blog.s3.amazonaws.com/default/default.jpg' # else : # if (user['image_file']!='default.jpg'): # #print(target_image_path + '/' + user['image_file']) # # s3.download_file('ece1779-final', # # '/tmp/'+ user['email'] + '/user_images/{}'.format(user['image_file']), # # '/tmp/'+user['image_file']) if user and bcrypt.check_password_hash(user['password'], form.password.data): #print(user['image_file']) try: place = get_coordinates( current_app.config['GOOGLEMAPS_KEY'], user['address']) place['infobox'] = "<b>Hello World</b>" user_current_location = [place['lat'], place['lng']] except: user_current_location = None session.permanent = True session['user'] = { 'username': user['username'], 'email': user['email'], 'image_file': user['image_file'], 'user_current_location': user_current_location } next_page = request.args.get('next') #flash('Login Successful', 'message') return redirect(next_page) if next_page else redirect( url_for('main.home')) else: flash('Login Unsuccessful. Please check email and password', 'danger') return render_template('login.html', title='Login', form=form)
def get_coordinates_from_address(address): coordinates = None try: coordinates = get_coordinates(GOOGLE_MAPS_API_KEY, address) except IndexError: print(f'Could not get coordinates for address: {address}') return coordinates
def map_offerhelp(): locations = [] # long list of coordinates place = get_coordinates(current_app.config['GOOGLEMAPS_KEY'], 'M4Y0C4') place['infobox'] = "<b>Hello World</b>" locations.append(place) place = get_coordinates(current_app.config['GOOGLEMAPS_KEY'], 'M4Y0C4') place['infobox'] = "<b>Hello World from other place</b>" locations.append(place) map_offerhelp = Map(identifier="map_offerhelp", style=("height:350px;" "width:700px;"), lat=locations[0]['lat'], lng=locations[0]['lng'], markers=[(loc['lat'], loc['lng'], loc['infobox']) for loc in locations], fit_markers_to_bounds=True) place1 = [locations[0]['lat'], locations[0]['lng']] place2 = [locations[1]['lat'], locations[1]['lng']] print("Distance:{}km".format(great_circle(place1, place2).km)) return render_template('map_offerhelp.html', map_offerhelp=map_offerhelp)
def post(post_id): if 'user' not in session: flash('Please log in to access this page', 'warning') return redirect(url_for('users.login')) post = Posts() post = post.get_post(post_id) print("_-----------------") print(session) print(session['user']['user_current_location']) locations = [] # long list of coordinates #place = get_coordinates(current_app.config['GOOGLEMAPS_KEY'], session['user']['user_current_location']) place = {} try: place['lat'] = session['user']['user_current_location'][0] place['lng'] = session['user']['user_current_location'][1] place['icon'] = 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png' place['infobox'] = "<b>Your Current Location</b>" locations.append(place) except: flash( "Your location shows error, Please input your location in search bar", "warning") return redirect(url_for('main.offerhelp')) place1 = {} place1 = get_coordinates(current_app.config['GOOGLEMAPS_KEY'], post.address) place1['icon'] = 'http://maps.google.com/mapfiles/ms/icons/red-dot.png' place1['infobox'] = "<b>Target Location</b>" locations.append(place1) print(locations) map_offerhelp = Map(identifier="map_offerhelp", style=("height:350px;" "width:730px;"), lat=locations[0]['lat'], lng=locations[0]['lng'], markers=[(loc['lat'], loc['lng'], loc['infobox'], loc['icon']) for loc in locations], fit_markers_to_bounds=True) print(post.timestamp) user_table = Users() #post = Post.query.get_or_404(post_id) return render_template('post.html', title=post.title, post=post, user_table=user_table, map_offerhelp=map_offerhelp)
def register(): try: if 'user' in session: return redirect(url_for('main.home')) form = RegistrationForm() users_table = Users() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') # user = User2(username=form.username.data, email=form.email.data, password=hashed_password) #db.session.add(user) #db.session.commit() username = form.username.data email = form.email.data password = hashed_password address = form.address.data try: place = get_coordinates(current_app.config['GOOGLEMAPS_KEY'], address) except: flash("Your address is invalid", "warning") return render_template('register.html', title='Register', form=form) user = users_table.get_user(username) user_emails = users_table.query_email(email) if user != None and len(user_emails) != 0: flash("Username already exists", "danger") elif user != None: flash("Username already exists", "danger") elif len(user_emails) != 0: if user_emails != None: print(user_emails) flash("Email already exists", "danger") else: users_table.put_user(username, email, password, address) flash( 'Your account has been created! You are now able to log in', 'success') return redirect(url_for('users.login')) return render_template('register.html', title='Register', form=form) except Exception as e: print(e) traceback.print_tb(e.__traceback__) return render_template('error.html') return ' '
def map_recall(): apikey = "AIzaSyBgvpdbo-uHRFsr3QPIqoq_P0jAnxIB9UQ" recalls = find_recall() markers = [] for recall in recalls[:10]: if "address_1" in recall: address = "%20".join([ recall["address_1"], recall["address_2"], recall["state"], recall["postal_code"] ]) try: location = get_coordinates(apikey, address) location['label'] = recall['recalling_firm'] except: continue markers.append(location) return Map(identifier="Tacoma", lat=47.2529, lng=-122.4443, markers=markers)
def new_post(): if 'user' not in session: flash('Please log in to access this page', 'warning') return redirect(url_for('users.login')) form = PostForm() posts_table = Posts() if form.validate_on_submit(): # post = Post(title=form.title.data, type=form.type.data, phone=form.phonember.data,emailaddress=form.emailaddress.data, # address=form.address.data,content=form.content.data, author=session['user']['username']) # db.session.add(post) # db.session.commit() try: place = get_coordinates(current_app.config['GOOGLEMAPS_KEY'], form.address.data) except: form.address.data = None flash("Your address is not valid", "warning") return render_template('create_post.html', title='New Post', form=form, legend='New Request') posts_table.put_post(title=form.title.data, type=form.type.data, phone=form.phonember.data, email=form.emailaddress.data, address=form.address.data, content=form.content.data, username=session['user']['username'], sdate=form.sdate.data.strftime('%Y-%m-%d'), fdate=form.fdate.data.strftime('%Y-%m-%d'), lat=place['lat'], lng=place['lng']) flash('Your request has been created!', 'success') return redirect(url_for('main.offerhelp')) return render_template('create_post.html', title='New Post', form=form, legend='New Request')
def offerhelp(): form = SearchForm() post_table = Posts() users = Users() s3 = boto3.client('s3') target_image_path = '/tmp' allusers = users.get_all_users() for user in allusers: url = user['image_file'] r = requests.get(url) if r.status_code == 403: url = s3.generate_presigned_url( 'get_object', Params={ 'Bucket': 'ece1779-final', 'Key': '/tmp/' + session['user']['email'] + '/user_images/{}'.format(user['image_name']) }, ExpiresIn=604800) users.update_imagefile2(user['username'], url) for user in allusers: if (imgisexist(user['image_file'], target_image_path)): #print('ece1779-final', user['email'] + '/user_images/{}'.format(user['image_file'])) #print(target_image_path + '/' + user['image_file']) print(user['image_file']) # s3.download_file('ece1779-final','/tmp/'+user['email'] + '/user_images/{}'.format(user['image_file']), # target_image_path + '/' + user['image_file']) if form.validate_on_submit(): #locations=post_table.query_post_by_address(form.location.data) types = post_table.get_all_posts() if form.type.data != "All": #print(form.type.data) types = post_table.query_post_by_type(form.type.data) sdates = post_table.query_post_by_sdate(form.sdate.data) fdates = post_table.query_post_by_fdate(form.fdate.data) search_results = extra_same_elem(types, sdates) search_results = extra_same_elem(search_results, fdates) #print(search_results) ##################################################################### locations = [] # long list of coordinates place = get_coordinates(current_app.config['GOOGLEMAPS_KEY'], form.location.data) place['infobox'] = "<b>Hello World</b>" locations.append(place) for item in search_results: #print(item) temp = {} temp['lat'] = item.lat temp['lng'] = item.lng temp['infobox'] = "<b>Hello World from other place</b>" locations.append(temp) print(locations) temp_results = [] i = 1 user_current_location = [locations[0]['lat'], locations[0]['lng']] session['user']['user_current_location'] = user_current_location #print(session['user']['user_current_location']) for item in search_results: post_location = [locations[i]['lat'], locations[i]['lng']] i += 1 item.distance = great_circle(user_current_location, post_location).km print("Distance:{}km".format(item.distance)) if form.distance.data >= item.distance: temp_results.append([item.distance, item.post_id]) #print(second_search_results) temp_results.sort() #print(temp_results) search_results.clear() for item in temp_results: post = post_table.get_post(item[1]) search_results.append(post) if len(search_results) > 4: break ##################################################################### print(search_results) user_table = Users() return render_template('offerhelp.html', posts=search_results, user_table=user_table, form=form, legend='Filter Request') else: post_table = Posts() # list of Post objects posts = post_table.get_all_posts() user_table = Users() return render_template('offerhelp.html', posts=posts, user_table=user_table, form=form, legend='Filter Request')