def place_hide(place_id): """HIDE PLACE""" # make sure we have a valid ObjectId coming in if str(ObjectId(place_id)) != place_id: load_page("error", "place_hide", "Failure: invalid place id") return render_template('error.html', reason='Encountered an invalid place id when trying to hide a place.', page="error") # find place to hide db = mongo.db.places.with_options( write_concern=WriteConcern(w=1, j=False, wtimeout=5000) ) hidden_place = db.find_one({'_id': ObjectId(place_id)}) # set share to false to hide place hide_place = db.update_one({'_id': ObjectId(place_id)}, {'$set': {"share_place": False}}) # format of update_one is: { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 } if hide_place.matched_count > 0: load_click('place_hide_success', 'post', 'place_hide') return render_template('success.html', reason=hidden_place['name'].title() + ' was successfully hidden', page="success") else: load_click('place_hide_failure', 'post', 'place_hide') return render_template('error.html', reason=hidden_place['name'].title() + ' could not be hidden.', page="error")
def add_place(): """ADD a place user input""" form = PlaceForm() form.address.country.choices = country_choice_list() form.event.address.country.choices = country_choice_list() if form.validate_on_submit(): # all is good with the post based on PlaceForm wftForm validation return push_place_to_db(form) elif (not form.event.data['has_event'] and form.event.errors and not form.email.errors and not form.name.errors and not form.description.errors and not form.activity_name.errors and not form.activity_icon.errors and not form.phone.errors and not form.website.errors and not form.image_url.errors and not form.address.errors and not form.review.errors): # if all but event are valid, and event is toggled off, suppress errors and push the place to the db return push_place_to_db(form) else: if app.config['DEBUG']: print('form.email: ' + str(form.email.errors)) print('form.name: ' + str(form.name.errors)) print('form.description: ' + str(form.description.errors)) print('form.activity_name: ' + str(form.activity_name.errors)) print('form.activity_icon: ' + str(form.activity_icon.errors)) print('form.phone: ' + str(form.phone.errors)) print('form.website: ' + str(form.website.errors)) print('form.image_url: ' + str(form.image_url.errors)) print('form.address: ' + str(form.address.errors)) print('form.review: ' + str(form.review.errors)) print('form.event: ' + str(form.event.errors)) # populate icon choices in form icons = filters.get_list_of_icons() load_page("place_add") return render_template('place/add_place.html', form=form, icons=icons, page="place_add")
def add_review(place_id): """add review page""" form = ReviewForm() form.use_place_email.data = "n" show_modal = False the_place = mongo.db.places.find_one({'_id': ObjectId(place_id)}) if the_place is not None: place_name = the_place['name'] else: load_page("error", "page", "could not find place.") return render_template('error.html', reason="I couldn't find the place you were looking for.", page="error") if form.validate_on_submit(): review = {'place': ObjectId(place_id), 'date': datetime.today(), 'rating': int(form.data['rating']), 'comments': filters.remove_html_tags(form.data['comments']).strip(), 'share': form.share.data} email = form.author.data.strip().lower() review['user'] = get_add_user_id(email) # check if user has added a review for this place in the last 7 days (want to make it harder to bloat reviews) one_week_ago = datetime.today() - timedelta(days=7) too_recent = mongo.db.reviews.find_one( {"date": {"$gte": one_week_ago}, "user": review['user'], "place": ObjectId(place_id)}) if too_recent is not None: show_modal = { 'status': "ERROR", 'message': "Sorry, it looks like you have already submitted a review within the last week." } else: show_modal = { 'status': "OK", 'message': "Thank you for adding to the community! It may take up to 5 minutes before your review shows up." } review_id = db_add_review(review) load_page("review_add") return render_template('add_review.html', id=place_id, name=place_name, form=form, show_modal=show_modal, page="review_add")
def get_places(status): """list places page""" try: list_places = retrieve_places_from_db(False, filter_form=False, place_id=False) except Exception as e: list_places = [] if status: if status == "OK": load_page("place_add_success", "modal") else: load_page("place_add_fail", "modal") else: load_page("place_list") return render_template('place/places.html', places=list_places, google_key=google_key, page="place_list", status=status)
def edit_place(status): """edit place picker page""" try: list_places = retrieve_places_from_db(True, filter_form=False, place_id=False) except Exception as e: list_places = [] if status: if status == "OK": load_page("place_update_success", "modal") else: load_page("place_update_fail", "modal") else: load_page("place_edit_list") return render_template('place/place_edit.html', places=list_places, filter=filters, google_key=google_key, page="place_edit_list", status=status)
def remove_place_auth(place_id, user_id, ): """authorize for deletion page""" form = CountMeInForm() places = retrieve_places_from_db(True, False, place_id) if form.validate_on_submit(): # all is good with the post based on email collection vai CountMeInForm wftForm validation # now see if the user email matches the record in the db for the creation of the place authorized = delete_authorized(place_id, form.email.data) if authorized['status'] == "OK": load_page("place_remove_auth_success") return render_template('place/remove_place_auth.html', form=form, places=places, google_key=google_key, page="remove_place_auth", status=authorized, place_id=place_id, ) else: load_page("place_remove_auth_fail") return render_template('place/remove_place_auth.html', form=form, places=places, google_key=google_key, page="remove_place_auth", status=authorized, place_id=place_id) else: load_page("place_remove_auth") return render_template('place/remove_place_auth.html', form=form, places=places, google_key=google_key, page="remove_place_auth", status=False)
def not_found(e): # defining function load_page("error", "page", '500') reason = "The server was unable to complete your request. Don't worry we've noted the issue and hope to fix is as soon as possible." return render_template('error.html', reason=reason, page="error"), 500
def not_found(e): # defining function load_page("error", "page", '404') reason="We can't seem to find that page right now. Don't worry we've noted the issue and have the best people working on it." return render_template('error.html', reason=reason, page="error")
def handle_csrf_error(e): load_page("error", "page", 'CSRF') reason="Hmm, the CSRF token is missing. Don't worry we've noted the issue and have our best people looking into it." return render_template('error.html', reason=reason, page="error")
def handle_db_error(e): load_page("error", "page", 'Exception') return render_template('error.html', reason=e, page="error")
def home(): """ initial/default routing for app is the home page """ load_page("home") return render_template('home.html', page="home")
def push_place_to_db(form, update=False, place_id=False): """unique entries for a place are the name and address, so build those first""" place = {'name': filters.remove_html_tags(form.name.data).strip().lower()} has_address = form.address.data['has_address'] address = {} if has_address: address['address_line_1'] = filters.remove_html_tags( form.address.data['address_line_1']).strip().lower() if form.address.data['address_line_2']: address['address_line_2'] = filters.remove_html_tags( form.address.data['address_line_2']).strip().lower() address['city'] = filters.remove_html_tags( form.address.data['city']).strip().lower() address['state'] = filters.remove_html_tags( form.address.data['state']).strip().lower() if form.address.data['postal_code']: address['postal_code'] = filters.remove_html_tags( form.address.data['postal_code']).strip().lower() address['country'] = form.address.data['country'] address_id = get_add_address_id(address) place['address'] = address_id else: place['address'] = '' # see if address and name is in db or not is_unique = place_unique(place) if is_unique is not None and not update: return redirect( url_for('places_bp.get_places', status="Place already exists.")) elif is_unique is not None and update and is_unique != ObjectId(place_id): return redirect( url_for('places_bp.edit_place', status="Place already exists.")) elif is_unique is not None and update and is_unique == ObjectId(place_id): # same name and address used when updating place['_id'] = ObjectId(place_id) elif is_unique is None and update: # name or address change of place place['_id'] = ObjectId(place_id) # add rest of place to the dictionary email = form.email.data.strip().lower() place['user'] = get_add_user_id(email) # place description place['description'] = filters.remove_html_tags( form.description.data).strip() place['phone'] = form.phone.data.strip() place['website'] = form.website.data.strip() place['image_url'] = form.image_url.data.strip() place['share_place'] = form.share_place.data # see if activity is in db or not activity_id = get_add_activity_id( filters.remove_html_tags(form.activity_name.data).strip().lower(), form.activity_icon.data) place['activity'] = activity_id # now we can add the place or update it place_id = db_add_place(place) # next get review has_review = form.review.data['has_review'] if has_review: review = { 'place': place_id, 'date': datetime.today(), 'user': get_add_user_id(email), 'rating': int(form.review.data['rating']), 'comments': filters.remove_html_tags(form.review.data['comments']).strip(), 'share': form.share_place.data } review_id = db_add_review(review) if review_id is None: load_page("error", "page", "failed to add review.") return render_template( 'error.html', reason='When adding the Place, we failed to add the review.', page="error") has_event = form.event.data['has_event'] if has_event: # create event object to the point of unique data [place_id, name, date_time_range] event = { 'place': place_id, 'name': filters.remove_html_tags( form.event.data['event_name']).strip().lower(), 'date_time_range': form.event.data['event_start_datetime'] } # see if event is in database or not is_unique = event_unique(event) if is_unique is not None: load_page("error", "page", "event already exists.") return render_template('error.html', reason='Event already exists.', page="error") # event is unique so format rest of form entries and load to db has_address = form.event.address.data['has_address'] event_address = {} if has_address: event_address['address_line_1'] = filters.remove_html_tags( form.event.address.data['address_line_1']).strip().lower() if form.event.address.data['address_line_2']: event_address['address_line_2'] = filters.remove_html_tags( form.event.address.data['address_line_2']).strip().lower() event_address['city'] = filters.remove_html_tags( form.event.address.data['city']).strip().lower() event_address['state'] = filters.remove_html_tags( form.event.address.data['state']).strip().lower() if form.event.address.data['postal_code']: event_address['postal_code'] = filters.remove_html_tags( form.event.address.data['postal_code']).strip().lower() event_address['country'] = form.event.address.data['country'] address_id = get_add_address_id(event_address) event_address_id = address_id else: event_address_id = '' # see if event's activity is in db or not event_activity_id = get_add_activity_id( filters.remove_html_tags( form.event.activity_name.data).strip().lower(), form.event.activity_icon.data) event['activity'] = event_activity_id event['details'] = filters.remove_html_tags( form.event.data['details']).strip() event['age_limit'] = form.event.data['age_limit'] # make sure if no-limit in list of age_limits, only have that entry in the list if 'no-limit' in event['age_limit']: event['age_limit'] = ['no-limit'] event['price_for_non_members'] = filters.remove_html_tags( form.event.data['price_for_non_members']).strip() event['address'] = event_address_id event['max_attendees'] = form.event.data['max_attendees'] event['attendees'] = [] event['share'] = form.share_place.data event_id = db_add_event(event) if event_id is None: load_page("error", "page", "Failed to load event.") return render_template( 'error.html', reason='When adding the place, we could not add the event.', page="error") if update: return redirect(url_for('places_bp.edit_place', status="OK")) else: return redirect(url_for('places_bp.get_places', status="OK"))
def update_place(place_id): """update place user input""" form = PlaceForm() # populate country choices form.address.country.choices = country_choice_list() icons = filters.get_list_of_icons() places = [] try: place = mongo.db.places.find_one({'_id': ObjectId(place_id)}) if form.validate_on_submit(): return push_place_to_db(form, True, place_id) elif (not form.event.data['has_event'] and form.event.errors and not form.email.errors and not form.name.errors and not form.description.errors and not form.activity_name.errors and not form.activity_icon.errors and not form.phone.errors and not form.website.errors and not form.image_url.errors and not form.address.errors and not form.review.errors): # if all but event are valid, and event is toggled off, suppress errors and push the place to the db return push_place_to_db(form, True, place_id) elif place is not None and len(form.errors) == 0: places.append(place) """populate event form""" form.event.has_event.data = False form.review.has_review.data = False form.name.data = place['name'].title() form.description.data = place['description'] """get the activity icon & name from the database""" activity = mongo.db.activities.find_one({"_id": place['activity']}) form.activity_name.data = activity['name'] form.activity_icon.data = activity['icon'] form.phone.data = place['phone'] form.website.data = place['website'] form.image_url.data = place['image_url'] """get email from the database""" email = mongo.db.users.find_one({"_id": place['user']}) if email is not None: form.email.data = email['email'] """get the address from the database""" if place['address'] != "": form.address.has_address.data = True address = mongo.db.addresses.find_one({'_id': place['address']}) form.address.address_line_1.data = address['address_line_1'] if 'address_line_2' in address.keys(): form.address.address_line_2.data = address['address_line_2'] form.address.city.data = address['city'] form.address.state.data = address['state'] if 'postal_code' in address.keys(): form.address.postal_code.data = address['postal_code'] form.address.country.data = address['country'] else: form.address.has_address.data = False form.share_place.data = place['share_place'] elif place is not None and len(form.errors) > 0: places.append(place) except Exception as e: load_page("error", "page", e) return render_template('error.html', reason=e, page="error") load_page("place_update") return render_template('place/update_place.html', form=form, icons=icons, update=True, places=places, page="place_update")
def place_remove(place_id): """DELETE PLACE""" result = remove_from_db(place_id) load_page(result['page'], 'place_remove', result['reason']) return render_template(result['template'], reason=result['reason'], page=result['page'])
def add_attendee(form, event_id, filter_form, filter_string): """Count me in form was posted, process it""" attendee = get_add_user_id(form.email.data) status = "OK" message = "Great!. You should be getting an email shortly with the invite." show_modal = True # see if id is already in list of attendees for the given event the_event = mongo.db.events.find_one({"_id": ObjectId(event_id)}) if the_event is None: status = "ERROR" message = "Oops. I'm sorry, but the Event is no longer active and I am unable to add you to it." else: already_attending = attendee in the_event['attendees'] max_attend = the_event['max_attendees'] event = mini_event(the_event) if already_attending: status = "WARNING" message = "Opps. It looks like you are already attending this event." elif len(the_event['attendees']) >= max_attend: status = "WARNING" message = "Oh no, it looks like the event reached max capacity." else: # add attendee to the list db = mongo.db.events.with_options( write_concern=WriteConcern(w=1, j=False, wtimeout=5000)) added_attendee = db.update_one({"_id": ObjectId(event_id)}, {"$push": { "attendees": attendee }}) if added_attendee is None: status = "ERROR" message = "Opps, it looks like we may have lost a bit of data due to network lag time, can you try again?" else: # send email to attendee email_sent = email_event(event, [{ 'email': form.email.data }], False, True) if email_sent: status = "OK" message = "Great!. You should be getting an email shortly with the invite." else: status = "ERROR" message = "Opps, it looks like we are having issues with our email server, can you try again?" modal = {'status': status, 'message': message, 'show': show_modal} event = mini_event(the_event) list_events = retrieve_events_from_db(False, False) activity_choices = unique_activities(list_events) filter_form.activity.choices = activity_choices # somehow filter_from activity choices are crap, when going back page = "event_join" if modal['status'] == "OK": page += "_success" else: page += "_fail" load_page(page) return render_template('event/events.html', form=form, events=list_events, filter=filter_string, show_modal=modal, google_key=google_key, layer_event=event, filter_form=filter_form, page=page)