def process_answer(data, question_text, jurisdiction_id, user):    
    answer = json.dumps(data)   # to convert to json
            
    questions = Question.objects.filter(question__iexact= question_text)
    if questions:
        question = questions[0]    
    else:
        print "no question '' has been set up"
                
    is_callout=0

    jurisdiction = Jurisdiction.objects.get(id=jurisdiction_id)            
    answer_reference_class_obj = AnswerReference()
    arcf = answer_reference_class_obj.save_answer(question.id, answer, jurisdiction_id, 'AddRequirement', user, is_callout)
def get_website_html(form_id, question_text, jurisdiction_id, request, mode="add"):
    requestProcessor = HttpRequestProcessor(request)
        
    data_website = {}
    data_website['value_existed'] = 'no'    
    value = ''

    #jurisdiction_id = requestProcessor.getParameter('jurisdiction_id')
    answer_reference_class_obj = AnswerReference()    
    # get current answer.  qa: u don't edit; u add a new record.
    answer = answer_reference_class_obj.get_jurisdiction_data(jurisdiction_id, question_text)
    if answer:
        data_website['value_existed'] = 'yes'        
        for answer_key in answer.keys():
            data_website[answer_key] = answer.get(answer_key)
                       
        urlUtil = UrlUtil(data_website['website'])
        value = "<a href='"+urlUtil.get_href()+"' target='_blank' >"+urlUtil.get_display_website()+"</a>"

    # info for the base_add.html
    data_website['value'] = value  # value to display
    data_website['question_label'] = 'Website'  # field label
    data_website['question_name'] = 'website'    # name used in name and id of form field
    data_website['mode'] = 'individual'         # not sure yet.
       
    # information need to configure the field, this case, a text field.
    data_form = {}               
    data_form['name'] = data_website['question_name']
    if mode == 'edit':
        data_form['value'] = data_website['website']   
    else:
        data_form['value'] = '' 
    data_form['id'] = form_id+'_field_'+ data_form['name'] 
    data_form['class'] = 'required' # for jquery validation
    data_form['style'] = 'width:210px'  # for additional styling
    data_form['attributes'] =  "maxLength='200'"   # may be needed for jquery validation
    data_form['help_text'] = 'Enter the URL of the website'       
    data_form['msg_error'] = ''     # for error msg coming back from server.
    
    form_field = requestProcessor.decode_jinga_template(request,'website/form_fields/text_field.html', data_form, '') 
    data_website['form_fields'] = form_field
    
    if mode == 'edit':
        body = requestProcessor.decode_jinga_template(request,'website/form_fields/base_edit.html', data_website, '')    
    else:
        body = requestProcessor.decode_jinga_template(request,'website/form_fields/base_add.html', data_website, '')          
    
    return body
def test_edit(request):
    requestProcessor = HttpRequestProcessor(request)
    jurisdiction_id = 130732   
    dajax = Dajax()    
    data = {}
    data['jurisdiction_id'] = jurisdiction_id
    answer_reference_class_obj = AnswerReference()
    ajax = requestProcessor.getParameter('ajax')
    if (ajax != None):    
        if (ajax == 'jurisdiction_website_submit'):     
            data['website'] = requestProcessor.getParameter('website')        

            jurisdiction_website_div = requestProcessor.getParameter('jurisdiction_website_div')   
            
            form_id = 'form_website'                
            question_text = 'website'       
            jurisdiction_id = requestProcessor.getParameter('jurisdiction_id')             
            process_answer(data, question_text, jurisdiction_id, request.user)                
                
            question_text = "website"
               
            body = get_website_html(form_id, question_text, jurisdiction_id, request, 'edit')
            dajax.assign('#'+str(jurisdiction_website_div),'innerHTML', body)
            
            return HttpResponse(dajax.json())  
        
        if (ajax == 'jurisdiction_email_submit'):     
            data['email'] = requestProcessor.getParameter('email')        

            jurisdiction_website_div = requestProcessor.getParameter('jurisdiction_email_div')   
            
            form_id = 'form_email'                
            question_text = 'website'       
            jurisdiction_id = requestProcessor.getParameter('jurisdiction_id')             
            process_answer(data, question_text, jurisdiction_id, request.user)                
                
            question_text = "website"
               
            body = get_email_html(form_id, question_text, jurisdiction_id, request, 'edit')
            dajax.assign('#'+str(jurisdiction_website_div),'innerHTML', body)
            
            return HttpResponse(dajax.json())         
        
        
        
        return HttpResponse(dajax.json())              
    ############### website - start #####################
    data['jurisdiction_website_div'] = 'jurisdiction_website_div'   
    data['form_website'] = 'form_website'   
    question_text = "website"
       
    body = get_website_html(data['form_website'], question_text, jurisdiction_id, request, 'edit')
     

    
    data['body_website'] = body

        
    return requestProcessor.render_to_response(request,'website/test/test_edit.html', data, '')    
def get_email_html(form_id, question_text, jurisdiction_id, request, mode="add"):
    requestProcessor = HttpRequestProcessor(request)
        
    data_email = {}
    data_email['value_existed'] = 'no'    
    value = ''

    #jurisdiction_id = requestProcessor.getParameter('jurisdiction_id')
    answer_reference_class_obj = AnswerReference()    
    # get current answer.  qa: u don't edit; u add a new record.
    answer = answer_reference_class_obj.get_jurisdiction_data(jurisdiction_id, question_text)
    if answer:
        data_email['value_existed'] = 'yes'        
        for answer_key in answer.keys():
            data_email[answer_key] = answer.get(answer_key)
                       
        value = data_email['email']   # can do formatting here

    data_email['value'] = value  # value to display
    data_email['question_label'] = 'Email'  # field label
    data_email['question_name'] = 'email'    # name used in name and id of form field
    data_email['mode'] = 'individual'         # not sure yet.
       
    # information need to configure the field, this case, a text field.
    data_form = {}                
    data_form['name'] = data_email['question_name']
    if mode == 'edit':
        data_form['value'] = data_email['email']   
    else:
        data_form['value'] = ''
    data_form['id'] = form_id+'_field_'+ data_form['name'] 
    data_form['class'] = 'required' # for jquery validation
    data_form['style'] = 'width:210px'
    data_form['attributes'] =  "maxLength='200'"   
    data_form['help_text'] = 'Valid email address please'       
    data_form['msg_error'] = ''
    
    form_field = requestProcessor.decode_jinga_template(request,'website/form_fields/text_field.html', data_form, '') 
    data_email['form_fields'] = form_field
    body = requestProcessor.decode_jinga_template(request,'website/form_fields/base_add.html', data_email, '') 
    
    return body    
Esempio n. 5
0
def custom_field(request):
    data = {}
    requestProcessor = HttpRequestProcessor(request)  
    dajax = Dajax()
    
    ajax = requestProcessor.getParameter('ajax')
    if (ajax != None):
        if (ajax == 'create_custom_field'):
            data['form_id'] = 'create_custom_field'      

            data['field_title'] = requestProcessor.getParameter('field_title') 
            data['field_value'] = requestProcessor.getParameter('field_value') 
            data['jurisdiction_id'] = requestProcessor.getParameter('jurisdiction_id') 
            data['category_id'] = requestProcessor.getParameter('category_id')   
            data['current_category'] = requestProcessor.getParameter('current_category')      
            
            if data['field_title'] == None:
                data['field_title'] = ''   
            if data['field_value'] == None:
                data['field_value'] = ''                      
                
            data['jurisdiction'] = Jurisdiction.objects.get(id=data['jurisdiction_id'])

            body = requestProcessor.decode_jinga_template(request,'website/blocks/create_custom_field.html', data, '') 
            dajax.assign('#fancyboxformDiv','innerHTML', body)
            script = requestProcessor.decode_jinga_template(request, 'website/blocks/create_custom_field.js', data, '')
            dajax.script(script)        
            dajax.script('controller.showModalDialog("#fancyboxformDiv");')

            return HttpResponse(dajax.json())  
        
        if (ajax == 'create_custom_field_submit'):  
            data = {}       
            data['form_id'] = 'create_custom_field'     
            data['field_title'] = requestProcessor.getParameter('field_title') 
            data['field_value'] = requestProcessor.getParameter('field_value') 
            data['jurisdiction_id'] = requestProcessor.getParameter('jurisdiction_id') 
            data['category_id'] = requestProcessor.getParameter('category_id')     
            current_category = requestProcessor.getParameter('current_category')   
            current_questions = requestProcessor.getParameter('current_questions') 
                                                               
            error_message = {}        
            error_message = validate_create_custom_field_form_data(data, data['form_id'])

            if len(error_message) == 0:
                msg_key = data['form_id']+ '_fail_reason'               
                try:
                    jurisdiction = Jurisdiction.objects.get(id=data['jurisdiction_id'])
                    
                    # look up template CF for this jurisdiction
                    templates = Template.objects.filter(jurisdiction=jurisdiction, template_type__iexact='CF', accepted = 1)
                    if len(templates) > 0:
                        template = templates[0]
                    else:
                        template = Template()
                        template.template_type = 'CF'
                        template.name = 'Jurisdiction Custom Field Template'
                        template.jurisdiction_id = data['jurisdiction_id']
                        #template.create_datetime = datetime.datetime.now()
                        template.accepted = 1
                        template.save()
                    # create the question

                    category_obj = QuestionCategory.objects.get(id=data['category_id'])
                    '''
                    questions = Question.objects.filter(category=category_obj, accepted=1).order_by('-display_order')
                    last_question = questions[0]
                    if last_question.display_order == None or last_question.display_order == '':
                        display_order = 0
                    else:
                        display_order = last_question.display_order
                    '''    
                    highest_display_order_obj = Question.objects.filter(category=category_obj, accepted=1).aggregate(Max('display_order'))
                    if highest_display_order_obj == None:
                        highest_display_order = 0
                    else:
                        highest_display_order = highest_display_order_obj['display_order__max']
                        
                    question_obj = Question();
                    question_obj.category_id = data['category_id']
                    question_obj.question = data['field_title']
                    question_obj.label = data['field_title']
                    question_obj.form_type = 'CF'
                    question_obj.qtemplate_id = template.id
                    question_obj.accepted = 1
                    question_obj.display_order = int(highest_display_order) + 5
                    #question_obj.create_datetime = datetime.datetime.now()
                    question_obj.creator_id = request.user.id
                    question_obj.save()
                    
                    template_question = TemplateQuestion()
                    template_question.template_id = template.id
                    template_question.question_id = question_obj.id
                    #template_question.create_datetime = datetime.datetime.now()
                    template_question.save()
                    # save the answer
                    data_answer = {}
                    data_answer['value'] = data['field_value']
                    answer = json.dumps(data_answer)   # to convert to json                    
                    is_callout=0           
                    answer_reference_class_obj = AnswerReference()
                    
                    #action_category_objs = ActionCategory.objects.filter(name__iexact='AddRequirement')
                    #action_category_obj = action_category_objs[0]   
                    validation_util_obj = FieldValidationCycleUtil()                    
                    arcf = validation_util_obj.save_answer(question_obj, answer, jurisdiction, 'AddRequirement', request.user, is_callout)
                    
                except Exception, e:
                    data[msg_key] = e.message
        
            #else:
            if len(error_message) > 0:
                for msg_key in error_message.keys():
                    data[msg_key] = error_message.get(msg_key)  
                    
                    body = requestProcessor.decode_jinga_template(request,'website/blocks/create_custom_field.html', data, '')
                    dajax.assign('#fancyboxformDiv','innerHTML', body)
                    script = requestProcessor.decode_jinga_template(request, 'website/blocks/create_custom_field.js', data, '')
                    dajax.script(script)
            else:
                data_cf = {}
                jurisdiction = Jurisdiction.objects.get(id=data['jurisdiction_id'])
                data_cf['jurisdiction'] = jurisdiction
                template_obj = Template()
                jurisdiction_templates = template_obj.get_jurisdiction_question_templates(jurisdiction)
               
                    
                data_cf['action'] = '/jurisdiction_id/'+str(jurisdiction.id)+'/'+current_category+'/'
                  
                data_cf = get_question_data(request, jurisdiction, question_obj, data)
                body = requestProcessor.decode_jinga_template(request,'website/jurisdictions/AHJ_cqa_qa.html', data_cf, '')
                script = requestProcessor.decode_jinga_template(request,'website/jurisdictions/AHJ_cqa_qa.js' , data, '')
                dajax.script(script)                                                   
            
                dajax.assign('#div_custom_question_content_'+str(data['category_id']),'innerHTML', body)    
                        
                        ######################################################               
                      
                #data['custom_questions'] = custom_questions
                #data['custom_questions'] = {}
                #data['custom_questions'][question_category_obj.id] = custom_questions
                
                #body = requestProcessor.decode_jinga_template(request,'website/blocks/custom_fields.html', data, '')                                   
                #dajax.assign('#custom_fields_'+str(question_category_obj.id),'innerHTML', body)
                dajax.script('jQuery.fancybox.close();')  
                dajax.script("controller.showMessage('Custom field created.', 'success')") 
                dajax.script('controller.updateUrlAnchor("#create_custom_field");')
                
                data = {}
                if current_category == 'all_info':
                    question_categories = QuestionCategory.objects.filter(accepted=1)
                    data['category'] = 'All categories'
                else:
                    question_categories = QuestionCategory.objects.filter(name__iexact=category_obj.name)
                    data['category'] = category_obj.name

                #data['category'] = question_obj.category.name
                data['jurisdiction'] = jurisdiction
                
                data['top_contributors'] = get_ahj_top_contributors(jurisdiction, current_category)  
                body = requestProcessor.decode_jinga_template(request,'website/jurisdictions/AHJ_top_contributors.html', data, '')   
                dajax.assign('#top-contributor','innerHTML', body)                           
                
            return HttpResponse(dajax.json())