def create_broadcast_message(service_id): data = request.get_json() validate(data, create_broadcast_message_schema) service = dao_fetch_service_by_id(data['service_id']) user = get_user_by_id(data['created_by']) template = dao_get_template_by_id_and_service_id(data['template_id'], data['service_id']) personalisation = data.get('personalisation', {}) broadcast_message = BroadcastMessage( service_id=service.id, template_id=template.id, template_version=template.version, personalisation=personalisation, areas={"areas": data.get("areas", []), "simple_polygons": data.get("simple_polygons", [])}, status=BroadcastStatusType.DRAFT, starts_at=_parse_nullable_datetime(data.get('starts_at')), finishes_at=_parse_nullable_datetime(data.get('finishes_at')), created_by_id=user.id, content=template._as_utils_template_with_personalisation( personalisation ).content_with_placeholders_filled_in, ) dao_save_object(broadcast_message) return jsonify(broadcast_message.serialize()), 201
def create_broadcast(): check_service_has_permission( BROADCAST_TYPE, authenticated_service.permissions, ) if request.content_type != 'application/cap+xml': raise BadRequestError( message=f'Content type {request.content_type} not supported', status_code=415, ) cap_xml = request.get_data() if not validate_xml(cap_xml, 'CAP-v1.2.xsd'): raise BadRequestError( message='Request data is not valid CAP XML', status_code=400, ) broadcast_json = cap_xml_to_dict(cap_xml) validate(broadcast_json, post_broadcast_schema) polygons = Polygons( list( chain.from_iterable( (area['polygons'] for area in broadcast_json['areas'])))) broadcast_message = BroadcastMessage( service_id=authenticated_service.id, content=broadcast_json['content'], reference=broadcast_json['reference'], areas={ 'areas': [area['name'] for area in broadcast_json['areas']], 'simple_polygons': polygons.smooth.simplify.as_coordinate_pairs_long_lat, }, status=BroadcastStatusType.PENDING_APPROVAL, api_key_id=api_user.id, stubbed=authenticated_service.restricted # The client may pass in broadcast_json['expires'] but it’s # simpler for now to ignore it and have the rules around expiry # for broadcasts created with the API match those created from # the admin app ) dao_save_object(broadcast_message) current_app.logger.info( f'Broadcast message {broadcast_message.id} created for service ' f'{authenticated_service.id} with reference {broadcast_json["reference"]}' ) return jsonify(broadcast_message.serialize()), 201
def create_broadcast_message(service_id): data = request.get_json() validate(data, create_broadcast_message_schema) service = dao_fetch_service_by_id(data['service_id']) user = get_user_by_id(data['created_by']) template = dao_get_template_by_id_and_service_id(data['template_id'], data['service_id']) broadcast_message = BroadcastMessage( service_id=service.id, template_id=template.id, template_version=template.version, personalisation=data.get('personalisation', {}), areas=data.get('areas', []), status=BroadcastStatusType.DRAFT, starts_at=_parse_nullable_datetime(data.get('starts_at')), finishes_at=_parse_nullable_datetime(data.get('finishes_at')), created_by_id=user.id, ) dao_save_object(broadcast_message) return jsonify(broadcast_message.serialize()), 201
def create_broadcast_message( template=None, *, service=None, # only used if template is not provided created_by=None, personalisation=None, content=None, status=BroadcastStatusType.DRAFT, starts_at=None, finishes_at=None, areas=None, stubbed=False): if template: service = template.service template_id = template.id template_version = template.version personalisation = personalisation or {} content = template._as_utils_template_with_personalisation( personalisation).content_with_placeholders_filled_in elif content: template_id = None template_version = None personalisation = None content = content else: pytest.fail('Provide template or content') broadcast_message = BroadcastMessage( service_id=service.id, template_id=template_id, template_version=template_version, personalisation=personalisation, status=status, starts_at=starts_at, finishes_at=finishes_at, created_by_id=created_by.id if created_by else service.created_by_id, areas=areas or { 'areas': [], 'simple_polygons': [] }, content=content, stubbed=stubbed) db.session.add(broadcast_message) db.session.commit() return broadcast_message
def create_broadcast_message( template, created_by=None, personalisation={}, status=BroadcastStatusType.DRAFT, starts_at=None, finishes_at=None, areas={}, ): broadcast_message = BroadcastMessage( service_id=template.service_id, template_id=template.id, template_version=template.version, personalisation=personalisation, status=status, starts_at=starts_at, finishes_at=finishes_at, created_by_id=created_by.id if created_by else template.created_by_id, areas=areas, ) db.session.add(broadcast_message) db.session.commit() return broadcast_message