-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackend_csv.py
More file actions
50 lines (43 loc) · 1.74 KB
/
backend_csv.py
File metadata and controls
50 lines (43 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import csv
import os
from models import *
class Backend:
def __init__(self, config):
self.config = config
def get_setting(self, name):
return self.config.get('csv', name)
def getPersonList(self):
with open(self.get_setting('PEOPLE_FILE'), "rt") as f:
people = []
for row in csv.DictReader(f):
id = int(row["ID"])
name = row["Name"]
student = (row["Student?"].lower() != "false" and
row["Student?"].lower() != "no" and
row["Student?"] != "0")
photoPath = row["Photo Path"]
try:
photoSize = os.stat(photoPath).st_size
except OSError:
photoSize = 0
people.append(Person(id, name, student, photoPath, photoSize, id))
return people
def getBadgePhoto(self, photoPath, localName):
# Avoid copy if both are the same file
if os.path.abspath(photoPath) == os.path.abspath(localName):
return
with open(photoPath, "rb") as f:
data = f.read()
with open(localPath, "wb") as f:
f.write(data)
def putTimeRecords(self, records):
if not records:
return set() # no records to put
with open(self.get_setting('RECORDS_FILE'), "a") as f:
writer = csv.writer(f)
for record in records:
writer.writerow([record.person.id, record.person.name,
record.person.student, "", record.inTime,
record.outTime, record.hours, record.recorded])
# report all as successfully added
return set(i for i, v in enumerate(records))