From 8e8fd0346c3fc8224afcf910a48b977ea4a23fc0 Mon Sep 17 00:00:00 2001 From: Abhishek Lodha Date: Wed, 27 Dec 2017 11:38:26 +0530 Subject: [PATCH 01/13] logs statements added to debug --- loop_ivr/helper_function.py | 10 +++------- loop_ivr/utils/config.py | 4 ++-- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/loop_ivr/helper_function.py b/loop_ivr/helper_function.py index 08c2305267..dd5b8b4937 100644 --- a/loop_ivr/helper_function.py +++ b/loop_ivr/helper_function.py @@ -197,6 +197,8 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, query = get_query.query_for_rates(crop_list , mandi_list, date_range=3) result = run_query(query) dataframe = remove_crop_outliers(ct_data = result) + logger.debug("DATAFRAME") + logger.debug(dataframe) if (not result) or dataframe == None or dataframe.empty: if not all_crop_flag and not all_mandi_flag: @@ -248,12 +250,6 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, min_price = round(PriceMin) - 1 max_price = round(PriceMax) + 1 temp_str = ('%s %s: %s %s-%s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(min_price),str(max_price)) - # if STD == 0: - # temp_str = ('%s %s: %s %s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(Av_Rate)) - # elif STD < 3: - # min_price = Av_Rate - STD - # max_price = Av_Rate + STD - # temp_str = ('%s %s: %s %s-%s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(min_price),str(max_price)) price_info_list.append(temp_str) # Save combination of crop and mandi for which data is not present in query on if query not for all mandi and crops. if not all_crop_flag and not all_mandi_flag: @@ -274,6 +270,7 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, temp_str = ('\n%s %s\n')%(mandi_name.rstrip(mandi_hi).rstrip(),mandi_hi) price_info_list.append(temp_str) price_info_list.append(agg_sms_no_price_for_combination) + price_info_list.append(('\n%s: %s')%(helpline_hi, EXOTEL_HELPLINE_NUMBER)) final_result = ''.join(price_info_list) price_info_incoming_obj.price_result = final_result @@ -292,7 +289,6 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, #send_sms(AGGREGATOR_SMS_NO, from_number, first_time_caller_message) send_info_using_textlocal(from_number, first_time_caller_message) else: - pass #send_sms(AGGREGATOR_SMS_NO, from_number, crop_code_list) send_info_using_textlocal(from_number, crop_code_list) # PriceInfoLog.objects.bulk_create(price_info_log_list) diff --git a/loop_ivr/utils/config.py b/loop_ivr/utils/config.py index 37dc56e55c..af3c4a0d18 100644 --- a/loop_ivr/utils/config.py +++ b/loop_ivr/utils/config.py @@ -35,9 +35,9 @@ MARKET_INFO_CALL_RESPONSE_URL = 'http://%s%s'%(CURRENT_DOMAIN, '/loopivr/market_info_response/') PUSH_MESSAGE_SMS_RESPONSE_URL = 'http://%s%s'%(CURRENT_DOMAIN, '/loopivr/push_message_sms_response/') # For production -MARKET_INFO_APP = '137265' +# MARKET_INFO_APP = '137265' # For Development -# MARKET_INFO_APP = '140902' +MARKET_INFO_APP = '140902' SMS_SENDER_NAME = 'LOOPDG' TEXT_LOCAL_SINGLE_SMS_API = 'https://api.textlocal.in/send/' From 87be11b795ef5f28c6300e193cce0512fb73e0b9 Mon Sep 17 00:00:00 2001 From: Abhishek Lodha Date: Wed, 27 Dec 2017 11:52:38 +0530 Subject: [PATCH 02/13] added log statements --- loop_ivr/views.py | 52 +++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/loop_ivr/views.py b/loop_ivr/views.py index e5709adbd8..f094c9b617 100644 --- a/loop_ivr/views.py +++ b/loop_ivr/views.py @@ -136,6 +136,7 @@ def crop_price_query(request): def handle_query_code(query_code, price_info_incoming_obj, farmer_number): + logger.debug("HANDLING QUERY CODE") if query_code == '' or query_code == 'None': if price_info_incoming_obj.call_source == 3: sms_content = [no_code_entered,'\n\n'] @@ -161,33 +162,36 @@ def handle_query_code(query_code, price_info_incoming_obj, farmer_number): price_info_incoming_obj.save() return HttpResponse(status=404) else : - # send corresponding response - query_code = query_code.split('**') - all_crop_flag = False - all_mandi_flag = False + try: + # send corresponding response + query_code = query_code.split('**') + all_crop_flag = False + all_mandi_flag = False - if len(query_code) >= 2: - crop_info, mandi_info = query_code[0], query_code[1] - elif len(query_code) == 1: - crop_info = query_code[0] - mandi_info = '' - if re.search(CONTAINS_ZERO,crop_info) is not None: - all_crop_flag=True - if re.search(CONTAINS_ZERO,mandi_info) is not None or mandi_info == '': - all_mandi_flag=True + if len(query_code) >= 2: + crop_info, mandi_info = query_code[0], query_code[1] + elif len(query_code) == 1: + crop_info = query_code[0] + mandi_info = '' + if re.search(CONTAINS_ZERO,crop_info) is not None: + all_crop_flag = True + if re.search(CONTAINS_ZERO,mandi_info) is not None or mandi_info == '': + all_mandi_flag = True - crop_list = get_valid_list('loop', 'crop', crop_info, farmer_number, all_crop_flag) - mandi_list = get_valid_list('loop', 'mandi', mandi_info, farmer_number, all_mandi_flag) - if len(crop_list) == 0: - if price_info_incoming_obj.call_source == 3: - send_wrong_query_sms_content(price_info_incoming_obj, farmer_number) + crop_list = get_valid_list('loop', 'crop', crop_info, farmer_number, all_crop_flag) + mandi_list = get_valid_list('loop', 'mandi', mandi_info, farmer_number, all_mandi_flag) + if len(crop_list) == 0: + if price_info_incoming_obj.call_source == 3: + send_wrong_query_sms_content(price_info_incoming_obj, farmer_number, query_code) + else: + price_info_incoming_obj.info_status = 2 + price_info_incoming_obj.save() + return HttpResponse(status=404) else: - price_info_incoming_obj.info_status = 2 - price_info_incoming_obj.save() - return HttpResponse(status=404) - else: - Thread(target=get_price_info, args=[farmer_number, crop_list, mandi_list, price_info_incoming_obj, all_crop_flag, all_mandi_flag]).start() - return HttpResponse(status=200) + Thread(target=get_price_info, args=[farmer_number, crop_list, mandi_list, price_info_incoming_obj, all_crop_flag, all_mandi_flag]).start() + return HttpResponse(status=200) + except Exception as e: + logger.debug(e) return HttpResponse(status=403) @csrf_exempt From 1cb91f94458d68adc18e00781da9cd06a1292c91 Mon Sep 17 00:00:00 2001 From: Abhishek Lodha Date: Wed, 27 Dec 2017 11:55:07 +0530 Subject: [PATCH 03/13] indentation removed --- loop_ivr/views.py | 107 +++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 54 deletions(-) diff --git a/loop_ivr/views.py b/loop_ivr/views.py index f094c9b617..24a7333f57 100644 --- a/loop_ivr/views.py +++ b/loop_ivr/views.py @@ -136,63 +136,62 @@ def crop_price_query(request): def handle_query_code(query_code, price_info_incoming_obj, farmer_number): - logger.debug("HANDLING QUERY CODE") - if query_code == '' or query_code == 'None': - if price_info_incoming_obj.call_source == 3: - sms_content = [no_code_entered,'\n\n'] - send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) - else: - price_info_incoming_obj.info_status = 3 - price_info_incoming_obj.save() - return HttpResponse(status=404) - elif query_code == '0': - if price_info_incoming_obj.call_source == 3: - sms_content = [] - send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) - else: - price_info_incoming_obj.info_status = 3 - price_info_incoming_obj.save() - return HttpResponse(status=404) - elif re.search(PATTERN_REGEX, query_code) is None: - # send wrong query code - if price_info_incoming_obj.call_source == 3: - send_wrong_query_sms_content(price_info_incoming_obj, farmer_number) - else: - price_info_incoming_obj.info_status = 2 - price_info_incoming_obj.save() - return HttpResponse(status=404) - else : - try: - # send corresponding response - query_code = query_code.split('**') - all_crop_flag = False - all_mandi_flag = False + if query_code == '' or query_code == 'None': + if price_info_incoming_obj.call_source == 3: + sms_content = [no_code_entered,'\n\n'] + send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) + else: + price_info_incoming_obj.info_status = 3 + price_info_incoming_obj.save() + return HttpResponse(status=404) + elif query_code == '0': + if price_info_incoming_obj.call_source == 3: + sms_content = [] + send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) + else: + price_info_incoming_obj.info_status = 3 + price_info_incoming_obj.save() + return HttpResponse(status=404) + elif re.search(PATTERN_REGEX, query_code) is None: + # send wrong query code + if price_info_incoming_obj.call_source == 3: + send_wrong_query_sms_content(price_info_incoming_obj, farmer_number) + else: + price_info_incoming_obj.info_status = 2 + price_info_incoming_obj.save() + return HttpResponse(status=404) + else : + try: + # send corresponding response + query_code = query_code.split('**') + all_crop_flag = False + all_mandi_flag = False - if len(query_code) >= 2: - crop_info, mandi_info = query_code[0], query_code[1] - elif len(query_code) == 1: - crop_info = query_code[0] - mandi_info = '' - if re.search(CONTAINS_ZERO,crop_info) is not None: - all_crop_flag = True - if re.search(CONTAINS_ZERO,mandi_info) is not None or mandi_info == '': - all_mandi_flag = True + if len(query_code) >= 2: + crop_info, mandi_info = query_code[0], query_code[1] + elif len(query_code) == 1: + crop_info = query_code[0] + mandi_info = '' + if re.search(CONTAINS_ZERO,crop_info) is not None: + all_crop_flag = True + if re.search(CONTAINS_ZERO,mandi_info) is not None or mandi_info == '': + all_mandi_flag = True - crop_list = get_valid_list('loop', 'crop', crop_info, farmer_number, all_crop_flag) - mandi_list = get_valid_list('loop', 'mandi', mandi_info, farmer_number, all_mandi_flag) - if len(crop_list) == 0: - if price_info_incoming_obj.call_source == 3: - send_wrong_query_sms_content(price_info_incoming_obj, farmer_number, query_code) - else: - price_info_incoming_obj.info_status = 2 - price_info_incoming_obj.save() - return HttpResponse(status=404) + crop_list = get_valid_list('loop', 'crop', crop_info, farmer_number, all_crop_flag) + mandi_list = get_valid_list('loop', 'mandi', mandi_info, farmer_number, all_mandi_flag) + if len(crop_list) == 0: + if price_info_incoming_obj.call_source == 3: + send_wrong_query_sms_content(price_info_incoming_obj, farmer_number, query_code) else: - Thread(target=get_price_info, args=[farmer_number, crop_list, mandi_list, price_info_incoming_obj, all_crop_flag, all_mandi_flag]).start() - return HttpResponse(status=200) - except Exception as e: - logger.debug(e) - return HttpResponse(status=403) + price_info_incoming_obj.info_status = 2 + price_info_incoming_obj.save() + return HttpResponse(status=404) + else: + Thread(target=get_price_info, args=[farmer_number, crop_list, mandi_list, price_info_incoming_obj, all_crop_flag, all_mandi_flag]).start() + return HttpResponse(status=200) + except Exception as e: + logger.debug(e) + return HttpResponse(status=403) @csrf_exempt def market_info_response(request): From 5c76e1be8297b2830efa0894083d9558fb1a0312 Mon Sep 17 00:00:00 2001 From: Abhishek Lodha Date: Wed, 27 Dec 2017 12:00:13 +0530 Subject: [PATCH 04/13] log statements added --- loop_ivr/helper_function.py | 5 ++--- loop_ivr/outliers/removal.py | 3 --- loop_ivr/views.py | 1 + 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/loop_ivr/helper_function.py b/loop_ivr/helper_function.py index dd5b8b4937..781e2a361b 100644 --- a/loop_ivr/helper_function.py +++ b/loop_ivr/helper_function.py @@ -190,11 +190,10 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, price_info_list.append(agg_sms_initial_line) price_info_list.append(AGGREGATOR_SMS_NO) price_info_list.append('\n') - # today_date = datetime.now() - # raw_query = raw_sql.last_three_trans.format('(%s)'%(crop_list[0],) if len(crop_list) == 1 else crop_list, '(%s)'%(mandi_list[0],) if len(mandi_list) == 1 else mandi_list, tuple((today_date-timedelta(days=day)).strftime('%Y-%m-%d') for day in range(0,3))) - # query_result = run_query(raw_query) query = get_query.query_for_rates(crop_list , mandi_list, date_range=3) + logger.debug("QUERY") + logger.debug(query) result = run_query(query) dataframe = remove_crop_outliers(ct_data = result) logger.debug("DATAFRAME") diff --git a/loop_ivr/outliers/removal.py b/loop_ivr/outliers/removal.py index 602b5cdf29..64464d2339 100644 --- a/loop_ivr/outliers/removal.py +++ b/loop_ivr/outliers/removal.py @@ -15,8 +15,6 @@ def remove_crop_outliers(ct_data=None): combined_transactions_data = call_methods(combined_transactions_data) - # combined_transactions_data.to_csv("final_data_after_outliers.csv") - for recursion_counter in range(0,3): combined_transactions_data.fillna(0,inplace=True) ct_data = combined_transactions_data[(combined_transactions_data['D/STD'] > 1.3)] @@ -55,7 +53,6 @@ def call_methods(combined_transactions_data): def get_statistics(combined_transactions_data): - ct_data_with_mean = combined_transactions_data.groupby(group_by_list).apply(compute_mean).reset_index(name='Av_Rate') combined_transactions_data = combined_transactions_data.merge(ct_data_with_mean,how='left',on=group_by_list) diff --git a/loop_ivr/views.py b/loop_ivr/views.py index 24a7333f57..d52443b310 100644 --- a/loop_ivr/views.py +++ b/loop_ivr/views.py @@ -136,6 +136,7 @@ def crop_price_query(request): def handle_query_code(query_code, price_info_incoming_obj, farmer_number): + logger.debug("HANDLING QUERY CODE") if query_code == '' or query_code == 'None': if price_info_incoming_obj.call_source == 3: sms_content = [no_code_entered,'\n\n'] From 7bff0225f4bf37faad04481d1be6e69546b1a4c7 Mon Sep 17 00:00:00 2001 From: Abhishek Lodha Date: Wed, 27 Dec 2017 12:05:58 +0530 Subject: [PATCH 05/13] exception checking --- loop_ivr/views.py | 56 +++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/loop_ivr/views.py b/loop_ivr/views.py index d52443b310..81588a1057 100644 --- a/loop_ivr/views.py +++ b/loop_ivr/views.py @@ -137,33 +137,33 @@ def crop_price_query(request): def handle_query_code(query_code, price_info_incoming_obj, farmer_number): logger.debug("HANDLING QUERY CODE") - if query_code == '' or query_code == 'None': - if price_info_incoming_obj.call_source == 3: - sms_content = [no_code_entered,'\n\n'] - send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) - else: - price_info_incoming_obj.info_status = 3 - price_info_incoming_obj.save() - return HttpResponse(status=404) - elif query_code == '0': - if price_info_incoming_obj.call_source == 3: - sms_content = [] - send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) - else: - price_info_incoming_obj.info_status = 3 - price_info_incoming_obj.save() - return HttpResponse(status=404) - elif re.search(PATTERN_REGEX, query_code) is None: - # send wrong query code - if price_info_incoming_obj.call_source == 3: - send_wrong_query_sms_content(price_info_incoming_obj, farmer_number) + try: + if query_code == '' or query_code == 'None': + if price_info_incoming_obj.call_source == 3: + sms_content = [no_code_entered,'\n\n'] + send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) + else: + price_info_incoming_obj.info_status = 3 + price_info_incoming_obj.save() + return HttpResponse(status=404) + elif query_code == '0': + if price_info_incoming_obj.call_source == 3: + sms_content = [] + send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) + else: + price_info_incoming_obj.info_status = 3 + price_info_incoming_obj.save() + return HttpResponse(status=404) + elif re.search(PATTERN_REGEX, query_code) is None: + # send wrong query code + if price_info_incoming_obj.call_source == 3: + send_wrong_query_sms_content(price_info_incoming_obj, farmer_number) + else: + price_info_incoming_obj.info_status = 2 + price_info_incoming_obj.save() + return HttpResponse(status=404) else: - price_info_incoming_obj.info_status = 2 - price_info_incoming_obj.save() - return HttpResponse(status=404) - else : - try: - # send corresponding response + # send corresponding response query_code = query_code.split('**') all_crop_flag = False all_mandi_flag = False @@ -190,8 +190,8 @@ def handle_query_code(query_code, price_info_incoming_obj, farmer_number): else: Thread(target=get_price_info, args=[farmer_number, crop_list, mandi_list, price_info_incoming_obj, all_crop_flag, all_mandi_flag]).start() return HttpResponse(status=200) - except Exception as e: - logger.debug(e) + except Exception as e: + logger.debug(e) return HttpResponse(status=403) @csrf_exempt From 2ccc6b6e1183baf92c6e8fecebd433419cda3199 Mon Sep 17 00:00:00 2001 From: Abhishek Lodha Date: Wed, 27 Dec 2017 12:16:36 +0530 Subject: [PATCH 06/13] exception handling --- loop_ivr/helper_function.py | 41 +++++----- loop_ivr/views.py | 155 +++++++++++------------------------- 2 files changed, 68 insertions(+), 128 deletions(-) diff --git a/loop_ivr/helper_function.py b/loop_ivr/helper_function.py index 781e2a361b..f1d546cf59 100644 --- a/loop_ivr/helper_function.py +++ b/loop_ivr/helper_function.py @@ -307,24 +307,27 @@ def send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_numb def send_wrong_query_sms_content(price_info_incoming_obj, farmer_number, query_code = None) : # Send Wrong code entered message to user. - if query_code == None: - try: - wrong_query_code = str(price_info_incoming_obj.query_code) if price_info_incoming_obj.query_code else '' - except Exception as e: - wrong_query_code = '' - else: - wrong_query_code = query_code - wrong_code_entered_message = wrong_code_entered - if wrong_query_code == '': - wrong_code_entered_message = wrong_code_entered_message%(wrong_query_code,) - else: - wrong_code_entered_message = wrong_code_entered_message%((' (%s:%s)')%(code_hi,wrong_query_code),) - crop_code_list = get_crop_code_list(N_TOP_SELLING_CROP, TOP_SELLING_CROP_WINDOW) - sms_content = [wrong_code_entered_message,'\n\n', crop_code_list, '\n\n', ('%s\n%s')%(remaining_crop_line, EXOTEL_HELPLINE_NUMBER)] - sms_content = ''.join(sms_content) + try: + if query_code == None: + try: + wrong_query_code = str(price_info_incoming_obj.query_code) if price_info_incoming_obj.query_code else '' + except Exception as e: + wrong_query_code = '' + else: + wrong_query_code = query_code + wrong_code_entered_message = wrong_code_entered + if wrong_query_code == '': + wrong_code_entered_message = wrong_code_entered_message%(wrong_query_code,) + else: + wrong_code_entered_message = wrong_code_entered_message%((' (%s:%s)')%(code_hi,wrong_query_code),) + crop_code_list = get_crop_code_list(N_TOP_SELLING_CROP, TOP_SELLING_CROP_WINDOW) + sms_content = [wrong_code_entered_message,'\n\n', crop_code_list, '\n\n', ('%s\n%s')%(remaining_crop_line, EXOTEL_HELPLINE_NUMBER)] + sms_content = ''.join(sms_content) - price_info_incoming_obj.price_result = sms_content - price_info_incoming_obj.info_status = 2 - price_info_incoming_obj.save() + price_info_incoming_obj.price_result = sms_content + price_info_incoming_obj.info_status = 2 + price_info_incoming_obj.save() - send_info_using_textlocal(farmer_number, sms_content, price_info_incoming_obj) + send_info_using_textlocal(farmer_number, sms_content, price_info_incoming_obj) + except Exception as e: + logger.debug(e) diff --git a/loop_ivr/views.py b/loop_ivr/views.py index 81588a1057..cb1a3d3057 100644 --- a/loop_ivr/views.py +++ b/loop_ivr/views.py @@ -136,62 +136,58 @@ def crop_price_query(request): def handle_query_code(query_code, price_info_incoming_obj, farmer_number): - logger.debug("HANDLING QUERY CODE") - try: - if query_code == '' or query_code == 'None': - if price_info_incoming_obj.call_source == 3: - sms_content = [no_code_entered,'\n\n'] - send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) - else: - price_info_incoming_obj.info_status = 3 - price_info_incoming_obj.save() - return HttpResponse(status=404) - elif query_code == '0': - if price_info_incoming_obj.call_source == 3: - sms_content = [] - send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) - else: - price_info_incoming_obj.info_status = 3 - price_info_incoming_obj.save() - return HttpResponse(status=404) - elif re.search(PATTERN_REGEX, query_code) is None: - # send wrong query code + if query_code == '' or query_code == 'None': + if price_info_incoming_obj.call_source == 3: + sms_content = [no_code_entered,'\n\n'] + send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) + else: + price_info_incoming_obj.info_status = 3 + price_info_incoming_obj.save() + return HttpResponse(status=404) + elif query_code == '0': + if price_info_incoming_obj.call_source == 3: + sms_content = [] + send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) + else: + price_info_incoming_obj.info_status = 3 + price_info_incoming_obj.save() + return HttpResponse(status=404) + elif re.search(PATTERN_REGEX, query_code) is None: + # send wrong query code + if price_info_incoming_obj.call_source == 3: + send_wrong_query_sms_content(price_info_incoming_obj, farmer_number) + else: + price_info_incoming_obj.info_status = 2 + price_info_incoming_obj.save() + return HttpResponse(status=404) + else : + # send corresponding response + query_code = query_code.split('**') + all_crop_flag = False + all_mandi_flag = False + + if len(query_code) >= 2: + crop_info, mandi_info = query_code[0], query_code[1] + elif len(query_code) == 1: + crop_info = query_code[0] + mandi_info = '' + if re.search(CONTAINS_ZERO,crop_info) is not None: + all_crop_flag = True + if re.search(CONTAINS_ZERO,mandi_info) is not None or mandi_info == '': + all_mandi_flag = True + + crop_list = get_valid_list('loop', 'crop', crop_info, farmer_number, all_crop_flag) + mandi_list = get_valid_list('loop', 'mandi', mandi_info, farmer_number, all_mandi_flag) + if len(crop_list) == 0: if price_info_incoming_obj.call_source == 3: - send_wrong_query_sms_content(price_info_incoming_obj, farmer_number) + send_wrong_query_sms_content(price_info_incoming_obj, farmer_number, query_code) else: price_info_incoming_obj.info_status = 2 price_info_incoming_obj.save() return HttpResponse(status=404) else: - # send corresponding response - query_code = query_code.split('**') - all_crop_flag = False - all_mandi_flag = False - - if len(query_code) >= 2: - crop_info, mandi_info = query_code[0], query_code[1] - elif len(query_code) == 1: - crop_info = query_code[0] - mandi_info = '' - if re.search(CONTAINS_ZERO,crop_info) is not None: - all_crop_flag = True - if re.search(CONTAINS_ZERO,mandi_info) is not None or mandi_info == '': - all_mandi_flag = True - - crop_list = get_valid_list('loop', 'crop', crop_info, farmer_number, all_crop_flag) - mandi_list = get_valid_list('loop', 'mandi', mandi_info, farmer_number, all_mandi_flag) - if len(crop_list) == 0: - if price_info_incoming_obj.call_source == 3: - send_wrong_query_sms_content(price_info_incoming_obj, farmer_number, query_code) - else: - price_info_incoming_obj.info_status = 2 - price_info_incoming_obj.save() - return HttpResponse(status=404) - else: - Thread(target=get_price_info, args=[farmer_number, crop_list, mandi_list, price_info_incoming_obj, all_crop_flag, all_mandi_flag]).start() - return HttpResponse(status=200) - except Exception as e: - logger.debug(e) + Thread(target=get_price_info, args=[farmer_number, crop_list, mandi_list, price_info_incoming_obj, all_crop_flag, all_mandi_flag]).start() + return HttpResponse(status=200) return HttpResponse(status=403) @csrf_exempt @@ -231,65 +227,6 @@ def market_info_response(request): price_info_incoming_obj.save() return HttpResponse(status=200) -# def crop_price_sms_content(request): -# if request.method == 'HEAD': -# return HttpResponse(status=200, content_type='text/plain') -# if request.method == 'GET': -# call_id = str(request.GET.getlist('CallSid')[0]) -# farmer_number = str(request.GET.getlist('From')[0]) -# dg_number = str(request.GET.getlist('To')[0]) -# try: -# price_info_obj = PriceInfoIncoming.objects.get(call_id=call_id, from_number=farmer_number, -# to_number=dg_number) -# if price_info_obj.return_result_to_app == 1: -# sms_content = price_info_obj.price_result -# price_info_obj.info_status = 1 -# price_info_obj.save() -# response = HttpResponse(sms_content, content_type='text/plain') -# else: -# response = HttpResponse(status=200, content_type='text/plain') -# except Exception as e: -# logger.debug(e) -# response = HttpResponse(status=200, content_type='text/plain') -# return response -# return HttpResponse(status=403) -# -# def no_code_message(request): -# if request.method == 'HEAD': -# return HttpResponse(status=200, content_type='text/plain') -# if request.method == 'GET': -# crop_code_list = get_crop_code_list(N_TOP_SELLING_CROP, TOP_SELLING_CROP_WINDOW) -# sms_content = [no_code_entered,'\n\n', crop_code_list, '\n\n', ('%s\n%s')%(remaining_crop_line, EXOTEL_HELPLINE_NUMBER)] -# sms_content = ''.join(sms_content) -# response = HttpResponse(sms_content, content_type='text/plain') -# return response -# return HttpResponse(status=403) -# -# def wrong_code_message(request): -# if request.method == 'HEAD': -# return HttpResponse(status=200, content_type='text/plain') -# if request.method == 'GET': -# crop_code_list = get_crop_code_list(N_TOP_SELLING_CROP, TOP_SELLING_CROP_WINDOW) -# call_id = str(request.GET.getlist('CallSid')[0]) -# farmer_number = str(request.GET.getlist('From')[0]) -# dg_number = str(request.GET.getlist('To')[0]) -# try: -# price_info_obj = PriceInfoIncoming.objects.get(call_id=call_id, from_number=farmer_number, -# to_number=dg_number) -# wrong_query_code = str(price_info_obj.query_code) if price_info_obj.query_code else '' -# except Exception as e: -# wrong_query_code = '' -# wrong_code_entered_message = wrong_code_entered -# if wrong_query_code == '': -# wrong_code_entered_message = wrong_code_entered_message%(wrong_query_code,) -# else: -# wrong_code_entered_message = wrong_code_entered_message%((' (%s:%s)')%(code_hi,wrong_query_code),) -# sms_content = [wrong_code_entered_message,'\n\n', crop_code_list, '\n\n', ('%s\n%s')%(remaining_crop_line, EXOTEL_HELPLINE_NUMBER)] -# sms_content = ''.join(sms_content) -# response = HttpResponse(sms_content, content_type='text/plain') -# return response -# return HttpResponse(status=403) - @csrf_exempt def push_message_sms_response(request): if request.method == 'POST': From bda3400e5d6f2d60c10a9d5174e454310ab54204 Mon Sep 17 00:00:00 2001 From: Abhishek Lodha Date: Wed, 27 Dec 2017 12:28:00 +0530 Subject: [PATCH 07/13] removed dataframe none check --- loop_ivr/helper_function.py | 44 +++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/loop_ivr/helper_function.py b/loop_ivr/helper_function.py index f1d546cf59..e36b9b3914 100644 --- a/loop_ivr/helper_function.py +++ b/loop_ivr/helper_function.py @@ -25,7 +25,6 @@ from loop_ivr.outliers.removal import remove_crop_outliers - def make_market_info_call(caller_number, dg_number, incoming_time, incoming_call_id, call_source): app_request_url = APP_REQUEST_URL%(EXOTEL_ID,EXOTEL_TOKEN,EXOTEL_ID) app_id = MARKET_INFO_APP @@ -199,7 +198,7 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, logger.debug("DATAFRAME") logger.debug(dataframe) - if (not result) or dataframe == None or dataframe.empty: + if (not result) or dataframe.empty: if not all_crop_flag and not all_mandi_flag: crop_name_list = ','.join(map(lambda crop_id: '%s (%s: %s)'%(crop_in_hindi_map.get(crop_id).encode("utf-8"),code_hi,str(crop_id)) if crop_in_hindi_map.get(crop_id) else '%s (%s: %s)'%(crop_map[crop_id].encode("utf-8"),code_hi,str(crop_id)), crop_list)) mandi_name_list = ','.join(map(lambda mandi_id: mandi_map[mandi_id].encode("utf-8").rstrip(mandi_hi).rstrip(), mandi_list)) @@ -307,27 +306,24 @@ def send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_numb def send_wrong_query_sms_content(price_info_incoming_obj, farmer_number, query_code = None) : # Send Wrong code entered message to user. - try: - if query_code == None: - try: - wrong_query_code = str(price_info_incoming_obj.query_code) if price_info_incoming_obj.query_code else '' - except Exception as e: - wrong_query_code = '' - else: - wrong_query_code = query_code - wrong_code_entered_message = wrong_code_entered - if wrong_query_code == '': - wrong_code_entered_message = wrong_code_entered_message%(wrong_query_code,) - else: - wrong_code_entered_message = wrong_code_entered_message%((' (%s:%s)')%(code_hi,wrong_query_code),) - crop_code_list = get_crop_code_list(N_TOP_SELLING_CROP, TOP_SELLING_CROP_WINDOW) - sms_content = [wrong_code_entered_message,'\n\n', crop_code_list, '\n\n', ('%s\n%s')%(remaining_crop_line, EXOTEL_HELPLINE_NUMBER)] - sms_content = ''.join(sms_content) + if query_code == None: + try: + wrong_query_code = str(price_info_incoming_obj.query_code) if price_info_incoming_obj.query_code else '' + except Exception as e: + wrong_query_code = '' + else: + wrong_query_code = query_code + wrong_code_entered_message = wrong_code_entered + if wrong_query_code == '': + wrong_code_entered_message = wrong_code_entered_message%(wrong_query_code,) + else: + wrong_code_entered_message = wrong_code_entered_message%((' (%s:%s)')%(code_hi,wrong_query_code),) + crop_code_list = get_crop_code_list(N_TOP_SELLING_CROP, TOP_SELLING_CROP_WINDOW) + sms_content = [wrong_code_entered_message,'\n\n', crop_code_list, '\n\n', ('%s\n%s')%(remaining_crop_line, EXOTEL_HELPLINE_NUMBER)] + sms_content = ''.join(sms_content) - price_info_incoming_obj.price_result = sms_content - price_info_incoming_obj.info_status = 2 - price_info_incoming_obj.save() + price_info_incoming_obj.price_result = sms_content + price_info_incoming_obj.info_status = 2 + price_info_incoming_obj.save() - send_info_using_textlocal(farmer_number, sms_content, price_info_incoming_obj) - except Exception as e: - logger.debug(e) + send_info_using_textlocal(farmer_number, sms_content, price_info_incoming_obj) From caba021b13db7667583b6cecfa6c34bb45f79247 Mon Sep 17 00:00:00 2001 From: Abhishek Lodha Date: Wed, 27 Dec 2017 12:32:39 +0530 Subject: [PATCH 08/13] condition added --- loop_ivr/helper_function.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/loop_ivr/helper_function.py b/loop_ivr/helper_function.py index e36b9b3914..c031301150 100644 --- a/loop_ivr/helper_function.py +++ b/loop_ivr/helper_function.py @@ -191,14 +191,10 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, price_info_list.append('\n') query = get_query.query_for_rates(crop_list , mandi_list, date_range=3) - logger.debug("QUERY") - logger.debug(query) result = run_query(query) dataframe = remove_crop_outliers(ct_data = result) - logger.debug("DATAFRAME") - logger.debug(dataframe) - if (not result) or dataframe.empty: + if (not result) or dataframe.empty or dataframe == None: if not all_crop_flag and not all_mandi_flag: crop_name_list = ','.join(map(lambda crop_id: '%s (%s: %s)'%(crop_in_hindi_map.get(crop_id).encode("utf-8"),code_hi,str(crop_id)) if crop_in_hindi_map.get(crop_id) else '%s (%s: %s)'%(crop_map[crop_id].encode("utf-8"),code_hi,str(crop_id)), crop_list)) mandi_name_list = ','.join(map(lambda mandi_id: mandi_map[mandi_id].encode("utf-8").rstrip(mandi_hi).rstrip(), mandi_list)) From 64bc07cb8e00271e691be530f5d9275ede050f86 Mon Sep 17 00:00:00 2001 From: Abhishek Lodha Date: Wed, 27 Dec 2017 12:42:44 +0530 Subject: [PATCH 09/13] logging --- loop_ivr/helper_function.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/loop_ivr/helper_function.py b/loop_ivr/helper_function.py index c031301150..c8193aecc2 100644 --- a/loop_ivr/helper_function.py +++ b/loop_ivr/helper_function.py @@ -24,6 +24,8 @@ from loop_ivr.models import PriceInfoLog, PriceInfoIncoming from loop_ivr.outliers.removal import remove_crop_outliers +import logging +logger = logging.getLogger(__name__) def make_market_info_call(caller_number, dg_number, incoming_time, incoming_call_id, call_source): app_request_url = APP_REQUEST_URL%(EXOTEL_ID,EXOTEL_TOKEN,EXOTEL_ID) @@ -190,7 +192,12 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, price_info_list.append(AGGREGATOR_SMS_NO) price_info_list.append('\n') + logger.debug(price_info_list) + # print price_info_list + query = get_query.query_for_rates(crop_list , mandi_list, date_range=3) + logger.debug("-------------------Query--------------") + logger.debug(query) result = run_query(query) dataframe = remove_crop_outliers(ct_data = result) From 33640892dab7dbcb5e5dc194bac432885877b9a1 Mon Sep 17 00:00:00 2001 From: Abhishek Lodha Date: Wed, 27 Dec 2017 12:46:29 +0530 Subject: [PATCH 10/13] removed price info log list creation --- loop_ivr/helper_function.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/loop_ivr/helper_function.py b/loop_ivr/helper_function.py index c8193aecc2..1c3e1022eb 100644 --- a/loop_ivr/helper_function.py +++ b/loop_ivr/helper_function.py @@ -173,7 +173,7 @@ def get_crop_code_list(number_of_crop, selling_crop_window): def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, all_crop_flag, all_mandi_flag): price_info_list = [] - price_info_log_list = [] + # price_info_log_list = [] crop_mandi_comb = [] crop_map = dict() mandi_map = dict() @@ -192,14 +192,13 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, price_info_list.append(AGGREGATOR_SMS_NO) price_info_list.append('\n') - logger.debug(price_info_list) - # print price_info_list - query = get_query.query_for_rates(crop_list , mandi_list, date_range=3) logger.debug("-------------------Query--------------") logger.debug(query) result = run_query(query) dataframe = remove_crop_outliers(ct_data = result) + logger.debug(dataframe) + if (not result) or dataframe.empty or dataframe == None: if not all_crop_flag and not all_mandi_flag: @@ -225,9 +224,9 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, if crop != prev_crop or mandi != prev_mandi: if not all_crop_flag and not all_mandi_flag: crop_mandi_comb.append((crop,mandi)) - price_info_log_obj = PriceInfoLog(price_info_incoming=price_info_incoming_obj, - crop_id=crop, mandi_id=mandi) - price_info_log_list.append(price_info_log_obj) + # price_info_log_obj = PriceInfoLog(price_info_incoming=price_info_incoming_obj, + # crop_id=crop, mandi_id=mandi) + # price_info_log_list.append(price_info_log_obj) crop_name = crop_in_hindi_map.get(crop).encode("utf-8") if crop_in_hindi_map.get(crop) else crop_map[crop].encode("utf-8") mandi_name = mandi_map[mandi].encode("utf-8") if crop != prev_crop: @@ -259,7 +258,7 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, if (crop,mandi) not in crop_mandi_comb: price_info_log_obj = PriceInfoLog(price_info_incoming=price_info_incoming_obj, crop_id=crop, mandi_id=mandi) - price_info_log_list.append(price_info_log_obj) + # price_info_log_list.append(price_info_log_obj) if result and (not dataframe.empty): crop_name = crop_in_hindi_map.get(crop).encode("utf-8") if crop_in_hindi_map.get(crop) else crop_map[crop].encode("utf-8") mandi_name = mandi_map[mandi].encode("utf-8") From f4568d726b5b10c4d67b1643d4d5a5b177090934 Mon Sep 17 00:00:00 2001 From: Abhishek Lodha Date: Wed, 27 Dec 2017 12:54:05 +0530 Subject: [PATCH 11/13] log added --- loop_ivr/helper_function.py | 145 ++++++++++++++++++------------------ 1 file changed, 73 insertions(+), 72 deletions(-) diff --git a/loop_ivr/helper_function.py b/loop_ivr/helper_function.py index 1c3e1022eb..29b823352f 100644 --- a/loop_ivr/helper_function.py +++ b/loop_ivr/helper_function.py @@ -194,91 +194,92 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, query = get_query.query_for_rates(crop_list , mandi_list, date_range=3) logger.debug("-------------------Query--------------") - logger.debug(query) result = run_query(query) dataframe = remove_crop_outliers(ct_data = result) logger.debug(dataframe) - - if (not result) or dataframe.empty or dataframe == None: - if not all_crop_flag and not all_mandi_flag: - crop_name_list = ','.join(map(lambda crop_id: '%s (%s: %s)'%(crop_in_hindi_map.get(crop_id).encode("utf-8"),code_hi,str(crop_id)) if crop_in_hindi_map.get(crop_id) else '%s (%s: %s)'%(crop_map[crop_id].encode("utf-8"),code_hi,str(crop_id)), crop_list)) - mandi_name_list = ','.join(map(lambda mandi_id: mandi_map[mandi_id].encode("utf-8").rstrip(mandi_hi).rstrip(), mandi_list)) - no_price_message = (agg_sms_no_price_crop_mandi)%(crop_name_list, mandi_name_list) - # If query for all Mandi - elif all_mandi_flag: - crop_name_list = ','.join(map(lambda crop_id: '%s (%s: %s)'%(crop_in_hindi_map.get(crop_id).encode("utf-8"),code_hi,str(crop_id)) if crop_in_hindi_map.get(crop_id) else '%s (%s: %s)'%(crop_map[crop_id].encode("utf-8"),code_hi,str(crop_id)), crop_list)) - no_price_message = (agg_sms_no_price_all_mandi)%(crop_name_list,) - # If query for all crops + try: + if (not result) or dataframe.empty or dataframe == None: + if not all_crop_flag and not all_mandi_flag: + crop_name_list = ','.join(map(lambda crop_id: '%s (%s: %s)'%(crop_in_hindi_map.get(crop_id).encode("utf-8"),code_hi,str(crop_id)) if crop_in_hindi_map.get(crop_id) else '%s (%s: %s)'%(crop_map[crop_id].encode("utf-8"),code_hi,str(crop_id)), crop_list)) + mandi_name_list = ','.join(map(lambda mandi_id: mandi_map[mandi_id].encode("utf-8").rstrip(mandi_hi).rstrip(), mandi_list)) + no_price_message = (agg_sms_no_price_crop_mandi)%(crop_name_list, mandi_name_list) + # If query for all Mandi + elif all_mandi_flag: + crop_name_list = ','.join(map(lambda crop_id: '%s (%s: %s)'%(crop_in_hindi_map.get(crop_id).encode("utf-8"),code_hi,str(crop_id)) if crop_in_hindi_map.get(crop_id) else '%s (%s: %s)'%(crop_map[crop_id].encode("utf-8"),code_hi,str(crop_id)), crop_list)) + no_price_message = (agg_sms_no_price_all_mandi)%(crop_name_list,) + # If query for all crops + else: + no_price_message = agg_sms_no_price_available + price_info_list.append('\n') + price_info_list.append(no_price_message) + crop_code_list = get_crop_code_list(N_TOP_SELLING_CROP, TOP_SELLING_CROP_WINDOW) + price_info_list.append(('\n\n%s')%(crop_code_list,)) else: - no_price_message = agg_sms_no_price_available - price_info_list.append('\n') - price_info_list.append(no_price_message) - crop_code_list = get_crop_code_list(N_TOP_SELLING_CROP, TOP_SELLING_CROP_WINDOW) - price_info_list.append(('\n\n%s')%(crop_code_list,)) - else: - prev_crop, prev_mandi, crop_name, mandi_name = -1, -1, '', '' - for index, row in dataframe.iterrows(): - crop, mandi, date, Av_Rate, STD, PriceMax, PriceMin = row['Crop'], row['Market_Real'], row['Date'], row['Av_Ratemean'], row['STDmean'], row['Pricemax'], row['Pricemin'] - delta = PriceMax - PriceMin - if crop != prev_crop or mandi != prev_mandi: - if not all_crop_flag and not all_mandi_flag: - crop_mandi_comb.append((crop,mandi)) - # price_info_log_obj = PriceInfoLog(price_info_incoming=price_info_incoming_obj, - # crop_id=crop, mandi_id=mandi) - # price_info_log_list.append(price_info_log_obj) - crop_name = crop_in_hindi_map.get(crop).encode("utf-8") if crop_in_hindi_map.get(crop) else crop_map[crop].encode("utf-8") - mandi_name = mandi_map[mandi].encode("utf-8") - if crop != prev_crop: - temp_str = ('\n%s: %s (%s: %s)\n\n%s %s\n')%(agg_sms_crop_line,crop_name,code_hi,str(crop),mandi_name.rstrip(mandi_hi).rstrip(),mandi_hi) - else: - temp_str = ('\n%s %s\n')%(mandi_name.rstrip(mandi_hi).rstrip(),mandi_hi) - price_info_list.append(temp_str) - prev_crop, prev_mandi = crop, mandi - if delta < 1: - Av_Rate = round(Av_Rate) - temp_str = ('%s %s: %s %s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(Av_Rate)) - elif 1 <= delta <=2: - if STD <= delta * 0.4: - Av_Rate = round(Av_Rate) - temp_str = ('%s %s: %s %s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(Av_Rate)) - else: - max_price = round(PriceMax) - min_price = round(PriceMin) - temp_str = ('%s %s: %s %s-%s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(min_price),str(max_price)) - elif delta > 2: - min_price = round(PriceMin) - 1 - max_price = round(PriceMax) + 1 - temp_str = ('%s %s: %s %s-%s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(min_price),str(max_price)) - price_info_list.append(temp_str) - # Save combination of crop and mandi for which data is not present in query on if query not for all mandi and crops. - if not all_crop_flag and not all_mandi_flag: - prev_crop, prev_mandi, crop_name, mandi_name = -1, -1, '', '' - for crop, mandi in itertools.product(crop_list, mandi_list): - if (crop,mandi) not in crop_mandi_comb: - price_info_log_obj = PriceInfoLog(price_info_incoming=price_info_incoming_obj, - crop_id=crop, mandi_id=mandi) - # price_info_log_list.append(price_info_log_obj) - if result and (not dataframe.empty): + prev_crop, prev_mandi, crop_name, mandi_name = -1, -1, '', '' + for index, row in dataframe.iterrows(): + crop, mandi, date, Av_Rate, STD, PriceMax, PriceMin = row['Crop'], row['Market_Real'], row['Date'], row['Av_Ratemean'], row['STDmean'], row['Pricemax'], row['Pricemin'] + delta = PriceMax - PriceMin + if crop != prev_crop or mandi != prev_mandi: + if not all_crop_flag and not all_mandi_flag: + crop_mandi_comb.append((crop,mandi)) + # price_info_log_obj = PriceInfoLog(price_info_incoming=price_info_incoming_obj, + # crop_id=crop, mandi_id=mandi) + # price_info_log_list.append(price_info_log_obj) crop_name = crop_in_hindi_map.get(crop).encode("utf-8") if crop_in_hindi_map.get(crop) else crop_map[crop].encode("utf-8") mandi_name = mandi_map[mandi].encode("utf-8") if crop != prev_crop: - prev_crop = crop temp_str = ('\n%s: %s (%s: %s)\n\n%s %s\n')%(agg_sms_crop_line,crop_name,code_hi,str(crop),mandi_name.rstrip(mandi_hi).rstrip(),mandi_hi) else: - prev_mandi = mandi temp_str = ('\n%s %s\n')%(mandi_name.rstrip(mandi_hi).rstrip(),mandi_hi) price_info_list.append(temp_str) - price_info_list.append(agg_sms_no_price_for_combination) + prev_crop, prev_mandi = crop, mandi + if delta < 1: + Av_Rate = round(Av_Rate) + temp_str = ('%s %s: %s %s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(Av_Rate)) + elif 1 <= delta <=2: + if STD <= delta * 0.4: + Av_Rate = round(Av_Rate) + temp_str = ('%s %s: %s %s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(Av_Rate)) + else: + max_price = round(PriceMax) + min_price = round(PriceMin) + temp_str = ('%s %s: %s %s-%s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(min_price),str(max_price)) + elif delta > 2: + min_price = round(PriceMin) - 1 + max_price = round(PriceMax) + 1 + temp_str = ('%s %s: %s %s-%s\n')%(date.strftime('%d'),MONTH_NAMES[int(date.strftime('%m'))],indian_rupee,str(min_price),str(max_price)) + price_info_list.append(temp_str) + # Save combination of crop and mandi for which data is not present in query on if query not for all mandi and crops. + if not all_crop_flag and not all_mandi_flag: + prev_crop, prev_mandi, crop_name, mandi_name = -1, -1, '', '' + for crop, mandi in itertools.product(crop_list, mandi_list): + if (crop,mandi) not in crop_mandi_comb: + price_info_log_obj = PriceInfoLog(price_info_incoming=price_info_incoming_obj, + crop_id=crop, mandi_id=mandi) + # price_info_log_list.append(price_info_log_obj) + if result and (not dataframe.empty): + crop_name = crop_in_hindi_map.get(crop).encode("utf-8") if crop_in_hindi_map.get(crop) else crop_map[crop].encode("utf-8") + mandi_name = mandi_map[mandi].encode("utf-8") + if crop != prev_crop: + prev_crop = crop + temp_str = ('\n%s: %s (%s: %s)\n\n%s %s\n')%(agg_sms_crop_line,crop_name,code_hi,str(crop),mandi_name.rstrip(mandi_hi).rstrip(),mandi_hi) + else: + prev_mandi = mandi + temp_str = ('\n%s %s\n')%(mandi_name.rstrip(mandi_hi).rstrip(),mandi_hi) + price_info_list.append(temp_str) + price_info_list.append(agg_sms_no_price_for_combination) - price_info_list.append(('\n%s: %s')%(helpline_hi, EXOTEL_HELPLINE_NUMBER)) - final_result = ''.join(price_info_list) - price_info_incoming_obj.price_result = final_result + price_info_list.append(('\n%s: %s')%(helpline_hi, EXOTEL_HELPLINE_NUMBER)) + final_result = ''.join(price_info_list) - price_info_incoming_obj.return_result_to_app = 0 - price_info_incoming_obj.info_status = 1 - price_info_incoming_obj.save() - send_info_using_textlocal(from_number, final_result, price_info_incoming_obj) + price_info_incoming_obj.price_result = final_result + price_info_incoming_obj.return_result_to_app = 0 + price_info_incoming_obj.info_status = 1 + price_info_incoming_obj.save() + send_info_using_textlocal(from_number, final_result, price_info_incoming_obj) + except Exception as e: + logger.debug(e) # If caller is calling first time then send crop code to them. if PriceInfoIncoming.objects.filter(from_number=from_number).count() == 1: @@ -291,8 +292,8 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, else: #send_sms(AGGREGATOR_SMS_NO, from_number, crop_code_list) send_info_using_textlocal(from_number, crop_code_list) - # PriceInfoLog.objects.bulk_create(price_info_log_list) + # PriceInfoLog.objects.bulk_create(price_info_log_list) def send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) : # Send No code entered message to user From 40e9646b3ae08d0bdc0d657345983837d7cd1b53 Mon Sep 17 00:00:00 2001 From: Abhishek Lodha Date: Wed, 27 Dec 2017 12:58:46 +0530 Subject: [PATCH 12/13] dataframe none comparison handled --- loop_ivr/helper_function.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/loop_ivr/helper_function.py b/loop_ivr/helper_function.py index 29b823352f..83f0039579 100644 --- a/loop_ivr/helper_function.py +++ b/loop_ivr/helper_function.py @@ -193,13 +193,11 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, price_info_list.append('\n') query = get_query.query_for_rates(crop_list , mandi_list, date_range=3) - logger.debug("-------------------Query--------------") result = run_query(query) dataframe = remove_crop_outliers(ct_data = result) - logger.debug(dataframe) try: - if (not result) or dataframe.empty or dataframe == None: + if (not result) or dataframe.empty or dataframe is None: if not all_crop_flag and not all_mandi_flag: crop_name_list = ','.join(map(lambda crop_id: '%s (%s: %s)'%(crop_in_hindi_map.get(crop_id).encode("utf-8"),code_hi,str(crop_id)) if crop_in_hindi_map.get(crop_id) else '%s (%s: %s)'%(crop_map[crop_id].encode("utf-8"),code_hi,str(crop_id)), crop_list)) mandi_name_list = ','.join(map(lambda mandi_id: mandi_map[mandi_id].encode("utf-8").rstrip(mandi_hi).rstrip(), mandi_list)) @@ -258,7 +256,7 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, price_info_log_obj = PriceInfoLog(price_info_incoming=price_info_incoming_obj, crop_id=crop, mandi_id=mandi) # price_info_log_list.append(price_info_log_obj) - if result and (not dataframe.empty): + if result and dataframe is not None and (not dataframe.empty): crop_name = crop_in_hindi_map.get(crop).encode("utf-8") if crop_in_hindi_map.get(crop) else crop_map[crop].encode("utf-8") mandi_name = mandi_map[mandi].encode("utf-8") if crop != prev_crop: From 54887fc4008f080421b7e5cdca32571119a2871e Mon Sep 17 00:00:00 2001 From: Abhishek Lodha Date: Wed, 27 Dec 2017 13:02:00 +0530 Subject: [PATCH 13/13] exotel app id achanges --- loop_ivr/helper_function.py | 2 -- loop_ivr/utils/config.py | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/loop_ivr/helper_function.py b/loop_ivr/helper_function.py index 83f0039579..5c4f5986a6 100644 --- a/loop_ivr/helper_function.py +++ b/loop_ivr/helper_function.py @@ -291,8 +291,6 @@ def get_price_info(from_number, crop_list, mandi_list, price_info_incoming_obj, #send_sms(AGGREGATOR_SMS_NO, from_number, crop_code_list) send_info_using_textlocal(from_number, crop_code_list) - # PriceInfoLog.objects.bulk_create(price_info_log_list) - def send_crop_code_sms_content(price_info_incoming_obj, sms_content, farmer_number) : # Send No code entered message to user crop_code_list = get_crop_code_list(N_TOP_SELLING_CROP, TOP_SELLING_CROP_WINDOW) diff --git a/loop_ivr/utils/config.py b/loop_ivr/utils/config.py index af3c4a0d18..37dc56e55c 100644 --- a/loop_ivr/utils/config.py +++ b/loop_ivr/utils/config.py @@ -35,9 +35,9 @@ MARKET_INFO_CALL_RESPONSE_URL = 'http://%s%s'%(CURRENT_DOMAIN, '/loopivr/market_info_response/') PUSH_MESSAGE_SMS_RESPONSE_URL = 'http://%s%s'%(CURRENT_DOMAIN, '/loopivr/push_message_sms_response/') # For production -# MARKET_INFO_APP = '137265' +MARKET_INFO_APP = '137265' # For Development -MARKET_INFO_APP = '140902' +# MARKET_INFO_APP = '140902' SMS_SENDER_NAME = 'LOOPDG' TEXT_LOCAL_SINGLE_SMS_API = 'https://api.textlocal.in/send/'