Ejemplo n.º 1
0
def upload_message_bank(request, domain):
    if request.method == "POST":
        form = MessageBankForm(request.POST, request.FILES)
        if form.is_valid():
            current_bank = get_message_bank(domain)
            message_id_map = {}
            for message in current_bank:
                message_id_map[message.fri_id.upper()] = message

            # The message bank is supposed to be static, and is intended
            # to be a one-time upload. So to prevent any issues with
            # overwriting messages or deleting messages by accident, this
            # api will only add new messages to the message bank.
            # If more specialized functionality is needed later on, a new
            # UI should be built.
            for message in form.cleaned_data["message_bank_file"]:
                msg_id = message["msg_id"]
                text = message["text"]
                if msg_id.upper() not in message_id_map:
                    msg = FRIMessageBankMessage(
                        domain=domain,
                        risk_profile=msg_id[0].upper(),
                        message=text,
                        fri_id=msg_id,
                    )
                    msg.save()
            messages.success(request, _("Message Bank Uploaded."))
        else:
            messages.error(request,
                           form._errors["message_bank_file"].as_text())
    else:
        messages.error(request, _("ERROR: POST Expected."))
    return HttpResponseRedirect(
        reverse(CustomProjectReportDispatcher.name(),
                args=[domain, MessageBankReport.slug]))
Ejemplo n.º 2
0
def upload_message_bank(request, domain):
    if request.method == "POST":
        form = MessageBankForm(request.POST, request.FILES)
        if form.is_valid():
            current_bank = get_message_bank(domain)
            message_id_map = {}
            for message in current_bank:
                message_id_map[message.fri_id.upper()] = message

            # The message bank is supposed to be static, and is intended
            # to be a one-time upload. So to prevent any issues with
            # overwriting messages or deleting messages by accident, this
            # api will only add new messages to the message bank.
            # If more specialized functionality is needed later on, a new
            # UI should be built.
            for message in form.cleaned_data["message_bank_file"]:
                msg_id = message["msg_id"]
                text = message["text"]
                if msg_id.upper() not in message_id_map:
                    msg = FRIMessageBankMessage(
                        domain = domain,
                        risk_profile = msg_id[0].upper(),
                        message = text,
                        fri_id = msg_id,
                    )
                    msg.save()
            messages.success(request, _("Message Bank Uploaded."))
        else:
            messages.error(request, form._errors["message_bank_file"].as_text())
    else:
        messages.error(request, _("ERROR: POST Expected."))
    return HttpResponseRedirect(reverse(CustomProjectReportDispatcher.name(), args=[domain, MessageBankReport.slug]))
Ejemplo n.º 3
0
def get_message_bank(domain, risk_profile=None, for_comparing=False):
    if risk_profile is not None:
        messages = FRIMessageBankMessage.view("fri/message_bank", key=[domain, risk_profile], include_docs=True).all()
    else:
        messages = FRIMessageBankMessage.view(
            "fri/message_bank", startkey=[domain], endkey=[domain, {}], include_docs=True
        ).all()

    if for_comparing:
        result = []
        for message in messages:
            result.append({"message": message, "compare_string": letters_only(message.message)})
        return result
    else:
        return messages
Ejemplo n.º 4
0
def custom_content_handler(reminder, handler, recipient, catch_up=False):
    """
    This method is invoked from the reminder event-handling thread to retrieve
    the next message to send.
    """
    case = reminder.case
    if catch_up:
        order = reminder.current_event_sequence_num
    else:
        message_offset = get_message_offset(case)
        try:
            assert message_offset is not None
        except:
            notify_exception(
                None,
                message=("Couldn't calculate the message offset. Check that "
                         "the right case properties are set."))
            return None
        order = get_message_number(reminder) - message_offset

    num_missed_windows = get_num_missed_windows(case)
    if (((not catch_up) and (order < num_missed_windows))
            or catch_up and (order >= num_missed_windows)):
        return None

    randomized_message = get_randomized_message(case, order)
    if randomized_message:
        message = FRIMessageBankMessage.get(
            randomized_message.message_bank_message_id)
        return message.message
    else:
        return None
Ejemplo n.º 5
0
def custom_content_handler(reminder, handler, recipient, catch_up=False):
    """
    This method is invoked from the reminder event-handling thread to retrieve
    the next message to send.
    """
    case = reminder.case
    if catch_up:
        order = reminder.current_event_sequence_num
    else:
        message_offset = get_message_offset(case)
        try:
            assert message_offset is not None
        except:
            notify_exception(None,
                message=("Couldn't calculate the message offset. Check that "
                         "the right case properties are set."))
            return None
        order = get_message_number(reminder) - message_offset

    num_missed_windows = get_num_missed_windows(case)
    if (((not catch_up) and (order < num_missed_windows)) or
        catch_up and (order >= num_missed_windows)):
        return None

    randomized_message = get_randomized_message(case, order)
    if randomized_message:
        message = FRIMessageBankMessage.get(randomized_message.message_bank_message_id)
        return message.message
    else:
        return None
Ejemplo n.º 6
0
def get_message_bank(domain, risk_profile=None, for_comparing=False):
    if risk_profile is not None:
        messages = FRIMessageBankMessage.view("fri/message_bank", key=[domain, risk_profile], include_docs=True).all()
    else:
        messages = FRIMessageBankMessage.view("fri/message_bank", startkey=[domain], endkey=[domain, {}], include_docs=True).all()

    if for_comparing:
        result = []
        for message in messages:
            result.append({
                "message": message,
                "compare_string": letters_only(message.message),
            })
        return result
    else:
        return messages
Ejemplo n.º 7
0
def custom_content_handler(reminder, handler, recipient):
    """
    This method is invoked from the reminder event-handling thread to retrieve
    the next message to send.
    """
    case = reminder.case
    order = ((reminder.schedule_iteration_num - 1) * 35) + reminder.current_event_sequence_num
    randomized_message = get_randomized_message(case, order)
    if randomized_message is None:
        randomize_messages(case)
        randomized_message = get_randomized_message(case, order)
    message = FRIMessageBankMessage.get(randomized_message.message_bank_message_id)
    return message.message
Ejemplo n.º 8
0
    def handle(self, **options):
        self.delete_docs(
            'FRIMessageBankMessage',
            FRIMessageBankMessage.view('fri/message_bank',
                                       include_docs=True).all())

        self.delete_docs(
            'FRIRandomizedMessage',
            FRIRandomizedMessage.view('fri/randomized_message',
                                      include_docs=True).all())

        self.delete_docs(
            'FRIExtraMessage',
            FRIExtraMessage.view('fri/extra_message', include_docs=True).all())
Ejemplo n.º 9
0
def custom_content_handler(reminder, handler, recipient, catch_up=False):
    """
    This method is invoked from the reminder event-handling thread to retrieve
    the next message to send.
    """
    case = reminder.case
    if catch_up:
        order = reminder.current_event_sequence_num
    else:
        order = get_message_number(reminder) - get_message_offset(case)

    num_missed_windows = get_num_missed_windows(case)
    if (((not catch_up) and (order < num_missed_windows)) or
        catch_up and (order >= num_missed_windows)):
        return None

    randomized_message = get_randomized_message(case, order)
    if randomized_message:
        message = FRIMessageBankMessage.get(randomized_message.message_bank_message_id)
        return message.message
    else:
        return None