def select_village(request, response): prompt = 'Select your village\n' villages = Village.objects.all() d = dict((i+1, v.id) for i, v in enumerate(villages)) request['params'].update(d) menu = [] if not request['input']: menu += [(village,) for village in villages] return response( message=prompt+print_menu(menu), next='select_village', params=request['params'] ) else: next = request['params'].get('landmarks', 'select_landmark') for village in villages: temp = request['params'].copy() temp['village_id']=village.id menu.append([village, next, temp]) return forward_from_menu(request, menu)
def select_landmark(request, start_response, get_next='select_landmark', post_next='list_ads'): prompt = 'Choose closest landmark\n' landmarks = Landmark.objects.filter(village=Village.objects.get( id=request['params'].get('village_id', 0))) create = int(request['params'].get('create', 1)) params = {'village_id': request['params'].get('village_id', '')} if create: # Hack hack hack! Need to clean out village ids from params but # keep these key fields params.update({ 'name': request['params'].get('name', ''), }) d = dict((i + 1, lm.id) for i, lm in enumerate(landmarks)) params.update(d) menu = [(lm, post_next) for lm in landmarks] if not request['input']: return start_response(message=prompt + print_menu(menu), next=get_next, params=params) else: if create: Buyer(phone_number=request['phone_number'], preference=int(request['params'].get('preference', 0)), name=request['params'].get('name', ''), landmark=Landmark.objects.get( id=request['params'][request['input']])).save() return forward_from_menu(request, menu)
def select_landmark(request, start_response, get_next='select_landmark', post_next='list_ads'): prompt = 'Choose closest landmark\n' landmarks = Landmark.objects.filter( village=Village.objects.get(id=request['params'].get('village_id', 0)) ) create = int(request['params'].get('create', 1)) params = {'village_id' : request['params'].get('village_id', '')} if create: # Hack hack hack! Need to clean out village ids from params but # keep these key fields params.update({ 'name' : request['params'].get('name', ''), }) d = dict((i+1, lm.id) for i, lm in enumerate(landmarks)) params.update(d) menu = [(lm, post_next) for lm in landmarks] if not request['input']: return start_response( message=prompt+print_menu(menu), next=get_next, params=params ) else: if create: Buyer( phone_number=request['phone_number'], preference=int(request['params'].get('preference', 0)), name=request['params'].get('name', ''), landmark=Landmark.objects.get(id=request['params'][request['input']]) ).save() return forward_from_menu(request, menu)
def home(request, response): greeting = 'Welcome to M-Maji\n' menu = (('Sell water', 'place_ad'), ('Buy water', 'list_ads')) message = greeting + print_menu(menu) if not request['input']: return response(message=message, next='home') else: return forward_from_menu(request, menu)
def new_buyer(request, response): menu = [ ('Find cheapest water near me', 'select_village', {'sort_by':'-cheap',\ 'preference':Buyer.CHEAP_PREFERENCE}), ('Find clean water only', 'select_village', {'sort_by':'-clean', \ 'preference':Buyer.CLEAN_PREFERENCE}), ] if not request['input']: return response(message=print_menu(menu), next='new_buyer') else: return forward_from_menu(request, menu)
def ads_options(request, response): header = 'Re-sort by:\n' menu = [ ('Price', 'list_ads'), ('Quality', 'list_ads'), ('Different location', 'select_village', {'create':0,'landmarks':'select_landmark_without_create'}) ] message = header + print_menu(menu) if not request['input']: return response(message=message, next='ads_options') else: return forward_from_menu(request, menu)
def ads_options(request, response): header = 'Re-sort by:\n' menu = [('Price', 'list_ads'), ('Quality', 'list_ads'), ('Different location', 'select_village', { 'create': 0, 'landmarks': 'select_landmark_without_create' })] message = header + print_menu(menu) if not request['input']: return response(message=message, next='ads_options') else: return forward_from_menu(request, menu)
def select_kiosk(request, response): prompt = 'Choose kiosk\n' kiosks = Kiosk.objects.filter(seller=Seller.objects.get( phone_number=request['phone_number'])) d = dict((i + 1, v.id) for i, v in enumerate(kiosks)) request['params'].update(d) menu = [(k, 'update_price', {'kiosk_id': k.id}) for k in kiosks] if not request['input']: return response(message=prompt + print_menu(menu), next='select_kiosk', params=request['params']) else: return forward_from_menu(request, menu)
def select_kiosk(request, response): prompt = 'Choose kiosk\n' kiosks = Kiosk.objects.filter( seller=Seller.objects.get(phone_number=request['phone_number']) ) d = dict((i+1, v.id) for i, v in enumerate(kiosks)) request['params'].update(d) menu = [(k, 'update_price', {'kiosk_id':k.id}) for k in kiosks] if not request['input']: return response( message=prompt+print_menu(menu), next='select_kiosk', params=request['params'] ) else: return forward_from_menu(request, menu)
def select_vendor_village(request, start_response): prompt = 'Select your village\n' villages = Village.objects.all() if not request['input']: d = dict((i + 1, v.id) for i, v in enumerate(villages)) request['params'].update(d) menu = [(v, ) for v in villages] return start_response(message=prompt + print_menu(menu), next='select_vendor_village', params=request['params']) else: menu = [] for v in villages: temp = request['params'].copy() temp['village_id'] = v.id menu.append((v, 'select_vendor_landmark', temp)) return forward_from_menu(request, menu)
def place_ad(request, start_response): try: seller = Seller.objects.get(phone_number=request['phone_number']) menu = [ ('New advertisement', 'select_kiosk', {'seller_id':seller.id}), ('Add kiosk', 'select_village', {'create':0, 'landmarks':'select_vendor_landmark'}) ] if not request['input']: return start_response( message=print_menu(menu), next='place_ad', params=request['params'] ) else: return forward_from_menu(request, menu) except Seller.DoesNotExist: return forward(request, 'enter_vendor_name')
def select_vendor_village(request, start_response): prompt = 'Select your village\n' villages = Village.objects.all() if not request['input']: d = dict((i+1, v.id) for i, v in enumerate(villages)) request['params'].update(d) menu = [(v,) for v in villages] return start_response( message=prompt+print_menu(menu), next='select_vendor_village', params=request['params'] ) else: menu = [] for v in villages: temp = request['params'].copy() temp['village_id'] = v.id menu.append((v,'select_vendor_landmark',temp)) return forward_from_menu(request, menu)
def place_ad(request, start_response): try: seller = Seller.objects.get(phone_number=request['phone_number']) menu = [('New advertisement', 'select_kiosk', { 'seller_id': seller.id }), ('Add kiosk', 'select_village', { 'create': 0, 'landmarks': 'select_vendor_landmark' })] if not request['input']: return start_response(message=print_menu(menu), next='place_ad', params=request['params']) else: return forward_from_menu(request, menu) except Seller.DoesNotExist: return forward(request, 'enter_vendor_name')
def select_vendor_landmark(request, start_response): prompt = 'Choose closest landmark\n' landmarks = Landmark.objects.filter( village=request['params']['village_id'] ) # Hack hack hack! Need to clean out village ids from params but # keep these key fields d = dict((i+1, lm.id) for i, lm in enumerate(landmarks)) request['params'].update(d) menu = [(lm,) for lm in landmarks] if not request['input']: return start_response( message=prompt+print_menu(menu), next='select_vendor_landmark', params=request['params'] ) else: try: seller = Seller.objects.get(phone_number=request['phone_number']) except Seller.DoesNotExist: seller = Seller( phone_number=request['phone_number'], name=request['params'].get('name', ''), ) seller.save() menu = [] for lm in landmarks: temp = request['params'].copy() temp['village_id'] = lm.id temp['landmark_id'] = request['params'][request['input']] temp['seller_id'] = seller.id menu.append([lm,'describe_kiosk_location',temp]) return forward_from_menu(request, menu)
def select_village(request, response): prompt = 'Select your village\n' villages = Village.objects.all() d = dict((i + 1, v.id) for i, v in enumerate(villages)) request['params'].update(d) menu = [] if not request['input']: menu += [(village, ) for village in villages] return response(message=prompt + print_menu(menu), next='select_village', params=request['params']) else: next = request['params'].get('landmarks', 'select_landmark') for village in villages: temp = request['params'].copy() temp['village_id'] = village.id menu.append([village, next, temp]) return forward_from_menu(request, menu)
def select_vendor_landmark(request, start_response): prompt = 'Choose closest landmark\n' landmarks = Landmark.objects.filter( village=request['params']['village_id']) # Hack hack hack! Need to clean out village ids from params but # keep these key fields d = dict((i + 1, lm.id) for i, lm in enumerate(landmarks)) request['params'].update(d) menu = [(lm, ) for lm in landmarks] if not request['input']: return start_response(message=prompt + print_menu(menu), next='select_vendor_landmark', params=request['params']) else: try: seller = Seller.objects.get(phone_number=request['phone_number']) except Seller.DoesNotExist: seller = Seller( phone_number=request['phone_number'], name=request['params'].get('name', ''), ) seller.save() menu = [] for lm in landmarks: temp = request['params'].copy() temp['village_id'] = lm.id temp['landmark_id'] = request['params'][request['input']] temp['seller_id'] = seller.id menu.append([lm, 'describe_kiosk_location', temp]) return forward_from_menu(request, menu)
except Buyer.DoesNotExist, be: return forward(request, 'new_buyer') if not request['input']: return start_response( message=print_menu(menu), next='list_ads', params=params ) # Buyer already made a selection on this page else: input = request['params'].get(request['input'], '') if input: request['params']['advertisement_id'] = input return forward_from_menu(request, menu) @ussd.handler def new_buyer(request, response): menu = [ ('Find cheapest water near me', 'select_village', {'sort_by':'-cheap',\ 'preference':Buyer.CHEAP_PREFERENCE}), ('Find clean water only', 'select_village', {'sort_by':'-clean', \ 'preference':Buyer.CLEAN_PREFERENCE}), ] if not request['input']: return response(message=print_menu(menu), next='new_buyer') else: return forward_from_menu(request, menu) @ussd.handler
] # First-time buyer except Buyer.DoesNotExist, be: return forward(request, 'new_buyer') if not request['input']: return start_response(message=print_menu(menu), next='list_ads', params=params) # Buyer already made a selection on this page else: input = request['params'].get(request['input'], '') if input: request['params']['advertisement_id'] = input return forward_from_menu(request, menu) @ussd.handler def new_buyer(request, response): menu = [ ('Find cheapest water near me', 'select_village', {'sort_by':'-cheap',\ 'preference':Buyer.CHEAP_PREFERENCE}), ('Find clean water only', 'select_village', {'sort_by':'-clean', \ 'preference':Buyer.CLEAN_PREFERENCE}), ] if not request['input']: return response(message=print_menu(menu), next='new_buyer') else: return forward_from_menu(request, menu)