You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
236 lines
7.3 KiB
236 lines
7.3 KiB
"""This parses the varlib.csv but only in a way
|
|
that is actually useful for the site"""
|
|
import csv
|
|
import os
|
|
import shutil
|
|
from tempfile import NamedTemporaryFile
|
|
|
|
script_dir = os.path.dirname(__file__)
|
|
data_dir = os.path.abspath(os.path.join(script_dir, "../data"))
|
|
image_dir = os.path.abspath(os.path.join(script_dir, "../static/images"))
|
|
fieldnames = [
|
|
"Id",
|
|
"Publication",
|
|
"Author",
|
|
"Year",
|
|
"Custodian",
|
|
"Fields",
|
|
"Type",
|
|
"Publishers",
|
|
"License",
|
|
"LicenseShort",
|
|
"Highlights",
|
|
"Comments",
|
|
"Currently borrowed by",
|
|
]
|
|
|
|
|
|
def parsecsv():
|
|
"""Test function to inspect csv file as dict"""
|
|
libcsv = open(os.path.join(data_dir, "varlib.csv"), "r")
|
|
with libcsv:
|
|
csv_as_dict = csv.DictReader(libcsv)
|
|
return csv_as_dict
|
|
|
|
|
|
def getpublications():
|
|
"""get an overview of all publications for the main page"""
|
|
libcsv = open(os.path.join(data_dir, "varlib.csv"), "r")
|
|
with libcsv:
|
|
csv_as_dict = csv.DictReader(libcsv)
|
|
publications = {}
|
|
for row in csv_as_dict:
|
|
year = row["Year"]
|
|
if not year:
|
|
year = "Unknown"
|
|
|
|
pubinfo = {
|
|
"Title": row["Publication"],
|
|
"Author": row["Author"],
|
|
"Type": row["Type"].lower().title(),
|
|
"Year": year,
|
|
"License": row["LicenseShort"].lower().title(),
|
|
"Image": hasimage(row["Id"]),
|
|
}
|
|
publications[row["Id"]] = pubinfo
|
|
return publications
|
|
|
|
|
|
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))
|
|
if os.path.exists(image_jpg):
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
|
|
def gettypes():
|
|
"""for the dynamic menu get the unique types of publicatons"""
|
|
libcsv = open(os.path.join(data_dir, "varlib.csv"), "r")
|
|
with libcsv:
|
|
csv_as_dict = csv.DictReader(libcsv)
|
|
listoftypes = []
|
|
for row in csv_as_dict:
|
|
lowertype = row["Type"].lower().title()
|
|
if lowertype not in listoftypes:
|
|
listoftypes.append(lowertype)
|
|
return listoftypes
|
|
|
|
|
|
def getyears():
|
|
"""for the dynamic menu get the unique years for publicatons"""
|
|
libcsv = open(os.path.join(data_dir, "varlib.csv"), "r")
|
|
with libcsv:
|
|
csv_as_dict = csv.DictReader(libcsv)
|
|
listofyears = []
|
|
for row in csv_as_dict:
|
|
uniqueyear = row["Year"]
|
|
if not uniqueyear:
|
|
uniqueyear = "Unknown"
|
|
if uniqueyear not in listofyears:
|
|
listofyears.append(uniqueyear)
|
|
listofyears.sort()
|
|
return listofyears
|
|
|
|
|
|
def getlicenses():
|
|
"""for the dynamic menu get the unique liscenses for publicatons"""
|
|
libcsv = open(os.path.join(data_dir, "varlib.csv"), "r")
|
|
with libcsv:
|
|
csv_as_dict = csv.DictReader(libcsv)
|
|
listoflicenses = []
|
|
for row in csv_as_dict:
|
|
license = row["LicenseShort"].lower().title()
|
|
if not license:
|
|
license = "No License Mentioned"
|
|
if license not in listoflicenses:
|
|
listoflicenses.append(license)
|
|
return listoflicenses
|
|
|
|
|
|
def getfieldsofinterest():
|
|
"""for the R&R page get the fields of interest from the publicatons"""
|
|
libcsv = open(os.path.join(data_dir, "varlib.csv"), "r")
|
|
with libcsv:
|
|
csv_as_dict = csv.DictReader(libcsv)
|
|
fieldsofinterest = {}
|
|
for row in csv_as_dict:
|
|
fields = row["Fields"].split(",")
|
|
fieldsofinterest[row["Id"]] = fields
|
|
return fieldsofinterest
|
|
|
|
|
|
def getpublicationfromcsvrow(row):
|
|
"""get entire publication info from a csv row"""
|
|
year = row["Year"]
|
|
if not year:
|
|
year = "Unknown"
|
|
|
|
license = row["License"]
|
|
if not license:
|
|
license = "No license mentioned"
|
|
|
|
borrowed = row["Currently borrowed by"]
|
|
if not borrowed:
|
|
borrowed = "No one"
|
|
|
|
pubinfo = {
|
|
"Title": row["Publication"],
|
|
"Author": row["Author"],
|
|
"Year": year,
|
|
"Custodian": row["Custodian"],
|
|
"Fields": row["Fields"],
|
|
"Type": row["Type"],
|
|
"Publishers": row["Publishers"],
|
|
"License": license,
|
|
"Highlights": row["Highlights"],
|
|
"Comments": row["Comments"],
|
|
"Borrowed": borrowed,
|
|
"Image": hasimage(row["Id"]),
|
|
}
|
|
return pubinfo
|
|
|
|
|
|
def getfullpublication(pubid):
|
|
"""For the single book view, most complete overview"""
|
|
libcsv = open(os.path.join(data_dir, "varlib.csv"), "r")
|
|
pubinfo = {}
|
|
with libcsv:
|
|
csv_as_dict = csv.DictReader(libcsv)
|
|
for row in csv_as_dict:
|
|
if pubid == row["Id"]:
|
|
pubinfo = getpublicationfromcsvrow(row)
|
|
|
|
# print(pubinfo)
|
|
return pubinfo
|
|
|
|
|
|
def generatenewpublicationid():
|
|
"""When uploading a book generate a new unique ID"""
|
|
libcsv = open(os.path.join(data_dir, "varlib.csv"), "r")
|
|
allidsincsv = []
|
|
with libcsv:
|
|
csv_as_dict = csv.DictReader(libcsv)
|
|
for row in csv_as_dict:
|
|
allidsincsv.append(int(row["Id"]))
|
|
return str(max(allidsincsv) + 1)
|
|
|
|
|
|
def writepublication(uploadform):
|
|
"""When uploading a publication writes entry to the csv"""
|
|
id = generatenewpublicationid()
|
|
with open(
|
|
os.path.join(data_dir, "varlib.csv"), "a", newline=""
|
|
) as csvfile:
|
|
csv_as_writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
|
|
csv_as_writer.writerow(
|
|
{
|
|
"Id": id,
|
|
"Publication": uploadform.uploadpublication.data,
|
|
"Author": uploadform.author.data,
|
|
"Year": uploadform.year.data,
|
|
"Custodian": uploadform.custodian.data,
|
|
"Fields": uploadform.fields.data,
|
|
"Type": uploadform.type.data,
|
|
"Publishers": uploadform.publishers.data,
|
|
"License": uploadform.license.data,
|
|
"LicenseShort": uploadform.licenseshort.data,
|
|
"Highlights": uploadform.highlights.data,
|
|
"Comments": uploadform.comments.data,
|
|
"Currently borrowed by": uploadform.borrowed.data,
|
|
}
|
|
)
|
|
print("succesfully written book to csv")
|
|
return id
|
|
|
|
|
|
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(data_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.
|
|
csv_as_writer.writeheader()
|
|
for row in csv_as_dict:
|
|
if pubid == row["Id"]:
|
|
print("publication changes borrower")
|
|
print(row["Publication"])
|
|
row["Currently borrowed by"] = borrower
|
|
csv_as_writer.writerow(row)
|
|
|
|
shutil.move(tempfile.name, filename)
|
|
|
|
|
|
def concatenate_csv_row(row):
|
|
rowcontent = []
|
|
rowcontent.append(row["Publication"])
|
|
rowcontent.append(row["Author"])
|
|
rowcontent.append(row["Fields"])
|
|
rowcontent.append(row["Type"])
|
|
rowcontent.append(row["Publishers"])
|
|
rowcontent.append(row["Highlights"])
|
|
rowcontent.append(row["Comments"])
|
|
return " ".join(rowcontent)
|
|
|