def index(): form = AddUpdateForm() if request.method == 'POST': if form.validate_on_submit(): if form.id.data < 1: # Insert Mode update_priority = True FeatureRequest.adjust_priorities(form.client.data, form.priority.data, 1) feature_request = FeatureRequest( priority=form.priority.data, title=form.title.data, description=form.description.data, target_date=form.target_date.data, client=form.client.data, product_area=form.product_area.data ) db.session.add(feature_request) else: # Update mode feature_request = FeatureRequest.query.get(form.id.data) current_priority = feature_request.priority update_priority = True if current_priority != form.priority.data else False feature_request.title = form.title.data feature_request.description = form.description.data feature_request.target_date = form.target_date.data feature_request.client = form.client.data feature_request.product_area = form.product_area.data if update_priority: feature_request.set_priority(form.priority.data) db.session.commit() return jsonify({'success': True, 'update_priority': update_priority}) else: return jsonify({'success': False, 'errors': form.errors}) # GET method data_choices = { 'clients': CLIENT_CHOICES, 'product_areas': PRODUCT_AREA_CHOICES } return render_template('index.html', form=form, data_choices=data_choices)
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