Commit 3c263651 authored by Robert Czechowski's avatar Robert Czechowski
Browse files

Also implement CSV group upload in postgres db, fix warnings

parent 8e43a126
......@@ -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();
......
......@@ -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>;
......
......@@ -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) {
......
......@@ -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();
}
......
......@@ -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")))))
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment