Skip to content
Snippets Groups Projects
Verified Commit 76558b2a authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Add support for getting current term and importing subjects and teachers

parent 21d53109
No related branches found
No related tags found
1 merge request!13Resolve "Support import from MySQL"
from django.utils import timezone
from . import models
DB_NAME = 'untis'
#####################
# BASIC DEFINITIONS #
#####################
class Basic(object):
def __init__(self):
self.filled = False
self.id = None
def create(self, db_obj):
self.filled = True
def run_using(obj):
return obj.using(DB_NAME)
def get_term_by_ids(term_id, school_year_id):
data = run_using(models.Terms.objects).get(term_id=term_id, schoolyear_id=school_year_id)
# print(data.schoolyear_id)
return data
########
# TERM #
########
class Term(object):
def __init__(self):
self.filled = False
self.id = None
self.name = None
self.school_year_id = None
def create(self, db_obj):
self.filled = True
self.id = db_obj.term_id
self.name = db_obj.longname
self.school_year_id = db_obj.schoolyear_id
def get_terms():
data = run_using(models.Terms.objects).all()
terms = []
for item in data:
term = Term()
term.create(item)
terms.append(term)
# print(term.name)
return terms
##############
# SCHOOLYEAR #
##############
class SchoolYear(object):
def __init__(self):
self.filled = False
self.id = None
self.name = None
def create(self, db_obj):
self.filled = True
self.id = db_obj.schoolyear_id
self.name = db_obj.schoolyearzoned
def get_school_years():
data = run_using(models.Schoolyear.objects).all()
years = []
for item in data:
year = SchoolYear()
year.create(item)
years.append(year)
# print(term.name)
return years
################
# HELP METHODS #
################
def clean_array(a, conv=None):
b = []
for el in a:
if el != '' and el != "0":
if conv is not None:
el = conv(el)
b.append(el)
return b
def untis_split_first(s, conv=None):
return clean_array(s.split(","), conv=conv)
def untis_split_second(s, conv=None):
return clean_array(s.split("~"), conv=conv)
def untis_split_third(s, conv=None):
return clean_array(s.split(";"), conv=conv)
DATE_FORMAT = "%Y%m%d"
def untis_date_to_date(untis):
return timezone.datetime.strptime(str(untis), DATE_FORMAT)
def date_to_untis_date(date):
return date.strftime(DATE_FORMAT)
...@@ -3,5 +3,6 @@ from django.urls import path ...@@ -3,5 +3,6 @@ from django.urls import path
from . import views from . import views
urlpatterns = [ urlpatterns = [
path("import", views.untis_import, name="untis_import"), path("import/xml/", views.xml_import, name="untis_xml_import"),
path("import/mysql/", views.mysql_import, name="untis_mysql_import"),
] ]
...@@ -2,7 +2,7 @@ from django.conf import settings ...@@ -2,7 +2,7 @@ from django.conf import settings
from untisconnect.api_helper import get_term_by_ids, run_using, untis_date_to_date, date_to_untis_date, \ from untisconnect.api_helper import get_term_by_ids, run_using, untis_date_to_date, date_to_untis_date, \
untis_split_first untis_split_first
from . import models from aleksis.apps.untis import models
from timetable.settings import untis_settings from timetable.settings import untis_settings
TYPE_TEACHER = 0 TYPE_TEACHER = 0
...@@ -55,57 +55,6 @@ def one_by_id(db_ref, obj): ...@@ -55,57 +55,6 @@ def one_by_id(db_ref, obj):
else: else:
return None return None
###########
# TEACHER #
###########
class Teacher(object):
def __init__(self):
self.filled = False
self.id = None
self.shortcode = None
self.first_name = None
self.name = None
self.full_name = None
def __str__(self):
if self.filled:
return (self.first_name or "") + " " + (self.name or "")
else:
return "Unbekannt"
def __eq__(self, other):
if not isinstance(other, Teacher):
# don't attempt to compare against unrelated types
return NotImplemented
return self.id == other.id
def create(self, db_obj):
self.filled = True
self.id = db_obj.teacher_id
self.shortcode = db_obj.name
self.name = db_obj.longname
self.first_name = db_obj.firstname
def get_all_teachers():
teachers = row_by_row(models.Teacher, Teacher)
teachers.sort(key=lambda a: a.shortcode)
return teachers
def get_teacher_by_id(id):
teacher = run_one(models.Teacher.objects).get(teacher_id=id)
return one_by_id(teacher, Teacher)
def get_teacher_by_shortcode(shortcode):
shortcode = shortcode.upper()
teacher = run_one(models.Teacher.objects).get(name__icontains=shortcode)
return one_by_id(teacher, Teacher)
######### #########
# CLASS # # CLASS #
######### #########
...@@ -272,62 +221,6 @@ def get_corridor_by_id(id): ...@@ -272,62 +221,6 @@ def get_corridor_by_id(id):
return one_by_id(corridor, Corridor) return one_by_id(corridor, Corridor)
###########
# SUBJECT #
###########
class Subject(object):
def __init__(self):
self.filled = False
self.id = None
self.shortcode = None
self.name = None
self.color = None
self.hex_color = None
def __str__(self):
if self.filled:
return self.shortcode or "Unbekannt"
else:
return "Unbekannt"
def __eq__(self, other):
if not isinstance(other, Teacher):
# don't attempt to compare against unrelated types
return NotImplemented
return self.id == other.id
def create(self, db_obj):
self.filled = True
self.id = db_obj.subject_id
self.shortcode = db_obj.name
self.name = db_obj.longname
self.color = db_obj.backcolor
# Convert UNTIS number to HEX
hex_bgr = str(hex(db_obj.backcolor)).replace("0x", "")
# Add beginning zeros if len < 6
if len(hex_bgr) < 6:
hex_bgr = "0" * (6 - len(hex_bgr)) + hex_bgr
# Change BGR to RGB
hex_rgb = hex_bgr[4:6] + hex_bgr[2:4] + hex_bgr[0:2]
# Add html #
self.hex_color = "#" + hex_rgb
def get_all_subjects():
subjects = row_by_row(models.Subjects, Subject, filter_term=False)
subjects.sort(key=lambda a: a.shortcode)
return subjects
def get_subject_by_id(id):
subject = run_one(models.Subjects.objects, filter_term=False).get(subject_id=id)
return one_by_id(subject, Subject)
class Absence(object): class Absence(object):
......
from django.utils import timezone
def clean_array(a, conv=None):
b = []
for el in a:
if el != '' and el != "0":
if conv is not None:
el = conv(el)
b.append(el)
return b
def untis_split_first(s, conv=None):
return clean_array(s.split(","), conv=conv)
def untis_split_second(s, conv=None):
return clean_array(s.split("~"), conv=conv)
def untis_split_third(s, conv=None):
return clean_array(s.split(";"), conv=conv)
DATE_FORMAT = "%Y%m%d"
def untis_date_to_date(untis):
return timezone.datetime.strptime(str(untis), DATE_FORMAT)
def date_to_untis_date(date):
return date.strftime(DATE_FORMAT)
def untis_colour_to_hex(colour: int) -> str:
# Convert UNTIS number to HEX
hex_bgr = str(hex(colour)).replace("0x", "")
# Add beginning zeros if len < 6
if len(hex_bgr) < 6:
hex_bgr = "0" * (6 - len(hex_bgr)) + hex_bgr
# Change BGR to RGB
hex_rgb = hex_bgr[4:6] + hex_bgr[2:4] + hex_bgr[0:2]
# Add html #
return "#" + hex_rgb
from datetime import datetime, date
from django.db.models import Model, QuerySet
from aleksis.apps.untis import models as mysql_models
from aleksis.apps.untis.util.mysql.api_helper import (
date_to_untis_date,
untis_colour_to_hex,
)
from aleksis.apps.chronos import models as chronos_models
from aleksis.core import models as core_models
DB_NAME = "untis"
def run_using(obj):
return obj.using(DB_NAME)
TERM: mysql_models.Terms = None
def run_default_filter(qs: QuerySet, filter_term: bool = True) -> QuerySet:
""" Add a default filter in order to select the correct term """
global TERM
term_id, schoolyear_id, school_id, version_id = (
TERM.term_id,
TERM.schoolyear_id,
TERM.school_id,
TERM.version_id,
)
if filter_term:
return run_using(qs).filter(
school_id=school_id,
schoolyear_id=schoolyear_id,
version_id=version_id,
term_id=term_id,
)
else:
return run_using(qs).filter(
school_id=school_id, schoolyear_id=schoolyear_id, version_id=version_id
)
def get_term(date: date) -> mysql_models.Terms:
""" Get term valid for the provided date """
terms = run_using(mysql_models.Terms.objects).filter(
datefrom__lte=date_to_untis_date(date), dateto__gte=date_to_untis_date(date)
)
if not terms.exists():
raise Exception("Term needed")
return terms[0]
def untis_import_mysql():
global TERM
date = datetime.now().date()
TERM = get_term(date)
# Subjects
subjects = run_default_filter(mysql_models.Subjects.objects, filter_term=False)
for subject in subjects:
if not subject.name:
raise Exception("Short name needed.")
short_name = subject.name[:10]
name = subject.longname if subject.longname else short_name
new_subject, created = chronos_models.Subject.objects.get_or_create(
abbrev=short_name, defaults={"name": name}
)
new_subject.name = name
new_subject.colour_fg = untis_colour_to_hex(subject.forecolor)
new_subject.colour_bg = untis_colour_to_hex(subject.backcolor)
new_subject.save()
# Teachers
teachers = run_default_filter(mysql_models.Teacher.objects)
for teacher in teachers:
if not teacher.name:
raise Exception("Short name needed.")
short_name = teacher.name[:5]
first_name = teacher.firstname if teacher.firstname else "?"
last_name = teacher.longname if teacher.longname else teacher.name
new_teacher, created = core_models.Person.objects.get_or_create(
short_name=short_name,
defaults={"first_name": first_name, "last_name": last_name},
)
...@@ -3,6 +3,7 @@ from django.http import HttpRequest, HttpResponse ...@@ -3,6 +3,7 @@ from django.http import HttpRequest, HttpResponse
from django.shortcuts import render from django.shortcuts import render
from aleksis.core.decorators import admin_required from aleksis.core.decorators import admin_required
from .util.mysql.main import untis_import_mysql
from .forms import UntisUploadForm from .forms import UntisUploadForm
from aleksis.apps.untis.util.xml.xml import untis_import_xml from aleksis.apps.untis.util.xml.xml import untis_import_xml
...@@ -24,3 +25,14 @@ def xml_import(request: HttpRequest) -> HttpResponse: ...@@ -24,3 +25,14 @@ def xml_import(request: HttpRequest) -> HttpResponse:
context["upload_form"] = upload_form context["upload_form"] = upload_form
return render(request, "untis/untis_import.html", context) return render(request, "untis/untis_import.html", context)
@login_required
@admin_required
def mysql_import(request: HttpRequest) -> HttpResponse:
context = {}
untis_import_mysql()
return HttpResponse("Import")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment