Пример #1
0
    def save_from_form(self, request):
        
        rec = {
            "qualifications": [],
            "interests": [],
            "applications": [],
            "experience": []
        }
        if 'paequals' in self.data:
            rec['paequals'] = self.data['paequals']
        else:
            rec['paequals'] = {}
        
        for key in request.form.keys():
            if not key.startswith("qualification_") and not key.startswith("interest_") and not key.startswith("application_") and not key.startswith("experience_") and key not in ['submit']:
                val = request.form[key]
                if key == "summer_school":
                    if val == "on":
                        rec[key] = "yes"
                    else:
                        rec[key] = "no"
                elif val == "on":
                    rec[key] = True
                elif val == "off":
                    rec[key] = False
                elif key in ['additional_qualifications','career_plans','issues_affecting_performance']:
                    rec[key] = util.dewindows(val)
                else:
                    rec[key] = val

        thirdquals = []
        fourthquals = []
        fifthquals = []
        sixthquals = []
        otherquals = []
        for k,v in enumerate(request.form.getlist('qualification_subject')):
            if v is not None and len(v) > 0 and v != " ":
                try:
                    qual = {
                        "subject": v,
                        "year": request.form.getlist('qualification_year')[k],
                        "level": request.form.getlist('qualification_level')[k],
                        "grade": request.form.getlist('qualification_grade')[k]
                    }
                    if qual['year'] == "Third year":
                        thirdquals.append(qual)
                    elif qual['year'] == "Fourth year":
                        fourthquals.append(qual)
                    elif qual['year'] == "Fifth year":
                        fifthquals.append(qual)
                    elif qual['year'] == "Sixth year":
                        sixthquals.append(qual)
                    else:
                        otherquals.append(qual)
                except:
                    pass
        rec['qualifications'] = thirdquals + fourthquals + fifthquals + sixthquals + otherquals

        for k,v in enumerate(request.form.getlist('interest_title')):
            if v is not None and len(v) > 0 and v != " ":
                try:
                    rec["interests"].append({
                        "title":v,
                        'brief_description': util.dewindows(request.form.getlist('interest_brief_description')[k])                        
                    })
                except:
                    pass
        for k,v in enumerate(request.form.getlist('application_subject')):
            if v is not None and len(v) > 0 and v != " ":
                try:
                    appn = {
                        "subject": v,
                        "institution": request.form.getlist('application_institution')[k],
                        "level": request.form.getlist('application_level')[k]
                    }
                    try:
                        appn['appid'] = request.form.getlist('application_appid')[k]
                        if 'appid' not in appn or appn['appid'] == "": appn['appid'] = Student.makeid()
                    except:
                        appn['appid'] = Student.makeid()
                    try:
                        appn['pae_reply_received'] = request.form.getlist('application_pae_reply_received')[k]
                    except:
                        pass
                    try:
                        appn['consider'] = request.form.getlist('application_consider')[k]
                    except:
                        pass
                    try:
                        appn['conditions'] = request.form.getlist('application_conditions')[k]
                    except:
                        pass
                    try:
                        appn['summer_school'] = request.form.getlist('application_summer_school')[k]
                    except:
                        pass
                    try:
                        appn['pae_emailed'] = request.form.getlist('application_pae_emailed')[k]
                    except:
                        pass
                    try:
                        appn['qid'] = request.form.getlist('application_qid')[k]
                    except:
                        pass
                    try:
                        if request.form.getlist('application_pae_requested')[k] == "Yes":
                            qid = uuid.uuid4().hex
                            rec['paequals'][qid] = rec['qualifications']
                            appn['qid'] = qid
                            if 'pae_requested' not in appn:
                                appn['pae_requested'] = datetime.now().strftime("%d/%m/%Y")
                            if '_process_paes_date' not in rec:
                                rec['_process_paes_date'] = datetime.now().strftime("%d/%m/%Y")
                                if rec['status'] == 'paes_all_received':
                                    rec['status'] = 'paes_in_progress'
                                if rec['status'] not in ['paes_in_progress']:
                                    rec['status'] = 'paes_requested'
                        elif request.form.getlist('application_pae_requested')[k] != "No":
                            appn['pae_requested'] = request.form.getlist('application_pae_requested')[k]
                    except:
                        pass
                    rec["applications"].append(appn)
                except:
                    pass
        for k,v in enumerate(request.form.getlist('experience_title')):
            if v is not None and len(v) > 0 and v != " ":
                try:
                    rec["experience"].append({
                        "title":v,
                        "date_from": request.form.getlist('experience_date_from')[k],
                        "date_to": request.form.getlist('experience_date_to')[k],
                        'brief_description': util.dewindows(request.form.getlist('experience_brief_description')[k])
                    })
                except:
                    pass

        if self.id is not None: rec['id'] = self.id
        self.data = rec
        self.save()
Пример #2
0
def download_csv(recordlist,keys):
    # re-order some of the keys
    if 'simd_pc' in keys:
        keys.remove('simd_pc')
        keys = ['simd_pc'] + keys
    if 'simd_decile' in keys:
        keys.remove('simd_decile')
        keys = ['simd_decile'] + keys
    if 'post_code' in keys:
        keys.remove('post_code')
        keys = ['post_code'] + keys
    if 'local_authority' in keys:
        keys.remove('local_authority')
        keys = ['local_authority'] + keys
    if 'shep_school' in keys:
        keys.remove('shep_school')
        keys = ['shep_school'] + keys
    if 'leaps_category' in keys:
        keys.remove('leaps_category')
        keys = ['leaps_category'] + keys
    if 'school' in keys:
        keys.remove('school')
        keys = ['school'] + keys
    if 'post_code' in keys:
        keys.remove('post_code')
        keys = ['post_code'] + keys
    if 'address' in keys:
        keys.remove('address')
        keys = ['address'] + keys
    if 'gender' in keys:
        keys.remove('gender')
        keys = ['gender','gender_other'] + keys
    if 'date_of_birth' in keys:
        keys.remove('date_of_birth')
        keys = ['date_of_birth'] + keys
    if 'last_name' in keys:
        keys.remove('last_name')
        keys = ['last_name'] + keys
    if 'first_name' in keys:
        keys.remove('first_name')
        keys = ['first_name'] + keys
    if 'applications' in keys:
        keys.remove('applications')
        keys = keys + ['applications','pae_requested','pae_replied','pae_consider','pae_conditions']

    # make a csv string of the records
    csvdata = StringIO.StringIO()
    firstrecord = True
    for record in recordlist:
        # for the first one, put the keys on the first line, otherwise just newline
        if firstrecord:
            fk = True
            for key in keys:
                if fk:
                    fk = False
                else:
                    csvdata.write(',')
                if key == 'simd_pc':
                    csvdata.write('"simd %"')
                elif key == 'summer_school':
                    csvdata.write('"summer school interest"')
                else:
                    csvdata.write('"' + key + '"')
            csvdata.write('\n')
            firstrecord = False
        else:
            csvdata.write('\n')
        # and then add each record as a line with the keys as chosen by the user
        firstkey = True
        for key in keys:
            if firstkey:
                firstkey = False
            elif key not in ['pae_requested','pae_replied','pae_consider','pae_conditions','gender_other']:
                csvdata.write(',')
            if (key in record.keys() or key in ['address','simd_pc']) and key != 'gender_other':
                if key == 'applications':
                    appns = ""
                    reqs = ""
                    repls = ""
                    cons = ""
                    conds = ""
                    firstline = True
                    for line in record[key]:
                        if firstline:
                            splitter = ""
                            firstline = False
                        else:
                            splitter = '\n'
                        appns += splitter + line['level'] + " " + line['subject'] + " at " + line['institution']
                        if 'pae_requested' in line:
                            reqs += splitter + line['pae_requested']
                        else:
                            reqs += splitter
                        if line.get('pae_reply_received',"") != "":
                            repls += splitter + line['pae_reply_received']
                            try:
                                cons += splitter + line['consider']
                            except:
                                cons += splitter
                            try:
                                conds += splitter + fixify(line['conditions'])
                            except:
                                conds += splitter
                    tidykey = appns + '","' + reqs + '","' + repls + '","' + cons + '","' + conds
                elif key in ['interests','qualifications','experience']:
                    tidykey = ""
                    firstline = True
                    for line in record[key]:
                        if firstline:
                            firstline = False
                        else:
                            tidykey += '\n'
                        if key == 'interests':
                            tidykey += fixify(line['title']) + " - " + fixify(line['brief_description'])
                        elif key =='qualifications':
                            tidykey += line['year'] + " grade " + line['grade'] + " in " + line['level'] + " " + line['subject']
                        elif key == 'experience':
                            tidykey += line['date_from'] + " to " + line['date_to'] + " " + fixify(line['title']) + " - " + fixify(line['brief_description'])
                elif key == 'address':
                    if record.get('address_line_1',False):
                        tidykey = fixify(record['address_line_1']) + '\r\n'
                    else:
                        tidykey = ''
                    if record.get('address_line_2',False):
                        tidykey += fixify(record['address_line_2']) + '\r\n'
                    if record.get('city',False):
                        tidykey += fixify(record['city'])
                elif key == 'simd_pc':
                    if record.get('simd_pc',False):
                        tidykey = record['simd_pc']
                    else:
                        try:
                            dec = int(record['simd_decile'])
                            if dec == 10 and int(record.get('simd_quintile',0)) == 5:
                                dec = 100
                            elif dec < 10:
                                dec = dec * 10
                            tidykey = str(dec)
                        except:
                            tidykey = ''
                elif key == 'gender':
                    tidykey = ''
                    if datetime(int(record['created_date'].split('-')[0]),int(record['created_date'].split('-')[1]),int(record['created_date'].split('-')[2].split(' ')[0])) > datetime(2018,6,1):
                        if record[key] == 'Male':
                            tidykey = 'Man / Male (including trans man)'
                        elif record[key] == 'Female':
                            tidykey = 'Woman / Female (including trans woman)'
                        elif record[key] == 'Other':
                            tidykey = 'In another way'
                        elif record[key] == 'Do not wish to disclose':
                            tidykey = 'Prefer not to say'
                        else:
                            tidykey = record[key]
                    else:
                        tidykey = record.get(key,'')
                    tidykey += '","' + fixify(record.get('gender_other','').replace('"',''))
                else:
                    if isinstance(record[key],bool):
                        if record[key]:
                            tidykey = "true"
                        else:
                            tidykey = "false"
                    else:
                        tidykey = fixify(record[key].replace('"',"'"))
                if record['archive'] in ['2012_2013'] and key != "applications":
                    try:
                        tidykey = util.dewindows(tidykey)
                    except:
                        pass
                    try:
                        tidykey = fixify(tidykey)
                    except:
                        pass
                try:
                    csvdata.write('"' + tidykey + '"')
                except:
                    print "errored on writing a key to the csvdata, probably because of ascii error"
            elif key not in ['pae_requested','pae_replied','pae_consider','pae_conditions','gender_other']:
                csvdata.write("")
    # dump to the browser as a csv attachment
    csvdata.seek(0)
    return send_file(
        csvdata, 
        mimetype='text/csv',
         attachment_filename="leaps_export_" + datetime.now().strftime("%d%m%Y%H%M") + ".csv",
        as_attachment=True
    )