Browse Source

added function comments and code cleanup

master
crunk 4 years ago
parent
commit
87b1348ddd
  1. 3
      library/borrowform.py
  2. 14
      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 from wtforms.validators import Length
class BorrowForm(FlaskForm): class BorrowForm(FlaskForm):
"""Borrow a book form.""" """Borrow a publication form."""
borrowed = StringField( borrowed = StringField(
"Fill in your name if you're going to borrow this publication.", "Fill in your name if you're going to borrow this publication.",

14
library/csvparser/csvparser.py

@ -25,6 +25,7 @@ fieldnames = [
def parsecsv(): def parsecsv():
"""Test function to inspect csv file as dict"""
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r") libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv: with libcsv:
csv_as_dict = csv.DictReader(libcsv) csv_as_dict = csv.DictReader(libcsv)
@ -32,6 +33,7 @@ def parsecsv():
def getpublications(): def getpublications():
"""get an overview of all publications for the main page"""
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r") libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv: with libcsv:
csv_as_dict = csv.DictReader(libcsv) csv_as_dict = csv.DictReader(libcsv)
@ -54,9 +56,8 @@ def getpublications():
def hasimage(id): 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_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): if os.path.exists(image_jpg):
return True return True
else: else:
@ -64,6 +65,7 @@ def hasimage(id):
def gettypes(): def gettypes():
"""for the dynamic menu get the unique types of publicatons"""
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r") libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv: with libcsv:
csv_as_dict = csv.DictReader(libcsv) csv_as_dict = csv.DictReader(libcsv)
@ -76,6 +78,7 @@ def gettypes():
def getyears(): def getyears():
"""for the dynamic menu get the unique years for publicatons"""
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r") libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv: with libcsv:
csv_as_dict = csv.DictReader(libcsv) csv_as_dict = csv.DictReader(libcsv)
@ -91,6 +94,7 @@ def getyears():
def getlicenses(): def getlicenses():
"""for the dynamic menu get the unique liscenses for publicatons"""
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r") libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv: with libcsv:
csv_as_dict = csv.DictReader(libcsv) csv_as_dict = csv.DictReader(libcsv)
@ -105,6 +109,7 @@ def getlicenses():
def getfieldsofinterest(): 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") libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv: with libcsv:
csv_as_dict = csv.DictReader(libcsv) csv_as_dict = csv.DictReader(libcsv)
@ -116,6 +121,7 @@ def getfieldsofinterest():
def getpublicationfromcsvrow(row): def getpublicationfromcsvrow(row):
"""get entire publication info from a csv row"""
year = row["Year"] year = row["Year"]
if not year: if not year:
year = "Unknown" year = "Unknown"
@ -146,6 +152,7 @@ def getpublicationfromcsvrow(row):
def getfullpublication(pubid): def getfullpublication(pubid):
"""For the single book view, most complete overview"""
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r") libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
pubinfo = {} pubinfo = {}
with libcsv: with libcsv:
@ -159,6 +166,7 @@ def getfullpublication(pubid):
def generatenewpublicationid(): def generatenewpublicationid():
"""When uploading a book generate a new unique ID"""
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r") libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
allidsincsv = [] allidsincsv = []
with libcsv: with libcsv:
@ -169,6 +177,7 @@ def generatenewpublicationid():
def writepublication(uploadform): def writepublication(uploadform):
"""When uploading a publication writes entry to the csv"""
id = generatenewpublicationid() id = generatenewpublicationid()
with open( with open(
os.path.join(script_dir, "varlib.csv"), os.path.join(script_dir, "varlib.csv"),
@ -196,6 +205,7 @@ def writepublication(uploadform):
def editborrowedby(pubid, borrower): def editborrowedby(pubid, borrower):
"""Edits the borrowed by field for a publication entry in csv"""
tempfile = NamedTemporaryFile('w+t', newline='', delete=False) tempfile = NamedTemporaryFile('w+t', newline='', delete=False)
filename = os.path.join(script_dir, "varlib.csv") filename = os.path.join(script_dir, "varlib.csv")
with open(filename, 'r', newline='') as libcsv, tempfile: with open(filename, 'r', newline='') as libcsv, tempfile:

12
library/page.py

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

1
library/uploadform.py

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

Loading…
Cancel
Save