sifrovacka #40

Merged
zelvuska merged 7 commits from sifrovacka into master 1 year ago
  1. 1
      mamweb/settings_common.py
  2. 2
      mamweb/urls.py
  3. 0
      sifrovacka/__init__.py
  4. 8
      sifrovacka/admin.py
  5. 5
      sifrovacka/apps.py
  6. 18
      sifrovacka/forms.py
  7. 34
      sifrovacka/migrations/0001_initial.py
  8. 28
      sifrovacka/migrations/0002_auto_20231015_1944.py
  9. 0
      sifrovacka/migrations/__init__.py
  10. 23
      sifrovacka/models.py
  11. 25
      sifrovacka/templates/sifrovacka/odpovedi_list.html
  12. 46
      sifrovacka/templates/sifrovacka/sifrovacka.html
  13. 17
      sifrovacka/urls.py
  14. 30
      sifrovacka/views.py

1
mamweb/settings_common.py

@ -154,6 +154,7 @@ INSTALLED_APPS = (
'soustredeni',
'treenode',
'vyroci',
'sifrovacka',
# Admin upravy:

2
mamweb/urls.py

@ -71,6 +71,8 @@ urlpatterns = [
# Výroční sraz
path('sraz/30-let/', include('vyroci.urls')),
# Miniapka na šifrovačku
path('sifrovacka/', include('sifrovacka.urls')),
]
# This is only needed when using runserver.

0
sifrovacka/__init__.py

8
sifrovacka/admin.py

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

5
sifrovacka/apps.py

@ -0,0 +1,5 @@
from django.apps import AppConfig
class SifrovackaConfig(AppConfig):
name = 'sifrovacka'

18
sifrovacka/forms.py

@ -0,0 +1,18 @@
from django.core.exceptions import ValidationError
from django.forms import ModelForm, Textarea
from .models import OdpovedUcastnika, SpravnaOdpoved
class SifrovackaForm(ModelForm):
class Meta:
model = OdpovedUcastnika
fields = ["sifra", "odpoved", ]
widgets = {
"odpoved": Textarea(attrs={'rows': 1, 'cols': 30}),
}
def clean_sifra(self):
sifra = self.cleaned_data.get('sifra')
if SpravnaOdpoved.objects.filter(sifra=sifra).count() == 0:
raise ValidationError("Tohle číslo šifry v databázi nemáme. Zkontrolujte si ho prosím.")
return sifra

34
sifrovacka/migrations/0001_initial.py

@ -0,0 +1,34 @@
# Generated by Django 3.2.22 on 2023-10-14 09:20
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('seminar', '0113_resitel_zasilat_cislo_papirove'),
]
operations = [
migrations.CreateModel(
name='SpravnaOdpoved',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('odpoved', models.TextField()),
('sifra', models.IntegerField()),
('skryty_text', models.TextField()),
],
),
migrations.CreateModel(
name='OdpovedUcastnika',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('odpoved', models.TextField(verbose_name='Tajenka')),
('sifra', models.IntegerField(verbose_name='Číslo šifry')),
('resitel', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='seminar.resitel')),
],
),
]

28
sifrovacka/migrations/0002_auto_20231015_1944.py

@ -0,0 +1,28 @@
# Generated by Django 3.2.22 on 2023-10-15 17:44
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('sifrovacka', '0001_initial'),
]
operations = [
migrations.AlterModelOptions(
name='odpoveducastnika',
options={'ordering': ['-timestamp']},
),
migrations.AddField(
model_name='odpoveducastnika',
name='timestamp',
field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='Timestamp'),
),
migrations.AlterField(
model_name='odpoveducastnika',
name='odpoved',
field=models.TextField(verbose_name='Tajenka bez diakritiky'),
),
]

0
sifrovacka/migrations/__init__.py

23
sifrovacka/models.py

@ -0,0 +1,23 @@
from django.db import models
from django.utils import timezone
from seminar.models.personalni import Resitel
# Create your models here.
class OdpovedUcastnika(models.Model):
class Meta:
ordering = ["-timestamp"]
resitel = models.ForeignKey(Resitel, blank=False, null=False, on_delete=models.CASCADE)
odpoved = models.TextField("Tajenka bez diakritiky", blank=False, null=False,)
sifra = models.IntegerField("Číslo šifry", blank=False, null=False,)
timestamp = models.DateTimeField("Timestamp", blank=False, null=False, default=timezone.now)
ledoian marked this conversation as resolved
Review

django.utils.timezone.now() mi na testwebu dává čas v UTC…

`django.utils.timezone.now()` mi na testwebu dává čas v UTC…
Review
Ale v https://mam-test.ks.matfyz.cz/sifrovacka/odpovedi/ a https://mam-test.ks.matfyz.cz/sifrovacka/ se to chová správně
Review

OK what the hell. If it ain't broke…

OK what the hell. If it ain't broke…
class SpravnaOdpoved(models.Model):
odpoved = models.TextField(blank=False, null=False,)
sifra = models.IntegerField(blank=False, null=False,)
skryty_text = models.TextField(blank=False, null=False,)

25
sifrovacka/templates/sifrovacka/odpovedi_list.html

@ -0,0 +1,25 @@
{% extends "base.html" %}
{% block content %}
<h1>{% block nadpis1a %}Šifrovačka odpovědi{% endblock nadpis1a %}</h1>
<table class="dosla_reseni">
<tr>
<th>Timestamp</th>
<th>Řešitel</th>
<th>Šifra</th>
<th>Odpověď</th>
</tr>
{% for u in object_list %}
<tr>
<td>{{ u.timestamp }}</td>
<td>{{ u.resitel }}</td>
<td>{{ u.sifra }}</td>
<td>{{ u.odpoved }}</td>
</tr>
{% endfor %}
</table>
{% endblock content %}

46
sifrovacka/templates/sifrovacka/sifrovacka.html

@ -0,0 +1,46 @@
{% extends "base.html" %}
{% block content %}
<br>
<h1>{% block nadpis1a %}M&Mí šifrovačka{% endblock nadpis1a %}</h1>
<br>
<h2>Zadat tajenku šifry:</h2>
<form action="{% url 'sifrovacka' %}" 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>
Review

To |safe je potřeba proč? V help-textech modelů by HTML být nemělo…

To `|safe` je potřeba proč? V help-textech modelů by HTML být nemělo…
Review

To jsem myslím zkopíroval z výročí :D

A určitě to není potřeba.

To jsem myslím zkopíroval z výročí :D A určitě to není potřeba.
</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="Tak pravím!">
</form>
{% endblock content %}

17
sifrovacka/urls.py

@ -0,0 +1,17 @@
from django.urls import path
from seminar.utils import org_required, resitel_or_org_required
from .views import SifrovackaView, SifrovackaListView
urlpatterns = [
path(
'',
resitel_or_org_required(SifrovackaView.as_view()),
name='sifrovacka'
),
path(
'odpovedi/',
org_required(SifrovackaListView.as_view()),
name='sifrovacka_odpovedi'
),
]

30
sifrovacka/views.py

@ -0,0 +1,30 @@
from django.urls import reverse
from django.views.generic import FormView, ListView
from seminar.views import formularOKView
from .forms import SifrovackaForm
from .models import OdpovedUcastnika, SpravnaOdpoved
from seminar.models.personalni import Resitel
# Create your views here.
class SifrovackaView(FormView):
template_name = 'sifrovacka/sifrovacka.html'
form_class = SifrovackaForm
def form_valid(self, form):
instance = form.save(commit=False)
resitel = Resitel.objects.get(osoba__user=self.request.user)
instance.resitel = resitel
instance.save()
sifra = SpravnaOdpoved.objects.filter(sifra=instance.sifra, odpoved__iexact=instance.odpoved.strip()).first()
if sifra is None:
return formularOKView(self.request, f'<h1>Bohužel vám hvězdy nebyly nakloněny. Rozumějte <i>máte to blbě</i>.</h1> <p><a href="{reverse("sifrovacka")}">Zkusit znovu.</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):
template_name = 'sifrovacka/odpovedi_list.html'
model = OdpovedUcastnika
Loading…
Cancel
Save