diff --git a/mysite/settings.py b/mysite/settings.py index 427c2dd..abf2b1b 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -121,4 +121,12 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' LOGIN_REDIRECT_URL = '/polls/' LOGOUT_REDIRECT_URL = '/polls/' +from django.contrib.messages import constants as messages +MESSAGE_TAGS = { + messages.DEBUG: 'alert-secondary', + messages.INFO: 'alert-info', + messages.SUCCESS: 'alert-success', + messages.WARNING: 'alert-warning', + messages.ERROR: 'alert-danger', +} diff --git a/polls/migrations/0002_notice_user_id.py b/polls/migrations/0002_notice_user_id.py new file mode 100644 index 0000000..0a6cf01 --- /dev/null +++ b/polls/migrations/0002_notice_user_id.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.6 on 2023-12-07 13:12 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('polls', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='notice', + name='user_id', + field=models.IntegerField(blank=True, default=None, null=True), + ), + ] diff --git a/polls/models.py b/polls/models.py index f1c3226..727e142 100644 --- a/polls/models.py +++ b/polls/models.py @@ -4,6 +4,7 @@ from django.db import models class Notice(models.Model): notice_title = models.CharField(max_length=80) notice_text = models.CharField(max_length=400) + user_id = models.IntegerField(default=None, null=True, blank=True) pub_start = models.DateTimeField() pub_end = models.DateTimeField() diff --git a/polls/views.py b/polls/views.py index bfe5b2c..6eae921 100644 --- a/polls/views.py +++ b/polls/views.py @@ -4,6 +4,7 @@ from .models import Notice from django.utils import timezone from .forms import NoticeForm from django.contrib.auth.decorators import login_required +from django.contrib import messages @login_required @@ -16,8 +17,13 @@ def new(request): notice.notice_text = form.cleaned_data['notice_text'] notice.pub_start = form.cleaned_data['pub_start'] notice.pub_end = form.cleaned_data['pub_end'] + notice.user_id = request.user.id notice.save() + messages.success(request, f"Neue Nachricht erstellt") return redirect('index') + else: + for msg in form.error_messages: + messages.error(request, f"{msg}: {form.error_messages[msg]}") context = {'form': NoticeForm()} return render(request, 'polls/edit.html', context) @@ -26,10 +32,13 @@ def delete(request, deleteId=None): if deleteId is not None: try: notice = Notice.objects.get(pk=deleteId) - if request.user.id == notice.user_id: + if request.user.id == notice.user_id or request.user.is_staff: notice.delete() + messages.success(request, f"Nachricht gelöscht") + else: + messages.warning(request, f"Keine Berechtigung") except: - pass + messages.warning(request, f"Nachricht nicht gefunden") return redirect('index') def index(request): diff --git a/templates/base.html b/templates/base.html index c85e851..f3c641c 100644 --- a/templates/base.html +++ b/templates/base.html @@ -45,6 +45,13 @@ +{% for message in messages %} +
+{% endfor %} + +{{ notice.notice_text }}
- {% if user.is_staff %} + + + {% if user.is_staff or notice.user_id == user.id %}
@@ -25,12 +27,14 @@
+ {% if user.is_authenticated %}
+ {% endif %}
{% endblock %}