Fixed getSeason() and update() in views.py

This commit is contained in:
2016-06-23 10:51:12 +02:00
parent c2eede9500
commit 6042838f28
3 changed files with 73 additions and 48 deletions

View File

@@ -54,7 +54,6 @@ class MandantsForm(forms.Form):
widget=forms.CheckboxSelectMultiple()
)
class UserForm(forms.ModelForm):
class Meta:
model = User

View File

@@ -72,6 +72,7 @@ def profile(request, pk):
debug = []
debug.append('avatar: ' + str(img))
# If it's a HTTP POST, we're interested in processing form data.
if request.method == 'POST':
user_form = UserForm(data=request.POST, instance=user)
profile_form = UserProfileForm(data=request.POST, instance=profile)
@@ -83,8 +84,10 @@ def profile(request, pk):
profile = profile_form.save(commit=False)
profile.user = user
# Did the user provide a profile picture?
if 'avatar' in request.FILES:
profile.avatar = request.FILES['avatar']
# Now we save the UserProfile model instance.
profile.save()
# create thumbnail
@@ -103,11 +106,16 @@ def profile(request, pk):
for item in request.POST:
debug.append(str(item) + " -> " + str(request.POST[item]))
# Invalid form or forms - mistakes or something else?
# Print problems to the terminal.
# They'll also be shown to the user.
else:
print user_form.errors, profile_form.errors
return redirect( '/accounts/profile/' + str(user.id) )
# Not a HTTP POST, so we render our form using two ModelForm instances.
# These forms will be blank, ready for user input.
else:
user_form = UserForm(
initial={ 'last_name': user.last_name,
@@ -138,8 +146,8 @@ def getSeason(request, ls, season):
name=team['teamName'],
iconURL = team['teamIconURL']
)
t.save()
if t.teamID is None:
t.save()
for match in season[0]:
m = Match(matchID=unicode(match['matchID']),
@@ -157,9 +165,12 @@ def getSeason(request, ls, season):
)
m.save()
return redirect("matchday", ls=ls, season=season, matchday=md)
return redirect("matchday",
ls=ls,
season=get_current_season(),
matchday=str(get_current_md(ls)).zfill(2)
)
@login_required
def update(request, ls, season, cur_md):
"""
@@ -258,7 +269,11 @@ def update(request, ls, season, cur_md):
tipp.save()
continue
return redirect("matchday", ls=ls, season=season, matchday=cur_md)
return redirect("matchday",
ls=ls,
season=season,
matchday=str(get_current_md(ls)).zfill(2)
)
@login_required
def matchday(request, ls, season, matchday, template_name='md.html'):
@@ -390,9 +405,8 @@ def matchday(request, ls, season, matchday, template_name='md.html'):
matches.append(item)
# get the newest blogposts
pIDs = RelPostMandant.objects.filter(mandant__in=mandants).values_list('post', flat=True)
posts = []
for post in Post.objects.filter(published=True, id__in=pIDs)[:1]:
for post in Post.objects.filter(published=True)[:1]:
try:
avatar = UserProfile.objects.get(user_id=post.author_id).avatar.name
except:
@@ -506,61 +520,66 @@ def blogindex(request, page):
@login_required
def newBlogpost(request):
debug = ''
ms = Mandant.objects.filter(
id__in=RelUserMandant.objects.filter(user=request.user).values_list('mandant', flat=True)
)
choices = []
for m in ms:
choices.append((m.name, m.description))
has_choices = False
if ms.count() > 1:
has_choices = True
if request.method == "POST":
"""
if this is a POST request we need to process the form data
"""
# create form instance and populate it with data from the request
if request == 'POST':
form_bp = BlogpostForm(request.POST)
form_mandant = MandantsForm(request.POST)
if form_bp.is_valid():
p = Post(author_id=request.user.id, content=form_bp.cleaned_data['content'])
p.save()
# check whether it's valid:
if form_bp.is_valid() and form_mandant.is_valid():
data = form.cleaned_data
#p = Post(author_id=request.user.id, content=data['content'])
#p.save()
if has_choices is True:
form_mandant = MandantsForm(request.POST)
form_mandant.fields["mandants"].choices = choices
if form_mandant.is_valid():
for m in form_mandant.cleaned_data['mandants']:
r = RelPostMandant(mandant=Mandant.objects.get(name=m), post=p)
r.save()
else:
r = RelPostMandant(mandant=Mandant.objects.get(name=ms[0]), post=p)
r.save()
return redirect( '/blog/1')
#for m in data['mandants']:
# pass
#debug += str(m) + " "
#mandants = RelUserMandant.objects.filter(user=request.user).values_list('mandant', flat=True)
"""
for m in mandants:
r = RelPostMandant(mandant=Mandant.objects.get(id=m), post=p)
r.save()
"""
# ...
# redirect to a new URL:
return redirect( '/blog/1')
blogpostForm = BlogpostForm()
form_mandant = MandantsForm()
form_mandant.fields["mandants"].choices = choices
form_mandant = MandantsForm(user=request.user)
return render(request, 'newblogpost.html', {
#'debug': debug,
'debug': debug,
'form_bp': blogpostForm,
'form_mandants': form_mandant.as_table(),
'has_choices': has_choices,
'ls': get_current_ls(),
'season': get_current_season()
})
def logout(request):
"""
Log users out and re-direct them to the main page.
"""
logout(request)
return HttpResponseRedirect('/')
def get_current_md(ls):
"""
get current matchday()
returns dict with keys
['groupName']
['groupOrderID']
['groupID']
"""
try:
ol = OpenLiga()
cur_md = ol.getCurrentGroup(ls)
except:
cur_md = 1
cur_md = {'groupName': u"1. Spieltag", 'groupOrderID': 1}
return(cur_md['groupOrderID'])