Rene 1 rok temu
rodzic
commit
0d40441e83

+ 3 - 0
AD/settings.py

@@ -128,3 +128,6 @@ STATICFILES_DIRS = [
 # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
 
 DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
+
+LOGIN_REDIRECT_URL = '/current-week-shifts/'
+LOGOUT_REDIRECT_URL = '/public'

+ 2 - 1
AD/urls.py

@@ -19,8 +19,9 @@ from django.urls import path, include
 from django.views.generic import RedirectView
 from django.urls import re_path as url
 
+
 urlpatterns = [
-    url(r'^favicon\.ico$',RedirectView.as_view(url='/static/favicon.png')),
+    url(r'^favicon\.ico$', RedirectView.as_view(url='/static/favicon.png')),
     path('admin/', admin.site.urls),
     path('', include('main.urls'))
 ]

BIN
db.sqlite3


+ 3 - 0
main/urls.py

@@ -1,5 +1,6 @@
 from django.urls import path
 from .views import create_multiple_shifts, current_week_shifts, edit_shift, delete_shift, delete_event, edit_event, create_event, public
+from django.contrib.auth import views as auth_views
 
 urlpatterns = [
     path('create-multiple-shifts/', create_multiple_shifts, name='create_multiple_shifts'),
@@ -9,5 +10,7 @@ urlpatterns = [
     path('delete-shift/<int:pk>/', delete_shift, name='delete_shift'),
     path('edit-event/<int:pk>/', edit_event, name='edit_event'),
     path('delete-event/<int:pk>/', delete_event, name='delete_event'),
+    path('login/', auth_views.LoginView.as_view(template_name='registration/login.html'), name='login'),
+    path('logout/', auth_views.LogoutView.as_view(next_page='/public'), name='logout'),
     path('public/', public, name='public'),
 ]

+ 7 - 0
main/views.py

@@ -3,6 +3,7 @@ from .forms import MultipleShiftForm, ShiftForm, EventForm
 from .models import Shift, Employee, Location, Event
 from django.utils.timezone import datetime, timedelta
 from django.utils import timezone
+from django.contrib.auth.decorators import user_passes_test
 import calendar
 
 
@@ -66,6 +67,12 @@ def create_event(request):
     return render(request, 'main/create_event.html', {'form': form})
 
 
+# Test function to check if the user is an admin
+def is_admin(user):
+    return user.is_authenticated and user.is_staff
+
+
+@user_passes_test(is_admin, login_url='/public')
 def current_week_shifts(request):
     # Standardmäßig die aktuelle Woche anzeigen
     today = timezone.now().date()

BIN
requirements.txt


+ 4 - 0
templates/main/current_week_shifts.html

@@ -82,6 +82,10 @@
     <div class="buttons-container">
         <a href="?start_date={{ previous_week }}" class="btn btn-primary">Eine Woche zurück</a>
         <a href="/current-week-shifts/" class="btn btn-primary">Aktuelle Woche</a>
+        <form method="post" action="{% url 'logout' %}" style="display: inline;">
+            {% csrf_token %}
+            <button type="submit" class="btn btn-secondary">Logout</button>
+        </form>
         <a href="?start_date={{ next_week }}" class="btn btn-primary">Eine Woche vorwärts</a>
     </div>
     <div class="container-fluid">

+ 7 - 7
templates/main/public.html

@@ -6,7 +6,7 @@
 <html lang="de">
 <head>
     <meta charset="UTF-8">
-    <meta http-equiv="refresh" content="120; URL=/current-week-shifts/" >
+    <meta http-equiv="refresh" content="120; URL=/public/" >
     <title>Schichten der aktuellen Woche</title>
     <link rel="icon" href="{% static 'favicon.png' %}">
     {% bootstrap_css %}
@@ -20,7 +20,6 @@
         .shift-none {
             background-color: white !important;
             text-align: center;
-            cursor: pointer;
         }
         .shift-vacation {
             background-color: blue !important;
@@ -81,7 +80,8 @@
 <body>
     <div class="buttons-container">
         <a href="?start_date={{ previous_week }}" class="btn btn-primary">Eine Woche zurück</a>
-        <a href="/current-week-shifts/" class="btn btn-primary">Aktuelle Woche</a>
+        <a href="/public/" class="btn btn-primary">Aktuelle Woche</a>
+        <a href="/login/" class="btn btn-secondary">Login</a>
         <a href="?start_date={{ next_week }}" class="btn btn-primary">Eine Woche vorwärts</a>
     </div>
     <div class="container-fluid">
@@ -103,9 +103,9 @@
                         {% for day, date in days_with_dates %}
                             {% with event=events|get_item:forloop.counter0 %}
                                 {% if event == None %}
-                                    <td class="shift-none" onclick="window.location.href='{% url 'create_event' %}?date={{ date }}'"></td>
+                                    <td class="shift-none"></td>
                                 {% else %}
-                                    <td class="shift-none" onclick="window.location.href='{% url 'edit_event' pk=event.id %}'" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-html="true" title="Name: {{ event.name }}<br> Location: {{ event.location }}<br> CVD: {{ event.cvd }}<br> CVT: {{ event.cvt }}">
+                                    <td class="shift-none" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-html="true" title="Name: {{ event.name }}<br> Location: {{ event.location }}<br> CVD: {{ event.cvd }}<br> CVT: {{ event.cvt }}">
                                             {% if event.name %}
                                                 {{ event.name }}
                                             {% else %}
@@ -131,7 +131,7 @@
                         {% for day, date in days_with_dates %}
                             {% with shift=shifts|get_item:forloop.counter0 %}
                                 {% if shift == None %}
-                                    <td class="shift-none" onclick="window.location.href='{% url 'create_multiple_shifts' %}?date={{ date }}'">
+                                    <td class="shift-none">
                                         F
                                     </td>
                                 {% else %}
@@ -145,7 +145,7 @@
                                         {% else %}
                                             shift-other
                                         {% endif %}
-                                    " onclick="window.location.href='{% url 'edit_shift' pk=shift.id %}'">
+                                    ">
                                             {% if shift.start and shift.end %}
                                                 {{ shift.start|time:"H:i" }} - {{ shift.end|time:"H:i" }}
                                             {% else %}

+ 38 - 0
templates/registration/login.html

@@ -0,0 +1,38 @@
+{% load bootstrap5 %}
+<!DOCTYPE html>
+<html lang="de">
+<head>
+    <meta charset="UTF-8">
+    <title>Login</title>
+    {% bootstrap_css %}
+    <style>
+        body {
+            font-family: Tahoma, sans-serif;
+            margin: 0;
+            padding: 0;
+            display: flex;
+            justify-content: center;
+            align-items: center;
+            height: 100vh;
+            background-color: #f8f9fa;
+        }
+        .login-container {
+            background: white;
+            padding: 20px;
+            border-radius: 8px;
+            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
+        }
+    </style>
+</head>
+<body>
+    <div class="login-container">
+        <h2>Login</h2>
+        <form method="post">
+            {% csrf_token %}
+            {% bootstrap_form form %}
+            <button type="submit" class="btn btn-primary">Login</button>
+        </form>
+    </div>
+    {% bootstrap_javascript %}
+</body>
+</html>