Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
bwinf
medal
Commits
3c263651
Commit
3c263651
authored
Sep 06, 2019
by
Robert Czechowski
Browse files
Also implement CSV group upload in postgres db, fix warnings
parent
8e43a126
Changes
5
Hide whitespace changes
Inline
Side-by-side
src/core.rs
View file @
3c263651
...
...
@@ -6,7 +6,6 @@ use db_objects::SessionUser;
use
helpers
;
use
oauth_provider
::
OauthProvider
;
use
webfw_iron
::{
json_val
,
to_json
};
use
serde_json
::
from_str
;
#[derive(Serialize,
Deserialize)]
pub
struct
SubTaskInfo
{
...
...
@@ -584,11 +583,11 @@ pub fn upload_groups<T: MedalConnection>(conn: &T, session_token: &str, csrf_tok
let
mut
group_code
=
""
.to_string
();
let
mut
name
=
""
.to_string
();
let
mut
group
=
Group
{
id
:
None
,
name
:
""
.to_string
(),
groupcode
:
""
.to_string
()
,
tag
:
""
.to_string
(),
admin
:
session
.id
,
members
:
Vec
::
new
()
};
Group
{
id
:
None
,
name
:
name
.clone
(),
groupcode
:
group_code
,
tag
:
""
.to_string
(),
admin
:
session
.id
,
members
:
Vec
::
new
()
};
for
line
in
v
{
if
name
!=
line
[
0
]
{
if
(
name
!=
""
)
{
if
name
!=
""
{
conn
.create_group_with_users
(
group
);
}
name
=
line
[
0
]
.clone
();
...
...
src/db_conn.rs
View file @
3c263651
...
...
@@ -22,7 +22,7 @@ pub trait MedalConnection {
lastname
:
&
str
)
->
Result
<
String
,
()
>
;
fn
create_user_with_groupcode
(
&
self
,
session
:
Option
<&
str
>
,
groupcode
:
&
str
)
->
Result
<
String
,
()
>
;
fn
create_group_with_users
(
&
self
,
mut
group
:
Group
);
fn
create_group_with_users
(
&
self
,
group
:
Group
);
fn
logout
(
&
self
,
session
:
&
str
);
fn
load_submission
(
&
self
,
session
:
&
SessionUser
,
task
:
i32
,
subtask
:
Option
<&
str
>
)
->
Option
<
Submission
>
;
...
...
src/db_conn_postgres.rs
View file @
3c263651
...
...
@@ -11,12 +11,12 @@ use db_objects::*;
use
helpers
;
trait
Queryable
{
fn
query_map_one
<
T
,
F
>
(
&
self
,
sql
:
&
str
,
params
:
&
[
&
postgres
::
types
::
ToSql
],
f
:
F
)
->
postgres
::
Result
<
Option
<
T
>>
fn
query_map_one
<
T
,
F
>
(
&
self
,
sql
:
&
str
,
params
:
&
[
&
dyn
postgres
::
types
::
ToSql
],
f
:
F
)
->
postgres
::
Result
<
Option
<
T
>>
where
F
:
FnOnce
(
postgres
::
rows
::
Row
<
'_
>
)
->
T
;
}
impl
Queryable
for
Connection
{
fn
query_map_one
<
T
,
F
>
(
&
self
,
sql
:
&
str
,
params
:
&
[
&
postgres
::
types
::
ToSql
],
f
:
F
)
->
postgres
::
Result
<
Option
<
T
>>
fn
query_map_one
<
T
,
F
>
(
&
self
,
sql
:
&
str
,
params
:
&
[
&
dyn
postgres
::
types
::
ToSql
],
f
:
F
)
->
postgres
::
Result
<
Option
<
T
>>
where
F
:
FnOnce
(
postgres
::
rows
::
Row
<
'_
>
)
->
T
{
let
rows
=
self
.query
(
sql
,
params
)
?
;
...
...
@@ -312,8 +312,16 @@ impl MedalConnection for Connection {
}
}
fn
create_group_with_users
(
&
self
,
mut
group
:
Group
)
{
unimplemented!
();
fn
create_group_with_users
(
&
self
,
mut
group
:
Group
)
{
// Generate group ID:
group
.save
(
self
);
for
user
in
group
.members
{
let
csrf_token
=
helpers
::
make_csrf_token
();
let
login_code
=
helpers
::
make_login_code
();
// TODO: check for collisions
self
.execute
(
"INSERT INTO session (firstname, lastname, csrf_token, permanent_login, logincode, grade, is_teacher, managed_by) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"
,
&
[
&
user
.firstname
,
&
user
.lastname
,
&
csrf_token
,
&
false
,
&
login_code
,
&
user
.grade
,
&
false
,
&
group
.id
])
.unwrap
();
}
}
fn
logout
(
&
self
,
session
:
&
str
)
{
...
...
src/db_conn_sqlite.rs
View file @
3c263651
...
...
@@ -11,12 +11,12 @@ use db_objects::*;
use
helpers
;
trait
Queryable
{
fn
query_map_one
<
T
,
F
>
(
&
self
,
sql
:
&
str
,
params
:
&
[
&
rusqlite
::
types
::
ToSql
],
f
:
F
)
->
rusqlite
::
Result
<
Option
<
T
>>
fn
query_map_one
<
T
,
F
>
(
&
self
,
sql
:
&
str
,
params
:
&
[
&
dyn
rusqlite
::
types
::
ToSql
],
f
:
F
)
->
rusqlite
::
Result
<
Option
<
T
>>
where
F
:
FnOnce
(
&
rusqlite
::
Row
)
->
T
;
}
impl
Queryable
for
Connection
{
fn
query_map_one
<
T
,
F
>
(
&
self
,
sql
:
&
str
,
params
:
&
[
&
rusqlite
::
types
::
ToSql
],
f
:
F
)
->
rusqlite
::
Result
<
Option
<
T
>>
fn
query_map_one
<
T
,
F
>
(
&
self
,
sql
:
&
str
,
params
:
&
[
&
dyn
rusqlite
::
types
::
ToSql
],
f
:
F
)
->
rusqlite
::
Result
<
Option
<
T
>>
where
F
:
FnOnce
(
&
rusqlite
::
Row
)
->
T
{
let
mut
stmt
=
self
.prepare
(
sql
)
?
;
let
mut
rows
=
stmt
.query
(
params
)
?
;
...
...
@@ -308,7 +308,6 @@ impl MedalConnection for Connection {
for
user
in
group
.members
{
let
csrf_token
=
helpers
::
make_csrf_token
();
let
login_code
=
helpers
::
make_login_code
();
// TODO: check for collisions
let
now
=
time
::
get_time
();
self
.execute
(
"INSERT INTO session_user (firstname, lastname, csrf_token, permanent_login, logincode, grade, is_teacher, managed_by) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)"
,
&
[
&
user
.firstname
,
&
user
.lastname
,
&
csrf_token
,
&
false
,
&
login_code
,
&
user
.grade
,
&
false
,
&
group
.id
])
.unwrap
();
}
...
...
src/webfw_iron.rs
View file @
3c263651
...
...
@@ -27,7 +27,7 @@ use db_conn::MedalConnection;
use
iron
::
typemap
::
Key
;
pub
use
serde_json
::
value
as
json_val
;
static
TASK_DIR
:
&
'static
str
=
"tasks"
;
static
TASK_DIR
:
&
str
=
"tasks"
;
macro_rules!
mime
{
(
$top:tt
/
$sub:tt
)
=>
(
...
...
@@ -92,7 +92,7 @@ impl iron_sessionstorage::Value for SessionToken {
pub
struct
CookieDistributor
{}
impl
AroundMiddleware
for
CookieDistributor
{
fn
around
(
self
,
handler
:
Box
<
Handler
>
)
->
Box
<
Handler
>
{
fn
around
(
self
,
handler
:
Box
<
dyn
Handler
>
)
->
Box
<
dyn
Handler
>
{
use
rand
::{
distributions
::
Alphanumeric
,
thread_rng
,
Rng
};
Box
::
new
(
move
|
req
:
&
mut
Request
|
->
IronResult
<
Response
>
{
...
...
@@ -578,7 +578,7 @@ fn group_csv_upload<C>(req: &mut Request) -> IronResult<Response>
println!
(
"{}"
,
group_data
);
let
group_id
=
with_conn!
[
core
::
upload_groups
,
C
,
req
,
&
session_token
,
&
csrf_token
,
&
group_data
]
.aug
(
req
)
?
;
with_conn!
[
core
::
upload_groups
,
C
,
req
,
&
session_token
,
&
csrf_token
,
&
group_data
]
.aug
(
req
)
?
;
Ok
(
Response
::
with
((
status
::
Found
,
Redirect
(
url_for!
(
req
,
"groups"
)))))
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment