Browse Source

added function comments and code cleanup

current_release
crunk 4 years ago
parent
commit
87b1348ddd
  1. 3
      library/borrowform.py
  2. 16
      library/csvparser/csvparser.py
  3. 12
      library/page.py
  4. 1
      library/uploadform.py

3
library/borrowform.py

@ -8,9 +8,8 @@ from wtforms import validators
from wtforms.validators import Length
class BorrowForm(FlaskForm):
"""Borrow a book form."""
"""Borrow a publication form."""
borrowed = StringField(
"Fill in your name if you're going to borrow this publication.",

16
library/csvparser/csvparser.py

@ -25,6 +25,7 @@ fieldnames = [
def parsecsv():
"""Test function to inspect csv file as dict"""
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv:
csv_as_dict = csv.DictReader(libcsv)
@ -32,6 +33,7 @@ def parsecsv():
def getpublications():
"""get an overview of all publications for the main page"""
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv:
csv_as_dict = csv.DictReader(libcsv)
@ -54,9 +56,8 @@ def getpublications():
def hasimage(id):
"""does this Id from the csv have an image uploaded"""
image_jpg = os.path.join(image_dir, "image-{0}.jpg".format(id))
image_png = os.path.join(image_dir, "image-{0}.png".format(id))
image_gif = os.path.join(image_dir, "image-{0}.gif".format(id))
if os.path.exists(image_jpg):
return True
else:
@ -64,6 +65,7 @@ def hasimage(id):
def gettypes():
"""for the dynamic menu get the unique types of publicatons"""
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv:
csv_as_dict = csv.DictReader(libcsv)
@ -76,6 +78,7 @@ def gettypes():
def getyears():
"""for the dynamic menu get the unique years for publicatons"""
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv:
csv_as_dict = csv.DictReader(libcsv)
@ -91,6 +94,7 @@ def getyears():
def getlicenses():
"""for the dynamic menu get the unique liscenses for publicatons"""
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv:
csv_as_dict = csv.DictReader(libcsv)
@ -105,6 +109,7 @@ def getlicenses():
def getfieldsofinterest():
"""for the R&R page get the fields of interest from the publicatons"""
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv:
csv_as_dict = csv.DictReader(libcsv)
@ -116,6 +121,7 @@ def getfieldsofinterest():
def getpublicationfromcsvrow(row):
"""get entire publication info from a csv row"""
year = row["Year"]
if not year:
year = "Unknown"
@ -146,6 +152,7 @@ def getpublicationfromcsvrow(row):
def getfullpublication(pubid):
"""For the single book view, most complete overview"""
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
pubinfo = {}
with libcsv:
@ -159,6 +166,7 @@ def getfullpublication(pubid):
def generatenewpublicationid():
"""When uploading a book generate a new unique ID"""
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
allidsincsv = []
with libcsv:
@ -169,6 +177,7 @@ def generatenewpublicationid():
def writepublication(uploadform):
"""When uploading a publication writes entry to the csv"""
id = generatenewpublicationid()
with open(
os.path.join(script_dir, "varlib.csv"),
@ -196,12 +205,13 @@ def writepublication(uploadform):
def editborrowedby(pubid, borrower):
"""Edits the borrowed by field for a publication entry in csv"""
tempfile = NamedTemporaryFile('w+t', newline='', delete=False)
filename = os.path.join(script_dir, "varlib.csv")
with open(filename, 'r', newline='') as libcsv, tempfile:
csv_as_dict = csv.DictReader(libcsv)
csv_as_writer = csv.DictWriter(tempfile, fieldnames=fieldnames)
#use the reader to read where, then writer to write the new row.
# use the reader to read where, then writer to write the new row.
csv_as_writer.writeheader()
for row in csv_as_dict:
if pubid == row["Id"]:

12
library/page.py

@ -28,7 +28,6 @@ from csvparser.csvparser import (
)
csrf = CSRFProtect()
APP = flask.Flask(__name__, static_folder="static")
APP.config['SECRET_KEY'] = 'ty4425hk54a21eee5719b9s9df7sdfklx'
@ -54,6 +53,7 @@ def index():
@APP.route("/upload", methods=["GET", "POST"])
def upload():
"""Upload route, a page to upload a book to the csv"""
uploadform = PublicationForm()
if request.method == 'POST':
if (uploadform.validate_on_submit() and
@ -69,7 +69,7 @@ def upload():
@APP.route("/<publicationID>", methods=["GET", "POST"])
def show_book(publicationID):
"""route for a publication, still needs to be made"""
"""route for a single publication, shows full info and allows borrowing"""
fullpublication = getfullpublication(publicationID)
borrowform = BorrowForm()
if request.method == 'POST':
@ -94,20 +94,21 @@ def show_book(publicationID):
@APP.route("/pastevents")
def pastevents():
"""show past events and book recommendations"""
"""show past R&R events and book recommendations"""
events = getevents()
return render_template("pastevents.html", events=events)
@APP.route("/upcoming")
def latestevent():
"""show upcoming or latest event and book recommendations"""
"""show upcoming or latest R&R events and book recommendations"""
event = getlatestevent()
return render_template("upcomingevent.html", event=event)
@APP.context_processor
def upcoming_or_latest():
"""determines wether the newest R&R event is upcoming or not"""
upcoming = True
ics = get("https://varia.zone/events.ics").text
gcal = Calendar.from_ical(ics)
@ -124,13 +125,16 @@ def upcoming_or_latest():
return dict(upcoming=upcoming)
def saveimage(image, id):
"""helper function that can save images"""
print(image.filename)
filename = secure_filename("image-{0}.jpg".format(id))
image.save(os.path.join("static/images/", filename))
def checksecret(secret):
"""small simple check to a secret, library group members can upload"""
with open("secret") as f:
secrethash = f.readline().rstrip()
if bcrypt.checkpw(secret.encode("utf-8"), secrethash.encode("utf-8")):

1
library/uploadform.py

@ -1,7 +1,6 @@
"""Form object declaration."""
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed
from werkzeug.utils import secure_filename
from wtforms import validators
from wtforms import (
StringField,

Loading…
Cancel
Save