-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdatabase.py
153 lines (114 loc) · 4.44 KB
/
database.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
from peewee import PostgresqlDatabase, SqliteDatabase
from peewee import Model, CharField, TextField, IntegerField, BooleanField, DateTimeField, ForeignKeyField, CompositeKey
import bcrypt
import config
if config.production:
db = PostgresqlDatabase(config.database.database, user=config.database.user, password=config.database.password)
else:
db = SqliteDatabase("dev.db")
class BaseModel(Model):
class Meta:
database = db
class Team(BaseModel):
name = CharField(unique=True)
affiliation = CharField(null=True)
restricts = TextField(default="")
key = CharField(unique=True, index=True)
eligibility = BooleanField(null=True)
def solved(self, challenge):
return ChallengeSolve.select().where(ChallengeSolve.team == self, ChallengeSolve.challenge == challenge).count()
def eligible(self):
if self.eligibility is not None:
return self.eligibility
return all([member.eligible() for member in self.members]) and self.members.count() <= 3
@property
def score(self):
challenge_points = sum([i.challenge.points for i in self.solves])
adjust_points = sum([i.value for i in self.adjustments])
return challenge_points + adjust_points
class User(BaseModel):
username = CharField(unique=True, index=True)
email = CharField(index=True)
email_confirmed = BooleanField(default=False)
email_confirmation_key = CharField()
password = CharField(null=True)
background = CharField()
country = CharField()
tshirt_size = CharField(null=True)
gender = CharField(null=True)
first_login = BooleanField(default=True)
restricts = TextField(default="")
team = ForeignKeyField(Team, related_name="members")
banned = BooleanField(default=False)
password_reset_token = CharField(null=True)
password_reset_expired = DateTimeField(null=True)
def set_password(self, pw):
self.password = bcrypt.hashpw(pw.encode("utf-8"), bcrypt.gensalt())
return
def check_password(self, pw):
return bcrypt.checkpw(pw.encode("utf-8"), self.password.encode("utf-8"))
def eligible(self):
return self.country == "ISL" and not self.banned
class UserAccess(BaseModel):
user = ForeignKeyField(User, related_name='accesses')
ip = CharField()
time = DateTimeField()
class Stage(BaseModel):
name = CharField()
alias = CharField(unique=True, index=True)
description = CharField(null=True)
class Challenge(BaseModel):
name = CharField()
alias = CharField(unique=True, index=True)
category = CharField()
author = CharField()
description = TextField()
points = IntegerField()
breakthrough_bonus = IntegerField(default=0)
enabled = BooleanField(default=True)
flag = TextField()
stage = ForeignKeyField(Stage, related_name='challenges')
class ChallengeSolve(BaseModel):
user = ForeignKeyField(User, related_name='solves')
team = ForeignKeyField(Team, related_name='solves')
challenge = ForeignKeyField(Challenge, related_name='solves')
time = DateTimeField()
class Meta:
primary_key = CompositeKey('team', 'challenge')
class ChallengeFailure(BaseModel):
user = ForeignKeyField(User, related_name='failures')
team = ForeignKeyField(Team, related_name='failures')
challenge = ForeignKeyField(Challenge, related_name='failures')
attempt = CharField()
time = DateTimeField()
class NewsItem(BaseModel):
summary = CharField()
description = TextField()
class TroubleTicket(BaseModel):
team = ForeignKeyField(Team, related_name='tickets')
summary = CharField()
description = TextField()
active = BooleanField(default=True)
opened_at = DateTimeField()
class TicketComment(BaseModel):
ticket = ForeignKeyField(TroubleTicket, related_name='comments')
comment_by = CharField()
comment = TextField()
time = DateTimeField()
class Notification(BaseModel):
team = ForeignKeyField(Team, related_name='notifications')
notification = TextField()
class ScoreAdjustment(BaseModel):
team = ForeignKeyField(Team, related_name='adjustments')
value = IntegerField()
reason = TextField()
class AdminUser(BaseModel):
username = CharField()
password = CharField()
secret = CharField()
class SshAccount(BaseModel):
team = ForeignKeyField(Team, null=True, related_name='ssh_account')
username = CharField()
password = CharField()
hostname = CharField()
port = IntegerField()