def submit_direct(request,state_abbr): "save direct info in Rocky, returns json" if not request.method == "POST": return HttpResponse(json.dumps({'error':'incorrect method','message':'this url expects POST'})) submitted_form = request.POST.copy() #set home_state_id from url submitted_form['home_state_id'] = state_abbr cleaned_form = cleanup_form(submitted_form) #submit to rocky rtv_response = rtv_proxy('POST',cleaned_form,'/api/v2/gregistrations.json') if rtv_response.has_key('error'): return HttpResponseServerError(rtv_response) return HttpResponse('OK')
def submit_direct(request,state_abbr): "save direct info in Rocky, returns json" if not request.method == "POST": return HttpResponse(json.dumps({'error':'incorrect method','message':'this url expects POST'})) submitted_form = request.POST.copy() #set home_state_id from url submitted_form['home_state_id'] = state_abbr cleaned_form = cleanup_form(submitted_form) #submit to rocky rtv_response = rtv_proxy('POST',cleaned_form,'/api/v3/gregistrations.json') print 'Submitting to Rocky' if rtv_response.has_key('error'): return HttpResponseServerError(rtv_response) #submit to covr print 'Submitting to State:' + state_abbr if state_abbr == 'CA': print 'Submitting to CA' covr_response = covr_proxy(cleaned_form) #covr_token = 'TOKEN-TOKEN-TOKEN-TOKEN' covr_token = covr_response.Token print 'Token: ' + covr_token #request.session['covr_token'] = 'REQUEST-TOKEN-TOKEN-TOKEN' #context = {} #context['covr_token'] = 'CONTEXT-TOKEN-TOKEN-TOKEN' #print 'form_%s_direct.html' % state_abbr.lower() #return render_to_response('form_%s_direct.html' % state_abbr.lower(), context, context_instance=RequestContext(request)) else: covr_token = ''; return HttpResponse(covr_token) return render_to_response('form_%s_direct.html', context, context_instance=RequestContext(request))
def get_branding(context): """Util method to get branding given partner id. Used in submit view, because we need access before the context processor runs. Not totally DRY, but better than faking an HttpRequest.""" # check for cobrand form first try: context["cobrandform"] = CoBrandForm.objects.get(partner_id=context["partner"]) context["customform"] = context["cobrandform"].toplevel_org return context except (CoBrandForm.DoesNotExist, ValueError): pass # then custom form context["cobrandform"] = None try: context["customform"] = CustomForm.objects.get(partner_id=context["partner"]) return context except (CustomForm.DoesNotExist, ValueError): pass # finally, try rtv whitelabel try: # check cache first cache_key = "rtv_whitelabel_%s" % context["partner"] if cache.get(cache_key): rtv_whitelabel = cache.get(cache_key) else: rtv_whitelabel = rtv_proxy( "GET", {"partner_id": context["partner"]}, "api/v2/partnerpublicprofiles/partner.json" ) cache.set(cache_key, rtv_whitelabel, 3600) # cache whitelabel hits for an hour # duck type a customform quack = { "partner_id": context["partner"], "name": rtv_whitelabel["org_name"], "logo": rtv_whitelabel["logo_image_URL"], "logo_link": rtv_whitelabel["org_URL"], "show_sms_box": rtv_whitelabel["partner_ask_sms_opt_in"], "show_volunteer_box": rtv_whitelabel["rtv_ask_volunteer"], } if context["language"] == "es": quack["question_1"] = rtv_whitelabel["survey_question_1_es"] quack["question_2"] = rtv_whitelabel["survey_question_2_es"] else: quack["question_1"] = rtv_whitelabel["survey_question_1_en"] quack["question_2"] = rtv_whitelabel["survey_question_2_en"] # check for missing logo image url if "/logos/original/missing.png" in quack["logo"]: quack["logo"] = None context["rtv_whitelabel"] = True # unlike in context_processor, this needs to be object-like # so create a CustomForm, but don't save it fakin_bacon = CustomForm(**quack) context["customform"] = fakin_bacon except KeyError: # whitelabel error logger.error("white label error, id %s" % context["partner"], exc_info=True, extra={"context": context}) return context
def submit(request): "Submit the posted form to the Rocky API" if request.method != "POST": return redirect('/registrants/new/') submitted_form = request.POST.copy() #make a mutable copy #fill in missing city/state fields if we have zipcodes zip_fields = ['home','mailing','prev'] for f in zip_fields: zipcode = submitted_form.get(f+'_zip_code').strip() city = submitted_form.get(f+'_city') if empty(city) and not empty(zipcode): try: place = ZipCode.objects.get(zipcode=zipcode) submitted_form[f+'_city'] = place.city.lower().title() submitted_form[f+'_state_id'] = place.state except (ZipCode.DoesNotExist,ValueError,IndexError): pass #this can happen if the user has to go back and resubmit the form, but the zipcode lookup js doesn't re-run #should probably also fix this client side... cleaned_form = cleanup_form(submitted_form) #send questions to api if answers were set #defaults first question_1 = "If you are in school, where do you go?" question_2 = "What issue do you care most about?" #then check cobrand and custom form branding = get_branding({'partner':cleaned_form['partner_id'],'language':cleaned_form['lang']}) if branding.get('cobrandform'): cobrand = branding['cobrandform'] if cobrand.question_1: question_1 = cobrand.question_1 if cobrand.question_2: question_2 = cobrand.question_2 elif branding.get('customform'): customform = branding.get('customform') if customform.question_1: question_1 = customform.question_1 if customform.question_2: question_2 = customform.question_2 if cleaned_form.has_key('survey_answer_1'): cleaned_form['survey_question_1'] = question_1 if cleaned_form.has_key('survey_answer_2'): cleaned_form['survey_question_2'] = question_2 #hit the rocky api rtv_response = rtv_proxy('POST',cleaned_form,'/api/v3/registrations.json') context = {} if rtv_response.has_key('pdfurl'): context['pdfurl'] = rtv_response['pdfurl'] if rtv_response.has_key('error'): #something went wrong that wasn't caught in the frontend validation #clean up error message for human consumption try: context['error'] = True messages.error(request, rtv_response['error']['message'].lower(), extra_tags=rtv_response['error']['field_name'].replace('_',' ').title()) #also mail the admins to see if there's a persistent problem logger.error('rocky error: validating %s' % rtv_response['error']['field_name'], exc_info=True, extra={'request':request}) except KeyError: context['error'] = True messages.error(request, rtv_response['error'], extra_tags="Rocky API") logger.error('rocky error: api issue',exc_info=True, extra={'request':request}) #check state id against list of valid abbreviations try: context['state_name'] = STATE_NAME_LOOKUP[submitted_form.get('home_state_id')] except KeyError: #unrecognized state context['error'] = True messages.error(request, _("Unrecognized state or zip. Most likely your zip code wasn't recognized. Please try another one.")) #get toplevel org for partner proxy submit if branding.get('cobrandform'): customform = branding['cobrandform'].toplevel_org submitted_form['cobrand'] = branding['cobrandform'].name #If customform and cobrand form, RTV and custom share main optin, #so use opt_in_email for partner_proxy_signup partner_proxy_signup = submitted_form['opt_in_email'] elif branding.get('customform'): customform = branding['customform'] #If customform but not cobrand form, set partner_proxy_signup to partner_opt_in_email partner_proxy_signup = submitted_form['opt_in_email'] else: customform = None partner_proxy_signup = None #if a custom form has endpoint, and we got user permission, post to the partner proxy if customform and customform.list_signup_endpoint and partner_proxy_signup: proxy_response = customform.submit(submitted_form) if proxy_response.get('error'): logger.error('rocky error: custom form: %s' % customform.name, exc_info=True,extra={'request':request}) context['error'] = True messages.error(request, _("Unknown error: the web administrators have been contacted."), extra_tags=proxy_response) #append branding to context, so partner logos appear in submit page context.update(branding) #send branding partner ids to context, for trackable social media links context['partner'] = submitted_form.get('partner_id') context['source'] = submitted_form.get('partner_tracking_id') context['email_address'] = submitted_form.get("email_address") #don't show partner for testing partner ids if request.GET.get('partner'): if request.GET.get('partner') in DEFAULT_PARTNER_IDS: context['has_partner'] = False else: context['has_partner'] = True else: context['has_partner'] = False if context.has_key('error'): return redirect('/registrants/error/') else: return render_to_response('submit.html', context, context_instance=RequestContext(request))
def get_branding(context): """Util method to get branding given partner id. Used in submit view, because we need access before the context processor runs. Not totally DRY, but better than faking an HttpRequest.""" #check for cobrand form first try: context['cobrandform'] = CoBrandForm.objects.get(partner_id=context['partner']) context['customform'] = context['cobrandform'].toplevel_org return context except (CoBrandForm.DoesNotExist,ValueError): pass #then custom form context['cobrandform'] = None try: context['customform'] = CustomForm.objects.get(partner_id=context['partner']) return context except (CustomForm.DoesNotExist,ValueError): pass #finally, try rtv whitelabel try: #check cache first cache_key = 'rtv_whitelabel_%s' % context['partner'] if cache.get(cache_key): rtv_whitelabel = cache.get(cache_key) else: rtv_whitelabel = rtv_proxy('GET',{'partner_id':context['partner']}, 'api/v3/partnerpublicprofiles/partner.json') cache.set(cache_key,rtv_whitelabel,1) #cache whitelabel hits for 3600 a bit #duck type a customform quack = {'partner_id':context['partner'], 'name':rtv_whitelabel['org_name'], 'logo':rtv_whitelabel['logo_image_URL'], 'logo_link':rtv_whitelabel['org_URL'], 'show_sms_box':rtv_whitelabel['partner_ask_sms_opt_in'], 'show_volunteer_box':rtv_whitelabel['rtv_ask_volunteer'], } if context['language'] == "es": quack['question_1'] = rtv_whitelabel['survey_question_1_es'] quack['question_2'] = rtv_whitelabel['survey_question_2_es'] elif context['language'] == "ko": quack['question_1'] = rtv_whitelabel['survey_question_1_ko'] quack['question_2'] = rtv_whitelabel['survey_question_2_ko'] elif context['language'] == "zh": quack['question_1'] = rtv_whitelabel['survey_question_1_zh'] quack['question_2'] = rtv_whitelabel['survey_question_2_zh'] elif context['language'] == "zh-TW": quack['question_1'] = rtv_whitelabel['survey_question_1_zh-TW'] quack['question_2'] = rtv_whitelabel['survey_question_2_zh-TW'] elif context['language'] == "tl": quack['question_1'] = rtv_whitelabel['survey_question_1_tl'] quack['question_2'] = rtv_whitelabel['survey_question_2_tl'] elif context['language'] == "ilo": quack['question_1'] = rtv_whitelabel['survey_question_1_ilo'] quack['question_2'] = rtv_whitelabel['survey_question_2_ilo'] elif context['language'] == "bn": quack['question_1'] = rtv_whitelabel['survey_question_1_bn'] quack['question_2'] = rtv_whitelabel['survey_question_2_bn'] elif context['language'] == "ur": quack['question_1'] = rtv_whitelabel['survey_question_1_ur'] quack['question_2'] = rtv_whitelabel['survey_question_2_ur'] elif context['language'] == "vi": quack['question_1'] = rtv_whitelabel['survey_question_1_vi'] quack['question_2'] = rtv_whitelabel['survey_question_2_vi'] elif context['language'] == "th": quack['question_1'] = rtv_whitelabel['survey_question_1_th'] quack['question_2'] = rtv_whitelabel['survey_question_2_th'] elif context['language'] == "hi": quack['question_1'] = rtv_whitelabel['survey_question_1_hi'] quack['question_2'] = rtv_whitelabel['survey_question_2_hi'] else: quack['question_1'] = rtv_whitelabel['survey_question_1_en'] quack['question_2'] = rtv_whitelabel['survey_question_2_en'] #check for missing logo image url if '/logos/original/missing.png' in quack['logo']: quack['logo'] = None context['rtv_whitelabel'] = True #unlike in context_processor, this needs to be object-like #so create a CustomForm, but don't save it fakin_bacon = CustomForm(**quack) context['customform'] = fakin_bacon except KeyError: #whitelabel error logger.error("white label error, id %s" % context['partner'], exc_info=True,extra={'context':context}) return context
def whitelabel(request): """ Context processor to add customform and cobrandform to request.session Checks proxy CoBrandForm, then CustomForm, then rocky whitelabel API """ context = {} #save partner if request.GET.get('partner'): partner = request.GET.get('partner') context['partner'] = partner context['has_partner'] = True elif not context.has_key('partner'): #no partner specified, return early context['partner'] = 1 context['has_partner'] = False return context #get whitelabel branding #try CoBrandForm first try: cobrand = CoBrandForm.objects.get(partner_id=partner) context['cobrandform'] = cobrand context['customform'] = cobrand.toplevel_org return context except (CoBrandForm.DoesNotExist,ValueError): pass #no CoBrandForm, try CustomForm context['cobrandform'] = None try: context['customform'] = CustomForm.objects.get(partner_id=partner) return context except (CustomForm.DoesNotExist,ValueError): context['customform'] = None try: #finally, try rtv whitelabel #check cache first cache_key = 'rtv_whitelabel_%s' % context['partner'] if cache.get(cache_key): rtv_whitelabel = cache.get(cache_key) else: rtv_whitelabel = rtv_proxy('GET',{'partner_id':context['partner']}, 'api/v2/partnerpublicprofiles/partner.json') cache.set(cache_key,rtv_whitelabel,3600) #cache whitelabel hits for an hour #duck type a customform quack = {'partner_id':context['partner'], 'name':rtv_whitelabel['org_name'], 'logo':rtv_whitelabel['logo_image_URL'], 'logo_link':rtv_whitelabel['org_URL']} if rtv_whitelabel['whitelabeled']: quack['show_sms_box'] = rtv_whitelabel['partner_ask_sms_opt_in'] quack['show_volunteer_box'] = rtv_whitelabel['ask_volunteer'] quack['show_email_optin'] = rtv_whitelabel['ask_email_opt_in'] else: quack['show_sms_box'] = rtv_whitelabel['rtv_ask_sms_opt_in'] quack['show_volunteer_box'] = rtv_whitelabel['rtv_ask_volunteer'] quack['show_email_optin'] = rtv_whitelabel['rtv_ask_email_opt_in'] if request.LANGUAGE_CODE == "es": quack['question_1'] = rtv_whitelabel['survey_question_1_es'] quack['question_2'] = rtv_whitelabel['survey_question_2_es'] else: quack['question_1'] = rtv_whitelabel['survey_question_1_en'] quack['question_2'] = rtv_whitelabel['survey_question_2_en'] #check for missing logo image url if '/logos/original/missing.png' in quack['logo']: quack['logo'] = None context['rtv_whitelabel'] = True context['customform'] = quack except KeyError,e: #whitelabel error logger.error("white label error %s, id %s" % (e,context['partner']), exc_info=True,extra={'request':request})