-
Notifications
You must be signed in to change notification settings - Fork 2
/
trawfile.py
52 lines (38 loc) · 1.54 KB
/
trawfile.py
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
51
52
import hashlib
from sqlalchemy import LargeBinary, Integer, Float, ForeignKey, Column, String
from sqlalchemy.orm import relationship
from jvalidate import ValidationError
from butype import *
class RawFile(db.Model, BUType):
""" An uploaded (raw) file, for example the PDF of a BUIP. """
__tablename__="raw_file"
id = Column(Integer, primary_key=True)
x_sha256 = Column(String(length=64), nullable=False, unique=True)
data = Column(LargeBinary, nullable=False)
# metadata that refers to this file
proposal_metadata = relationship("ProposalMetadata",
uselist=False,
back_populates = "raw_file")
def __init__(self, data=None):
self.data=data
dhash=hashlib.sha256(data).hexdigest()
orf=RawFile.by_hash(dhash)
if orf is not None:
raise ValidationError("Raw file with hash '%s' exists already." % dhash)
self.xUpdate()
def public(self):
return (self.proposal_metadata is not None and
self.proposal_metadata.file_public)
def serialize(self):
return bytes(self.data)
def hashref(self):
return sha256(self.serialize())
def toJ(self):
raise RuntimeError("Cannot create JSON representation of raw file.")
def xUpdate(self):
self.x_sha256=self.hashref()
def dependencies(self):
if self.proposal_metadata:
return [self.proposal_metadata]
else:
return []