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 = CustomerManager.retrieve_by_id(id_=customer_id) if customer is not None and customer.health_status: flash("Customer is already set to positive!") elif customer is not None: customer.set_health_status(status=True) CustomerManager.update_customer(customer.id) schedule_revert_customer_health_status(customer.id) notify_restaurant_owners_about_positive_past_customer( customer.id) notify_restaurant_owners_about_positive_booked_customer( customer.id) notify_customers_about_positive_contact(customer.id) flash("Customer set to positive!") return redirect( url_for('auth.authority', id=current_user.id, positive_id=0))
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 = CustomerManager.retrieve_by_ssn(ssn=customer_ident) elif track_type == 'Email': customer = CustomerManager.retrieve_by_email( email=customer_ident) else: customer = CustomerManager.retrieve_by_phone( 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 revert_customer_health_status(customer_id): customer = CustomerManager.retrieve_by_id(customer_id) if customer: customer.set_health_status(False) CustomerManager.update_customer(customer=customer) return else: raise ValueError('Customer does not exist anymore')
def test_contact_tracing(self): self.login_test_authority() from tests.models.test_customer import TestCustomer from gooutsafe.dao.customer_manager import CustomerManager customer, _ = TestCustomer.generate_random_customer() CustomerManager.create_customer(customer=customer) rv = self.client.get('ha/contact/' + str(customer.id), follow_redirects=True) self.assertEqual(rv.status_code, 200)
def load_random_customer(n: int): from gooutsafe.dao.customer_manager import CustomerManager from tests.models.test_customer import TestCustomer for _ in range(0, n): customer, _ = TestCustomer.generate_random_customer() CustomerManager.create_customer(customer=customer) print('Random users added to db')
def test_mark_positive_authorized_inex(self): authority = self.login_test_authority() from tests.models.test_customer import TestCustomer from gooutsafe.dao.customer_manager import CustomerManager customer, _ = TestCustomer.generate_random_customer() customer.set_health_status(True) CustomerManager.create_customer(customer=customer) rv = self.client.post('ha/mark_positive/' + str(customer.id), follow_redirects=True) self.assertEqual(rv.status_code, 200)
def test_search_customer_with_ssn(self): from tests.models.test_customer import TestCustomer from gooutsafe.dao.customer_manager import CustomerManager customer, (_, _, _, _, _, ssn, _, _) = TestCustomer.generate_random_customer() CustomerManager.create_customer(customer=customer) self.login_test_authority() rv = self.client.post('/ha/search_customer', data=dict(track_type='SSN', customer_ident=ssn), follow_redirects=True) self.assertEqual(rv.status_code, 200)
def check_customer_health_status(): logger.info('Executing the task <customer_healthcheck>') positive_users = CustomerManager.retrieve_all_positive() if positive_users is None: # skip computation return for positive_user in positive_users: time_delta = datetime.datetime.utcnow( ) - positive_user.health_status_change_datetime if time_delta >= timedelta(days=14): # unmarking positive_user.set_health_status(False) CustomerManager.update_customer(customer=positive_user) logger.info('Task <customer_healthcheck> finished')
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: authority = AuthorityManager.retrieve_by_id(id) ha_form = AuthorityForm() pos_customers = CustomerManager.retrieve_all_positive() search_customer = CustomerManager.retrieve_by_id(positive_id) return render_template('authority_profile.html', current_user=authority, form=ha_form, pos_customers=pos_customers, search_customer=search_customer) return redirect(url_for('home.index'))
def test_create_delete(self): restaurant, _ = TestRestaurant.generate_random_restaurant() customer, _ = TestCustomer.generate_random_customer() from gooutsafe.dao.customer_manager import CustomerManager from gooutsafe.dao.restaurant_manager import RestaurantManager # Adding restaurant RestaurantManager.create_restaurant(restaurant=restaurant) # Adding user CustomerManager.create_customer(customer=customer) self.like_manager.LikeManager.create_like(customer.id, restaurant.id) self.like_manager.LikeManager.delete_like(customer.id, restaurant.id) self.assertEqual( False, self.like_manager.LikeManager.like_exists( restaurant_id=restaurant.id, user_id=customer.id))
def get_user_by_phone(user_phone): """ Get a user by its current id :param user_phone: user it :return: json response """ user = CustomerManager.retrieve_by_phone(user_phone) if user is None: response = {'status': 'User not present'} return jsonify(response), 404 return jsonify(user.serialize()), 200
def get_all_positive_customers(): """Get all positive customers Returns: json response """ pos_customers = CustomerManager.retrieve_all_positive() if pos_customers is None: response = {'status': 'No positive customers'} return jsonify(response), 404 return jsonify([customer.serialize() for customer in pos_customers]), 200
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 = CustomerManager.retrieve_by_id(id_=contact_id) if customer is not None: pos_reservations = ReservationManager.retrieve_by_customer_id( user_id=customer.id) cust_contacts = [] restaurant_contacts = [] date_contacts = [] for res in pos_reservations: contacts = ReservationManager.retrieve_all_contact_reservation_by_id( res.id) for c in contacts: cust = CustomerManager.retrieve_by_id(c.user_id) cust_contacts.append(cust) restaurant_contacts.append( RestaurantManager.retrieve_by_id(c.restaurant_id).name) date_contacts.append(c.start_time.date()) 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 get_user_by_ssn(user_ssn): """Get a user by his ssn Args: customer_ssn (string) Returns: json response """ customer = CustomerManager.retrieve_by_ssn(user_ssn) if customer is None: response = {'status': 'Costumer not present'} return jsonify(response), 404 return jsonify(customer.serialize()), 200
def my_profile(): """This method allows the customer to see its personal page Returns: Redirects the view to personal page of the customer """ reservations = ReservationManager.retrieve_by_customer_id(current_user.id) form = ReservationForm() social_form = AddSocialNumberForm() customer = CustomerManager.retrieve_by_id(current_user.id) restaurants = RestaurantManager.retrieve_all() return render_template('customer_profile.html', customer=customer, reservations=reservations, restaurants=restaurants, form=form, social_form=social_form)
def reservation_details(restaurant_id, reservation_id): """ Given a restaurant, this method returns all its reservations Args: restaurant_id (int): univocal identifier of the restaurant reservation_id (int): univocal identifier of the reservations Returns: [type]: [description] """ reservation = ReservationManager.retrieve_by_id(reservation_id) user = CustomerManager.retrieve_by_id(reservation.user.id) table = reservation.table restaurant = reservation.restaurant return render_template("reservation_details.html", reservation=reservation, user=user, table=table, restaurant=restaurant)
def profile(id): """This method allows the customer to see its personal page Args: id (int): univocal identifier of the customer Returns: Redirects the view to personal page of the customer """ if current_user.id == id: reservations = ReservationManager.retrieve_by_customer_id(id) form = ReservationForm() social_form = AddSocialNumberForm() customer = CustomerManager.retrieve_by_id(id) restaurants = RestaurantManager.retrieve_all() return render_template('customer_profile.html', customer=customer, reservations=reservations, restaurants=restaurants, form=form, social_form=social_form) return redirect(url_for('home.index'))
def mark_customer(id): customer = CustomerManager.retrieve_by_id(id) customer.set_health_status(status=True) CustomerManager.update_customer(customer.id) return 200