Commit 4a94522f authored by Robert Czechowski's avatar Robert Czechowski
Browse files

Add functionality for group csv upload

parent be8ae7e0
......@@ -2,9 +2,11 @@ use time;
use db_conn::MedalConnection;
use db_objects::{Grade, Group, Submission, Taskgroup};
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 {
......@@ -562,6 +564,8 @@ pub fn group_csv<T: MedalConnection>(conn: &T, session_token: &str) -> MedalValu
Ok(("groupcsv".to_string(), data))
}
// TODO: Should creating the users and groups happen in a batch operation to speed things up?
pub fn upload_groups<T: MedalConnection>(conn: &T, session_token: &str, csrf_token: &str, group_data: &str) -> MedalResult<()> {
let session = conn.get_session(&session_token)
.ok_or(MedalError::AccessDenied)?
......@@ -572,6 +576,37 @@ pub fn upload_groups<T: MedalConnection>(conn: &T, session_token: &str, csrf_tok
return Err(MedalError::CsrfCheckFailed);
}
println!("{}",group_data);
let mut v: Vec<Vec<String>> = serde_json::from_str(group_data).or(Err(MedalError::AccessDenied))?; // TODO: Change error type
v.sort_unstable_by(|a, b| a[0].partial_cmp(&b[0]).unwrap());
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() };
for line in v {
if name != line[0] {
if (name != "") {
conn.create_group_with_users(group);
}
name = line[0].clone();
group_code = helpers::make_group_code();
// TODO: check for collisions
group = Group { id: None, name: name.clone(), groupcode: group_code, tag: name.clone(), admin: session.id, members: Vec::new() };
}
let mut user = SessionUser::group_user_stub();
user.grade = line[1].parse::<i32>().unwrap_or(0);
user.firstname = Some(line[2].clone());
user.lastname = Some(line[3].clone());
group.members.push(user);
}
conn.create_group_with_users(group);
Ok(())
}
......
......@@ -22,6 +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 logout(&self, session: &str);
fn load_submission(&self, session: &SessionUser, task: i32, subtask: Option<&str>) -> Option<Submission>;
......
......@@ -312,6 +312,10 @@ impl MedalConnection for Connection {
}
}
fn create_group_with_users(&self, mut group: Group) {
unimplemented!();
}
fn logout(&self, session: &str) {
self.execute("UPDATE session SET session_token = NULL WHERE session_token = $1", &[&session]).unwrap();
}
......
......@@ -300,6 +300,19 @@ impl MedalConnection for Connection {
_ => Err(()),
}
}
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
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();
}
}
fn logout(&self, session: &str) {
self.execute("UPDATE session_user SET session_token = NULL WHERE session_token = ?1", &[&session]).unwrap();
......
......@@ -191,6 +191,38 @@ impl SessionUser {
}
}
pub fn group_user_stub() -> Self {
SessionUser { id: 0,
session_token: None,
csrf_token: "".to_string(),
last_login: None,
last_activity: None,
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,
oauth_foreign_id: None,
oauth_provider: None,
}
}
pub fn is_alive(&self) -> bool {
let duration = if self.permanent_login { Duration::days(90) } else { Duration::minutes(90) };
let now = time::get_time();
......
......@@ -87,14 +87,26 @@ function skip(i) {
senddata[i].skip=true;
}
function clear(i) {
function clear() {
document.getElementById("data").innerHTML = "";
senddata = [];
document.getElementById("result").style.display = "none";
}
function copy_data() {
document.getElementById("send_data").value = JSON.stringify(senddata);
// We hope senddata is no longer used after sending …
// Should a stable sort method be used?
senddata.sort(function (a, b) {return (a.data[0]).localeCompare(b.data[0]);});
var actual_send_data = [];
for (var i = 0; i < senddata.length; i++) {
if (!senddata[i].skip) {
actual_send_data.push(senddata[i].data);
}
}
document.getElementById("send_data").value = JSON.stringify(actual_send_data);
}
</script>
......
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