From 6042838f28fbe0017093fdec5d78465ffc0028b4 Mon Sep 17 00:00:00 2001 From: Martin Bley Date: Thu, 23 Jun 2016 10:51:12 +0200 Subject: [PATCH] Fixed getSeason() and update() in views.py --- tipp/forms.py | 1 - tipp/views.py | 99 ++++++++++++++++++++++++++++++--------------------- tippy/urls.py | 21 +++++++---- 3 files changed, 73 insertions(+), 48 deletions(-) diff --git a/tipp/forms.py b/tipp/forms.py index 63afa76..d3e4bb2 100644 --- a/tipp/forms.py +++ b/tipp/forms.py @@ -54,7 +54,6 @@ class MandantsForm(forms.Form): widget=forms.CheckboxSelectMultiple() ) - class UserForm(forms.ModelForm): class Meta: model = User diff --git a/tipp/views.py b/tipp/views.py index a77c9c7..590f39c 100644 --- a/tipp/views.py +++ b/tipp/views.py @@ -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']) diff --git a/tippy/urls.py b/tippy/urls.py index 6aef1ea..3db9250 100644 --- a/tippy/urls.py +++ b/tippy/urls.py @@ -8,13 +8,20 @@ admin.autodiscover() urlpatterns = patterns('', # Examples: url(r'^$', 'tipp.views.home', name='home'), - url(r'^login$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}), - url(r'^logout$', 'django.contrib.auth.views.logout', {'next_page': '/'}), - url(r'^getSeason/(?P.*)/(?P\d{4})$', 'tipp.views.getSeason'), - url(r'^update/(?P.*)/(?P\d{4})/(?P\d{2})$', 'tipp.views.update', name='update'), - url(r'^matchday/(?P.{3,})/(?P\d{4})/(?P\d{2})$', 'tipp.views.matchday', name='matchday'), - url(r'^charts/(?P\w{3,})/(?P\d{4})$', 'tipp.views.charts', name='charts'), - url(r'^charts/(?P\w{3,})/(?P\d{4})/(?P\w{1,})$', 'tipp.views.charts', name='charts'), + url(r'^login$', 'django.contrib.auth.views.login', { + 'template_name': 'login.html'}), + url(r'^logout$', 'django.contrib.auth.views.logout', { + 'next_page': '/'}), + url(r'^getSeason/(?P.*)/(?P\d{4})$', + 'tipp.views.getSeason'), + url(r'^update/(?P.*)/(?P\d{4})/(?P\d{2})$', + 'tipp.views.update', name='update'), + url(r'^matchday/(?P.{3,})/(?P\d{4})/(?P\d{2})$', + 'tipp.views.matchday', name='matchday'), + url(r'^charts/(?P\w{3,})/(?P\d{4})$', 'tipp.views.charts', + name='charts'), + url(r'^charts/(?P\w{3,})/(?P\d{4})/(?P\w{1,})$', + 'tipp.views.charts', name='charts'), url(r'^accounts/profile/(\d+)', 'tipp.views.profile'), url(r'^accounts/', include('django.contrib.auth.urls')), url(r'^blog/newpost$', 'tipp.views.newBlogpost'),