def edit(request, prn): if "username" not in request.session: print "no session found" request.session['redirect'] = request.get_full_path() return our_redirect("/ldap_login") if prn != request.session['username']: print "prn", prn, type(prn) print "username", request.session['username'], type( request.session['username']) return HttpResponse('Please Edit ur own form :@') '''The problem with this view : We(I) are doing it the old-fashioned way. We(I) are not using the power of Models which allow automatic server-side validation -- i need to read on that. ''' #was prn passed and is it numeric really? if prn is not None and prn.isdigit() is True: #are we authorized to edit this ? #do we hv global edit privileges ? #kya ye hamara hi resume hai ? #do we hv the record ? s = student.objects.filter(pk=prn) #see the way to unzip a tuple that is returned by a func if len(s) is 0: return HttpResponse('This user doesnt exists') else: s = s[0] debugger("Resume found! Using it") #get all the records and tell us whether they were creatd or retrieved #have moved this to the student_info.models, because all Model info must come from there and tomo if we add a new model, we shouldn't have to come here to provide it's functionality. table = tables.get_tables(s) #get company specific required fields '''We are segregating the company Specific thigs into 3 sequences... a) the main table, which consists all rows of the main table structurw. Who are neither special kinds, nor are already filled. b) cs are the data filled by this partucular student. Main table fetches all the data to be collected per student. CS is the data ctually filled by the students. this is used to prefill the foem while editing.and the maintable is required for the "form" for a new user. ''' cs = companySpecificData.objects.filter( primary_table=s).order_by('valueOf') maintable = companySpecific.objects.exclude( fieldType='special').exclude( companyspecificdata__in=cs).order_by('displayText') print "CS ====", cs print "Maintable....", maintable table['mt'] = maintable table['cs'] = cs table['flag'] = 'edit' c = RequestContext(request, table) t = loader.get_template('student_info/form.html') return HttpResponse(t.render(c))
def edit(request,prn): if "username" not in request.session: print "no session found" request.session['redirect'] = request.get_full_path(); return our_redirect("/ldap_login") if prn != request.session['username']: print "prn", prn, type(prn) print "username", request.session['username'],type(request.session['username']) return HttpResponse('Please Edit ur own form :@') '''The problem with this view : We(I) are doing it the old-fashioned way. We(I) are not using the power of Models which allow automatic server-side validation -- i need to read on that. ''' #was prn passed and is it numeric really? if prn is not None and prn.isdigit() is True: #are we authorized to edit this ? #do we hv global edit privileges ? #kya ye hamara hi resume hai ? #do we hv the record ? s = student.objects.filter(pk=prn); #see the way to unzip a tuple that is returned by a func if len(s) is 0: return HttpResponse('This user doesnt exists'); else: s=s[0] debugger("Resume found! Using it"); #get all the records and tell us whether they were creatd or retrieved #have moved this to the student_info.models, because all Model info must come from there and tomo if we add a new model, we shouldn't have to come here to provide it's functionality. table=tables.get_tables(s) #get company specific required fields '''We are segregating the company Specific thigs into 3 sequences... a) the main table, which consists all rows of the main table structurw. Who are neither special kinds, nor are already filled. b) cs are the data filled by this partucular student. Main table fetches all the data to be collected per student. CS is the data ctually filled by the students. this is used to prefill the foem while editing.and the maintable is required for the "form" for a new user. ''' cs=companySpecificData.objects.filter(primary_table=s).order_by('valueOf') maintable=companySpecific.objects.exclude(fieldType='special').exclude(companyspecificdata__in = cs).order_by('displayText'); print "CS ====",cs print "Maintable....",maintable table['mt']=maintable; table['cs']=cs; table['flag']='edit'; c = RequestContext(request,table); t = loader.get_template('student_info/form.html'); return HttpResponse(t.render(c));
def pisapdf(request, prn): if prn is not None: s = student.objects.get(pk=prn) else: return HttpResponse("Pass me a prn, dude!") try: #have we ever recorded a pdf creation for this student ? r, new_record_created = resume.objects.get_or_create(prn=s) pdf_file_name = "%s/%s/%s.pdf" % (RESUME_STORE, prn, prn) got_file = False if not new_record_created: #yeah we have already created, but does it exist ? if (path.exists(pdf_file_name)) and (datetime.fromtimestamp( path.getmtime(pdf_file_name)) >= s.last_update): #the file exists and is fresher than the student's update time stamp. pdf_file = open(pdf_file_name) got_file = True debugger("Using old file at %s" % pdf_file_name) #the file doesn't exist OR the file is stale than the student data OR no record of ever creating a pdf for this user exists: debugger("No old file found, creating fresh!") if not got_file: try: data = tables.get_tables(s) t = loader.get_template('moderncv/pdfview.html') c = Context(data) c.update({'pagesize': 'A4'}) html = t.render(c) result = StringIO.StringIO() pdf = pisa.pisaDocument( StringIO.StringIO(html.encode("utf-8", 'replace')), result) #save the file in temp location f = open("/tmp/%s.pdf" % prn, 'w+b') f.write(result.getvalue()) f.close() del result #to clean memory #move the pdf to permanent location destination_dir = '%s/%s/' % (RESUME_STORE, prn) try: debugger("Trying to change in %s" % destination_dir) chdir(destination_dir ) #if we can't change into it, create it! except OSError: mkdir(destination_dir) finally: chdir(FULL_PATH) copy_pdf_command = "cp -v /tmp/%s.pdf %s/%s/%s.pdf" % ( prn, RESUME_STORE, prn, prn) #copy the .pdf to the user's directory in STORE so that we can reuse it get_done(copy_pdf_command) print "Updating timestamp for the PDF generation in our records" r.last_pdf_generated = datetime.now() r.save() print "Using newly created pdf file at %s" % pdf_file_name except Exception as e: traceback.print_exc() print "Exception occurred when creating a fresh pdf : %s" % e return HttpResponse( "Error when generating the resume for %s " % prn) except Exception as e: traceback.print_exc() print "Exception occurred when in pisapdf : %s" % e return HttpResponse("Error when getting resume for %s " % prn) #open the pdf file print "Opening the file at : %s" % pdf_file_name resume_pdf = open(pdf_file_name) #prepare the file to be sent response = HttpResponse(resume_pdf.read(), mimetype="application/pdf") resume_pdf.close() #name the file properly response[ 'Content-Disposition'] = "attachment; filename=SICSR_%s_resume.pdf" % s.fullname return response
def pisapdf(request,prn,return_PDF_directly=True): '''generates a PDF resume using the HTML template by using the pisaPDF generation library. if return_PDF_directly is True, it will return the PDF in the request if return_PDF_directly is False, it will simply return the filesystem path for the PDF. ''' if prn is not None: s = student.objects.get(pk=prn); else: return HttpResponse("Pass me a prn, dude!"); try: #have we ever recorded a pdf creation for this student ? r,new_record_created = resume.objects.get_or_create(prn=s); pdf_file_name = "%s/%s.pdf" % (RESUME_STORE, prn); got_file = False; if not new_record_created: #yeah we have already created, but does it exist ? if (path.exists(pdf_file_name)) and (datetime.fromtimestamp(path.getmtime(pdf_file_name)) >= s.last_update): #the file exists and is fresher than the student's update time stamp. pdf_file = open(pdf_file_name); got_file = True; debugger("Using old file at %s" % pdf_file_name); #the file doesn't exist OR the file is stale than the student data OR no record of ever creating a pdf for this user exists: debugger("No old file found, creating fresh!") if not got_file: try: data = tables.get_tables(s); t = loader.get_template('moderncv/pdfview.html') c = Context(data); c.update({'pagesize':'A4'}); html = t.render(c) result = StringIO.StringIO(); pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("utf-8",'replace')),result); #save the file in temp location f = open("/tmp/%s.pdf" % prn,'w+b'); f.write(result.getvalue()); f.close(); del result #to clean memory #move the pdf to permanent location destination_dir = '%s/%s/' % (RESUME_STORE, prn) try: debugger("Trying to change in %s" % destination_dir); chdir(destination_dir) #if we can't change into it, create it! except OSError: mkdir(destination_dir); finally: chdir(FULL_PATH); copy_pdf_command = "cp -v /tmp/%s.pdf %s/%s.pdf" % (prn, RESUME_STORE,prn); #copy the .pdf to the user's directory in STORE so that we can reuse it get_done(copy_pdf_command); print "Updating timestamp for the PDF generation in our records" r.last_pdf_generated = datetime.now(); r.save(); print "Using newly created pdf file at %s" % pdf_file_name except Exception as e: traceback.print_exc(); print "Exception occurred when creating a fresh pdf : %s" % e; return HttpResponse("Error when generating the resume for %s " % prn); except Exception as e: traceback.print_exc(); print "Exception occurred when in pisapdf : %s" % e; return HttpResponse("Error when getting resume for %s " % prn ); if return_PDF_directly is True: #open the pdf file print "Opening the file at : %s" % pdf_file_name resume_pdf = open(pdf_file_name); #prepare the file to be sent response = HttpResponse(resume_pdf.read(), mimetype="application/pdf"); resume_pdf.close(); #name the file properly response['Content-Disposition'] = "attachment; filename=SICSR_%s_resume.pdf" % s.fullname; else: response = pdf_file_name return response;