Beispiel #1
0
def super_survey():
    #FIXME better way to see if survey?
    # FIXME move function
    # user_survey_section_ids = [x.user_survey_sections.order_by(UserSurveySection.completed_date.desc().
    #     nullslast()).first().id if x.user_survey_sections.order_by(UserSurveySection.completed_date.desc().
    #     nullslast()).first() else None for x in g.user.organizations]
    user_survey_section_ids = chain(g.user)

    survey_tables = unpack(user_survey_section_ids)
    if  request.method == 'POST':
        session['user_survey_section_ids'] = []
        for survey_table in survey_tables:
            session['user_survey_section_ids'].append(request.form.
                    get(str(survey_table.user_survey_section_id)))
        if session['user_survey_section_ids']:
            return redirect(url_for('selection'))
    return render_template('super_survey.html',survey_tables=survey_tables,ids = user_survey_section_ids) #package=package)
Beispiel #2
0
def selection():
    if not session.get('user_survey_section_ids',None):
        return redirect(url_for('super_survey'))
    # TODO rename survey_table
    # TODO move into own function
    survey_tables = unpack(session['user_survey_section_ids'])
    if request.method == 'POST':
        for survey_table in survey_tables:
            data_section_total = 0
            survey_section = SurveySection.query.get(survey_table.survey_section_id)
            user_survey_section = UserSurveySection.query.get(survey_table.user_survey_section_id)
            data_id = user_survey_section.data.id
            data = Data.query.get(data_id)
            organization = Organization.query.get(user_survey_section.organization.id)
            period = Period.query.get(user_survey_section.period.id)
            assigned_due = AssignedDue.query.get(user_survey_section.assigned_due.id)
            if user_survey_section.completed_date == None:
                nuss = user_survey_section
            else:
                nuss = UserSurveySection()
            if survey_section.order == 9:
                question_ids = []
                heading = False
                for q in survey_section.questions:
                    if custom_strip(str(q.name)) != heading:
                        heading = custom_strip(str(q.name))
                        question_ids.append(request.form.get(custom_strip(str(q.name))))
            else:
                question_ids = request.form.getlist(str(survey_table.user_survey_section_id))
            for question in survey_section.questions:
                if unicode(question.id) in question_ids: # because unicode
                    answer = Answer(tf=True)
                else:
                    answer = Answer(tf=False)
                nuss.answers.append(answer)
                option_choice = OptionChoice.query.filter_by(name='True').one()
                question_option = db.session.query(QuestionOption).\
                        filter((QuestionOption.question == question)
                                & (QuestionOption.option_choice == option_choice)).first()
                question_option.answers.append(answer)
                if answer.tf:
                    data_section_total += question_option.question.value
            # should be nuss if there is already a completed


            nuss.completed_date  = datetime.datetime.utcnow()
            organization.user_survey_sections.append(nuss)
            survey_section.user_survey_sections.append(nuss)
            period.user_survey_sections.append(nuss)
            assigned_due.user_survey_sections.append(nuss)
            data.user_survey_sections.append(nuss)
            # db.session.add(data) #TODO is this necessary?
            db.session.flush()

            #TODO generalize, ugly solution
            if nuss.survey_section.order == 2:
                nuss.data.standard_form = data_section_total
            elif nuss.survey_section.order == 3:
                nuss.data.marketing_education = data_section_total
            elif nuss.survey_section.order == 4:
                nuss.data.record_availability = data_section_total
            elif nuss.survey_section.order == 5:
                nuss.data.family_centerdness = data_section_total
            elif nuss.survey_section.order == 6:
                nuss.data.pc_networking = data_section_total
            elif nuss.survey_section.order == 7:
                nuss.data.education_and_training = data_section_total
            elif nuss.survey_section.order == 8:
                nuss.data.team_funding = data_section_total
            elif nuss.survey_section.order == 9:
                nuss.data.coverage = data_section_total
            elif nuss.survey_section.order == 10:
                nuss.data.pc_for_expired_pts = data_section_total
            elif nuss.survey_section.order == 11:
                nuss.data.hospital_pc_screening = data_section_total
            elif nuss.survey_section.order == 12:
                nuss.data.pc_follow_up = data_section_total
            elif nuss.survey_section.order == 13:
                nuss.data.post_discharge_services = data_section_total
            elif nuss.survey_section.order == 14:
                nuss.data.bereavement_contacts = data_section_total
            elif nuss.survey_section.order == 15:
                nuss.data.certification = data_section_total
            elif nuss.survey_section.order == 16:
                nuss.data.team_wellness = data_section_total
            elif nuss.survey_section.order == 17:
                nuss.data.care_coordination = data_section_total
            db.session.commit()
        # possible don't need this datas it was a commit problem.
        data_values = extract_data(data)
        data_values = [int(x) for x in data_values] #TODO unicode
        #FIXME upload_s3 is calling plotpolar shouldn't
        #TODO refactor name groupings
        time = str(datetime.datetime.utcnow())
        upload_s3(survey_table.survey_header,
                survey_table.organization,
                survey_table.period_name,
                time + ' ' + survey_table.survey_header
                + ' ' + survey_table.period_name + ' ' + survey_table.organization ,
                [survey_table.survey_header, survey_table.period_name, survey_table.organization, data_values])
        file_url =  get_url_s3('/'.join([survey_table.survey_header,
            survey_table.organization, survey_table.period_name, time + ' ' + survey_table.survey_header
                + ' ' + survey_table.period_name + ' ' + survey_table.organization]))
        data.url = file_url
        data.timestamp = time
        db.session.commit()
        #TODO consider saving just 1
        session['user_survey_section_ids'] = None
        return redirect(url_for('organization'))

    return render_template('selection.html',survey_tables=survey_tables,
            SurveySection=SurveySection,user =g.user)