Fixed getSeason() and update() in views.py
This commit is contained in:
@@ -54,7 +54,6 @@ class MandantsForm(forms.Form):
|
||||
widget=forms.CheckboxSelectMultiple()
|
||||
)
|
||||
|
||||
|
||||
class UserForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = User
|
||||
|
||||
@@ -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,9 +146,9 @@ def getSeason(request, ls, season):
|
||||
name=team['teamName'],
|
||||
iconURL = team['teamIconURL']
|
||||
)
|
||||
if t.teamID is None:
|
||||
t.save()
|
||||
|
||||
|
||||
for match in season[0]:
|
||||
m = Match(matchID=unicode(match['matchID']),
|
||||
matchDateTime=datetime.strptime(unicode(match['matchDateTime']), '%Y-%m-%d %H:%M:%S'),
|
||||
@@ -157,8 +165,11 @@ 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)
|
||||
|
||||
if form_bp.is_valid():
|
||||
p = Post(author_id=request.user.id, content=form_bp.cleaned_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()
|
||||
# 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()
|
||||
|
||||
#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'])
|
||||
|
||||
|
||||
@@ -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<ls>.*)/(?P<season>\d{4})$', 'tipp.views.getSeason'),
|
||||
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'^login$', 'django.contrib.auth.views.login', {
|
||||
'template_name': 'login.html'}),
|
||||
url(r'^logout$', 'django.contrib.auth.views.logout', {
|
||||
'next_page': '/'}),
|
||||
url(r'^getSeason/(?P<ls>.*)/(?P<season>\d{4})$',
|
||||
'tipp.views.getSeason'),
|
||||
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/', include('django.contrib.auth.urls')),
|
||||
url(r'^blog/newpost$', 'tipp.views.newBlogpost'),
|
||||
|
||||
Reference in New Issue
Block a user