Browse Source

Messages

main
Oliver 1 year ago
parent
commit
845cc0d1ab

+ 9
- 1
mysite/settings.py View File

@@ -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',
}

+ 18
- 0
polls/migrations/0002_notice_user_id.py View File

@@ -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),
),
]

+ 1
- 0
polls/models.py View File

@@ -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()


+ 11
- 2
polls/views.py View File

@@ -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):

+ 7
- 0
templates/base.html View File

@@ -45,6 +45,13 @@
</div>
</nav>

{% for message in messages %}
<div class="alert {{ message.tags }}" role="alert">
{{ message }}
</div>
{% endfor %}


<div class="container">
{% block content %} Platzhalter {% endblock %}


+ 5
- 1
templates/polls/index.html View File

@@ -12,7 +12,9 @@
<h3>{{ notice.notice_title }}</h3>
<p>{{ notice.notice_text }}</p>

{% if user.is_staff %}


{% if user.is_staff or notice.user_id == user.id %}
<p>
<a href="{% url 'delete' deleteId=notice.id %}"
class="btn btn-danger">
@@ -25,12 +27,14 @@

<hr>

{% if user.is_authenticated %}
<p>
<a href="{% url 'new' %}"
class="btn btn-primary">
Neue Meldung erstellen
</a>
</p>
{% endif %}


{% endblock %}

Loading…
Cancel
Save