def download_resume(resume_id, file_format): """Download resume in prescribed format""" # TODO: abstract this; maybe a decorator? resume = Resume.query.filter_by(id=resume_id, user=current_user).first() if not resume or file_format not in ResumeBabel.get_supported_formats(): abort(404) # Abstract resume file operations. Maybe attach to resume object resume_name = '%d.json' % (resume_id) resume_path = os.path.join(app.config['RESUME_FOLDER'], resume_name) file_name = '%d.%s' % (resume_id, file_format) file_path = os.path.join(app.config['RESUME_FOLDER'], file_name) resume_json = None if os.path.isfile(resume_path): resume_json = open(resume_path, 'r').read() if not os.path.isfile(file_path): out_fd = open(file_path, 'w') r = ResumeBabel(resume_json) r.export_resume(out_fd, file_format) out_fd.close() as_attachment = False if request.args.get('download'): as_attachment = True # TODO: mimetype='application/json' ???? return send_from_directory( app.config['RESUME_FOLDER'], file_name, attachment_filename=('resume.' + file_format), cache_timeout=1, as_attachment=as_attachment)
def resumes(): """ Resume management. Screen is gateway to perform the following actions: 1. Create new resume 2. Clone existing resume 3. Delete resume 4. Rename resume 5. Download resume in a specific format (eg, html or txt) """ # Create new resume by POSTing to this route # TODO: abstract this if-statement into post(), get() methods if request.method == 'POST': # TODO: form validation; whitespace, length, etc resume = Resume(request.form['title'], current_user) db.session.add(resume) db.session.commit() # TODO: abstract this "?api=1" functionality. Write api w/ subdomain? if request.args.get('api'): return jsonify(response='OK') # Display all of the user's resumes on this screen resumes = Resume.query.filter_by(user=current_user).all() return render_template('resumes.html', resumes=resumes, has_js=True, formats=ResumeBabel.get_supported_formats())
def resume(resume_id): """Edit an individual resume""" # TODO: abstract this; maybe a decorator? resume = Resume.query.filter_by(id=resume_id, user=current_user).first() if not resume: abort(404) # TODO: we only support POST if "?api=1". Is this necessary? # abstract exceptions to "flash" message for non-api if request.method == 'POST': if request.args.get('api'): try: if len(request.form['resume']) > 65535: raise Exception('Resume json can be no more than 64k') new_resume = json.loads(request.form['resume']) # TODO: (more?) properly validate json required_fields = {'contact': {}, 'education': [], 'experiences': {}} for field, field_type in required_fields.iteritems(): value = new_resume.get(field, None) if value is None or type(value) != type(field_type): raise Exception('Resume must have these fields: ' + str(required_fields)) # Delete the user's existing resumes # TODO: this is repeated code. refactor. to_delete = glob.glob( '%s/%d.*' % (app.config['RESUME_FOLDER'], resume_id)) for filename in to_delete: os.unlink(filename) # Then save the new resume json # TODO: abstract this resume/file creation bit resume_name = '%d.json' % (resume_id) resume_path = os.path.join( app.config['RESUME_FOLDER'], resume_name) fd = open(resume_path, 'w') fd.write(request.form['resume']) fd.close() return jsonify(response='OK', error=[]) except Exception as ex: return jsonify(response='Error', error=[str(ex)]) return render_template('resume.html', has_js=True, get_resume=True, formats=ResumeBabel.get_supported_formats())
import collections import json import os import sys from resumebabel.resumebabel import ResumeBabel if __name__ == '__main__': input_json = sys.argv[1] output_file = sys.argv[2] # TODO: make sure output_format is not empty output_format = (os.path.splitext(output_file)[1])[1:] resume = open(input_json).read() r = ResumeBabel(resume) out_fd = open(output_file, 'wb') out_fd.write(r.export_resume(output_format)) out_fd.close()
import collections import json import os import sys from resumebabel.resumebabel import ResumeBabel if __name__ == '__main__': input_json = sys.argv[1] output_file = sys.argv[2] # TODO: make sure output_format is not empty output_format = (os.path.splitext(output_file)[1])[1:] resume = open(input_json).read() r = ResumeBabel(resume) out_fd = open(output_file, 'wb') r.export_resume(out_fd, output_format) out_fd.close()