def delete_user(user_id): """Deletes the data of the user from the database. Args: user_id (int): takes the unique id as a parameter Returns: Redirects the view to the home page """ """ WE HAVE TO SEND A MESSAGE TO BROKER; USER ID= IS ELIMINATED if user is not None and user.type == "operator": restaurant = RestaurantManager.retrieve_by_operator_id(id) if restaurant is not None: RestaurantManager.delete_restaurant(restaurant) """ UserManager.delete_user_by_id(user_id) response_object = { 'status': 'success', 'message': 'Successfully deleted', } return jsonify(response_object), 202
def update_operator(id): """This method allows the operator to edit their personal information. Args: id (int): the univocal id for the operator Returns: Redirects the view to the personal page of the operator """ if request.method == "PUT": post_data = request.get_json() email = post_data.get('email') password = post_data.get('password') user = UserManager.retrieve_by_id(id) user.set_email(email) user.set_password(password) UserManager.update_user(user) response_object = { 'status': 'success', 'message': 'Updated', } return jsonify(response_object), 204
def create_customer(): """This method allows the creation of a new customer """ post_data = request.get_json() email = post_data.get('email') password = post_data.get('password') searched_user = UserManager.retrieve_by_email(email) if searched_user is not None: return jsonify({'status': 'Already present'}), 200 user = Customer() birthday = datetime.datetime.strptime(post_data.get('birthdate'), '%Y-%m-%d') user.set_email(email) user.set_password(password) if post_data.get('social_number') != "": user.set_social_number(post_data.get('social_number')) user.set_firstname(post_data.get('firstname')) user.set_lastname(post_data.get('lastname')) user.set_birthday(birthday) user.set_phone(post_data.get('phone')) UserManager.create_user(user) response_object = { 'user': user.serialize(), 'status': 'success', 'message': 'Successfully registered', } return jsonify(response_object), 201
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.retrieve_by_email(email) if user is None: flash('The user does not exist!') elif user.authenticate(password) is True: login_user(user) if user.type == 'operator': return redirect('/operator/%d' % user.id) elif user.type == 'customer': return redirect('/profile/%d' % user.id) else: return redirect('/authority/%d/0' % user.id) else: flash('Invalid password') return render_template('login.html', form=form, re_login=re)
def notifications(): """[summary] Returns: [type]: [description] """ notifications = NotificationManager.retrieve_by_target_user_id(current_user.id) processed_notification_info = [] if current_user.type == "customer": for notification in notifications: restaurant_name = RestaurantManager.retrieve_by_id(notification.contagion_restaurant_id).name processed_notification_info.append({"timestamp": notification.timestamp, "contagion_datetime": notification.contagion_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: info = {"timestamp": notification.timestamp, "contagion_datetime": notification.contagion_datetime} is_future = notification.timestamp < notification.contagion_datetime info['is_future'] = is_future if is_future: customer_phone_number = UserManager.retrieve_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 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 """ if current_user.id == id_: user = UserManager.retrieve_by_id(id_) if user is not None and user.type == "operator": restaurant = RestaurantManager.retrieve_by_operator_id(id_) if restaurant is not None: RestaurantManager.delete_restaurant(restaurant) UserManager.delete_user_by_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() user = UserManager.retrieve_by_id(id) if request.method == "POST": if social_form.is_submitted(): social_number = social_form.data['social_number'] user.set_social_number(social_number) UserManager.update_user(user) return redirect(url_for('auth.profile', id=user.id))
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() user = Customer() else: user = Operator() if request.method == 'POST': if form.validate_on_submit(): email = form.data['email'] searched_user = UserManager.retrieve_by_email(email) if searched_user is not None: flash("Data already present in the database.") return render_template('create_user.html', form=form) form.populate_obj(user) user.set_password(form.password.data) UserManager.create_user(user) login_user(user) user.authenticated = True if user.type == 'operator': return redirect(url_for('auth.operator', id=user.id)) else: return redirect(url_for('auth.profile', id=user.id)) 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 get_user_by_email(user_email): """ Get a user by its current email :param user_email: user email :return: json response """ user = UserManager.retrieve_by_email(user_email) if user is None: response = {'status': 'User not present'} return jsonify(response), 404 return jsonify(user.serialize()), 200
def get_user(user_id): """ Get a user by its current id :param user_id: user it :return: json response """ user = UserManager.retrieve_by_id(user_id) if user is None: response = {'status': 'User not present'} return jsonify(response), 404 return jsonify(user.serialize()), 200
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 """ user = UserManager.retrieve_by_id(id) if request.method == "PUT": post_data = request.get_json() social_number = post_data.get('social_number') user.set_social_number(social_number) UserManager.update_user(user) response_object = { 'status': 'success', 'message': 'Added social number', } return jsonify(response_object), 204
def create_operator(): """ This method allows the creation of a new operator """ post_data = request.get_json() email = post_data.get('email') password = post_data.get('password') searched_user = UserManager.retrieve_by_email(email) if searched_user is not None: return jsonify({'status': 'Already present'}), 200 user = Operator() user.set_email(email) user.set_password(password) UserManager.create_user(user) response_object = { 'user': user.serialize(), 'status': 'success', 'message': 'Successfully registered', } return jsonify(response_object), 201
def authenticate(auth): """ Authentication resource for generic user. :param auth: a dict with email and password keys. :return: the response 200 if credentials are correct, else 401 """ user = UserManager.retrieve_by_email(auth['email']) response = {'authentication': 'failure', 'user': None} response_code = 401 if user and user.authenticate(auth['password']): response['authentication'] = 'success' response['user'] = user.serialize() response_code = 200 return jsonify(response), response_code
def update_user(id): """This method allows the user to edit their personal information. Args: id (int): the univocal id for the user Returns: Redirects the view to the personal page of the user """ user = UserManager.retrieve_by_id(id) if user.type == "customer": form = UpdateCustomerForm() elif user.type == "operator": form = LoginForm() if request.method == "POST": if form.is_submitted(): email = form.data['email'] searched_user = UserManager.retrieve_by_email(email) if searched_user is not None and id != searched_user.id: flash("Data already present in the database.") return render_template('update_customer.html', form=form) password = form.data['password'] user.set_email(email) user.set_password(password) if user.type == "customer": phone = form.data['phone'] user.set_phone(phone) UserManager.update_user(user) return redirect(url_for('auth.profile', id=user.id)) elif user.type == "operator": UserManager.update_user(user) return redirect(url_for('auth.operator', id=user.id)) return render_template('update_customer.html', form=form)