Skip to content

Commit

Permalink
*** empty log message ***
Browse files Browse the repository at this point in the history
  • Loading branch information
Frans Korhonen committed Apr 26, 2009
1 parent 0d02675 commit e061dbd
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 18 deletions.
3 changes: 2 additions & 1 deletion web/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@
'web.tupa',
'django.contrib.admin',
'django.contrib.formtools',
'django.template'
'django.template',
'django.contrib.databrowse'
)
5 changes: 2 additions & 3 deletions web/templates/tupa/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

<A href='uusiKisa/maarita'>Luo uusi kisa</A><br>
<h1> Kisat:</h1>
{% if latest_kisa_list %}
{% for kisa in latest_kisa_list %}
{% if object_list %}
{% for kisa in object_list %}
<A href='{{kisa.nimi}}/' > {{kisa.nimi}} </A> <br>
{% endfor %}


{% else %}
<p>Ei määriteltyjä kisoja.</p>
{% endif %}
Expand Down
1 change: 1 addition & 0 deletions web/templates/tupa/maarita.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ <h1> {{ heading }}</h1>

{% if forms %}
{% for form in forms%}
<h2>{{form.label}}</h2>
{{form}} <br>
{% endfor%}
{% endif %}
Expand Down
Binary file modified web/tupa.db
Binary file not shown.
71 changes: 71 additions & 0 deletions web/tupa/apina.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

from models import *
from django.forms import ModelForm
from django import forms

class apina(ModelForm) :
nimi = forms.CharField()
def __init__(self,tehtava,*argv,**argkw) :
super(ModelForm,self).__init__(*argv,**argkw)
self.tehtava=tehtava
def tallenna(self):
kaava = super(ModelForm,self).save(commit=False)
kaava.tehtava = self.tehtava
kaava.nimi= self.cleaned_data['nimi']
return kaava
class Meta:
exclude = ('tehtava','kaava')
model = OsapisteKaava

psChoices= (("p","pienin"),("s","suurin"))
tyyppiChoices= (("piste","piste"),("aika","aika"))
kerroinChoices =(("1.5","1.5 (pienin tulos saa parhaat)"),("0.5","0.5 (suurin tulos saa parhaat pisteet)"))

class interpoloi(apina):
tyyppi = forms.ChoiceField(choices=tyyppiChoices)
jaettavat_pisteet = forms.FloatField()
kerroin = forms.ChoiceField(choices=kerroinChoices,widget=forms.RadioSelect)
def save(self):
kaava = self.tallenna()
maariteet = SyoteMaarite.objects.filter(tehtava=self.tehtava).filter(nimi="op_"+kaava.nimi)
maarite=None
if not maariteet :
maarite = SyoteMaarite()
else :
maarite = maaritteet[0]
maarite.nimi="op_"+kaava.nimi
maarite.tehtava = self.tehtava
maarite.tyyppi=self.cleaned_data['tyyppi']

maksimi=None
if self.cleaned_data['kerroin'] == "0.5" :
maksimi = "s"
elif self.cleaned_data['kerroin'] == "1.5" :
maksimi = "p"

kaava.kaava = "interpoloi(" +maarite.nimi +","
kaava.kaava = kaava.kaava + self.cleaned_data['kerroin']
kaava.kaava = kaava.kaava + ",med,"+ maksimi +"," + str(self.cleaned_data['jaettavat_pisteet']) + ")"
kaava.save()
maarite.save()

class kisapiste(apina):
nimi = forms.CharField()
kuvaus = forms.CharField()

def save(self):
kaava = self.tallenna()
kaava.kaava = self.maarite.nimi
maariteet = SyoteMaarite.objects.filter(tehtava=self.tehtava).filter(nimi="op_"+kaava.nimi)
if not maariteet :
maarite = SyoteMaarite()
else :
maarite = maaritteet[0]
self.maarite.nimi="op_"+kaava.nimi
self.maarite.tehtava = self.tehtava
self.maarite.kali_vihje=self.cleaned_data['kuvaus']
self.maarite.tyyppi="piste"

kaava.save()
self.maarite.save()

45 changes: 43 additions & 2 deletions web/tupa/formit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@
from models import *
from django.forms import ModelForm
from django.forms.models import inlineformset_factory
from decimal import *
import re

KaavaFormSet = inlineformset_factory(Tehtava,OsapisteKaava,extra=3 )
MaariteFormSet = inlineformset_factory(Tehtava,SyoteMaarite,extra=3 )
VartioFormSet = inlineformset_factory(Sarja,Vartio,extra=10,fields=('nro','nimi') )
SarjaFormSet = inlineformset_factory(Kisa,Sarja,extra=4 )

#TehtavaForm
def tupaform_factory(model,overrides,excludeFields=None,fields=None) :
class uusi(ModelForm) :
def __init__(self,post,instance=None,sarja=None) :

super(ModelForm,self).__init__(post,instance=instance)
def save(self):
pass
class Meta:
pass
return uusi

class TehtavaForm(ModelForm):
def __init__(self,post,instance=None,sarja=None) :
Expand All @@ -21,12 +34,13 @@ def save(self):
tehtava.save()
return tehtava
class Meta:
fields = ('nimi', 'jarjestysnro','kaava')
model = Tehtava
fields = ('nimi', 'jarjestysnro','kaava')

class PisteSyoteForm(ModelForm):
arvo = forms.FloatField(required=False)
def __init__(self,maarite=None,vartio=None,*argv,**argkw) :
def __init__(self,maarite,vartio,*argv,**argkw) :
self.arvo=forms.TimeField(required=False)
super(ModelForm,self).__init__(*argv,**argkw)
self.maarite=maarite
self.vartio=vartio
Expand All @@ -35,13 +49,40 @@ def save(self):
syote.maarite=self.maarite
syote.vartio=self.vartio
if self.cleaned_data['arvo'] :
syote.arvo = self.cleaned_data['arvo']
syote.save()
elif syote.id :
syote.delete()
class Meta:
exclude = ('maarite','vartio')
model = Syote

class AikaSyoteForm(PisteSyoteForm) :
arvo=forms.CharField(required=False)
def clean_arvo(self):
arvo=self.cleaned_data['arvo']
haku = re.match(r"^(\d*):(\d*):(\d*)\Z",arvo)
if haku:
return str(int(haku.group(1))*60*60 + int(haku.group(2))*60 + int(haku.group(3)))
elif not arvo :
return arvo
else :
raise forms.ValidationError('Syota aikaa muodossa: (hh:mm:ss)')

def SyoteForm(*argv,**argkw) :
if argv[0].tyyppi=="aika":
syotteet=Syote.objects.filter(maarite=argv[0]).filter(vartio=argv[1])
aikaVakio= None
if syotteet :
arvo = Decimal(syotteet[0].arvo)
h = divmod(arvo , 60*60)[0]
min = divmod(arvo , 60)[0]- h*60
sec = arvo - (h*60*60) - (min*60)
aikaVakio = str(h) +":"+str(min) +":"+str(sec)
return AikaSyoteForm(initial={ 'arvo': aikaVakio },*argv,**argkw)
else :
return PisteSyoteForm(*argv,**argkw)

class KisaForm(ModelForm):
class Meta:
model = Kisa
Expand Down
1 change: 1 addition & 0 deletions web/tupa/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ class Meta:
class OsapisteKaava(models.Model) :
#gen_dia_class OsapisteKaava

nimi = models.CharField(max_length=255)
kaava = models.CharField(max_length=255)
tehtava = models.ForeignKey(Tehtava)

Expand Down
Binary file modified web/tupa/tietokanta.dia
Binary file not shown.
21 changes: 14 additions & 7 deletions web/tupa/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@

import re
from formit import *

def index(request):
kisat = Kisa.objects.all()
return render_to_response('tupa/index.html', {'latest_kisa_list': kisat })
from apina import *

def kisa(request,kisa_nimi) :
kisa = get_object_or_404(Kisa, nimi=kisa_nimi)
Expand Down Expand Up @@ -107,6 +104,16 @@ def maaritaTehtava(request, kisa_nimi, tehtava_id=None, sarja_id=None):
tehtavaForm = TehtavaForm( posti,instance=tehtava,sarja=sarja )
if tehtavaForm.is_valid() :
tehtava=tehtavaForm.save()
# Osapiste
osaForm= kisapiste(tehtava, posti, prefix="osapiste")
if osaForm.is_valid() :
osaForm.save()
osaForm.label="Kisapiste generaatio:"
# Interpolointi
interForm= interpoloi(tehtava, posti, prefix="interpoloi")
if interForm.is_valid() :
interForm.save()
interForm.label="Interpolinti generaatio:"
# Määritteet
maariteFormit=MaariteFormSet(posti,instance=tehtava,prefix="maarite")
if maariteFormit.is_valid() :
Expand All @@ -125,8 +132,8 @@ def maaritaTehtava(request, kisa_nimi, tehtava_id=None, sarja_id=None):
return render_to_response('tupa/maarita.html',
{ 'heading' : "Maarita Tehtava" ,
'taakse' : "../../../" ,
'forms' : (tehtavaForm,) ,
'formsets' : ( maariteFormit,kaavaFormit,) })
'forms' : (tehtavaForm,osaForm,interForm,) ,
'formsets' : ( maariteFormit,kaavaFormit,)})

def syotaKisa(request, kisa_nimi):
sarjat = Sarja.objects.filter(kisa__nimi=kisa_nimi)
Expand Down Expand Up @@ -159,7 +166,7 @@ def syotaTehtava(request, kisa_nimi , tehtava_id) :
formi=None
if syotteet:
syote=syotteet[0]
formi = PisteSyoteForm(m,v,posti,instance=syote,prefix=v.nimi+m.nimi,)
formi = SyoteForm(m,v,posti,instance=syote,prefix=v.nimi+m.nimi,)
if formi.is_valid() :
formi.save()

Expand Down
Binary file modified web/tupa/views.pyc
Binary file not shown.
21 changes: 16 additions & 5 deletions web/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
from django.conf.urls.defaults import *

from tupa.models import *
from django.contrib import admin
admin.autodiscover()
from django.views.generic.simple import direct_to_template

genericViews = patterns('django.views.generic.list_detail',
(r'^tupa/$','object_list', {'template_name': 'tupa/index.html', 'queryset': Kisa.objects.all() } ),)
from django.contrib import databrowse

databrowse.site.register(Kisa)
databrowse.site.register(Sarja)
databrowse.site.register(Tehtava)
databrowse.site.register(SyoteMaarite)
databrowse.site.register(Syote)
databrowse.site.register(Vartio)

urlpatterns = patterns('web.tupa.views.',
(r'^tupa/$', 'index'),
databrowse = patterns('',(r'^tupa/databrowse/(.*)', databrowse.site.root),)
urlpatterns = genericViews + databrowse +patterns('web.tupa.views.',
(r'^tupa/admin/(.*)', admin.site.root ),
(r'^tupa/(?P<kisa_nimi>\w+)/$', 'kisa'),
(r'^tupa/uusiKisa/maarita/$', 'maaritaKisa'),
Expand All @@ -17,5 +29,4 @@
(r'^tupa/(?P<kisa_nimi>\w+)/syota/tehtava/(?P<tehtava_id>\d+)/$', 'syotaTehtava'),
(r'^tupa/(?P<kisa_nimi>\w+)/tulosta/$', 'tulosta'),
(r'^tupa/(?P<kisa_nimi>\w+)/tulosta/sarja/(?P<sarja_id>\d+)/$', 'tulostaSarja'),
(r'^tupa/(?P<kisa_nimi>\w+)/tulosta/piirit/$', 'piirit'),
)
(r'^tupa/(?P<kisa_nimi>\w+)/tulosta/piirit/$', 'piirit'),)

0 comments on commit e061dbd

Please sign in to comment.