def edit_request(request_id): """ View for editing a feature request. :param request_id: :return: """ feature_request = Feature.query.filter_by(id=str(request_id), requested_by=str( current_user.id)).first() if feature_request: feature_form = FeatureRequestForm(obj=feature_request, formdata=request.form) if request.method in ['POST', 'PUT', 'PATCH' ] and feature_form.validate_on_submit(): data = {**feature_form.data} _ = data.pop('csrf_token', None) review_client_feature_priorities(data) for key, value in data.items(): setattr(feature_request, key, value) db.session.add(feature_request) save_changes(db.session) flash('Feature request updated successfully!') return redirect(url_for('home.home')) return render_template('forms/edit-request-feature.html', form=feature_form, request_id=request_id) else: return redirect(url_for('home.home'))
def logout(): """Logout the current user.""" user = current_user user.authenticated = False db.session.add(user) save_changes(db.session) logout_user() return redirect(url_for('auth.login'))
def test_create(self): user1 = User(email='*****@*****.**', first_name='susan', last_name='daniel', password='******') user2 = User(email='*****@*****.**', first_name='susan', last_name='daniel', password='******') user3 = User(email='*****@*****.**', first_name='susan', last_name='daniel', password='******') db.session.add_all([user1, user2, user3]) save_changes(db.session) users = User.query.all() self.assertEqual(len(users), 3)
def get_user(email: str, password: str): user = User( email=email, first_name='Admin', last_name='Admin', password=password ) db.session.add(user) save_changes(db.session) return user
def delete_request(request_id): """ View for deleting a feature request. :param request_id: :return: """ feature_request = Feature.query.get(str(request_id)) feature_request.deleted = True db.session.add(feature_request) save_changes(db.session) flash('Feature deleted successfully!') return redirect(url_for('home.home'))
def get_feature_request(title: str, client: Client, user: User, priority: int): feature = Feature( title=title, description='Platform users should have 2nd factor authentication', client=str(client.id), requested_by=str(user.id), client_priority=priority, target_date=datetime.date(2019, 8, 8), product_area=Feature.PRODUCT_AREA_POLICIES ) db.session.add(feature) save_changes(db.session) return feature
def create_request(): """ View for creating a feature request. :return: """ feature_form = FeatureRequestForm(request.form) if request.method == 'POST' and feature_form.validate_on_submit(): data = {**feature_form.data} _ = data.pop('csrf_token', None) review_client_feature_priorities(data) feature = Feature(requested_by=str(current_user.id), request_status=Feature.REQUEST_STATUS_PENDING, **data) db.session.add(feature) save_changes(db.session) flash('Feature request created successfully!') return redirect(url_for('home.home')) return render_template('forms/request-feature.html', form=feature_form)
def login(): """For GET requests, display the login form. For POSTS, login the current user by processing the form. """ form = LoginForm(formdata=request.form) if request.method == 'POST' and form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user: if flask_bcrypt.check_password_hash(user.password, form.password.data): user.authenticated = True db.session.add(user) save_changes(db.session) login_user(user, remember=True) next_page = flask.request.args.get('next') if not is_safe_url(next_page): return flask.abort(400) return redirect(next_page or url_for('home.home')) if current_user and current_user.is_authenticated: return redirect(url_for('home.home')) return render_template('forms/login.html', form=form)
def review_client_feature_priorities(data): """ Method for checking and reordering of previous client feature request priorities. :param data: :return: """ client_id = data.get('client') previous_requests_for_client = Feature.query.filter( Feature.client_priority >= data['client_priority']).filter_by( client=client_id, deleted=False).order_by('client_priority').all() assigned_request_priority = Feature.query.filter_by( client_priority=data['client_priority'], client=client_id, deleted=False).all() if assigned_request_priority: last_changed = data['client_priority'] for previous_request in previous_requests_for_client: if previous_request.client_priority == last_changed: previous_request.client_priority += 1 last_changed = previous_request.client_priority previous_requests_for_client = previous_requests_for_client[::-1] db.session.bulk_save_objects(previous_requests_for_client) save_changes(db.session)
def create_test_user(): user = User(email='*****@*****.**', first_name='susan', last_name='daniel', password='******') db.session.add(user) save_changes(db.session) return user
def get_client(name: str) -> Client: client = Client(name=name) db.session.add(client) save_changes(db.session) return client
def setUp(self): db.create_all() save_changes(db.session)