db_objects.rs 5.52 KB
Newer Older
1
2
extern crate time;

Robert Czechowski's avatar
Robert Czechowski committed
3
use self::time::{Duration, Timespec};
4

5
#[derive(Clone)]
6
7
8
9
10
11
12
pub struct SessionUser {
    pub id: u32,
    pub session_token: Option<String>, // delete this to log out
    pub csrf_token: String,
    pub last_login: Option<Timespec>,
    pub last_activity: Option<Timespec>,
    pub permanent_login: bool,
Robert Czechowski's avatar
Robert Czechowski committed
13

14
15
    pub username: Option<String>,
    pub password: Option<String>,
Robert Czechowski's avatar
Robert Czechowski committed
16
    pub salt: Option<String>,
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    pub logincode: Option<String>,
    pub email: Option<String>,
    pub email_unconfirmed: Option<String>,
    pub email_confirmationcode: Option<String>,

    pub firstname: Option<String>,
    pub lastname: Option<String>,
    pub street: Option<String>,
    pub zip: Option<String>,
    pub city: Option<String>,
    pub nation: Option<String>,
    pub grade: u8,

    pub is_teacher: bool,
    pub managed_by: Option<u32>,
    pub pms_id: Option<u32>,
    pub pms_school_id: Option<u32>,
}

36
37
38
39
40
41
42
43
44
45
46
// Short version for display
#[derive(Clone, Default)]
pub struct UserInfo {
    pub id: u32,
    pub username: Option<String>,
    pub logincode: Option<String>,
    pub firstname: Option<String>,
    pub lastname: Option<String>,
}

#[derive(Clone)]
Robert Czechowski's avatar
Robert Czechowski committed
47
48
49
50
51
52
pub struct Group {
    pub id: Option<u32>,
    pub name: String,
    pub groupcode: String,
    pub tag: String,
    pub admin: u32,
Robert Czechowski's avatar
Robert Czechowski committed
53
    pub members: Vec<SessionUser>,
Robert Czechowski's avatar
Robert Czechowski committed
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
pub struct Contest {
    pub id: Option<u32>,
    pub location: String,
    pub filename: String,
    pub name: String,
    pub duration: u32,
    pub public: bool,
    pub start: Option<Timespec>,
    pub end: Option<Timespec>,
    pub taskgroups: Vec<Taskgroup>,
}

pub struct Taskgroup {
    pub id: Option<u32>,
    pub contest: u32,
    pub name: String,
    pub tasks: Vec<Task>,
}

pub struct Task {
    pub id: Option<u32>,
    pub taskgroup: u32,
    pub location: String,
    pub stars: u8,
}

pub struct Submission {
Robert Czechowski's avatar
Robert Czechowski committed
83
84
85
    pub id: Option<u32>,
    pub session_user: u32,
    pub task: u32,
86
    pub grade: u8,
Robert Czechowski's avatar
Robert Czechowski committed
87
    pub validated: bool,
88
    pub nonvalidated_grade: u8,
Robert Czechowski's avatar
Robert Czechowski committed
89
90
91
92
    pub needs_validation: bool,
    pub subtask_identifier: Option<String>,
    pub value: String,
    pub date: Timespec,
93
94
}

95
#[derive(Clone, Copy, Default)]
96
97
98
pub struct Grade {
    pub taskgroup: u32,
    pub user: u32,
99
    pub grade: Option<u8>,
100
101
102
103
104
105
106
107
108
    pub validated: bool,
}

pub struct Participation {
    pub contest: u32,
    pub user: u32,
    pub start: Timespec,
}

Robert Czechowski's avatar
Robert Czechowski committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
pub trait HasId {
    fn get_id(&self) -> Option<u32>;
    fn set_id(&mut self, id: u32);
}
impl HasId for Submission {
    fn get_id(&self) -> Option<u32> { self.id }
    fn set_id(&mut self, id: u32) { self.id = Some(id); }
}
impl HasId for Task {
    fn get_id(&self) -> Option<u32> { self.id }
    fn set_id(&mut self, id: u32) { self.id = Some(id); }
}
impl HasId for Taskgroup {
    fn get_id(&self) -> Option<u32> { self.id }
    fn set_id(&mut self, id: u32) { self.id = Some(id); }
}
impl HasId for Contest {
    fn get_id(&self) -> Option<u32> { self.id }
    fn set_id(&mut self, id: u32) { self.id = Some(id); }
}
impl HasId for Group {
    fn get_id(&self) -> Option<u32> { self.id }
    fn set_id(&mut self, id: u32) { self.id = Some(id); }
}
133
134

impl Contest {
Robert Czechowski's avatar
Robert Czechowski committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    pub fn new(location: String, filename: String, name: String, duration: u32, public: bool,
               start: Option<Timespec>, end: Option<Timespec>)
               -> Self
    {
        Contest { id: None,
                  location: location,
                  filename: filename,
                  name: name,
                  duration: duration,
                  public: public,
                  start: start,
                  end: end,
                  taskgroups: Vec::new() }
    }
149
150
151
152
}

impl SessionUser {
    pub fn minimal(id: u32, session_token: String, csrf_token: String) -> Self {
Robert Czechowski's avatar
Robert Czechowski committed
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
        SessionUser { id: id,
                      session_token: Some(session_token),
                      csrf_token: csrf_token,
                      last_login: None,
                      last_activity: None, // now?
                      // müssen die überhaupt außerhalb der datenbankabstraktion sichtbar sein?
                      permanent_login: false,

                      username: None,
                      password: None,
                      salt: None,
                      logincode: None,
                      email: None,
                      email_unconfirmed: None,
                      email_confirmationcode: None,

                      firstname: None,
                      lastname: None,
                      street: None,
                      zip: None,
                      city: None,
                      nation: None,
                      grade: 0,

                      is_teacher: false,
                      managed_by: None,
                      pms_id: None,
                      pms_school_id: None }
181
    }
182
183

    pub fn ensure_alive(self) -> Option<Self> {
184
        let duration = if self.permanent_login { Duration::days(90) } else { Duration::minutes(90) };
185
186
187
        let now = time::get_time();
        if now - self.last_activity? < duration {
            Some(self)
Robert Czechowski's avatar
Robert Czechowski committed
188
        } else {
189
            None
Robert Czechowski's avatar
Robert Czechowski committed
190
        }
191
192
193
194
195
    }

    pub fn ensure_logged_in(self) -> Option<Self> {
        if self.password.is_some() || self.logincode.is_some() || self.pms_id.is_some() {
            self.ensure_alive()
Robert Czechowski's avatar
Robert Czechowski committed
196
        } else {
197
198
199
            None
        }
    }
200
201
202
}

impl Taskgroup {
Robert Czechowski's avatar
Robert Czechowski committed
203
    pub fn new(name: String) -> Self { Taskgroup { id: None, contest: 0, name: name, tasks: Vec::new() } }
204
205
206
207
}

impl Task {
    pub fn new(location: String, stars: u8) -> Self {
Robert Czechowski's avatar
Robert Czechowski committed
208
209
        Task { id: None, taskgroup: 0, location: location, stars: stars }
    }
210
}