def search_user(filter_user: FilterUser): if filter_user.email == '' and filter_user.fiscal_code == '' and filter_user.phone == '': return None, 'At least one in fiscal code, email or phone number is required' if filter_user.phone != None and filter_user.phone != '' and len( filter_user.phone) < 9: return None, 'Invalid phone number' if filter_user.fiscal_code != None and filter_user.fiscal_code != '' and len( filter_user.fiscal_code) != 16: return None, 'Invalid fiscal code' #if filter_user.firstname != "": # q = q.filter(func.lower(User.firstname) == func.lower(filter_user.firstname)) #if filter_user.lastname != "": # q = q.filter(func.lower(User.lastname) == func.lower(filter_user.lastname)) if filter_user.email != None and filter_user.email != '': user = User.get(email=filter_user.email) if filter_user.phone != None and filter_user.phone != '': user = User.get(phone=filter_user.phone) if filter_user.fiscal_code != None and filter_user.fiscal_code != '': user = User.get(fiscal_code=filter_user.fiscal_code) if user == None: return None, 'No user found' else: return user, 'OK'
def loader(user_id): if not currently_logged_in.get(str(user_id)): user = User.get(id=user_id) if user: user.is_authenticated = True currently_logged_in[str(user_id)] = user or AnonymousUser() return currently_logged_in.get(str(user_id))
def setUp(self) -> None: self.app = create_app() # create some test user self.user_id = User.create(**user_data, fiscal_code="aaaaaaaaaaabb", phone=str( random.randint(1111111111, 9999999999)), password="******") self.user = User.get(id=self.user_id)
def test_get(self): with self.app.test_request_context(): all = User.all() self.assertIsNotNone(all) u = User.filter("and_(User.id > 1, User.id < 3)") self.assertIsNotNone(u) u = User.filter("and_(User.id < -1, User.id < -3)") self.assertIsNone(u) u = User.get(id=-1) self.assertIsNone(u)
def edit_user(user_id): if current_user.id != int(user_id): return render_template("error.html", error_message="You aren't supposed to be here!") form = UserProfileEditForm(obj=User.get(id=int(user_id))) if request.method == 'POST': try: if not form.validate_on_submit: raise FormValidationError("Can't validate the form") edit_user_data(int(user_id), form) return redirect('/users/edit/' + user_id) except GoOutSafeError: return render_template("useredit.html", form=form) except Exception as e: return render_template("error.html", error_message=str(e)) return render_template("useredit.html", form=form)
def home(page: int): reservations, more = Reservation.get_paged_reservations( restaurant_id=current_user.restaurant_id, page=page) users = [] for reservation in reservations: user = User.get(id=reservation.user_id) users.append(user) res = zip(reservations, users) if not reservations and page > 1: return "", 404 else: return render_template("reservations.html", reservations=res, current_page=page, morepages=more, customers=Reservation.get_seated_customers( restaurant_id=current_user.restaurant_id), today=False)
def create(email, firstname, lastname, password, dateofbirth, name, lat, lon, phone, extra_info=None): """Creates a new restaurant by the given parameters and returns the User object. If the User creation cannot be performed the db is reverted to a consistent state. Raises: Exception: if the creation cannot be performed Returns: User: the restaurant operator """ body_restaurant = { 'name': str(name), 'lat': float(lat), 'lon': float(lon), 'phone': str(phone), 'extra_info': str(extra_info) } req = safe_post(f"{Restaurant.BASE_URL}/restaurants/new", json=body_restaurant) if req.status_code != 201: raise Exception(str(req)) restaurant_id = req.json() ret = User.create(email=email, firstname=firstname, \ lastname=lastname, password=password, dateofbirth=dateofbirth, \ restaurant_id=restaurant_id) logging.warning(f"{ret}") if not ret: # user creation didn't go well, reverting restaurant db Restaurant.delete(restaurant_id) return None return User.get(id=ret)
def mark_user(user_id: int): """ Mark a user as positive. Args: userid (int): Id of the customer Returns: str: '' in case of success, a error message string in case of failure. """ user = User.get(id=user_id) user_dict = None if user == None: message = 'Error! Unable to mark the user. User not found' elif user.is_positive == False: user.is_positive = True user.reported_positive_date = datetime.now() user.submit() message = '' requests.get( f"http://{os.environ.get('GOS_NOTIFICATION')}/notifications/contact_tracing/{user_id}" ) else: message = 'You\'ve already marked this user as positive!' return message, user
def test_create(self): with self.app.test_request_context(): u = User.get(id=self.user_id) self.assertIsNotNone(u)