def monitoring_companies(): monitoring_list = list( query_resource('monitoring', lookup={'schedule.interval': { '$ne': None }})) companies = get_items_by_id( [ObjectId(m['company']) for m in monitoring_list], 'companies') return jsonify(companies), 200
def update_users(id): profile = find_one('monitoring', _id=ObjectId(id)) if not profile: return NotFound(gettext('monitoring Profile not found')) updates = flask.request.get_json() if 'users' in updates: updates['users'] = [ u['_id'] for u in get_items_by_id([ObjectId(u) for u in updates['users']], 'users') if u['company'] == profile.get('company') ] get_resource_service('monitoring').patch(id=ObjectId(id), updates=updates) return jsonify({'success': True}), 200
def filter_users(self, m, company): """ return a list of users email addresses from the profile that are enabled and belong to the company the profile belongs to. It will also add any email addresses that may be associated with the profile if are available and are unique :param m: :param company: :return: List of valid users """ email_addresses = [ email['email'] for email in [ u for u in get_items_by_id([ObjectId(u) for u in m['users']], 'users') if u['is_enabled'] and u['company'] == company['_id'] ] ] # append any addresses from the profile if m.get('email'): for address in re.split(r'[, ]*', m.get('email')): if address not in email_addresses: email_addresses.append(address) return email_addresses
def process_form_request(updates, request_updates, form): if 'schedule' in request_updates: updates['schedule'] = request_updates['schedule'] if updates['schedule'].get('interval') == 'immediate': updates['always_send'] = False updates['last_run_time'] = None if 'users' in request_updates: updates['users'] = [ u['_id'] for u in get_items_by_id( [ObjectId(u) for u in request_updates['users']], 'users') if u['company'] == ObjectId(form.company.data) ] if form.company.data: updates['company'] = ObjectId(form.company.data) if 'keywords' in request_updates: updates['keywords'] = request_updates['keywords'] if 'email' in request_updates: updates['email'] = request_updates.get('email').replace(' ', '')
def get_subscriber_activity_report(): args = deepcopy(request.args.to_dict()) # Elastic query aggregations = {'action': {'terms': {'field': 'action', 'size': 0}}} must_terms = [] source = {} if args.get('company'): must_terms.append({'term': {'company': args.get('company')}}) if args.get('action'): must_terms.append({'term': {'action': args.get('action')}}) if args.get('section'): must_terms.append({'term': {'section': args.get('section')}}) date_range = get_date_filters(args) if date_range.get('gt') or date_range.get('lt'): must_terms.append({"range": {"versioncreated": date_range}}) source['sort'] = [{'versioncreated': 'desc'}] if len(must_terms) > 0: source['query'] = {'bool': {'must': must_terms}} source['size'] = 25 source['from'] = int(args.get('from', 0)) source['aggs'] = aggregations if source['from'] >= 1000: # https://www.elastic.co/guide/en/elasticsearch/guide/current/pagination.html#pagination return abort(400) # Get the results results = superdesk.get_resource_service('history').fetch_history( source, args.get('export')) docs = results['items'] hits = results['hits'] # Enhance the results wire_ids = [] agenda_ids = [] company_ids = [] user_ids = [] for doc in docs: if doc.get('section') == 'agenda': agenda_ids.append(doc.get('item')) else: wire_ids.append(doc.get('item')) company_ids.append(ObjectId(doc.get('company'))) user_ids.append(ObjectId(doc.get('user'))) agenda_items = get_entity_dict(get_items_by_id(agenda_ids, 'agenda')) wire_items = get_entity_dict(get_items_by_id(wire_ids, 'items')) company_items = get_entity_dict(get_items_by_id(company_ids, 'companies'), True) user_items = get_entity_dict(get_items_by_id(user_ids, 'users'), True) def get_section_name(s): return next( (sec for sec in newsroom_app.sections if sec.get('_id') == s), {}).get('name') for doc in docs: if doc.get('item') in wire_items: doc['item'] = { 'item_text': wire_items[doc['item']].get('headline'), '_id': wire_items[doc['item']]['_id'], 'item_href': '/{}?item={}'.format(doc['section'], doc['item']) } elif doc.get('item') in agenda_items: doc['item'] = { 'item_text': (agenda_items[doc['item']].get('name') or agenda_items[doc['item']].get('slugline')), '_id': agenda_items[doc['item']]['_id'], 'item_href': '/agenda?item={}'.format(doc['item']) } if doc.get('company') in company_items: doc['company'] = company_items[doc.get('company')].get('name') if doc.get('user') in user_items: user = user_items[doc.get('user')] doc['user'] = "******".format(user.get('first_name'), user.get('last_name')) doc['section'] = get_section_name(doc['section']) doc['action'] = doc['action'].capitalize() if not request.args.get('export'): results = { 'results': docs, 'name': gettext('SubscriberActivity'), 'aggregations': hits.get('aggregations') } return results else: field_names = [ 'Company', 'Section', 'Item', 'Action', 'User', 'Created' ] temp_file = io.StringIO() attachment_filename = '%s.csv' % utcnow().strftime('%Y%m%d%H%M%S') writer = csv.DictWriter(temp_file, delimiter=',', fieldnames=field_names) writer.writeheader() for doc in docs: row = { 'Company': doc.get('company'), 'Section': doc.get('section'), 'Item': (doc.get('item') or {})['item_text'], 'Action': doc.get('action'), 'User': doc.get('user'), 'Created': doc.get('versioncreated').strftime('%H:%M %d/%m/%y'), } writer.writerow(row) temp_file.seek(0) mimetype = 'text/plain' # Creating the byteIO object from the StringIO Object mem = io.BytesIO() mem.write(temp_file.getvalue().encode('utf-8')) # seeking was necessary. Python 3.5.2, Flask 0.12.2 mem.seek(0) temp_file.close() attachment_filename = secure_filename(attachment_filename) return send_file(mem, mimetype=mimetype, attachment_filename=attachment_filename, as_attachment=True)
def send_alerts(self, monitoring_list, created_from, created_from_time, now): general_settings = get_settings_collection().find_one( GENERAL_SETTINGS_LOOKUP) error_recipients = [] if general_settings and general_settings['values'].get( 'system_alerts_recipients'): error_recipients = general_settings['values'][ 'system_alerts_recipients'].split(',') from newsroom.email import send_email for m in monitoring_list: if m.get('users'): internal_req = ParsedRequest() internal_req.args = { 'navigation': str(m['_id']), 'created_from': created_from, 'created_from_time': created_from_time, 'skip_user_validation': True } items = list( get_resource_service('monitoring_search').get( req=internal_req, lookup=None)) template_kwargs = {'profile': m} if items: company = get_entity_or_404(m['company'], 'companies') try: template_kwargs.update({ 'items': items, 'section': 'wire', }) truncate_article_body(items, m) _file = get_monitoring_file(m, items) attachment = base64.b64encode(_file.read()) formatter = app.download_formatters[ m['format_type']]['formatter'] # If there is only one story to send and the headline is to be used as the subject if m.get('headline_subject', False) and len(items) == 1: subject = items[0].get( 'headline', m.get('subject') or m['name']) else: subject = m.get('subject') or m['name'] send_email( [ u['email'] for u in get_items_by_id( [ObjectId(u) for u in m['users']], 'users') ], subject, text_body=render_template('monitoring_email.txt', **template_kwargs), html_body=render_template('monitoring_email.html', **template_kwargs), attachments_info=[{ 'file': attachment, 'file_name': formatter.format_filename(None), 'content_type': 'application/{}'.format( formatter.FILE_EXTENSION), 'file_desc': 'Monitoring Report for Celery monitoring alerts for profile: {}' .format(m['name']) }]) except Exception: logger.exception( '{0} Error processing monitoring profile {1} for company {2}.' .format(self.log_msg, m['name'], company['name'])) if error_recipients: # Send an email to admin template_kwargs = { 'name': m['name'], 'company': company['name'], 'run_time': now, } send_email( error_recipients, gettext( 'Error sending alerts for monitoring: {0}'. format(m['name'])), text_body=render_template( 'monitoring_error.txt', **template_kwargs), html_body=render_template( 'monitoring_error.html', **template_kwargs), ) elif m['schedule'].get('interval') != 'immediate' and m.get( 'always_send'): send_email( [ u['email'] for u in get_items_by_id( [ObjectId(u) for u in m['users']], 'users') ], m.get('subject') or m['name'], text_body=render_template( 'monitoring_email_no_updates.txt', **template_kwargs), html_body=render_template( 'monitoring_email_no_updates.html', **template_kwargs), ) get_resource_service('monitoring').patch(m['_id'], { 'last_run_time': local_to_utc(app.config['DEFAULT_TIMEZONE'], now) })
def get_items_for_monitoring_report(_ids, monitoring_profile, full_text=False): items = get_items_by_id(_ids, 'items') truncate_article_body(items, monitoring_profile, full_text) return items