<s> from custom . fri . forms import MessageBankForm <EOL> from custom . fri . reports . reports import MessageBankReport <EOL> from custom . fri . api import get_message_bank <EOL> from custom . fri . models import FRIMessageBankMessage <EOL> from corehq . apps . reports . dispatcher import CustomProjectReportDispatcher <EOL> from corehq . apps . domain . decorators import require_previewer , login_and_domain_required <EOL> from django . http import HttpResponseRedirect , Http404 <EOL> from django . core . urlresolvers import reverse <EOL> from django . contrib import messages <EOL> from django . utils . translation import ugettext as _ , ugettext_noop <EOL> @ require_previewer <EOL> @ login_and_domain_required <EOL> def upload_message_bank ( request , domain ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = MessageBankForm ( request . POST , request . FILES ) <EOL> if form . is_valid ( ) : <EOL> current_bank = get_message_bank ( domain ) <EOL> message_id_map = { } <EOL> for message in current_bank : <EOL> message_id_map [ message . fri_id . upper ( ) ] = message <EOL> for message in form . cleaned_data [ "<STR_LIT>" ] : <EOL> msg_id = message [ "<STR_LIT>" ] <EOL> text = message [ "<STR_LIT:text>" ] <EOL> if msg_id . upper ( ) not in message_id_map : <EOL> msg = FRIMessageBankMessage ( <EOL> domain = domain , <EOL> risk_profile = msg_id [ <NUM_LIT:0> ] . upper ( ) , <EOL> message = text , <EOL> fri_id = msg_id , <EOL> ) <EOL> msg . save ( ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> else : <EOL> messages . error ( request , form . _errors [ "<STR_LIT>" ] . as_text ( ) ) <EOL> else : <EOL> messages . error ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( reverse ( CustomProjectReportDispatcher . name ( ) , args = [ domain , MessageBankReport . slug ] ) ) </s>
