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() widget=forms.CheckboxSelectMultiple()
) )
class UserForm(forms.ModelForm): class UserForm(forms.ModelForm):
class Meta: class Meta:
model = User model = User

View File

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

View File

@@ -8,13 +8,20 @@ admin.autodiscover()
urlpatterns = patterns('', urlpatterns = patterns('',
# Examples: # Examples:
url(r'^$', 'tipp.views.home', name='home'), url(r'^$', 'tipp.views.home', name='home'),
url(r'^login$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}), url(r'^login$', 'django.contrib.auth.views.login', {
url(r'^logout$', 'django.contrib.auth.views.logout', {'next_page': '/'}), 'template_name': 'login.html'}),
url(r'^getSeason/(?P<ls>.*)/(?P<season>\d{4})$', 'tipp.views.getSeason'), url(r'^logout$', 'django.contrib.auth.views.logout', {
url(r'^update/(?P<ls>.*)/(?P<season>\d{4})/(?P<cur_md>\d{2})$', 'tipp.views.update', name='update'), 'next_page': '/'}),
url(r'^matchday/(?P<ls>.{3,})/(?P<season>\d{4})/(?P<matchday>\d{2})$', 'tipp.views.matchday', name='matchday'), url(r'^getSeason/(?P<ls>.*)/(?P<season>\d{4})$',
url(r'^charts/(?P<ls>\w{3,})/(?P<season>\d{4})$', 'tipp.views.charts', name='charts'), 'tipp.views.getSeason'),
url(r'^charts/(?P<ls>\w{3,})/(?P<season>\d{4})/(?P<pos>\w{1,})$', 'tipp.views.charts', name='charts'), url(r'^update/(?P<ls>.*)/(?P<season>\d{4})/(?P<cur_md>\d{2})$',
'tipp.views.update', name='update'),
url(r'^matchday/(?P<ls>.{3,})/(?P<season>\d{4})/(?P<matchday>\d{2})$',
'tipp.views.matchday', name='matchday'),
url(r'^charts/(?P<ls>\w{3,})/(?P<season>\d{4})$', 'tipp.views.charts',
name='charts'),
url(r'^charts/(?P<ls>\w{3,})/(?P<season>\d{4})/(?P<pos>\w{1,})$',
'tipp.views.charts', name='charts'),
url(r'^accounts/profile/(\d+)', 'tipp.views.profile'), url(r'^accounts/profile/(\d+)', 'tipp.views.profile'),
url(r'^accounts/', include('django.contrib.auth.urls')), url(r'^accounts/', include('django.contrib.auth.urls')),
url(r'^blog/newpost$', 'tipp.views.newBlogpost'), url(r'^blog/newpost$', 'tipp.views.newBlogpost'),