def launch_engine_one(json_req): # prendo json request # req = decodeRequest('{"resource_id": "In5","template_id":6,"user_id":7,"from_date":"22 Feb 2017","to_date":"10 Mar 2017"}') req = decodeRequest(json_req) ''' template=getTemplate(req.template_id) resource=getResource(req.resource_id) user=getUser(req.user_id) ''' # query al db con req.template_id template = Template(template_id=1, category="Edu", title="Titolo", description="Descrizione molto bella", nmsgmin=7, nmsgmax=7, period=15, channels=["SMS", "Messenger"]) # query al db con req.user_id user = Aged(aged_id=1, name="Anselmo", channels=["WhatsApp", "SMS", "Messenger"], hour_preference='0') with open('csv/prova_import_resources.csv') as csvmessages: resources = csv.DictReader(csvmessages) for r in resources: if r['R_ID'] == req.resource_id: resource = mapResource(r) break '''Compose miniplan ''' if resource.periodic == 'Yes': response = message_prescheduler.schedulePeriodic(req, resource, template, user) elif template.category == 'Eventi' or template.category == 'Opportunità': response = message_prescheduler.scheduleLogarithmic(req, resource, template, user) else: response = message_prescheduler.scheduleEquallyDividedPeriod(req, resource, template, user) ''' bozza api post data = {'generation_date': 'today', 'from_date': req.from_date, 'to_date': req.to_date, 'resource_id': req.resource_id, 'template_id': req.template_id, 'intervention_id': 'int_id', 'miniplan_body': 'json'} requests.post("http://.../endpoint/setNewMiniplanGenerated/", params=data) ''' '''Encode response: builds json ''' return encodeResponse(response[0], response[1])
def mapProfile(aged_dict): ''' Maps a aged dictionary to a aged class :param aged_dict: a aged dict :return: a aged class ''' aged = Aged(int(aged_dict['ID'])) aged.name = aged_dict['name'] aged.channels = aged_dict['channels'].split(', ') if aged_dict['hour_preference'] == 'None': aged.hour_preference = None else: aged.hour_preference = aged_dict['hour_preference'] return aged
def getAged(id_aged): ''' Makes a API call to get the details of a user having the id, fill a User class and returns it :param id_user: id of the user to retrieve :return: user class filled ''' json_aged = requests.get(getApipath() + 'getProfile/' + id_aged).json()[0] if 'Profile' in json_aged: json_aged = json_aged['Profile'] else: return None aged = Aged(id_aged) aged.name = json_aged['name'] aged.surname = json_aged['surname'] json_communicative = \ requests.get(getApipath() + 'getProfileCommunicativeDetails/' + id_aged).json()[0] if 'Profile' in json_communicative: json_communicative = json_communicative['Profile'] else: return None aged.channels = [] for c in json_communicative['available_channels'].split(', '): if c == 'FB': aged.channels.append('Facebook') else: aged.channels.append(c) aged.message_frequency = json_communicative['message_frequency'] if json_communicative['topics'] is not None: aged.topics = json_communicative['topics'].split(', ') aged.communication_style = json_communicative['communication_style'] json_tech = requests.get(getApipath() + 'getProfileTechnicalDetails/' + id_aged).json()[0] if 'Profile' in json_tech: json_tech = json_tech['Profile'] else: return None aged.address = json_tech['address'] aged.telephone_home_number = json_tech['telephone_home_number'] aged.mobile_phone_number = json_tech['mobile_phone_number'] aged.email = json_tech['email'] aged.facebook = json_tech['facebook_account'] aged.telegram = json_tech['telegram_account'] json_hourPref = requests.get(getApipath() + 'getProfileHourPreferences/' + id_aged).json()[0] if 'Preferences' in json_hourPref: json_hourPref = json_hourPref['Preferences'][0] else: return None if json_hourPref['hour_period_name']=='day': aged.hour_preference = None elif json_hourPref['hour_period_name']=='morning': aged.hour_preference = '0' elif json_hourPref['hour_period_name'] == 'afternoon': aged.hour_preference = '1' return aged