2020-12-21 19:26:12 +01:00
|
|
|
"""This parses the varlib.csv but only in a way
|
|
|
|
that is actually useful for the site"""
|
2021-04-17 18:16:44 +02:00
|
|
|
from tempfile import NamedTemporaryFile
|
|
|
|
import shutil
|
2020-12-11 16:54:09 +01:00
|
|
|
import csv
|
|
|
|
import os
|
|
|
|
|
|
|
|
script_dir = os.path.dirname(__file__)
|
2021-01-09 20:52:31 +01:00
|
|
|
image_dir = os.path.abspath(os.path.join(script_dir, "../static/images"))
|
2021-04-05 21:42:43 +02:00
|
|
|
fieldnames = [
|
|
|
|
'Id',
|
|
|
|
'Publication',
|
|
|
|
'Author',
|
|
|
|
'Year',
|
|
|
|
'Custodian',
|
|
|
|
'Fields',
|
|
|
|
'Type',
|
|
|
|
'Publishers',
|
|
|
|
'License',
|
|
|
|
'LicenseShort',
|
|
|
|
'Highlights',
|
|
|
|
'Comments',
|
|
|
|
'Currently borrowed by',
|
|
|
|
]
|
2020-12-11 16:54:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
def parsecsv():
|
2021-04-18 00:07:16 +02:00
|
|
|
"""Test function to inspect csv file as dict"""
|
2020-12-11 16:54:09 +01:00
|
|
|
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
|
|
|
|
with libcsv:
|
|
|
|
csv_as_dict = csv.DictReader(libcsv)
|
|
|
|
return csv_as_dict
|
|
|
|
|
|
|
|
|
|
|
|
def getpublications():
|
2021-04-18 00:07:16 +02:00
|
|
|
"""get an overview of all publications for the main page"""
|
2020-12-11 16:54:09 +01:00
|
|
|
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
|
|
|
|
with libcsv:
|
|
|
|
csv_as_dict = csv.DictReader(libcsv)
|
2020-12-21 18:41:55 +01:00
|
|
|
publications = {}
|
2020-12-11 16:54:09 +01:00
|
|
|
for row in csv_as_dict:
|
2020-12-21 19:26:12 +01:00
|
|
|
year = row["Year"]
|
|
|
|
if not year:
|
|
|
|
year = "Unknown"
|
2021-01-09 20:52:31 +01:00
|
|
|
|
2020-12-21 18:58:46 +01:00
|
|
|
pubinfo = {
|
|
|
|
"Title": row["Publication"],
|
|
|
|
"Author": row["Author"],
|
2020-12-30 00:06:45 +01:00
|
|
|
"Type": row["Type"].lower().title(),
|
2020-12-21 19:26:12 +01:00
|
|
|
"Year": year,
|
2020-12-30 00:06:45 +01:00
|
|
|
"License": row["LicenseShort"].lower().title(),
|
2021-01-09 20:52:31 +01:00
|
|
|
"Image": hasimage(row["Id"])
|
2020-12-21 18:58:46 +01:00
|
|
|
}
|
2020-12-21 18:41:55 +01:00
|
|
|
publications[row["Id"]] = pubinfo
|
|
|
|
return publications
|
2020-12-11 16:54:09 +01:00
|
|
|
|
2020-12-21 18:58:46 +01:00
|
|
|
|
2021-01-09 20:52:31 +01:00
|
|
|
def hasimage(id):
|
2021-04-18 00:07:16 +02:00
|
|
|
"""does this Id from the csv have an image uploaded"""
|
2021-04-17 23:34:28 +02:00
|
|
|
image_jpg = os.path.join(image_dir, "image-{0}.jpg".format(id))
|
|
|
|
if os.path.exists(image_jpg):
|
2021-01-09 20:52:31 +01:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-12-21 18:41:55 +01:00
|
|
|
def gettypes():
|
2021-04-18 00:07:16 +02:00
|
|
|
"""for the dynamic menu get the unique types of publicatons"""
|
2020-12-21 18:41:55 +01:00
|
|
|
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
|
|
|
|
with libcsv:
|
|
|
|
csv_as_dict = csv.DictReader(libcsv)
|
|
|
|
listoftypes = []
|
|
|
|
for row in csv_as_dict:
|
2020-12-30 00:06:45 +01:00
|
|
|
lowertype = row["Type"].lower().title()
|
2020-12-21 18:41:55 +01:00
|
|
|
if lowertype not in listoftypes:
|
|
|
|
listoftypes.append(lowertype)
|
|
|
|
return listoftypes
|
2020-12-11 16:54:09 +01:00
|
|
|
|
2020-12-21 18:58:46 +01:00
|
|
|
|
|
|
|
def getyears():
|
2021-04-18 00:07:16 +02:00
|
|
|
"""for the dynamic menu get the unique years for publicatons"""
|
2020-12-21 18:58:46 +01:00
|
|
|
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
|
|
|
|
with libcsv:
|
|
|
|
csv_as_dict = csv.DictReader(libcsv)
|
|
|
|
listofyears = []
|
|
|
|
for row in csv_as_dict:
|
2020-12-21 19:26:12 +01:00
|
|
|
uniqueyear = row["Year"]
|
|
|
|
if not uniqueyear:
|
|
|
|
uniqueyear = "Unknown"
|
|
|
|
if uniqueyear not in listofyears:
|
|
|
|
listofyears.append(uniqueyear)
|
|
|
|
listofyears.sort()
|
2020-12-21 18:58:46 +01:00
|
|
|
return listofyears
|
|
|
|
|
|
|
|
|
2020-12-29 22:56:28 +01:00
|
|
|
def getlicenses():
|
2021-04-18 00:07:16 +02:00
|
|
|
"""for the dynamic menu get the unique liscenses for publicatons"""
|
2020-12-29 22:56:28 +01:00
|
|
|
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
|
|
|
|
with libcsv:
|
|
|
|
csv_as_dict = csv.DictReader(libcsv)
|
|
|
|
listoflicenses = []
|
|
|
|
for row in csv_as_dict:
|
2020-12-30 00:06:45 +01:00
|
|
|
license = row["LicenseShort"].lower().title()
|
2020-12-29 22:56:28 +01:00
|
|
|
if not license:
|
2020-12-30 00:06:45 +01:00
|
|
|
license = "No License Mentioned"
|
2020-12-29 22:56:28 +01:00
|
|
|
if license not in listoflicenses:
|
|
|
|
listoflicenses.append(license)
|
|
|
|
return listoflicenses
|
|
|
|
|
|
|
|
|
2021-01-10 00:07:22 +01:00
|
|
|
def getfieldsofinterest():
|
2021-04-18 00:07:16 +02:00
|
|
|
"""for the R&R page get the fields of interest from the publicatons"""
|
2021-01-10 00:07:22 +01:00
|
|
|
libcsv = open(os.path.join(script_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
|
|
|
|
|
|
|
|
|
2020-12-29 22:56:28 +01:00
|
|
|
def getpublicationfromcsvrow(row):
|
2021-04-18 00:07:16 +02:00
|
|
|
"""get entire publication info from a csv row"""
|
2020-12-29 22:56:28 +01:00
|
|
|
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,
|
2021-04-17 21:54:36 +02:00
|
|
|
"Image": hasimage(row["Id"])
|
2020-12-29 22:56:28 +01:00
|
|
|
}
|
|
|
|
return pubinfo
|
|
|
|
|
|
|
|
|
|
|
|
def getfullpublication(pubid):
|
2021-04-18 00:07:16 +02:00
|
|
|
"""For the single book view, most complete overview"""
|
2020-12-29 22:56:28 +01:00
|
|
|
libcsv = open(os.path.join(script_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
|
|
|
|
|
|
|
|
|
2021-04-05 21:42:43 +02:00
|
|
|
def generatenewpublicationid():
|
2021-04-18 00:07:16 +02:00
|
|
|
"""When uploading a book generate a new unique ID"""
|
2021-04-05 21:42:43 +02:00
|
|
|
libcsv = open(os.path.join(script_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):
|
2021-04-18 00:07:16 +02:00
|
|
|
"""When uploading a publication writes entry to the csv"""
|
2021-04-05 21:42:43 +02:00
|
|
|
id = generatenewpublicationid()
|
|
|
|
with open(
|
|
|
|
os.path.join(script_dir, "varlib.csv"),
|
|
|
|
'a',
|
|
|
|
newline='') as csvfile:
|
2021-04-17 18:16:44 +02:00
|
|
|
csv_as_writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
|
|
|
|
csv_as_writer.writerow(
|
2021-04-05 21:42:43 +02:00
|
|
|
{
|
|
|
|
"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
|
2021-04-17 18:16:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def editborrowedby(pubid, borrower):
|
2021-04-18 00:07:16 +02:00
|
|
|
"""Edits the borrowed by field for a publication entry in csv"""
|
2021-04-17 18:16:44 +02:00
|
|
|
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)
|
2021-04-18 00:07:16 +02:00
|
|
|
# use the reader to read where, then writer to write the new row.
|
2021-04-17 18:16:44 +02:00
|
|
|
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)
|