-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
185 lines (132 loc) · 4.86 KB
/
models.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
#
# SPDX-FileCopyrightText: 2013-2021 Sequent Tech Inc <[email protected]>
#
# SPDX-License-Identifier: AGPL-3.0-only
#
import json
from datetime import datetime
from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.types import TypeDecorator, VARCHAR
from frestq.app import db
class Election(db.Model):
'''
Represents an election, with multiple possible questions, each of them
will be tallied separatedly with its own session (and its own pubkey).
'''
id = db.Column(db.BigInteger, primary_key=True)
num_parties = db.Column(db.Integer)
threshold_parties = db.Column(db.Integer)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
last_updated_at = db.Column(db.DateTime, default=datetime.utcnow)
title = db.Column(db.Unicode(255))
description = db.Column(db.UnicodeText)
# converted into and from JSON
questions = db.Column(db.UnicodeText)
start_date = db.Column(db.DateTime)
end_date = db.Column(db.DateTime)
status = db.Column(db.Unicode(128))
callback_url = db.Column(db.Unicode(1024))
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def __repr__(self):
return '<Election %r>' % self.title
def to_dict(self, full=False):
'''
Return an individual instance as a dictionary.
'''
ret = {
'title': self.title,
'id': self.id,
'num_parties': self.num_parties,
'threshold_parties': self.threshold_parties,
'created_at': self.created_at,
'last_updated_at': self.last_updated_at,
'status': self.status,
'callback_url': self.callback_url
}
if full:
ret['authorities'] = [a.to_dict() for a in self.authorities]
return ret
class Session(db.Model):
'''
Refers mixnet session, with its own public key and protinfo
'''
id = db.Column(db.Unicode(255), primary_key=True)
election_id = db.Column(db.Integer, db.ForeignKey('election.id'))
question_number = db.Column(db.Integer)
election = db.relationship('Election',
backref=db.backref('sessions', lazy='dynamic', order_by=question_number))
status = db.Column(db.Unicode(128))
public_key = db.Column(db.UnicodeText)
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def __repr__(self):
return '<Session %r>' % self.title
def to_dict(self, full=False):
'''
Return an individual instance as a dictionary.
'''
return {
'id': self.id,
'election_id': self.election_id,
'status': self.status,
'public_key': self.public_key,
'question_number': self.question_number
}
class Authority(db.Model):
'''
Represents an authority
'''
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Unicode(255))
ssl_cert = db.Column(db.UnicodeText)
orchestra_url = db.Column(db.Unicode(1024))
election_id = db.Column(db.Integer, db.ForeignKey('election.id'))
election = db.relationship('Election',
backref=db.backref('authorities', lazy='dynamic'))
created_at = db.Column(db.DateTime, default=datetime.utcnow)
last_updated_at = db.Column(db.DateTime, default=datetime.utcnow)
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def __repr__(self):
return '<Authority %r>' % self.name
def to_dict(self):
'''
Return an individual instance as a dictionary.
'''
return {
'id': self.id,
'name': self.name,
'ssl_cert': self.ssl_cert,
'orchestra_url': self.orchestra_url,
'election_id': self.election_id
}
class Ballot(db.Model):
session_id = db.Column(db.Unicode(255), db.ForeignKey('session.id'), primary_key=True)
ballot_hash = db.Column(db.Unicode(45), primary_key=True)
session = db.relationship('Session',
backref=db.backref('ballots', lazy='dynamic'))
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def __repr__(self):
return '<Ballot %r>' % self.ballot_hash
def to_dict(self):
'''
Return an individual instance as a dictionary.
'''
return {
'session_id': self.session_id,
'ballot_hash': self.ballot_hash
}
class QueryQueue(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
task = db.Column(db.Unicode(20))
data = db.Column(db.UnicodeText)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
doing = db.Column(db.Boolean, default=False)