Skip to content

Commit

Permalink
Merge pull request #274 from vplan-fr/main
Browse files Browse the repository at this point in the history
2024-03-10-3
  • Loading branch information
Belissimo-T authored Mar 10, 2024
2 parents ad3895c + 09fd273 commit 7fd5b2e
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 48 deletions.
12 changes: 6 additions & 6 deletions backend/lesson_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import typing

from .vplan_utils import (
periods_to_block_label, parse_periods, _parse_form_pattern, ParsedForm, parsed_forms_to_str, forms_to_str,
get_label_of_periods, parse_periods, _parse_form_pattern, ParsedForm, parsed_forms_to_str, forms_to_str,
_loose_parse_form_pattern
)
from . import teacher as teacher_model
Expand Down Expand Up @@ -167,7 +167,7 @@ def _to_text_segments(self, lesson_date: datetime.date) -> list[LessonInfoTextSe
return [
LessonInfoTextSegment("verlegt von "),
LessonInfoTextSegment(
f"Block {periods_to_block_label(self.periods)}",
get_label_of_periods(self.periods),
link=LessonInfoTextSegmentLink(
type=self.plan_type,
value=sorted(self.plan_value),
Expand All @@ -181,7 +181,7 @@ def _to_text_segments(self, lesson_date: datetime.date) -> list[LessonInfoTextSe
LessonInfoTextSegment("statt "),
LessonInfoTextSegment(
f"{de_weekday_to_str(self.date.weekday())} ({self.date.strftime('%d.%m.%Y')}) "
f"{periods_to_block_label(self.periods)}. Block",
f"{get_label_of_periods(self.periods)}",
link=LessonInfoTextSegmentLink(
type=self.plan_type,
value=sorted(self.plan_value),
Expand Down Expand Up @@ -261,7 +261,7 @@ def _to_text_segments(self, lesson_date: datetime.date) -> list[LessonInfoTextSe
*self._course_text_segments(lesson_date, None),
LessonInfoTextSegment(" verlegt nach "),
LessonInfoTextSegment(
f"Block {periods_to_block_label(self.periods)}",
get_label_of_periods(self.periods),
link=LessonInfoTextSegmentLink(
type=self.plan_type,
value=sorted(self.plan_value),
Expand All @@ -276,7 +276,7 @@ def _to_text_segments(self, lesson_date: datetime.date) -> list[LessonInfoTextSe
LessonInfoTextSegment(" gehalten am "),
LessonInfoTextSegment(
f"{de_weekday_to_str(self.date.weekday())} ({self.date.strftime('%d.%m.%Y')}) "
f"{periods_to_block_label(self.periods)}. Block",
f"{get_label_of_periods(self.periods)}",
link=LessonInfoTextSegmentLink(
type=self.plan_type,
value=sorted(self.plan_value),
Expand All @@ -291,7 +291,7 @@ def _to_text_segments(self, lesson_date: datetime.date) -> list[LessonInfoTextSe
LessonInfoTextSegment(" verlegt nach "),
LessonInfoTextSegment(
f"{de_weekday_to_str(self.date.weekday())} ({self.date.strftime('%d.%m.%Y')}) "
f"{periods_to_block_label(self.periods)}. Block",
f"{get_label_of_periods(self.periods)}",
link=LessonInfoTextSegmentLink(
type=self.plan_type,
value=sorted(self.plan_value),
Expand Down
54 changes: 42 additions & 12 deletions backend/vplan_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,23 +205,53 @@ def forms_to_str(forms: typing.Iterable[str]) -> str:
return parsed_forms_to_str([ParsedForm.from_str(form) for form in forms])


def periods_to_block_label(periods: typing.Iterable[int]) -> str:
periods = list(set(periods))
def get_block_of_period(period: int) -> int:
return (period - 1) // 2 + 1

periods.sort()

rests = {
0: "/Ⅱ", # "²⁄₂",
1: "/Ⅰ" # "¹⁄₂", # ½
}
if len(periods) == 1:
return f"{(periods[0] - 1) // 2 + 1}{rests[periods[0] % 2]}"
def get_periods_of_block(block: int) -> list[int]:
return [block * 2 - 1, block * 2]


def get_label_of_periods(periods: typing.Iterable[int]) -> str:
periods = sorted(set(periods))

elif len(periods) == 2 and periods[0] % 2 == 1 and periods[1] - periods[0] == 1:
return f"{periods[-1] // 2}"
# work out whether we can display as "Block X" or "Stunde X"
all_block_periods = set(sum((get_periods_of_block(get_block_of_period(p)) for p in periods), []))
if not all_block_periods - set(periods):
# block label
blocks = sorted(set(get_block_of_period(p) for p in periods))
seqs = _increasing_sequences(blocks)
out = []
for seq in seqs:
if len(seq) == 1:
out.append(f"{seq[0]}")
elif len(seq) == 2:
out.append(f"{seq[0]},{seq[-1]}")
else:
out.append(f"{seq[0]}-{seq[-1]}")

if len(blocks) == 1:
return f"Block {out[0]}"
else:
return f"Blöcke {','.join(out)}"

else:
return ", ".join([periods_to_block_label([p]) for p in periods])
# stunde label
seqs = _increasing_sequences(periods)
out = []
for seq in seqs:
if len(seq) == 1:
out.append(f"{seq[0]}")
elif len(seq) == 2:
out.append(f"{seq[0]},{seq[-1]}")
else:
out.append(f"{seq[0]}-{seq[-1]}")

if len(periods) == 1:
return f"Stunde {out[0]}"
else:
return f"Stunden {','.join(out)}"


def _parse_periods(period_str: str) -> list[int]:
Expand Down
34 changes: 8 additions & 26 deletions client/src/components/Lesson.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,14 @@
import Dropdown from "../base_components/Dropdown.svelte";
import {selected_favorite, settings} from "../stores";
import {arraysEqual} from "../utils.js";
import {getLabelOfPeriods} from "../periods_utils.js";
export let lesson;
export let date;
export let plan_type;
export let plan_value;
export let display_time = true;
function periods_to_block_label(periods) {
periods.sort(function (a, b) {
return a - b;
});
const rests = {
0: "/Ⅱ",
1: "/Ⅰ",
};
if (periods.length === 1) {
return `${Math.floor((periods[0] - 1) / 2) + 1}${rests[periods[0] % 2]}`;
} else if (periods.length === 2 && periods[0] % 2 === 1) {
return `${Math.floor(periods[periods.length - 1] / 2)}`;
} else {
return periods.map(p => periods_to_block_label([p])).join(", ");
}
}
$: only_teacher_absent = lesson.scheduled_teachers?.length !== 0 && lesson.current_teachers?.length === 0 && lesson.takes_place;
$: teachers = (lesson.takes_place && !only_teacher_absent ? lesson.current_teachers : lesson.scheduled_teachers) || [];
Expand All @@ -52,7 +34,7 @@
<!-- Optional: Time -->
{#if display_time}
<div class="vert-align max-width-center lesson-time-info">
<span class="lesson-period">{periods_to_block_label(lesson.periods)}</span>
<span class="lesson-period">{getLabelOfPeriods(lesson.periods)}</span>
<span class="lesson-time">{lesson.begin}</span>
</div>
{/if}
Expand Down Expand Up @@ -224,7 +206,7 @@
<span class="grow">{forms_str}</span>
<span class="material-symbols-outlined dropdown-arrow">arrow_drop_down</span>
</button>
{#each forms as form}
<button on:click={() => {
plan_type = "forms";
Expand All @@ -246,7 +228,7 @@
<span class="grow">{teachers.join(', ')}</span>
<span class="material-symbols-outlined dropdown-arrow">arrow_drop_down</span>
</button>
{#each teachers as teacher}
<button on:click={() => {
plan_type = "teachers";
Expand All @@ -268,7 +250,7 @@
<span class="grow">{rooms.join(', ')}</span>
<span class="material-symbols-outlined dropdown-arrow">arrow_drop_down</span>
</button>
{#each rooms as room}
<button on:click={() => {
plan_type = "rooms";
Expand All @@ -295,7 +277,7 @@
@media only screen and (min-width: 1501px) {
font-size: 1em;
margin-left: .2em;
&.centered_txt {
position: absolute;
right: 10px;
Expand Down Expand Up @@ -334,7 +316,7 @@
flex: 1;
white-space: nowrap;
}
&.center-align {
text-align: center;
}
Expand Down Expand Up @@ -458,7 +440,7 @@
width: 100%;
height: 100%;
}
.teachers button, .rooms button, .forms button {
flex: 1;
transition: background-color .2s ease;
Expand Down
5 changes: 3 additions & 2 deletions client/src/components/Plan.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import { swipe } from 'svelte-gestures';
import {indexed_db, settings, title, selected_favorite, favorites} from '../stores.js';
import {arraysEqual, format_date, navigate_page, format_timestamp} from "../utils.js";
import {periods_to_block_label, sameBlock, get_plan_version, get_teacher_data, load_plan, gen_location_hash, load_lessons, apply_preferences} from "../plan.js";
import {sameBlock, get_plan_version, get_teacher_data, load_plan, gen_location_hash, load_lessons, apply_preferences} from "../plan.js";
import {getLabelOfPeriods} from "../periods_utils.js";
import Dropdown from '../base_components/Dropdown.svelte';
export let api_base;
Expand Down Expand Up @@ -228,7 +229,7 @@
{#each lessons as lesson, i}
{#if external_times}
{#if !all_lessons[i-1] || (!arraysEqual(lesson.periods, lessons[i-1].periods))}
<span class="lesson-time" class:gap={lessons[i-1] && !sameBlock(lesson.periods, lessons[i-1].periods)}>{periods_to_block_label(lesson.periods)}{lesson.begin != null && lesson.end != null ? ": " + lesson.begin + " - " + lesson.end : ". Block"}</span>
<span class="lesson-time" class:gap={lessons[i-1] && !sameBlock(lesson.periods, lessons[i-1].periods)}>{getLabelOfPeriods(lesson.periods)}{lesson.begin != null && lesson.end != null ? ": " + lesson.begin + " - " + lesson.end : ""}</span>
{/if}
{/if}
<Lesson lesson={lesson} bind:plan_type bind:plan_value bind:date display_time={!external_times} />
Expand Down
109 changes: 109 additions & 0 deletions client/src/periods_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
function increasingSequences(seq, key = x => x) {
let out = [];
let currentSeq = [];
let last = null;

for (let elem of seq) {
if (last === elem) {
// fallback if something bad happens
return [[elem]];
}

if (last === null || key(elem) === key(last) + 1) {
currentSeq.push(elem);
} else {
out.push(currentSeq);
currentSeq = [elem];
}

last = elem;
}

out.push(currentSeq);

return out;
}

function getBlockOfPeriod(period) {
return Math.floor((period - 1) / 2) + 1;
}

function getPeriodsOfBlock(block) {
return [block * 2 - 1, block * 2];
}

function periods_to_block_label(periods) {
periods.sort(function (a, b) {
return a - b;
});

const rests = {
0: "/Ⅱ",
1: "/Ⅰ",
};

if (periods.length === 1) {
return `${Math.floor((periods[0] - 1) / 2) + 1}${rests[periods[0] % 2]}`;
} else if (periods.length === 2 && periods[0] % 2 === 1) {
return `${Math.floor(periods[periods.length - 1] / 2)}`;
} else {
return periods.map(p => periods_to_block_label([p])).join(", ");
}
}

function periods_to_block_or_periods_label(periods) {
periods = [...new Set(periods)].sort();

// work out whether we can display as "Block X" or "Stunde X"
let allBlockPeriods = new Set(
[].concat(...periods.map(p => getPeriodsOfBlock(getBlockOfPeriod(p))))
);

if ([...allBlockPeriods].every(p => periods.includes(p))) {
// block label
let blocks = [...new Set(periods.map(p => getBlockOfPeriod(p)))].sort();
let seqs = increasingSequences(blocks);
let out = [];

for (let seq of seqs) {
if (seq.length === 1) {
out.push(`${seq[0]}`);
} else if (seq.length === 2) {
out.push(`${seq[0]},${seq[seq.length - 1]}`);
} else {
out.push(`${seq[0]}-${seq[seq.length - 1]}`);
}
}

if (blocks.length === 1) {
return `Block ${out[0]}`;
} else {
return `Blöcke ${out.join(',')}`;
}

} else {
// stunde label
let seqs = increasingSequences(periods);
let out = [];

for (let seq of seqs) {
if (seq.length === 1) {
out.push(`${seq[0]}`);
} else if (seq.length === 2) {
out.push(`${seq[0]},${seq[seq.length - 1]}`);
} else {
out.push(`${seq[0]}-${seq[seq.length - 1]}`);
}
}

if (periods.length === 1) {
return `Stunde ${out[0]}`;
} else {
return `Stunden ${out.join(',')}`;
}
}
}

export function getLabelOfPeriods(periods) {
return "Block " + periods_to_block_label(periods);
}
11 changes: 9 additions & 2 deletions deploy_plan_loader.fish
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@ end
# Remove existing nohup.out
echo "=> Removing existing nohup.out..."
if rm -f nohup.out
echo " -> Sucess"
echo " -> Success"
else
echo " -> No existing nohup.out file. (or error)"
end

echo "=> Removing existing nohup.out2..."
if rm -f nohup.out2
echo " -> Success"
else
echo " -> No existing nohup.out2 file. (or error)"
end

# Start the plan loader
echo "=> Starting plan loader..."
nohup venv/bin/python3 -m backend.load_plans --ignore-exceptions --never-raise-out-of-proxies -l DEBUG -i 300 > nohup.out 2> /dev/null < /dev/null &
nohup venv/bin/python3 -m backend.load_plans --ignore-exceptions --never-raise-out-of-proxies -l DEBUG -i 300 > nohup.out2 2> nohup.out < /dev/null &

# Disown the process
echo "=> Disowning..."
Expand Down

0 comments on commit 7fd5b2e

Please sign in to comment.