Skip to content
Snippets Groups Projects
Commit 62b95bc7 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Work at #263, #259 (refactoring + dashboard)

parent cd57d3c9
No related branches found
No related tags found
1 merge request!86Merge school-apps
......@@ -6,5 +6,4 @@ urlpatterns = [
path('api', views.api_information, name="api_information"),
path('api/notifications/read/<int:id>', views.api_read_notification, name="api_read_notification"),
path('api/my-plan', views.api_my_plan_html, name="api_my_plan_html"),
path('test/', views.test_notification, name='test'),
]
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from django.shortcuts import render, redirect, get_object_or_404
from django.shortcuts import render, get_object_or_404
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils import timezone, formats
from helper import get_newest_articles, get_current_events
from schoolapps.settings import SHORT_WEEK_DAYS, LONG_WEEK_DAYS
from timetable.hints import get_all_hints_by_class_and_time_period, get_all_hints_for_teachers_by_time_period
from timetable.views import get_next_weekday_with_time, get_type_and_object_of_user, get_calendar_week
from helper import get_newest_articles, get_current_events, get_newest_article_from_news
from schoolapps.settings import LONG_WEEK_DAYS
from timetable.helper import get_name_for_next_week_day_from_today, get_type_and_object_of_user
from timetable.views import get_next_weekday_with_time, get_calendar_week
from untisconnect.api import TYPE_TEACHER, TYPE_CLASS
from untisconnect.plan import get_plan
from .models import Activity, register_notification, Notification
# from .apps import DashboardConfig
from mailer import send_mail_with_template
from .models import Activity, Notification
from userinformation import UserInformation
# Create your views here.
@login_required
def index(request):
""" Index page: Lists activities und notifications """
context = {
}
""" Dashboard: Show daily relevant information """
return render(request, 'dashboard/index.html', context)
return render(request, 'dashboard/index.html')
@login_required
def api_information(request):
""" API request: Give information for dashboard in JSON """
# Load activities
activities = Activity.objects.filter(user=request.user).order_by('-created_at')[:5]
......@@ -38,24 +31,9 @@ def api_information(request):
notifications = request.user.notifications.all().filter(user=request.user).order_by('-created_at')[:5]
unread_notifications = request.user.notifications.all().filter(user=request.user, read=False).order_by(
'-created_at')
# user_type = UserInformation.user_type(request.user)
newest_articles = get_newest_articles(limit=1)
if len(newest_articles) >= 0:
newest_article = newest_articles[0]
else:
newest_article = None
print(newest_articles)
newest_article = get_newest_article_from_news()
next_weekday = get_next_weekday_with_time(timezone.now(), timezone.now().time())
if next_weekday.date() == timezone.now().date():
date_formatted = "heute"
print("Gleicher Tag")
elif next_weekday.date() == timezone.now().date() + timezone.timedelta(days=1):
print("Nächster Tag")
date_formatted = "morgen"
else:
print("Ganz anderer Tag")
date_formatted = LONG_WEEK_DAYS[next_weekday.isoweekday() - 2]
date_formatted = get_name_for_next_week_day_from_today()
# Get user type (student, teacher, etc.)
_type, el = get_type_and_object_of_user(request.user)
......@@ -112,6 +90,8 @@ def api_information(request):
@login_required
def api_read_notification(request, id):
""" API request: Mark notification as read """
notification = get_object_or_404(Notification, id=id, user=request.user)
notification.read = True
notification.save()
......@@ -120,6 +100,8 @@ def api_read_notification(request, id):
@login_required
def api_my_plan_html(request):
""" API request: Get rendered lessons with substitutions for dashboard """
# Get user type (student, teacher, etc.)
_type, el = get_type_and_object_of_user(request.user)
hints = None
......@@ -168,16 +150,5 @@ def api_my_plan_html(request):
return JsonResponse({"success": True, "lessons": lessons})
@login_required
def test_notification(request):
""" Sends a test mail """
# send_mail_with_template("Test", [request.user.email], 'mail/email.txt', 'mail/email.html', {'user': request.user})
register_notification(user=request.user, title="Ihr Antrag wurde genehmigt",
description="Ihr Antrag XY wurde von der Schulleitung genehmigt.", app="AUB",
link=reverse("aub_details", args=[1]))
print(reverse("aub_details", args=[1]))
return redirect(reverse('dashboard'))
def error_404(request, exception):
return render(request, 'common/404.html')
......@@ -2,7 +2,6 @@ import os
from uuid import uuid4
from django.template.loader_tags import register
from datetime import datetime
from django.utils import timezone, formats
from ics import Calendar
......@@ -96,6 +95,14 @@ def get_newest_articles(domain: str = WP_DOMAIN,
return posts
def get_newest_article_from_news():
newest_articles: list = get_newest_articles(limit=1, category_whitelist=[1, 27])
if len(newest_articles) >= 0:
return newest_articles[0]
else:
return None
# Set calendar here
CALENDAR_URL: str = "https://nimbus.katharineum.de/remote.php/dav/public-calendars/owit7yysLB2CYNTq?export"
CALENDAR: Calendar = Calendar(requests.get(CALENDAR_URL).text)
......
import datetime
from django.utils import timezone
from schoolapps.settings import LONG_WEEK_DAYS
from untisconnect.api import TYPE_TEACHER, get_teacher_by_shortcode, TYPE_CLASS, get_class_by_name, get_all_teachers, \
get_all_classes, get_all_rooms, get_all_subjects
from userinformation import UserInformation
def get_name_for_next_week_day_from_today() -> str:
"""
Return the next week day as you would say it from today: "today", "tomorrow" or "<weekday>"
:return: Formatted date
"""
# Next weekday
next_weekday: timezone.datetime = get_next_weekday_with_time(timezone.now(), timezone.now().time())
if next_weekday.date() == timezone.now().date():
# Today
date_formatted = "heute"
elif next_weekday.date() == timezone.now().date() + timezone.timedelta(days=1):
# Tomorrow
date_formatted = "morgen"
else:
# Other weekday
date_formatted = LONG_WEEK_DAYS[next_weekday.isoweekday() - 2]
return date_formatted
def get_type_and_object_of_user(user):
_type = UserInformation.user_type(user)
if _type == UserInformation.TEACHER:
# Teacher
_type = TYPE_TEACHER
shortcode = user.username
el = get_teacher_by_shortcode(shortcode)
plan_id = el.id
raw_type = "teacher"
elif _type == UserInformation.STUDENT:
# Student
_type = TYPE_CLASS
_name = UserInformation.user_classes(user)[0]
el = get_class_by_name(_name)
plan_id = el.id
raw_type = "class"
else:
return None, None
return _type, el
def get_all_context():
teachers = get_all_teachers()
classes = get_all_classes()
rooms = get_all_rooms()
subjects = get_all_subjects()
context = {
'teachers': teachers,
'classes': classes,
'rooms': rooms,
'subjects': subjects
}
return context
def get_calendar_weeks(year=timezone.datetime.now().year):
weeks = []
# Get first day of year > first calendar week
first_day_of_year = timezone.datetime(year=year, month=1, day=1)
if first_day_of_year.isoweekday() != 1:
days_to_next_monday = 1 - first_day_of_year.isoweekday()
first_day_of_year += datetime.timedelta(days=days_to_next_monday)
# Go for all weeks in year and create week dict
first_day_of_week = first_day_of_year
for i in range(52):
calendar_week = i + 1
last_day_of_week = first_day_of_week + datetime.timedelta(days=4)
weeks.append({
"calendar_week": calendar_week,
"first_day": first_day_of_week,
"last_day": last_day_of_week
})
first_day_of_week += datetime.timedelta(weeks=1)
return weeks
def find_out_what_is_today(year=None, month=None, day=None):
date = timezone.datetime.now()
time = datetime.datetime.now().time()
if year is not None and day is not None and month is not None:
date = timezone.datetime(year=year, month=month, day=day)
if date != timezone.datetime.now():
time = datetime.time(0)
return date, time
def current_calendar_week():
return timezone.datetime.now().isocalendar()[1]
def current_year():
return timezone.datetime.now().year
def get_calendar_week(calendar_week, year=timezone.datetime.now().year):
weeks = get_calendar_weeks(year=year)
for week in weeks:
if week["calendar_week"] == calendar_week:
return week
return None
def get_next_weekday(date):
"""Get the next weekday by a datetime object"""
if date.isoweekday() in {6, 7}:
if date.isoweekday() == 6:
plus = 2
else:
plus = 1
date += datetime.timedelta(days=plus)
return date
def get_next_weekday_with_time(date, time) -> datetime.datetime:
"""Get the next weekday by a datetime object"""
if time > datetime.time(15, 35):
date += datetime.timedelta(days=1)
if date.isoweekday() in {6, 7}:
if date.isoweekday() == 6:
plus = 2
else:
plus = 1
date += datetime.timedelta(days=plus)
return date
import datetime
import os
import traceback
from PyPDF2 import PdfFileMerger
from django.contrib.auth.decorators import login_required, permission_required
......@@ -12,83 +11,22 @@ from debug.models import register_traceback, register_return_0
from schoolapps.settings import SHORT_WEEK_DAYS, LONG_WEEK_DAYS
from timetable.filters import HintFilter
from timetable.forms import HintForm
from timetable.helper import get_type_and_object_of_user, get_all_context, get_calendar_week, get_calendar_weeks, \
get_next_weekday, current_calendar_week, current_year, find_out_what_is_today, get_next_weekday_with_time
from timetable.hints import get_all_hints_by_time_period, get_all_hints_by_class_and_time_period, \
get_all_hints_for_teachers_by_time_period, get_all_hints_not_for_teachers_by_time_period
from timetable.pdf import generate_class_tex, generate_pdf
from untisconnect.plan import get_plan, TYPE_TEACHER, TYPE_CLASS, TYPE_ROOM, parse_lesson_times
from untisconnect.plan import get_plan, parse_lesson_times
from untisconnect.sub import get_substitutions_by_date, generate_sub_table, get_header_information
from untisconnect.api import *
from untisconnect.events import get_all_events_by_date
from userinformation import UserInformation
from schoolapps.settings import BASE_DIR
from .models import Hint
####################
# HELPER FUNCTIONS #
####################
def get_all_context():
teachers = get_all_teachers()
classes = get_all_classes()
rooms = get_all_rooms()
subjects = get_all_subjects()
context = {
'teachers': teachers,
'classes': classes,
'rooms': rooms,
'subjects': subjects
}
return context
def get_calendar_weeks(year=timezone.datetime.now().year):
weeks = []
# Get first day of year > first calendar week
first_day_of_year = timezone.datetime(year=year, month=1, day=1)
if first_day_of_year.isoweekday() != 1:
days_to_next_monday = 1 - first_day_of_year.isoweekday()
first_day_of_year += datetime.timedelta(days=days_to_next_monday)
# Go for all weeks in year and create week dict
first_day_of_week = first_day_of_year
for i in range(52):
calendar_week = i + 1
last_day_of_week = first_day_of_week + datetime.timedelta(days=4)
weeks.append({
"calendar_week": calendar_week,
"first_day": first_day_of_week,
"last_day": last_day_of_week
})
first_day_of_week += datetime.timedelta(weeks=1)
return weeks
def get_calendar_week(calendar_week, year=timezone.datetime.now().year):
weeks = get_calendar_weeks(year=year)
for week in weeks:
if week["calendar_week"] == calendar_week:
return week
return None
def get_next_weekday(date):
"""Get the next weekday by a datetime object"""
if date.isoweekday() in {6, 7}:
if date.isoweekday() == 6:
plus = 2
else:
plus = 1
date += datetime.timedelta(days=plus)
return date
#############
# OVERVIEWS #
#############
......@@ -125,8 +63,7 @@ def quicklaunch(request):
@login_required
@permission_required("timetable.show_plan")
def plan(request, plan_type, plan_id, regular="", year=timezone.datetime.now().year,
calendar_week=timezone.datetime.now().isocalendar()[1]):
def plan(request, plan_type, plan_id, regular="", year=current_year(), calendar_week=current_calendar_week()):
"""
[DJANGO VIEW]
Show a timetable (class, teacher, room, smart/regular)
......@@ -146,8 +83,8 @@ def plan(request, plan_type, plan_id, regular="", year=timezone.datetime.now().y
smart = True
# Get monday and friday of week
monday_of_week = get_calendar_week(calendar_week, year)["first_day"]
friday = monday_of_week + datetime.timedelta(days=4)
monday = get_calendar_week(calendar_week, year)["first_day"]
friday = monday + datetime.timedelta(days=4)
# Init hints
hints = None
......@@ -160,8 +97,8 @@ def plan(request, plan_type, plan_id, regular="", year=timezone.datetime.now().y
# Get hints
if smart:
hints = list(get_all_hints_for_teachers_by_time_period(monday_of_week, friday))
hints_b = list(get_all_hints_not_for_teachers_by_time_period(monday_of_week, friday))
hints = list(get_all_hints_for_teachers_by_time_period(monday, friday))
hints_b = list(get_all_hints_not_for_teachers_by_time_period(monday, friday))
elif plan_type == 'class':
# Class
......@@ -170,7 +107,7 @@ def plan(request, plan_type, plan_id, regular="", year=timezone.datetime.now().y
# Get hints
if smart:
hints = list(get_all_hints_by_class_and_time_period(el, monday_of_week, friday))
hints = list(get_all_hints_by_class_and_time_period(el, monday, friday))
elif plan_type == 'room':
# Room
......@@ -180,7 +117,7 @@ def plan(request, plan_type, plan_id, regular="", year=timezone.datetime.now().y
raise Http404('Plan not found.')
# Get plan
plan = get_plan(_type, plan_id, smart=smart, monday_of_week=monday_of_week)
plan = get_plan(_type, plan_id, smart=smart, monday_of_week=monday)
context = {
"smart": smart,
......@@ -203,40 +140,13 @@ def plan(request, plan_type, plan_id, regular="", year=timezone.datetime.now().y
return render(request, 'timetable/plan.html', context)
def get_type_and_object_of_user(user):
_type = UserInformation.user_type(user)
if _type == UserInformation.TEACHER:
# Teacher
_type = TYPE_TEACHER
shortcode = user.username
el = get_teacher_by_shortcode(shortcode)
plan_id = el.id
raw_type = "teacher"
elif _type == UserInformation.STUDENT:
# Student
_type = TYPE_CLASS
_name = UserInformation.user_classes(user)[0]
el = get_class_by_name(_name)
plan_id = el.id
raw_type = "class"
else:
return None, None
return _type, el
@login_required
@permission_required("timetable.show_plan")
def my_plan(request, year=None, month=None, day=None):
date = timezone.datetime.now()
time_now = datetime.datetime.now().time()
if year is not None and day is not None and month is not None:
date = timezone.datetime(year=year, month=month, day=day)
if date != timezone.datetime.now():
time_now = datetime.time(0)
date, time = find_out_what_is_today(year, month, day)
# Get next weekday if it is a weekend
next_weekday = get_next_weekday_with_time(date, time_now)
next_weekday = get_next_weekday_with_time(date, time)
if next_weekday != date:
return redirect("timetable_my_plan", next_weekday.year, next_weekday.month, next_weekday.day)
......@@ -291,20 +201,6 @@ def my_plan(request, year=None, month=None, day=None):
return render(request, 'timetable/myplan.html', context)
def get_next_weekday_with_time(date, time) -> datetime.datetime:
"""Get the next weekday by a datetime object"""
if time > datetime.time(15, 35):
date += datetime.timedelta(days=1)
if date.isoweekday() in {6, 7}:
if date.isoweekday() == 6:
plus = 2
else:
plus = 1
date += datetime.timedelta(days=plus)
return date
#################
# SUBSTITUTIONS #
#################
......@@ -374,15 +270,10 @@ def sub_pdf(request, plan_date=None):
def substitutions(request, year=None, month=None, day=None):
"""Show substitutions in a classic view"""
date = timezone.datetime.now()
time_now = datetime.datetime.now().time()
if year is not None and day is not None and month is not None:
date = timezone.datetime(year=year, month=month, day=day)
if date != timezone.datetime.now():
time_now = datetime.time(0)
date, time = find_out_what_is_today(year, month, day)
# Get next weekday if it is a weekend
next_weekday = get_next_weekday_with_time(date, time_now)
next_weekday = get_next_weekday_with_time(date, time)
if next_weekday != date:
return redirect("timetable_substitutions_date", next_weekday.year, next_weekday.month, next_weekday.day)
......@@ -433,7 +324,6 @@ def add_hint(request):
if form.is_valid():
i = form.save()
i.save()
# return redirect('timetable_add_hint')
form = HintForm()
msg = "success"
else:
......
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