示例#1
0
def schedulePeriodic(request, resource, template, aged):
    print "Schedule Day"
    errors = {}

    if type(request.from_date) is not datetime:
        times = convertDatetime(request, template, resource)
        startime = times[0]
        endtime = times[1]
        period = times[2]
        expirationtime = times[3]
    else:
        startime = request.from_date
        endtime = request.to_date
        period = timedelta(days=template.period * 7)
        expirationtime = resource.to_date
        if expirationtime == None:
            expirationtime = endtime

    valid_interval = endtime - startime
    if valid_interval > period:
        endtime = startime + period

    if template.nmsgmin != template.nmsgmax:
        nmsg = rnd.randrange(template.nmsgmin, template.nmsgmax + 1)
    else:
        nmsg = template.nmsgmax

    miniplan = [Message(count, aged.aged_id, intervention_session_id=1) for count in xrange(nmsg)]

    channels = getChannelsAvailable(template, aged)

    with open('csv/prova_import_messages.csv') as csvmessages:
        messages = csv.DictReader(csvmessages)
        msgs_tosend = getListMessages(messages, nmsg, resource, channels)

    er = checkForErrors(errors, endtime, expirationtime, startime, miniplan, nmsg, len(msgs_tosend))
    errors = er[0]
    miniplan = er[1]
    if er[2]:
        endtime = er[3]
    else:
        return errors, miniplan

    day_of_event = mapDay(resource.on_day)
    if day_of_event == None:
        errors['ErrorNoDay'] = 'Error no day specified for periodic messages'
        miniplan = []
        return errors, miniplan

    # length of the loop depending on the msgs found
    if len(msgs_tosend) < nmsg:
        lenloop = len(msgs_tosend)
    else:
        lenloop = nmsg

    c = 0
    i = 0
    current_date = startime.date()
    while current_date < endtime.date():
        if current_date.weekday() == day_of_event - 1:
            if c % int(resource.every) == 0:
                if i > lenloop:
                    break
                miniplan[i].date = current_date
                miniplan[i].time = scheduleHour(aged, None)
                miniplan[i].attached_audio = msgs_tosend[i]['Audio']
                miniplan[i].attached_media = msgs_tosend[i]['Media']
                miniplan[i].URL = msgs_tosend[i]['URL']
                miniplan[i].channel = msgs_tosend[i]['Channel']
                miniplan[i].message_text = generate_message_text(aged, msgs_tosend[i]['Text'],
                                                                 msgs_tosend[i]['URL'])
                i += 1
            c += 1

        current_date += timedelta(days=1)

    miniplan = checkMsgsOneDay(miniplan, endtime)

    return errors, miniplan
示例#2
0
def scheduleEquallyDividedPeriod(request, resource, template, aged):
    '''
    Returns the miniplan with the temporal interval between the msgs divided equally
    :param request: a request class
    :param template: a template class
    :param aged: a user class
    :return: a miniplan that is a list of messages class with all the fields completed
    '''
    print "Schedule Day"
    errors = {}

    if type(request.from_date) is not datetime:
        times = convertDatetime(request, template, resource)
        startime = times[0]
        endtime = times[1]
        period = times[2]
    else:
        startime = request.from_date
        endtime = request.to_date
        period = timedelta(days=template.period * 7)

    valid_interval = endtime - startime
    if valid_interval > period:
        valid_interval = period

    if template.nmsgmin != template.nmsgmax:
        nmsg = rnd.randrange(template.nmsgmin, template.nmsgmax + 1)
    else:
        nmsg = template.nmsgmax
    step_send_msg = valid_interval / nmsg

    # creates miniplan that is a list of messages
    miniplan = [Message(count, aged.aged_id, intervention_session_id=1) for count in xrange(nmsg)]

    channels = getChannelsAvailable(template, aged)

    with open('csv/prova_import_messages.csv') as csvmessages:
        messages = csv.DictReader(csvmessages)
        msgs_tosend = getListMessages(messages, nmsg, resource, channels)

    er = checkForErrors(errors, endtime, None, startime, miniplan, nmsg, len(msgs_tosend))
    errors = er[0]
    miniplan = er[1]
    if er[2]:
        endtime = er[3]
    else:
        return errors, miniplan

    # length of the loop depending on the msgs found
    if len(msgs_tosend) < nmsg:
        lenloop = len(msgs_tosend)
    else:
        lenloop = nmsg

    # for nmsg fill the miniplan msgs
    date = startime
    for i in range(0, lenloop):
        miniplan[i].date = date.date()
        miniplan[i].time = scheduleHour(aged, None)
        miniplan[i].message_text = generate_message_text(aged, msgs_tosend[i]['Text'], msgs_tosend[i]['URL'])
        miniplan[i].attached_audio = msgs_tosend[i]['Audio']
        miniplan[i].attached_media = msgs_tosend[i]['Media']
        miniplan[i].URL = msgs_tosend[i]['URL']
        miniplan[i].channel = msgs_tosend[i]['Channel']

        date += step_send_msg

    miniplan = checkMsgsOneDay(miniplan, endtime)

    return errors, miniplan
示例#3
0
def scheduleLogarithmic(request, resource, template, aged):
    '''
    Returns the miniplan scheduled with more frequency at the end of the interval
    It divides the interval for every msg with logaritmic growth:1 1/2 1/3 1/4
    Check on period(valid weeks): if request interval is larger that period then user period as interval
    Last message always sent the day before the event
    :param request: a request class
    :param template: a template class
    :param aged: a user class
    :return: a miniplan that is a list of messages class with all the fields completed
    '''
    print "Schedule Day"
    errors = {}

    if type(request.from_date) is not datetime:
        times = convertDatetime(request, template, resource)
        startime = times[0]
        endtime = times[1]
        period = times[2]
        expirationtime = times[3]
    else:
        startime = request.from_date
        endtime = request.to_date
        period = timedelta(days=template.period * 7)
        expirationtime = resource.to_date
        if expirationtime == None:
            expirationtime = endtime

    if template.nmsgmin != template.nmsgmax:
        nmsg = rnd.randrange(template.nmsgmin, template.nmsgmax + 1)
    else:
        nmsg = template.nmsgmax

    miniplan = [Message(count, aged.aged_id, intervention_session_id=1) for count in xrange(nmsg)]

    valid_interval = endtime - startime
    if valid_interval > period:
        valid_interval = period

    channels = getChannelsAvailable(template, aged)

    with open('csv/prova_import_messages.csv') as csvmessages:
        messages = csv.DictReader(csvmessages)
        msgs_tosend = getListMessages(messages, nmsg, resource, channels)

    er = checkForErrors(errors, endtime, expirationtime, startime, miniplan, nmsg, len(msgs_tosend))
    errors = er[0]
    miniplan = er[1]
    if er[2]:
        endtime = er[3]
    else:
        return errors, miniplan

    # length of the loop depending on the msgs found
    if len(msgs_tosend) < nmsg:
        lenloop = len(msgs_tosend)
    else:
        lenloop = nmsg

    valid_interval = timedelta(seconds=valid_interval.total_seconds())
    for i in range(0, lenloop):
        date = endtime - valid_interval

        miniplan[i].date = date.date()

        miniplan[i].time = scheduleHourFromDate(aged, date).time()

        miniplan[i].message_text = generate_message_text(aged, msgs_tosend[i]['Text'], msgs_tosend[i]['URL'])

        miniplan[i].attached_audio = msgs_tosend[i]['Audio']
        miniplan[i].attached_media = msgs_tosend[i]['Media']
        miniplan[i].URL = msgs_tosend[i]['URL']
        miniplan[i].channel = msgs_tosend[i]['Channel']

        valid_interval = timedelta(seconds=valid_interval.total_seconds() / (i + 2))

    miniplan = checkMsgsOneDay(miniplan, endtime)

    return errors, miniplan
示例#4
0
def schedulePPendulum(request, resource, template, aged):
    errors = {}

    u = generateXMLDoc(aged)
    r = generateXMLDoc(resource)
    c = None  # per ora non usato
    template.message_structure
    templateInfo = {}
    templateInfo['tags'] = template.message_structure
    #todo il tone e' da aggiungere come parametro alla classe template--> template.tone
    #templateInfo['tone'] = template.tone
    templateInfo['tone'] = "Neutral"


    if type(request.from_date) is not Pendulum:
        times = convertPendulum(request, template, resource)
        startime = times[0]
        endtime = times[1]
        period = times[2]
        expirationtime = times[3]
    else:
        startime = request.from_date
        endtime = request.to_date
        period = pendulum.Period(startime, startime.add(weeks=template.period))
        expirationtime = resource.to_date
        if expirationtime == None:
            expirationtime = endtime

    valid_interval = endtime - startime
    if valid_interval > period:
        valid_interval = period

    if template.nmsgmin != template.nmsgmax:
        nmsg = rnd.randrange(template.nmsgmin, template.nmsgmax + 1)
    else:
        nmsg = template.nmsgmax

    miniplan = [Message(count, aged.aged_id, intervention_session_id=1) for count in xrange(nmsg)]

    miniplanID = uuid.uuid4()

    channels = getChannelsAvailable(template, aged)

    with open('csv/prova_import_messages.csv') as csvmessages:
        messages = csv.DictReader(csvmessages)
        msgs_tosend = getListMessages(messages, nmsg, resource, channels)

    er = checkForErrors(errors, endtime, expirationtime, startime, miniplan, nmsg, len(msgs_tosend))
    errors = er[0]
    miniplan = er[1]
    if er[2]:
        endtime = er[3]
    else:
        return errors, miniplan

    day_of_event = mapDay(resource.on_day)
    if day_of_event == None:
        errors['ErrorNoDay'] = 'Error no day specified for periodic messages'
        miniplan = []
        return errors, miniplan

    # length of the loop depending on the msgs found
    if len(msgs_tosend) < nmsg:
        lenloop = len(msgs_tosend)
    else:
        lenloop = nmsg

    c = 0
    i = 0
    for dt in valid_interval.range("days"):
        if dt.day_of_week == day_of_event:
            if c % int(resource.every) == 0:
                if i > lenloop:
                    break
                miniplan[i].miniplan_id = miniplanID
                miniplan[i].date = dt.date()
                miniplan[i].time_1 = dt.date()
                miniplan[i].time_2 = dt.subtract(days=1).date()
                miniplan[i].time = scheduleHour(aged, None)
                miniplan[i].attached_audio = msgs_tosend[i]['Audio']
                miniplan[i].attached_media = msgs_tosend[i]['Media']
                miniplan[i].URL = msgs_tosend[i]['URL']

                miniplan[i].channel = msgs_tosend[i]['Channel']
                #miniplan[i].message_text = generate_message_text(aged, msgs_tosend[i]['Text'],msgs_tosend[i]['URL'])
                message_body = msgs_tosend[i]['Text']
                m = createMessageJson(message_body, templateInfo)
                miniplan[i].message_text = composeMessage(u, r, c, m)

                miniplan[i].intervention_session_id = request.intervention_session_id
                miniplan[i].pilot_id = request.pilot_id

                i += 1
            c += 1

    miniplan = checkMsgsOneDay(miniplan, endtime)

    return errors, miniplan