LOGIN_REDIRECT_URL = '/polls/' | LOGIN_REDIRECT_URL = '/polls/' | ||||
LOGOUT_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', | |||||
} |
# 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), | |||||
), | |||||
] |
class Notice(models.Model): | class Notice(models.Model): | ||||
notice_title = models.CharField(max_length=80) | notice_title = models.CharField(max_length=80) | ||||
notice_text = models.CharField(max_length=400) | notice_text = models.CharField(max_length=400) | ||||
user_id = models.IntegerField(default=None, null=True, blank=True) | |||||
pub_start = models.DateTimeField() | pub_start = models.DateTimeField() | ||||
pub_end = models.DateTimeField() | pub_end = models.DateTimeField() | ||||
from django.utils import timezone | from django.utils import timezone | ||||
from .forms import NoticeForm | from .forms import NoticeForm | ||||
from django.contrib.auth.decorators import login_required | from django.contrib.auth.decorators import login_required | ||||
from django.contrib import messages | |||||
@login_required | @login_required | ||||
notice.notice_text = form.cleaned_data['notice_text'] | notice.notice_text = form.cleaned_data['notice_text'] | ||||
notice.pub_start = form.cleaned_data['pub_start'] | notice.pub_start = form.cleaned_data['pub_start'] | ||||
notice.pub_end = form.cleaned_data['pub_end'] | notice.pub_end = form.cleaned_data['pub_end'] | ||||
notice.user_id = request.user.id | |||||
notice.save() | notice.save() | ||||
messages.success(request, f"Neue Nachricht erstellt") | |||||
return redirect('index') | return redirect('index') | ||||
else: | |||||
for msg in form.error_messages: | |||||
messages.error(request, f"{msg}: {form.error_messages[msg]}") | |||||
context = {'form': NoticeForm()} | context = {'form': NoticeForm()} | ||||
return render(request, 'polls/edit.html', context) | return render(request, 'polls/edit.html', context) | ||||
if deleteId is not None: | if deleteId is not None: | ||||
try: | try: | ||||
notice = Notice.objects.get(pk=deleteId) | 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() | notice.delete() | ||||
messages.success(request, f"Nachricht gelöscht") | |||||
else: | |||||
messages.warning(request, f"Keine Berechtigung") | |||||
except: | except: | ||||
pass | |||||
messages.warning(request, f"Nachricht nicht gefunden") | |||||
return redirect('index') | return redirect('index') | ||||
def index(request): | def index(request): |
</div> | </div> | ||||
</nav> | </nav> | ||||
{% for message in messages %} | |||||
<div class="alert {{ message.tags }}" role="alert"> | |||||
{{ message }} | |||||
</div> | |||||
{% endfor %} | |||||
<div class="container"> | <div class="container"> | ||||
{% block content %} Platzhalter {% endblock %} | {% block content %} Platzhalter {% endblock %} | ||||
<h3>{{ notice.notice_title }}</h3> | <h3>{{ notice.notice_title }}</h3> | ||||
<p>{{ notice.notice_text }}</p> | <p>{{ notice.notice_text }}</p> | ||||
{% if user.is_staff %} | |||||
{% if user.is_staff or notice.user_id == user.id %} | |||||
<p> | <p> | ||||
<a href="{% url 'delete' deleteId=notice.id %}" | <a href="{% url 'delete' deleteId=notice.id %}" | ||||
class="btn btn-danger"> | class="btn btn-danger"> | ||||
<hr> | <hr> | ||||
{% if user.is_authenticated %} | |||||
<p> | <p> | ||||
<a href="{% url 'new' %}" | <a href="{% url 'new' %}" | ||||
class="btn btn-primary"> | class="btn btn-primary"> | ||||
Neue Meldung erstellen | Neue Meldung erstellen | ||||
</a> | </a> | ||||
</p> | </p> | ||||
{% endif %} | |||||
{% endblock %} | {% endblock %} |