def create_service(): data = request.get_json() if not data.get("user_id"): errors = {"user_id": ["Missing data for required field."]} raise InvalidRequest(errors, status_code=400) data.pop("service_domain", None) # validate json with marshmallow service_schema.load(data) user = get_user_by_id(data.pop("user_id")) # unpack valid json into service object valid_service = Service.from_json(data) dao_create_service(valid_service, user) try: # try-catch; just in case, we don't want to error here ZenDeskSell().send_create_service(valid_service, user) except Exception as e: current_app.logger.exception(e) return jsonify(data=service_schema.dump(valid_service).data), 201
def update_service(service_id): req_json = request.get_json() fetched_service = dao_fetch_service_by_id(service_id) # Capture the status change here as Marshmallow changes this later service_going_live = fetched_service.restricted and not req_json.get( 'restricted', True) current_data = dict(service_schema.dump(fetched_service).data.items()) current_data.update(request.get_json()) service = service_schema.load(current_data).data if 'email_branding' in req_json: email_branding_id = req_json['email_branding'] service.email_branding = None if not email_branding_id else EmailBranding.query.get( email_branding_id) if 'letter_branding' in req_json: letter_branding_id = req_json['letter_branding'] service.letter_branding = None if not letter_branding_id else LetterBranding.query.get( letter_branding_id) dao_update_service(service) if service_going_live: send_notification_to_service_users( service_id=service_id, template_id=current_app.config['SERVICE_NOW_LIVE_TEMPLATE_ID'], personalisation={ 'service_name': current_data['name'], 'message_limit': '{:,}'.format(current_data['message_limit']) }, include_user_fields=['name']) return jsonify(data=service_schema.dump(fetched_service).data), 200
def update_service(service_id): try: service = get_model_services(service_id=service_id) except DataError: return jsonify(result="error", message="Invalid service id"), 400 except NoResultFound: return jsonify(result="error", message="Service not found"), 404 if request.method == 'DELETE': status_code = 202 delete_model_service(service) else: status_code = 200 # TODO there has got to be a better way to do the next three lines upd_serv, errors = service_schema.load(request.get_json()) if errors: return jsonify(result="error", message=errors), 400 update_dict, errors = service_schema.dump(upd_serv) # TODO FIX ME # Remove update_service model which is added to db.session db.session.rollback() try: save_model_service(service, update_dict=update_dict) except DAOException as e: return jsonify(result="error", message=str(e)), 400 return jsonify(data=service_schema.dump(service).data), status_code
def create_service(): data = request.get_json() if not data.get('user_id'): errors = {'user_id': ['Missing data for required field.']} raise InvalidRequest(errors, status_code=400) # validate json with marshmallow service_schema.load(data) user = get_user_by_id(data.pop('user_id')) # unpack valid json into service object valid_service = Service.from_json(data) dao_create_service(valid_service, user) return jsonify(data=service_schema.dump(valid_service).data), 201
def create_service(): data = request.get_json() if not data.get('user_id'): errors = {'user_id': ['Missing data for required field.']} raise InvalidRequest(errors, status_code=400) data.pop('service_domain', None) # validate json with marshmallow service_schema.load(data) user = get_user_by_id(data.pop('user_id')) # unpack valid json into service object valid_service = Service.from_json(data) with transaction(): dao_create_service(valid_service, user) set_default_free_allowance_for_service(service=valid_service, year_start=None) return jsonify(data=service_schema.dump(valid_service).data), 201
def create_service(): # TODO what exceptions get passed from schema parsing? service, errors = service_schema.load(request.get_json()) if errors: return jsonify(result="error", message=errors), 400 # I believe service is already added to the session but just needs a # db.session.commit try: save_model_service(service) except DAOException as e: return jsonify(result="error", message=str(e)), 400 return jsonify(data=service_schema.dump(service).data), 201
def update_service(service_id): req_json = request.get_json() fetched_service = dao_fetch_service_by_id(service_id) # Capture the status change here as Marshmallow changes this later service_going_live = fetched_service.restricted and not req_json.get( "restricted", True) message_limit_changed = fetched_service.message_limit != req_json.get( "message_limit", fetched_service.message_limit) current_data = dict(service_schema.dump(fetched_service).data.items()) current_data.update(request.get_json()) service = service_schema.load(current_data).data if "email_branding" in req_json: email_branding_id = req_json["email_branding"] service.email_branding = None if not email_branding_id else EmailBranding.query.get( email_branding_id) if "letter_branding" in req_json: letter_branding_id = req_json["letter_branding"] service.letter_branding = None if not letter_branding_id else LetterBranding.query.get( letter_branding_id) dao_update_service(service) if message_limit_changed: redis_store.delete(daily_limit_cache_key(service_id)) redis_store.delete(near_daily_limit_cache_key(service_id)) redis_store.delete(over_daily_limit_cache_key(service_id)) if not fetched_service.restricted: _warn_service_users_about_message_limit_changed( service_id, current_data) if service_going_live: _warn_services_users_about_going_live(service_id, current_data) try: # Two scenarios, if there is a user that has requested to go live, we will use that user # to create a user-service/contact-deal pair between notify and zendesk sell # If by any chance there is no tracked request to a user, notify will try to identify the user # that created the service and then create a user-service/contact-deal relationship if service.go_live_user_id: user = get_user_by_id(service.go_live_user_id) else: user = dao_fetch_service_creator(service.id) ZenDeskSell().send_go_live_service(service, user) except Exception as e: current_app.logger.exception(e) return jsonify(data=service_schema.dump(fetched_service).data), 200
def update_service(service_id): req_json = request.get_json() fetched_service = dao_fetch_service_by_id(service_id) # Capture the status change here as Marshmallow changes this later service_going_live = fetched_service.restricted and not req_json.get('restricted', True) service_go_live_requested = 'go_live_user' in req_json current_data = dict(service_schema.dump(fetched_service).data.items()) current_data.update(request.get_json()) service = service_schema.load(current_data).data if 'email_branding' in req_json: email_branding_id = req_json['email_branding'] service.email_branding = None if not email_branding_id else EmailBranding.query.get(email_branding_id) if 'letter_branding' in req_json: letter_branding_id = req_json['letter_branding'] service.letter_branding = None if not letter_branding_id else LetterBranding.query.get(letter_branding_id) dao_update_service(service) if service_go_live_requested: template = dao_get_template_by_id(current_app.config['NOTIFY_ADMIN_OF_GO_LIVE_REQUEST_TEMPLATE_ID']) service_url = "{}/services/{}".format(current_app.config['ADMIN_BASE_URL'], str(service.id)) saved_notification = persist_notification( template_id=template.id, template_version=template.version, recipient=get_or_build_support_email_address(), service=template.service, personalisation={ 'service_name': service.name, 'service_dashboard_url': service_url }, notification_type=EMAIL_TYPE, api_key_id=None, key_type=KEY_TYPE_NORMAL, reply_to_text=get_or_build_support_email_address() ) send_notification_to_queue(saved_notification, research_mode=False, queue=QueueNames.NOTIFY) if service_going_live: send_notification_to_service_users( service_id=service_id, template_id=current_app.config['SERVICE_NOW_LIVE_TEMPLATE_ID'], personalisation={ 'service_name': current_data['name'], 'message_limit': '{:,}'.format(current_data['message_limit']) }, include_user_fields=['name'] ) return jsonify(data=service_schema.dump(fetched_service).data), 200