Python Source Code: fedit.py
#!/usr/bin/env python
import cgi
import textwrap
import os
import common
#----------------------------------------------------------
# The Web Language Project
# Mark Brautigam
# May-June 2015
# http://www.mixed-up.com/markb/
#----------------------------------------------------------
# Get the ID field from the hidden form field. It is required
# for any further processing. ID = line number.
#
form = cgi.FieldStorage()
id = form.getvalue('id', '')
if (id == '') :
print ("Location: fread.py\n\n");
id = int(id)
# Make sure the file has at least this many lines, otherwise return.
#
f = open ('../data/courses.txt', 'r')
courses = f.readlines()
ncourses = len(courses)
if ((id < 0) or (id > ncourses)) :
print ("Location: fread.py\n\n");
college = form.getvalue('college', '')
dept = form.getvalue('dept', '')
course = form.getvalue('course', '')
desc = form.getvalue('desc', '')
submit = form.getvalue('submit', '')
common.headers1();
common.headers2();
print common.sidebarX("python", "fedit", "py");
coursedata = courses[id].strip().split("|")
print textwrap.dedent("""\
<div id="content">
<h2>Python: Edit the data file</h2>
""")
#-------------------------------------------------------------
def printForm (coursedata) :
#-------------------------------------------------------------
print textwrap.dedent("""\
<form name='coursesform' action='fedit.py' method='POST'>
<fieldset>
<legend>Edit a course</legend>
<table>
""")
#print ("<tr><td>College:</td><td><input type='text' name='college' value='{}' /></td></tr>".format(coursedata[0]));
#print ("<tr><td>Department:</td><td><input type='text' name='dept' value='{}' /></td></tr>".format(coursedata[1]));
#print ("<tr><td>Course #:</td><td><input type='text' name='course' value='{}' /></td></tr>".format(coursedata[2]));
#print ("<tr><td>Course name:</td><td><input type='text' name='desc' value='{}' /></td></tr>".format(coursedata[3]));
#print ("<input type='hidden' name='id' value='{}' />".format(id));
print ("<tr><td>College:</td><td><input type='text' name='college' value='%s' /></td></tr>" % (coursedata[0]));
print ("<tr><td>Department:</td><td><input type='text' name='dept' value='%s' /></td></tr>" % (coursedata[1]));
print ("<tr><td>Course #:</td><td><input type='text' name='course' value='%s' /></td></tr>" % (coursedata[2]));
print ("<tr><td>Course name:</td><td><input type='text' name='desc' value='%s' /></td></tr>" % (coursedata[3]));
print ("<input type='hidden' name='id' value='%s' />" % (id));
print textwrap.dedent("""\
<tr><td></td><td><input type='submit' id='submit' name='submit' value='Edit' /></td></tr>
</table>
</fieldset>
</form>
""")
#-------------------------------------------------------------
def formHasData (formData) :
#-------------------------------------------------------------
nData = len(formData)
for i in range (0, nData) :
if (formData[i].strip() != '') :
return True;
return False;
#-------------------------------------------------------------
def printFeedback (coursedata, college, dept, course, desc) :
#-------------------------------------------------------------
print "<p>The following data was edited in the data file: </p>";
print "<table class='results'>\n";
print " <tr><th>Field</th><th>Old data</th><th>New data</th></tr>"
#print " <tr><td>College</td><td>{}</td><td>{}</td></tr>".format(coursedata[0],college);
#print " <tr><td>Department</td><td>{}</td><td>{}</td></tr>".format(coursedata[1],dept);
#print " <tr><td>Course #</td><td>{}</td><td>{}</td></tr>".format(coursedata[2],course);
#print " <tr><td>Course name</td><td>{}</td><td>{}</td></tr>".format(coursedata[3],desc);
print " <tr><td>College</td><td>%s</td><td>%s</td></tr>" % (coursedata[0],college);
print " <tr><td>Department</td><td>%s</td><td>%s</td></tr>" % (coursedata[1],dept);
print " <tr><td>Course #</td><td>%s</td><td>%s</td></tr>" % (coursedata[2],course);
print " <tr><td>Course name</td><td>%s</td><td>%s</td></tr>" % (coursedata[3],desc);
print "</table>"
#-------------------------------------------------------------
# The main Python code is here.
# Main code must be after all the functions.
#-------------------------------------------------------------
printForm(coursedata);
if (submit == 'Edit' and formHasData([college, dept, course, desc])) :
# write the new data to a temp file
#
# Beware. The print function above includes CR by default.
# BUT the fp.write function below does not include the CR.
fp = open ('../data/courses.tmp', 'w')
for i in range (0, ncourses) :
if (i == id) :
#dataToWrite = "{}|{}|{}|{}\n".format(college,dept,course,desc)
dataToWrite = "%s|%s|%s|%s\n" % (college,dept,course,desc)
#print (dataToWrite)
#fp.write ("{}".format(dataToWrite))
fp.write ("%s" % (dataToWrite))
else :
#fp.write ("{}".format(courses[i]))
fp.write ("%s" % (courses[i]))
fp.close
os.rename ("../data/courses.txt", "../data/courses.bak");
os.rename ("../data/courses.tmp", "../data/courses.txt");
printFeedback (coursedata, college, dept, course, desc);
#-------------------------------------------------------------
print textwrap.dedent("""\
<p><a href='fread.py'>Show the contents of this file »</a></p>
<p><a href='source.py?f=4'>Show Python source code »</a>
</div>
""")
common.footers();