Exemple #1
0
def update_notification_list(number, add_or_remove="add"):
    """
    Add / Remove a user's number from the list of numbers to be notified when sending updates.
    """

    try:
        # read list from JSON file
        with app.open_instance_resource('notification_list.json', mode='r') as f:
            try:
                notification_list = json.loads(f.read())
            except ValueError as e:
                # start with clean list, if the file does not yet contain a list
                notification_list = []
                pass
                # add / remove item
        if add_or_remove == "add":
            if not number in notification_list:
                notification_list.append(number)
        elif add_or_remove == "remove":
            if number in notification_list:
                i = notification_list.index(number)
                notification_list = notification_list[0:i] + notification_list[i+1::]
                # write updated list to file
        with app.open_instance_resource('notification_list.json', mode='w') as f:
            f.write(json.dumps(notification_list, indent=4))
    except Exception as e:
        if add_or_remove == "add":
            logger.exception("Error saving number to the notification list.")
        else:
            logger.exception("Error removing number from the notification list.")
    return
Exemple #2
0
    def on_model_change(self, form, model, is_created):

        # send SMS notifications before saving message to database
        msg = VumiMessage({"content": model.content})
        count_tot = 0
        model.user = current_user

        try:
            with app.open_instance_resource('notification_list.json', mode='r') as f:
                try:
                    notification_list = json.loads(f.read())
                except ValueError:
                    # start with clean list, if the file does not yet contain a list
                    notification_list = []
                    pass
                for number in notification_list:
                    logger.debug("sending update to: " + number)
                    msg.send(number)
                    count_tot += 1
            model.notes = "Update sent to " + str(count_tot) + " user(s)."
        except Exception:
            tmp = "Error sending update broadcast via SMS."
            logger.exception(tmp)
            model.notes = tmp
        return