def update_customer(id): """This method allows the customer to edit their personal information. Args: id (int): the univocal id for the customer Returns: Redirects the view to the personal page of the customer """ form = UpdateCustomerForm() if form.is_submitted(): email = form.data['email'] password = form.data['password'] phone = form.data['phone'] searched_user = UserManager.get_user_by_email(email) if searched_user is not None and id != searched_user.id: flash("Email already present in the database.") return render_template('update_customer.html', form=form) response = UserManager.update_customer(id, email, password, phone) if response.status_code != 204: flash("Error while updating the user") return redirect(url_for('auth.profile', id=id)) return render_template('update_customer.html', form=form)
def contact_tracing(contact_id): """This method allows the health authority to retrieve the list of contacts, given a positive user Args: contact_id (id): univocal id of the user Returns: Redirects the view to the health authority's home page """ if current_user is not None and current_user.type == 'authority': customer = UserManager.get_user_by_id(user_id=contact_id) if customer is not None: tracing_list = ntm.get_contact_tracing_list(customer_id=customer.id) cust_contacts = [] restaurant_contacts = [] date_contacts = [] for res in tracing_list: customer = UserManager.get_user_by_id(res['contact_id']) cust_contacts.append(customer) restaurant = RestaurantManager.get_restaurant_sheet(restaurant_id=res['restaurant_id']) restaurant_contacts.append(restaurant['restaurant']['name']) reservation = ReservationManager.get_reservation(reservation_id=res['reservation_id']) date_contacts.append(reservation['start_time']) return render_template('contact_tracing_positive.html', customer=customer, pos_contact=cust_contacts, res_contact=restaurant_contacts, date_contact=date_contacts) else: return redirect(url_for('auth.authority', id=current_user.id, positive_id=0)) else: return redirect(url_for('home.index'))
def search_customer(): """Method that the health authority uses to search through the users. Returns: Redirects the view to the home page of the health authority. If this method is accessed by an unathorized user, it redirects the view to the index page """ if current_user is not None and current_user.type == 'authority': form = AuthorityForm() customer = None if request.method == 'POST': track_type = form.data['track_type'] customer_ident = form.data['customer_ident'] if track_type == 'SSN': customer = UserManager.get_user_by_social_number(customer_ident) elif track_type == 'Email': customer = UserManager.get_user_by_email(customer_ident) else: customer = UserManager.get_user_by_phone(customer_ident) if customer is None: flash("The customer doesn't exist") return redirect(url_for('auth.authority', id=current_user.id, positive_id=0)) return redirect(url_for('auth.authority', id=current_user.id, positive_id=customer.id)) else: return redirect(url_for('home.index'))
def create_user_type(type_): """This method allows the creation of a new user into the database Args: type_ (string): as a parameter takes a string that defines the type of the new user Returns: Redirects the user into his profile page, once he's logged in """ form = LoginForm() if type_ == "customer": form = UserForm() if form.is_submitted(): email = form.data['email'] password = form.data['password'] if type_ == "operator": response = UserManager.create_operator(email, password) else: social_number = form.data['social_number'] firstname = form.data['firstname'] lastname = form.data['lastname'] birthdate = form.data['birthdate'] date = birthdate.strftime('%Y-%m-%d') phone = form.data['phone'] response = UserManager.create_customer('customer', email, password, social_number, firstname, lastname, date, phone) user = response.json() if user["status"] == "success": to_login = User.build_from_json(user["user"]) login_user(to_login) if to_login.type == "operator": return redirect(url_for('auth.operator', op_id=to_login.id)) else: return redirect(url_for('auth.profile', id=to_login.id)) else: flash("Invalid credentials") return render_template('create_user.html', form=form, user_type=type_) else: for fieldName, errorMessages in form.errors.items(): for errorMessage in errorMessages: flash('The field %s is incorrect: %s' % (fieldName, errorMessage)) return render_template('create_user.html', form=form, user_type=type_)
def login(re=False): """Allows the user to log into the system Args: re (bool, optional): boolean value that describes whenever the user's session is new or needs to be reloaded. Defaults to False. Returns: Redirects the view to the personal page of the user """ form = LoginForm() if form.is_submitted(): email, password = form.data['email'], form.data['password'] user = UserManager.authenticate_user(email, password) if user is None: # user is not authenticated flash('Invalid credentials') else: # user is authenticated login_user(user) if user.type == 'operator': return redirect(url_for('auth.operator', op_id=user.id)) elif user.type == 'customer': return redirect(url_for('auth.profile', id=user.id)) else: return redirect('/authority/%d/0' % user.id) return render_template('login.html', form=form, re_login=re)
def load_user(user_id): """ We need to connect to users endpoint and load the user. Here we can implement the redis caching :param user_id: user id :return: the user object """ user = UserManager.get_user_by_id(user_id) user.authenticated = True return user
def authority(id, positive_id): """This method allows the Health Authority to see its personal page. Args: id (int): the univocal identifier for the Health Authority positive_id (int): the identifier of the positive user Returns: Redirects to the page of the Health Authority """ if current_user.id == id: ha_form = AuthorityForm() pos_customers = UserManager.get_all_positive_customer() if positive_id != 0: search_customer = UserManager.get_user_by_id(positive_id) else: # authority clicks on "Profile" search_customer = None return render_template('authority_profile.html', form=ha_form, pos_customers=pos_customers, search_customer=search_customer) return redirect(url_for('home.index'))
def notifications(): """Get all notifications for the user Returns: Redirects the view to the notifications page """ # TODO check datetime for notification # get all notifications from the manager notifications = ntm.retrieve_by_target_user_id(user_id=current_user.id) processed_notification_info = [] if current_user.type == "customer": for notification in notifications: restaurant_name = \ RestaurantManager.get_restaurant_sheet(notification['contagion_restaurant_id'])['restaurant']['name'] cont_datetime = datetime.fromtimestamp( (notification['contagion_datetime']['$date'] / 1000)).date() cont_timestamp = datetime.fromtimestamp( (notification['timestamp']['$date'] / 1000)) processed_notification_info.append({ "timestamp": cont_timestamp, "contagion_datetime": cont_datetime, "contagion_restaurant_name": restaurant_name }) return render_template('customer_notifications.html', current_user=current_user, notifications=processed_notification_info) elif current_user.type == "operator": for notification in notifications: cont_datetime = datetime.fromtimestamp( (notification['contagion_datetime']['$date'] / 1000)).date() cont_timestamp = datetime.fromtimestamp( (notification['timestamp']['$date'] / 1000)) info = { "timestamp": cont_timestamp, "contagion_datetime": cont_datetime } is_future = notification['timestamp']['$date'] < notification[ 'contagion_datetime']['$date'] info['is_future'] = is_future if is_future: customer_phone_number = UserManager.get_user_by_id( notification['positive_customer_id']).phone info['customer_phone_number'] = customer_phone_number processed_notification_info.append(info) return render_template('operator_notifications.html', current_user=current_user, notifications=processed_notification_info)
def mark_positive(customer_id): """Through this method the health authority can set the health status of a specific user to "positive". Args: customer_id ([int]): univocal id of the user Returns: Redirects the view to the health authority's home page """ if current_user is not None and current_user.type == 'authority': if request.method == 'POST': customer = UserManager.get_user_by_id(customer_id) if customer is not None and customer.health_status: flash("Customer is already set to positive!") #TODO set health status for customer elif customer is not None: response = UserManager.update_health_status(customer.id) if response.status_code == 200: flash("Customer set to positive!") ntm.trigger_contact_tracing(positive_id=customer.id) else: flash("Error during the operation") return redirect(url_for('auth.authority', id=current_user.id, positive_id=0))
def delete_user(id): """Deletes the data of the user from the database. Args: id_ (int): takes the unique id as a parameter Returns: Redirects the view to the home page """ response = UserManager.delete_user(id) if response.status_code != 202: flash("Error while deleting the user") return redirect(url_for('auth.profile', id=id)) return redirect(url_for('home.index'))
def add_social_number(id): """Allows the user to insert their SSN. Args: id (int): the univocal id for the user Returns: Redirects the view to the personal page of the user """ social_form = AddSocialNumberForm() if social_form.is_submitted(): social_number = social_form.data['social_number'] response = UserManager.add_social_number(id, social_number) if response.status_code != 204: flash("Error while updating the user") return redirect(url_for('auth.profile', id=id))
def reservation_all(restaurant_id): """Returns the whole list of reservations, given a restaurant. It also gives to the operator the opportunity to filter reservations by date, so it's possible to count people. Args: restaurant_id (int): univocal identifier of the restaurant Returns: The template of the reservations. """ filter_form = FilterForm() response = ReservationManager.get_all_reservation_restaurant(restaurant_id) print(response) if response.status_code != 200: flash("There are no reservations") restaurant = {} reservations = {} people = 0 return render_template("restaurant_reservation.html", restaurant=restaurant, reservations=reservations, filter_form=filter_form, people=people) json_resp = response.json() reservations = json_resp['reservations'] print(reservations) _, _, json_details = ReservationManager.get_restaurant_detatils( restaurant_id) restaurant = json_details['restaurant'] users = [] people = 0 if reservations: for r in reservations: start_time = datetime.strptime(r['start_time'], "%Y-%m-%d %H:%M:%S") r['start_time'] = datetime.strftime(start_time, "%Y-%m-%d %H:%M") end_time = datetime.strptime(r['end_time'], "%Y-%m-%d %H:%M:%S") r['end_time'] = datetime.strftime(end_time, "%Y-%m-%d %H:%M") user_dict = {} user_id = r['user_id'] user = UserManager.get_user_by_id(user_id) r['lastname'] = user.extra_data['lastname'] if r['is_confirmed']: people = people + r['people_number'] if request.method == 'POST': if filter_form.is_submitted(): filter_date = filter_form.data['filter_date'] start_time = filter_form.data['start_time'] end_time = filter_form.data['end_time'] if filter_date is not None and start_time is not None and end_time is not None: start_date_time = datetime.combine(filter_date, start_time) end_date_time = datetime.combine(filter_date, end_time) start_date_time = datetime.strftime(start_date_time, "%Y-%m-%d %H:%M:%S") end_date_time = datetime.strftime(end_date_time, "%Y-%m-%d %H:%M:%S") response = ReservationManager.filtered_reservations( restaurant_id, start_date_time, end_date_time) json_resp = response.json() reservations = json_resp['reservations'] return render_template("restaurant_reservation.html", restaurant=restaurant, reservations=reservations, filter_form=filter_form, people=people) else: flash("The inserted data are not valid") return render_template("restaurant_reservation.html", restaurant=restaurant, reservations=reservations, filter_form=filter_form, people=people)
def create_authority(): UserManager.create_authority() return redirect(url_for('home.index'))