def test_db_add_feature_request(self): feature_request = FeatureRequest() feature_request.title = 'Does adding a request work?' feature_request.priority = 1 feature_request.client_id = 1 feature_request.product_area = 'Billing' feature_request.target_date = datetime.strptime( '2018-06-20', "%Y-%m-%d").date() self.db.session.add(feature_request)
def feature_requests(): if request.method == "POST": if not dummy_loguin(''): response = {'message': 'You need loguin!'} return make_response(jsonify(response)), 301 title = request.data.get('title', False) description = str(request.data.get('description', "")) product_area = request.data.get('product_area', False) client_id = request.data.get('client_id', False) client_priority = int(request.data.get('client_priority', 1)) try: date_target = datetime.strptime( request.data.get('date_target', ''), '%Y-%m-%d') except ValueError as error: date_target = None if title and product_area and client_id and date_target: # fr = FeatureRequest.query.filter_by(client_id=client_id, client_priority=client_priority).first() # # if fr is not None: # response = jsonify({ # 'message': 'Exists priority {} for client "{}" '.format(client_priority, DUMMY_CLIENT_MAP[client_id]) # }) # return make_response(response),402 fr = FeatureRequest(title=str(title)) fr.description = description fr.product_area = str(product_area) fr.client_id = int(client_id) fr.client_priority = int(client_priority) fr.date_target = date_target fr.save() priority_to_update = client_priority features_to_update_count = FeatureRequest.query.filter_by( client_id=client_id, client_priority=priority_to_update).count() while features_to_update_count > 1: feature_to_update = FeatureRequest.query.filter_by( client_id=client_id, client_priority=priority_to_update).order_by( 'id').first() feature_to_update.client_priority = feature_to_update.client_priority + 1 priority_to_update = priority_to_update + 1 feature_to_update.save() features_to_update_count = FeatureRequest.query.filter_by( client_id=client_id, client_priority=priority_to_update).count() response = jsonify({ 'id': fr.id, 'title': fr.title, 'description': fr.description, 'product_area': fr.product_area, 'client_id': fr.client_id, 'client_priority': fr.client_priority, 'date_target': fr.date_target, }) r = make_response(response) r.headers.add('Access-Control-Allow-Origin', '*') return r, 201 else: response = jsonify({ 'message': 'Error, need some parameters', 'title': title, 'description': description, 'product_area': product_area, 'client_id': client_id, 'client_priority': client_priority, 'date_target': date_target, }) r = make_response(response) r.headers.add('Access-Control-Allow-Origin', '*') return r, 401 if request.method == "GET": # GET # get all the feature_requests results = [] all_fr = FeatureRequest.query.all() for fr in all_fr: obj = { 'id': fr.id, 'title': fr.title, 'description': fr.description, 'product_area': fr.product_area, 'client_id': fr.client_id, 'client_priority': fr.client_priority, 'date_target': fr.date_target.strftime('%Y-%m-%d'), } results.append(obj) r = make_response(jsonify(results)) r.headers.add('Access-Control-Allow-Origin', '*') return r, 200 else: response = {'message': 'Error, please contact administrator'} r = make_response(jsonify(response)) r.headers.add('Access-Control-Allow-Origin', '*') return r, 401