Update
This commit is contained in:
parent
2b65b6a1c5
commit
f13c9c905f
15 changed files with 322 additions and 981 deletions
|
@ -3,11 +3,8 @@ use celery::error::TaskError;
|
|||
use celery::task::TaskResult;
|
||||
use chrono::prelude::*;
|
||||
use diesel::prelude::*;
|
||||
use now::DateTimeNow;
|
||||
|
||||
use crate::config;
|
||||
use crate::db;
|
||||
use crate::untis;
|
||||
use crate::{config, db};
|
||||
|
||||
async fn fetch_schoolyears(client: &untis::Client, conn: &mut PgConnection) -> Result<i32> {
|
||||
let existing_schoolyears = db::schema::schoolyears::table
|
||||
|
@ -272,42 +269,140 @@ async fn fetch_substitutions(
|
|||
conn: &mut PgConnection,
|
||||
schoolyear_id: i32,
|
||||
) -> Result<()> {
|
||||
let now = Utc::now();
|
||||
let beginning_of_week = now.beginning_of_week().date_naive();
|
||||
let end_of_week = now.end_of_week().date_naive();
|
||||
// if diesel::select(diesel::dsl::not(diesel::expression::exists::exists(
|
||||
// db::schema::substitution_queries::table
|
||||
// .filter(db::schema::substitution_queries::active)
|
||||
// .filter(db::schema::substitution_queries::end_date.eq(end_of_week)),
|
||||
// )))
|
||||
// .get_result::<bool>(conn)?
|
||||
// {}
|
||||
let today = Utc::now().date_naive();
|
||||
if diesel::select(diesel::dsl::not(diesel::expression::exists::exists(
|
||||
db::schema::substitution_queries::table
|
||||
.filter(db::schema::substitution_queries::date.eq(today)),
|
||||
)))
|
||||
.get_result::<bool>(conn)?
|
||||
{
|
||||
diesel::insert_into(db::schema::substitution_queries::table)
|
||||
.values(db::models::NewSubstitutionQuery {
|
||||
schoolyear_id,
|
||||
date: today,
|
||||
active: true,
|
||||
})
|
||||
.execute(conn)?;
|
||||
}
|
||||
|
||||
// for query in db::schema::substitution_queries::table
|
||||
// .filter(db::schema::substitution_queries::active)
|
||||
// .load::<db::models::SubstitutionQuery>(conn)?
|
||||
// {
|
||||
// dbg!(&query);
|
||||
// }
|
||||
for query in db::schema::substitution_queries::table
|
||||
.filter(db::schema::substitution_queries::active)
|
||||
.load::<db::models::SubstitutionQuery>(conn)?
|
||||
{
|
||||
let now = Utc::now().naive_utc();
|
||||
let substs = client.substitutions(&query.date, &query.date, None).await?;
|
||||
for substitution in substs {
|
||||
let substitution_id = diesel::insert_into(db::schema::substitutions::table)
|
||||
.values(db::models::NewSubstitution {
|
||||
substitution_query_id: query.id,
|
||||
subst_type: substitution.subst_type.into(),
|
||||
lesson_id: substitution.lesson_id,
|
||||
start_time: substitution.start_time,
|
||||
end_time: substitution.end_time,
|
||||
text: substitution.text.as_deref(),
|
||||
})
|
||||
.returning(db::schema::substitutions::id)
|
||||
.get_result::<i32>(conn)?;
|
||||
|
||||
diesel::insert_into(db::schema::substitution_classes::table)
|
||||
.values(
|
||||
&substitution
|
||||
.classes
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, c)| {
|
||||
Ok(db::models::NewSubstitutionClass {
|
||||
substitution_id,
|
||||
position: i as i16,
|
||||
class_id: db::schema::classes::table
|
||||
.filter(db::schema::classes::untis_id.eq(c.id))
|
||||
.select(db::schema::classes::id)
|
||||
.get_result::<i32>(conn)?,
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<db::models::NewSubstitutionClass>>>()?,
|
||||
)
|
||||
.execute(conn)?;
|
||||
diesel::insert_into(db::schema::substitution_teachers::table)
|
||||
.values(
|
||||
&substitution
|
||||
.teachers
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, t)| {
|
||||
Ok(db::models::NewSubstitutionTeacher {
|
||||
substitution_id,
|
||||
position: i as i16,
|
||||
teacher_id: if t.id == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(
|
||||
db::schema::teachers::table
|
||||
.filter(db::schema::teachers::untis_id.eq(t.id))
|
||||
.select(db::schema::teachers::id)
|
||||
.get_result::<i32>(conn)?,
|
||||
)
|
||||
},
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<db::models::NewSubstitutionTeacher>>>()?,
|
||||
)
|
||||
.execute(conn)?;
|
||||
diesel::insert_into(db::schema::substitution_subjects::table)
|
||||
.values(
|
||||
&substitution
|
||||
.subjects
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, s)| {
|
||||
Ok(db::models::NewSubstitutionSubject {
|
||||
substitution_id,
|
||||
position: i as i16,
|
||||
subject_id: db::schema::subjects::table
|
||||
.filter(db::schema::subjects::untis_id.eq(s.id))
|
||||
.select(db::schema::subjects::id)
|
||||
.get_result::<i32>(conn)?,
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<db::models::NewSubstitutionSubject>>>()?,
|
||||
)
|
||||
.execute(conn)?;
|
||||
diesel::insert_into(db::schema::substitution_rooms::table)
|
||||
.values(
|
||||
&substitution
|
||||
.rooms
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, r)| {
|
||||
Ok(db::models::NewSubstitutionRoom {
|
||||
substitution_id,
|
||||
position: i as i16,
|
||||
room_id: if r.id == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(
|
||||
db::schema::rooms::table
|
||||
.filter(db::schema::rooms::untis_id.eq(r.id))
|
||||
.select(db::schema::rooms::id)
|
||||
.get_result::<i32>(conn)?,
|
||||
)
|
||||
},
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<db::models::NewSubstitutionRoom>>>()?,
|
||||
)
|
||||
.execute(conn)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[celery::task]
|
||||
pub async fn update_info() -> TaskResult<()> {
|
||||
let mut client = untis::Client {
|
||||
api_url: match url::Url::parse(&config::CONFIG.untis_api_url) {
|
||||
Ok(x) => x,
|
||||
Err(e) => return Err(TaskError::UnexpectedError(e.to_string())),
|
||||
},
|
||||
rpc_url: match url::Url::parse(&config::CONFIG.untis_rpc_url) {
|
||||
Ok(x) => untis::Client::gen_rpc_url(x, &config::CONFIG.untis_school),
|
||||
Err(e) => return Err(TaskError::UnexpectedError(e.to_string())),
|
||||
},
|
||||
username: config::CONFIG.untis_username.to_owned(),
|
||||
password: config::CONFIG.untis_password.to_owned(),
|
||||
session: None,
|
||||
authorization: None,
|
||||
let mut client = match config::untis_from_env() {
|
||||
Ok(x) => x,
|
||||
Err(e) => return Err(TaskError::UnexpectedError(e.to_string())),
|
||||
};
|
||||
|
||||
if let Err(e) = client.login().await {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue