Velmi fuj hromadné přidání úložek
This commit is contained in:
		
							parent
							
								
									4dfe3bef7b
								
							
						
					
					
						commit
						1e2ab81bce
					
				
					 3 changed files with 76 additions and 0 deletions
				
			
		
							
								
								
									
										19
									
								
								seminar/templates/generic_form.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								seminar/templates/generic_form.html
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,19 @@ | |||
| {% extends "base.html" %} | ||||
| {% load static %} | ||||
| 
 | ||||
| 
 | ||||
| {% block content %} | ||||
| <h1> | ||||
|   {% block nadpis1a %} | ||||
|     {{ nadpis }} | ||||
|   {% endblock %} | ||||
| </h1> | ||||
| <form action="{% url 'hromadne_pridani' %}" method="post"> | ||||
|   {% csrf_token %} | ||||
|   <table class="form"> | ||||
|     {{ form.as_table }} | ||||
| </table> | ||||
|     <input type="submit" value="Odeslat"> | ||||
| </form> | ||||
| 
 | ||||
| {% endblock %} | ||||
|  | @ -105,4 +105,11 @@ urlpatterns = [ | |||
| 	path('', views.TitulniStranaView.as_view(), name='titulni_strana'), | ||||
| 	path('jak-resit/', views.JakResitView.as_view(), name='jak_resit'), | ||||
| 	path('stare-novinky/', views.StareNovinkyView.as_view(), name='stare_novinky'), | ||||
| 
 | ||||
| 	# Dočasné & neodladěné: | ||||
| 	path( | ||||
| 		'hidden/hromadne_pridani', | ||||
| 		org_required(views.HromadnePridaniView.as_view()), | ||||
| 		name="hromadne_pridani" | ||||
| 	), | ||||
| ] | ||||
|  |  | |||
|  | @ -1,4 +1,9 @@ | |||
| from http import HTTPStatus | ||||
| 
 | ||||
| from django.db import transaction | ||||
| from django.forms import Form, CharField, IntegerField | ||||
| from django.views.generic import FormView | ||||
| 
 | ||||
| import seminar.models as m | ||||
| from django.shortcuts import render, get_object_or_404 | ||||
| 
 | ||||
|  | @ -14,3 +19,48 @@ def problemView(request, pk): | |||
| 	status_code = HTTPStatus.NOT_FOUND | ||||
| 	return render(request, template, context=ctx, status=status_code) | ||||
| 
 | ||||
| 
 | ||||
| # FIXME Tohle možná patří do forms.py | ||||
| # FIXME Tohle není odladěné, selhává to nekontrolovaně | ||||
| #  a obecně je to fujky nekomentovaný kód | ||||
| class HromadnePridaniForm(Form): | ||||
| 	tema = CharField(label="Název tématu:") | ||||
| 	dil = IntegerField(label="Díl:", min_value=1) | ||||
| 	body = CharField(label="Počty bodů (0 pro problém) oddělené čárkami:") | ||||
| 
 | ||||
| 
 | ||||
| # FIXME Tohle není odladěné, selhává to nekontrolovaně | ||||
| #  a obecně je to fujky nekomentovaný kód | ||||
| class HromadnePridaniView(FormView): | ||||
| 	form_class = HromadnePridaniForm | ||||
| 	template_name = "generic_form.html" | ||||
| 	success_url = 'hromadne_pridani' | ||||
| 
 | ||||
| 	def get_context_data(self, **kwargs): | ||||
| 		context = super().get_context_data() | ||||
| 		context["nadpis"] = "Hromadné přidání úloh" | ||||
| 		return context | ||||
| 
 | ||||
| 	def form_valid(self, form): | ||||
| 		cd = form.cleaned_data | ||||
| 		tema = cd["tema"] | ||||
| 		dil = cd["dil"] | ||||
| 		body = list(map(int, cd["body"].split(","))) | ||||
| 
 | ||||
| 		t = m.Problem.objects.get(nazev__exact=tema, nadproblem=None) | ||||
| 		with transaction.atomic(): | ||||
| 			pfx = f"{t.nazev}, díl {dil}, " | ||||
| 
 | ||||
| 			for k, b in enumerate(body): | ||||
| 				u = m.Uloha.objects.create( | ||||
| 					nadproblem=t, | ||||
| 					nazev=pfx + f"{'úloha' if b > 0 else 'problém'} {k + 1}", | ||||
| 					autor=t.autor, | ||||
| 					garant=t.garant, | ||||
| 					max_body=b, | ||||
| 					cislo_zadani=m.Cislo.get(t.rocnik.rocnik, dil), | ||||
| 					kod=k, | ||||
| 					stav=m.Problem.STAV_ZADANY, | ||||
| 				) | ||||
| 				u.opravovatele.set(t.opravovatele.all()) | ||||
| 		return super().form_valid(form) | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue