Nápovědy v šifrovačce #46

Merged
zelvuska merged 1 commits from sifrovackav2 into master 7 months ago
  1. 4
      sifrovacka/admin.py
  2. 14
      sifrovacka/forms.py
  3. 65
      sifrovacka/migrations/0004_napoveda_napovezenoucastnikovi.py
  4. 17
      sifrovacka/models.py
  5. 48
      sifrovacka/templates/sifrovacka/napoveda.html
  6. 23
      sifrovacka/templates/sifrovacka/napovedy_list.html
  7. 2
      sifrovacka/templates/sifrovacka/sifrovacka.html
  8. 12
      sifrovacka/urls.py
  9. 27
      sifrovacka/views.py

4
sifrovacka/admin.py

@ -1,8 +1,10 @@
from django.contrib import admin from django.contrib import admin
from .models import OdpovedUcastnika, SpravnaOdpoved from .models import OdpovedUcastnika, SpravnaOdpoved, NapovezenoUcastnikovi, Napoveda
# Register your models here. # Register your models here.
admin.site.register(OdpovedUcastnika) admin.site.register(OdpovedUcastnika)
admin.site.register(SpravnaOdpoved) admin.site.register(SpravnaOdpoved)
admin.site.register(Napoveda)
admin.site.register(NapovezenoUcastnikovi)

14
sifrovacka/forms.py

@ -1,6 +1,6 @@
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.forms import ModelForm, Textarea from django.forms import ModelForm, Textarea
from .models import OdpovedUcastnika, SpravnaOdpoved from .models import OdpovedUcastnika, SpravnaOdpoved, NapovezenoUcastnikovi, Napoveda
class SifrovackaForm(ModelForm): class SifrovackaForm(ModelForm):
@ -16,3 +16,15 @@ class SifrovackaForm(ModelForm):
if SpravnaOdpoved.objects.filter(sifra=sifra).count() == 0: if SpravnaOdpoved.objects.filter(sifra=sifra).count() == 0:
raise ValidationError("Tohle číslo šifry v databázi nemáme. Zkontrolujte si ho prosím.") raise ValidationError("Tohle číslo šifry v databázi nemáme. Zkontrolujte si ho prosím.")
return sifra return sifra
class NapovedaForm(ModelForm):
class Meta:
model = NapovezenoUcastnikovi
fields = ["sifra",]
def clean_sifra(self):
sifra = self.cleaned_data.get('sifra')
if Napoveda.objects.filter(sifra=sifra).count() == 0:
raise ValidationError("K tomuto číslu šifry nemáme nápovědu. Zkonstolujte si ho prosím.")
return sifra

65
sifrovacka/migrations/0004_napoveda_napovezenoucastnikovi.py

@ -0,0 +1,65 @@
# Generated by Django 4.2.8 on 2024-04-14 12:57
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
(
"seminar",
"0114_related_name_se_zmenilo_a_django_chce_migraci_tak_dostane_migraci",
),
("sifrovacka", "0003_odpoveducastnika_uspech"),
]
operations = [
migrations.CreateModel(
name="Napoveda",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("text", models.TextField()),
("sifra", models.IntegerField()),
],
),
migrations.CreateModel(
name="NapovezenoUcastnikovi",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("sifra", models.IntegerField(verbose_name="Číslo šifry")),
(
"timestamp",
models.DateTimeField(
default=django.utils.timezone.now, verbose_name="Timestamp"
),
),
(
"resitel",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="seminar.resitel",
),
),
],
options={
"ordering": ["-timestamp"],
},
),
]

17
sifrovacka/models.py

@ -25,3 +25,20 @@ class SpravnaOdpoved(models.Model):
def __str__(self): def __str__(self):
return f"{self.sifra}: {self.odpoved}" return f"{self.sifra}: {self.odpoved}"
class NapovezenoUcastnikovi(models.Model):
class Meta:
ordering = ["-timestamp"]
resitel = models.ForeignKey(Resitel, blank=False, null=False, on_delete=models.CASCADE)
sifra = models.IntegerField("Číslo šifry", blank=False, null=False,)
timestamp = models.DateTimeField("Timestamp", blank=False, null=False, default=timezone.now)
class Napoveda(models.Model):
text = models.TextField(blank=False, null=False,)
sifra = models.IntegerField(blank=False, null=False,)
def __str__(self):
return f"{self.sifra}: {self.text}"

48
sifrovacka/templates/sifrovacka/napoveda.html

@ -0,0 +1,48 @@
{% extends "base.html" %}
{% block content %}
<br>
<h1>{% block nadpis1a %}M&Mí šifrovačka{% endblock nadpis1a %}</h1>
<br>
<h2>Získat nápovědu k šifře:</h2>
<form action="{% url 'sifrovacka_napoveda' %}" method="post">
<table class="form">
{{form.non_field_errors}}
{% for field in form %}
<tr>
<td>
<label class="field-label{% if field.field.required %} field-required{% endif %}" for="{{ field.id_for_label }}">
{{ field.label }}
</label>
</td>
<td {% if field.help_text %} class="field-with-comment"{% endif %}>
{{ field }}
<span class="field-comment">{{ field.help_text|safe }}</span>
</td>
</tr>
{% if field.errors %}
<tr>
<td colspan="2"><span class="field-error">{{ field.errors }}</span></td>
</tr>
{% endif %}
{% endfor %}
</table>
{% csrf_token %}
<input type="submit" value="Chci nápovědu">
</form>
<a href="{% url 'sifrovacka' %}">Nechceme nápovědu, známe řešení!</a>
{% endblock content %}

23
sifrovacka/templates/sifrovacka/napovedy_list.html

@ -0,0 +1,23 @@
{% extends "base.html" %}
{% block content %}
<h1>{% block nadpis1a %}Šifrovačka vzaté nápovědy{% endblock nadpis1a %}</h1>
<table class="dosla_reseni">
<tr>
<th>Timestamp</th>
<th>Řešitel</th>
<th>Šifra</th>
</tr>
{% for u in object_list %}
<tr>
<td>{{ u.timestamp }}</td>
<td>{{ u.resitel }}</td>
<td>{{ u.sifra }}</td>
</tr>
{% endfor %}
</table>
{% endblock content %}

2
sifrovacka/templates/sifrovacka/sifrovacka.html

@ -43,4 +43,6 @@
<input type="submit" value="Tak pravím!"> <input type="submit" value="Tak pravím!">
</form> </form>
<a href="{% url 'sifrovacka_napoveda' %}">Získat nápovědu</a>
{% endblock content %} {% endblock content %}

12
sifrovacka/urls.py

@ -1,7 +1,7 @@
from django.urls import path from django.urls import path
from seminar.utils import org_required, resitel_or_org_required from seminar.utils import org_required, resitel_or_org_required
from .views import SifrovackaView, SifrovackaListView from .views import SifrovackaView, SifrovackaListView, NapovedaView, NapovedaListView
urlpatterns = [ urlpatterns = [
path( path(
@ -14,4 +14,14 @@ urlpatterns = [
org_required(SifrovackaListView.as_view()), org_required(SifrovackaListView.as_view()),
name='sifrovacka_odpovedi' name='sifrovacka_odpovedi'
), ),
path(
'napoveda/',
resitel_or_org_required(NapovedaView.as_view()),
name='sifrovacka_napoveda'
),
path(
'napovedy/',
org_required(NapovedaListView.as_view()),
name='sifrovacka_napovedy'
),
] ]

27
sifrovacka/views.py

@ -2,8 +2,8 @@ from django.urls import reverse
from django.views.generic import FormView, ListView from django.views.generic import FormView, ListView
from seminar.views import formularOKView from seminar.views import formularOKView
from .forms import SifrovackaForm from .forms import SifrovackaForm, NapovedaForm
from .models import OdpovedUcastnika, SpravnaOdpoved from .models import OdpovedUcastnika, SpravnaOdpoved, Napoveda, NapovezenoUcastnikovi
from seminar.models.personalni import Resitel from seminar.models.personalni import Resitel
@ -24,10 +24,31 @@ class SifrovackaView(FormView):
instance.uspech = True instance.uspech = True
instance.save() instance.save()
return formularOKView(self.request, f'<h1>{sifra.skryty_text}</h1> <p><a href="{reverse("sifrovacka")}">Odevzdat další.</a></p><br><br><br>') return formularOKView(self.request, f'<h1>{sifra.skryty_text}</h1> <p><a href="{reverse("sifrovacka")}">Odevzdat další.</a></p><br><br><br>')
class SifrovackaListView(ListView): class SifrovackaListView(ListView):
template_name = 'sifrovacka/odpovedi_list.html' template_name = 'sifrovacka/odpovedi_list.html'
model = OdpovedUcastnika model = OdpovedUcastnika
class NapovedaView(FormView):
template_name = 'sifrovacka/napoveda.html'
form_class = NapovedaForm
def form_valid(self, form):
instance = form.save(commit=False)
resitel = Resitel.objects.get(osoba__user=self.request.user)
instance.resitel = resitel
if NapovezenoUcastnikovi.objects.filter(resitel=resitel, sifra=instance.sifra).first() is None:
instance.save()
napoveda = Napoveda.objects.filter(sifra=instance.sifra).first()
return formularOKView(self.request, f'<h1>Nápověda k šifře číslo {instance.sifra} je:</h1><p>{napoveda.text}</p> <p><a href="{reverse("sifrovacka")}">Odevzdat řešení.</a></p><br><br><br>')
class NapovedaListView(ListView):
template_name = 'sifrovacka/napovedy_list.html'
model = NapovezenoUcastnikovi

Loading…
Cancel
Save