77from django .contrib .auth .models import User
88from django .conf import settings
99from django .template .loader import render_to_string
10+ from django .utils .html import strip_tags
1011
1112from elasticsearch .helpers import bulk
1213from utils .elasticsearch import construct_es_data
2324time_1_week = timedelta (days = 7 ) # for digest mode
2425digest_time = int (10314 ) # weekday - hour - min for digest timing (5 minutes once a week, Monday dawn)
2526daily_retro = int (654 ) # hour - min for daily retropective email timing (5 minutes a day) | Should not contain a leading 0!
26- max_length = 860 # after this length (at the first space) we cut the sent content
27+ max_length = 860 # after this length (at the first space) we cut the sent content. In case of HTML tags we remove tags (to avoid chunked tags)
2728events_sent_to = {} # to document sent events before re-sending them via specific following
2829template_types = {
2930 99 : 'design/generic_notification.html' ,
3637
3738
3839# Used for 'Notification GUID' record creation in 'send_notification(4th param)'
40+ # Should be ~ github.com/IFRCGo/go-frontend/blob/develop/src/root/views/account.js#L80
3941RTYPE_NAMES = {
40- 0 : 'Emergency' ,
41- 1 : 'Appeal' ,
42- 2 : 'Field Report' ,
42+ 0 : 'Emergency' , # not to use in rtype_of_subscr
43+ 1 : 'Appeal' , # not to use in rtype_of_subscr
44+ 2 : 'Field Report' , # not to use in rtype_of_subscr – L752 depends: RTYPE_NAMES[rtype]
4345 3 : 'Surge Alert' ,
46+ 4 : 'Country' ,
47+ 5 : 'Region' ,
48+ 6 : 'Disaster Type' ,
49+ 7 : 'PER Due Date' ,
4450 8 : 'Followed Emergency' ,
4551 9 : 'Surge Deployment' ,
52+ 10 : 'Surge Approaching end of Mission' ,
4653 11 : 'Weekly Digest' ,
4754 12 : 'Field Report/Emergency' ,
4855 13 : 'New Operation' ,
@@ -434,6 +441,7 @@ def construct_template_record(self, rtype, record):
434441 if rtype != RecordType .WEEKLY_DIGEST :
435442 shortened = self .get_record_content (record , rtype )
436443 if len (shortened ) > max_length :
444+ shortened = strip_tags (shortened )
437445 shortened = shortened [:max_length ] + \
438446 shortened [max_length :].split (' ' , 1 )[0 ] + '...' # look for the first space
439447
@@ -442,7 +450,7 @@ def construct_template_record(self, rtype, record):
442450 'resource_uri' : self .get_resource_uri (record , rtype ),
443451 'admin_uri' : self .get_admin_uri (record , rtype ),
444452 'title' : self .get_record_title (record , rtype ),
445- 'description' : shortened ,
453+ 'description' : shortened if shortened else None ,
446454 'key_figures' : {
447455 'affected' : self .get_fieldreport_keyfigures (
448456 [record .num_affected , record .gov_num_affected , record .other_num_affected ]),
@@ -467,11 +475,87 @@ def construct_template_record(self, rtype, record):
467475 'epi_figures_source' : self .get_epi_figures_source_name (record .epi_figures_source ),
468476 'sit_fields_date' : record .sit_fields_date ,
469477 'actions_taken' : self .get_actions_taken (record .id ),
470- 'actions_others' : record .actions_others ,
478+ 'actions_others' : record .actions_others . split ( " \n " ) if record . actions_others else None ,
471479 'gov_assistance' : 'Yes' if record .request_assistance else 'No' ,
472480 'ns_assistance' : 'Yes' if record .ns_request_assistance else 'No' ,
473481 'dtype_id' : record .dtype_id ,
474482 'visibility' : record .visibility ,
483+ 'other_fields' : {
484+ 'appeal' : record .appeal ,
485+ 'appeal_amount' : record .appeal_amount ,
486+ 'bulletin' : record .bulletin ,
487+ 'countries' : ', ' .join (i .name for i in record .countries .all ()),
488+ 'contacts' : [{'ctype' : c .ctype ,
489+ 'name' : c .name ,
490+ 'title' : c .title ,
491+ 'email' : c .email ,
492+ 'phone' : c .phone } for c in record .contacts .all ()[::- 1 ]],
493+ #not used this way, but shortened: 'description': record.description,
494+ 'districts' : ', ' .join (i .name for i in record .districts .all ()),
495+ 'dref' : record .dref ,
496+ 'dref_amount' : record .dref_amount ,
497+ 'epi_cases_since_last_fr' : record .epi_cases_since_last_fr ,
498+ 'epi_deaths_since_last_fr' : record .epi_deaths_since_last_fr ,
499+ 'epi_notes_since_last_fr' : record .epi_notes_since_last_fr .split ("\n " ) if record .epi_notes_since_last_fr else None ,
500+ 'eru_base_camp' : record .eru_base_camp ,
501+ 'eru_base_camp_units' : record .eru_base_camp_units ,
502+ 'eru_basic_health_care' : record .eru_basic_health_care ,
503+ 'eru_basic_health_care_units' : record .eru_basic_health_care_units ,
504+ 'eru_deployment_hospital' : record .eru_deployment_hospital ,
505+ 'eru_deployment_hospital_units' : record .eru_deployment_hospital_units ,
506+ 'eru_it_telecom' : record .eru_it_telecom ,
507+ 'eru_it_telecom_units' : record .eru_it_telecom_units ,
508+ 'eru_logistics' : record .eru_logistics ,
509+ 'eru_logistics_units' : record .eru_logistics_units ,
510+ 'eru_referral_hospital' : record .eru_referral_hospital ,
511+ 'eru_referral_hospital_units' : record .eru_referral_hospital_units ,
512+ 'eru_relief' : record .eru_relief ,
513+ 'eru_relief_units' : record .eru_relief_units ,
514+ 'eru_water_sanitation_15' : record .eru_water_sanitation_15 ,
515+ 'eru_water_sanitation_15_units' : record .eru_water_sanitation_15_units ,
516+ 'eru_water_sanitation_20' : record .eru_water_sanitation_20 ,
517+ 'eru_water_sanitation_20_units' : record .eru_water_sanitation_20_units ,
518+ 'eru_water_sanitation_40' : record .eru_water_sanitation_40 ,
519+ 'eru_water_sanitation_40_units' : record .eru_water_sanitation_40_units ,
520+ 'event' : record .event ,
521+ 'external_partners' : ', ' .join (i .name for i in record .external_partners .all ()),
522+ 'forecast_based_action' : record .forecast_based_action ,
523+ 'forecast_based_action_amount' : record .forecast_based_action_amount ,
524+ 'imminent_dref' : record .imminent_dref ,
525+ 'imminent_dref_amount' : record .imminent_dref_amount ,
526+ 'notes_health' : record .notes_health ,
527+ 'notes_ns' : record .notes_ns ,
528+ 'notes_socioeco' : record .notes_socioeco ,
529+ 'other_sources' : record .other_sources .split ("\n " ) if record .other_sources else None ,
530+ 'start_date' : record .start_date ,
531+ 'supported_activities' : ', ' .join (i .name for i in record .supported_activities .all ()),
532+ # not used: 'fact': record.fact,
533+ # not used: 'affected_pop_centres': record.affected_pop_centres,
534+ # not used: 'gov_affected_pop_centres': record.gov_affected_pop_centres,
535+ # not used: 'gov_num_highest_risk': record.gov_num_highest_risk,
536+ # not used: 'gov_num_potentially_affected': record.gov_num_potentially_affected,
537+ # not used: 'health_min_num_assisted': record.health_min_num_assisted,
538+ # not used: 'who_num_assisted': record.who_num_assisted,
539+ # not used: 'ifrc_staff': record.ifrc_staff,
540+ # not used: 'is_covid_report': record.is_covid_report,
541+ # not used: 'num_expats_delegates': record.num_expats_delegates,
542+ # not used: 'num_fact': record.num_fact,
543+ # not used: 'num_highest_risk': record.num_highest_risk,
544+ # not used: 'num_ifrc_staff': record.num_ifrc_staff,
545+ # not used: 'num_localstaff': record.num_localstaff,
546+ # not used: 'num_potentially_affected': record.num_potentially_affected,
547+ # not used: 'rdrt': record.rdrt,
548+ # not used: 'num_rdrt': record.num_rdrt,
549+ # not used: 'num_volunteers': record.num_volunteers,
550+ # not used: 'other_affected_pop_centres': record.other_affected_pop_centres,
551+ # not used: 'other_num_highest_risk': record.other_num_highest_risk,
552+ # not used: 'other_num_potentially_affected': record.other_num_potentially_affected,
553+ # not used: 'regions': ', '.join(i.label for i in record.regions.all()),
554+ # not used: 'report_date': record.report_date,
555+ # not used: 'rid': record.rid,
556+ # not used: 'status': 'Early Warning' if record.status == 8 else 'Event-related',
557+ # not used: 'summary' here, but in the title via "get_record_title"
558+ },
475559 }
476560 elif rtype == RecordType .APPEAL :
477561 optypes = {
0 commit comments