First import of migration to python 3
This commit is contained in:
395
tipp/views.py
395
tipp/views.py
@@ -17,41 +17,30 @@ GNU General Public License for more details.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with TipPy. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import render_to_response, redirect, render, \
|
||||
get_object_or_404
|
||||
from django.contrib.auth import authenticate, login
|
||||
from django.contrib.auth.models import User
|
||||
"""
|
||||
import django.shortcuts
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.views import password_reset, \
|
||||
password_reset_confirm
|
||||
from django.core.context_processors import csrf
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.core.paginator import Paginator, InvalidPage, EmptyPage, \
|
||||
from django.core.paginator import Paginator, EmptyPage, \
|
||||
PageNotAnInteger
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.utils import timezone
|
||||
from django.utils.text import slugify
|
||||
from django.db.models import Sum, Max
|
||||
from django.db.models import Sum
|
||||
from django.conf import settings
|
||||
from os.path import join as pjoin
|
||||
from tipp.models import *
|
||||
from tipp.forms import *
|
||||
from openliga import *
|
||||
from tipp.openliga import *
|
||||
from datetime import datetime
|
||||
from PIL import Image as PImage
|
||||
import urllib2, pytz, os, sys
|
||||
|
||||
# setting utf-8 as default encoding to avoid errors in file
|
||||
# uploads and form data
|
||||
reload(sys)
|
||||
sys.setdefaultencoding('utf8')
|
||||
import pytz
|
||||
import os
|
||||
|
||||
timezoneLocal = pytz.timezone('Europe/Berlin')
|
||||
|
||||
|
||||
def about(request):
|
||||
return render(request, 'about.html')
|
||||
return django.shortcuts.render(request, 'about.html')
|
||||
|
||||
|
||||
@login_required
|
||||
@@ -60,11 +49,12 @@ def home(request):
|
||||
season = get_current_season()
|
||||
md = get_current_md(ls)
|
||||
|
||||
return redirect("matchday",
|
||||
ls = ls,
|
||||
season = season,
|
||||
matchday=str(md).zfill(2)
|
||||
)
|
||||
return django.shortcuts.redirect("matchday",
|
||||
ls=ls,
|
||||
season=season,
|
||||
matchday=str(md).zfill(2)
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
def profile(request, pk):
|
||||
@@ -75,7 +65,7 @@ def profile(request, pk):
|
||||
img = profile.avatar.name
|
||||
else:
|
||||
img = None
|
||||
|
||||
|
||||
debug = []
|
||||
debug.append('avatar: ' + str(img))
|
||||
|
||||
@@ -87,7 +77,7 @@ def profile(request, pk):
|
||||
)
|
||||
|
||||
if user_form.is_valid() and profile_form.is_valid():
|
||||
|
||||
|
||||
user = user_form.save()
|
||||
|
||||
profile = profile_form.save(commit=False)
|
||||
@@ -102,49 +92,52 @@ def profile(request, pk):
|
||||
# create thumbnail
|
||||
imfn = pjoin(settings.MEDIA_ROOT, profile.avatar.name)
|
||||
im = PImage.open(imfn)
|
||||
im.thumbnail((50,50), PImage.ANTIALIAS)
|
||||
im.thumbnail((50, 50), PImage.ANTIALIAS)
|
||||
im.save(settings.MEDIA_ROOT + profile.avatar.name)
|
||||
if 'avatar-clear' in request.POST:
|
||||
if 'on' in request.POST['avatar-clear']:
|
||||
image = pjoin(settings.MEDIA_ROOT, img)
|
||||
debug.append('Is the image ' + str(image)
|
||||
+ ' a regular file? '
|
||||
+ str(os.path.isfile(image))
|
||||
)
|
||||
debug.append('Is the image ' + str(image)
|
||||
+ ' a regular file? '
|
||||
+ str(os.path.isfile(image))
|
||||
)
|
||||
if os.path.isfile(image):
|
||||
os.remove(image)
|
||||
profile.save()
|
||||
|
||||
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:
|
||||
print user_form.errors, profile_form.errors
|
||||
print(
|
||||
user_form.errors,
|
||||
profile_form.errors
|
||||
)
|
||||
|
||||
return redirect( '/accounts/profile/' + str(user.id) )
|
||||
return django.shortcuts.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,
|
||||
'first_name': user.first_name,
|
||||
'email': user.email },
|
||||
initial={'last_name': user.last_name,
|
||||
'first_name': user.first_name,
|
||||
'email': user.email},
|
||||
instance=user
|
||||
)
|
||||
profile_form = UserProfileForm(instance=profile)
|
||||
|
||||
return render(request, 'registration/profile.html', {
|
||||
'user_form': user_form,
|
||||
return django.shortcuts.render(request, 'registration/profile.html', {
|
||||
'user_form': user_form,
|
||||
'profile_form': profile_form,
|
||||
'debug': debug,
|
||||
'img': img}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@@ -152,14 +145,13 @@ def getSeason(request, ls, season):
|
||||
ol = OpenLiga()
|
||||
teams = ol.getTeams(str(season), ls)
|
||||
season = ol.getSeason(str(season), ls)
|
||||
md = get_current_md(ls)
|
||||
|
||||
return_vals = []
|
||||
for team in teams[0]:
|
||||
t = Team(
|
||||
teamID=team['teamID'],
|
||||
name=team['teamName'],
|
||||
iconURL = team['teamIconURL']
|
||||
iconURL=team['teamIconURL']
|
||||
)
|
||||
|
||||
"""
|
||||
@@ -167,36 +159,34 @@ def getSeason(request, ls, season):
|
||||
"""
|
||||
try:
|
||||
to = Team.objects.get(teamID=int(t.teamID))
|
||||
if to.iconURL is None:
|
||||
t.save(update_fields=['iconURL'])
|
||||
except Team.DoesNotExist:
|
||||
t.save()
|
||||
|
||||
if to.iconURL is None:
|
||||
t.save(update_fields=['iconURL'])
|
||||
|
||||
|
||||
for match in season[0]:
|
||||
m = Match(matchID=unicode(match['matchID']),
|
||||
matchDateTime=datetime.strptime(
|
||||
unicode(match['matchDateTime']), '%Y-%m-%d %H:%M:%S'),
|
||||
group=int(match['groupID']),
|
||||
matchday=int(match['groupOrderID']),
|
||||
matchday_name=unicode(match['groupName']),
|
||||
idTeam1=Team.objects.get(teamID=int(match['idTeam1'])),
|
||||
idTeam2=Team.objects.get(teamID=int(match['idTeam2'])),
|
||||
pointsTeam1=match['pointsTeam1'],
|
||||
pointsTeam2=match['pointsTeam2'],
|
||||
finished=bool(match['matchIsFinished']),
|
||||
season=match['leagueSaison'],
|
||||
leagueShortcut=match['leagueShortcut']
|
||||
)
|
||||
m = Match(matchID=match['matchID'],
|
||||
matchDateTime=match['matchDateTime'],
|
||||
group=int(match['groupID']),
|
||||
matchday=int(match['groupOrderID']),
|
||||
matchday_name=match['groupName'],
|
||||
idTeam1=Team.objects.get(teamID=int(match['idTeam1'])),
|
||||
idTeam2=Team.objects.get(teamID=int(match['idTeam2'])),
|
||||
pointsTeam1=match['pointsTeam1'],
|
||||
pointsTeam2=match['pointsTeam2'],
|
||||
finished=bool(match['matchIsFinished']),
|
||||
season=match['leagueSaison'],
|
||||
leagueShortcut=match['leagueShortcut']
|
||||
)
|
||||
m.save()
|
||||
|
||||
return redirect("matchday",
|
||||
ls=ls,
|
||||
season=get_current_season(),
|
||||
matchday=str(get_current_md(ls)).zfill(2)
|
||||
)
|
||||
|
||||
return django.shortcuts.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):
|
||||
"""
|
||||
@@ -205,7 +195,7 @@ def update(request, ls, season, cur_md):
|
||||
"""
|
||||
|
||||
ol = OpenLiga()
|
||||
md_matches = ol.getMatchday(str(season), int(cur_md) ,ls)
|
||||
md_matches = ol.getMatchday(str(season), int(cur_md), ls)
|
||||
|
||||
# Achtung: DateTimeField prüfen
|
||||
for match in md_matches[0]:
|
||||
@@ -214,40 +204,46 @@ def update(request, ls, season, cur_md):
|
||||
try:
|
||||
for result in matchResults[0]:
|
||||
if result['resultName'] == "Halbzeit":
|
||||
pointsTeam1=int(result['pointsTeam1'])
|
||||
pointsTeam2=int(result['pointsTeam2'])
|
||||
pointsTeam1 = int(result['pointsTeam1'])
|
||||
pointsTeam2 = int(result['pointsTeam2'])
|
||||
elif result['resultName'] == "Endergebnis":
|
||||
pointsTeam1=int(result['pointsTeam1'])
|
||||
pointsTeam2=int(result['pointsTeam2'])
|
||||
pointsTeam1 = int(result['pointsTeam1'])
|
||||
pointsTeam2 = int(result['pointsTeam2'])
|
||||
break
|
||||
elif str(result['resultName']) == u"nach Verlängerung":
|
||||
pointsTeam1=int(result['pointsTeam1'])
|
||||
pointsTeam2=int(result['pointsTeam2'])
|
||||
pointsTeam1 = int(result['pointsTeam1'])
|
||||
pointsTeam2 = int(result['pointsTeam2'])
|
||||
else:
|
||||
pointsTeam1=-1
|
||||
pointsTeam2=-1
|
||||
pointsTeam1 = -1
|
||||
pointsTeam2 = -1
|
||||
|
||||
except:
|
||||
pointsTeam1=-1
|
||||
pointsTeam2=-1
|
||||
|
||||
pointsTeam1 = -1
|
||||
pointsTeam2 = -1
|
||||
|
||||
m = Match(matchID=unicode(match['matchID']),
|
||||
matchDateTime=match['matchDateTime'],
|
||||
pointsTeam1=pointsTeam1,
|
||||
pointsTeam2=pointsTeam2,
|
||||
finished=bool(match['matchIsFinished']),
|
||||
)
|
||||
m = Match(matchID=match['matchID'],
|
||||
matchDateTime=match['matchDateTime'],
|
||||
pointsTeam1=pointsTeam1,
|
||||
pointsTeam2=pointsTeam2,
|
||||
finished=bool(match['matchIsFinished']),
|
||||
)
|
||||
m.save(update_fields=['matchDateTime', 'pointsTeam1', 'pointsTeam2', 'finished'])
|
||||
pointsTeam1=-1
|
||||
pointsTeam2=-1
|
||||
pointsTeam1 = -1
|
||||
pointsTeam2 = -1
|
||||
|
||||
# evaluate scores
|
||||
scores = Score.objects.get(client=Mandant.objects.get(name='huse'))
|
||||
finished_matches=Match.objects.filter(
|
||||
finished=True,
|
||||
# evaluate scores
|
||||
""" get mandants current user is related """
|
||||
mandants = RelUserMandant.objects.filter(user=request.user). \
|
||||
values_list('mandant', flat=True)
|
||||
|
||||
# scores = Score.objects.get(client=Mandant.objects.get(name='huse'))
|
||||
scores = Score.objects.get(client=mandants[0])
|
||||
|
||||
finished_matches = Match.objects.filter(
|
||||
finished=True,
|
||||
leagueShortcut=ls,
|
||||
season=season,
|
||||
matchday = cur_md
|
||||
matchday=cur_md
|
||||
)
|
||||
for match in finished_matches:
|
||||
tipps = Tipp.objects.filter(matchID=match.matchID)
|
||||
@@ -256,8 +252,8 @@ def update(request, ls, season, cur_md):
|
||||
goals_total = match.pointsTeam1 + match.pointsTeam2
|
||||
|
||||
# Tipp is acurate
|
||||
if (tipp.pointsTeam1 == match.pointsTeam1
|
||||
and tipp.pointsTeam2 == match.pointsTeam2):
|
||||
if (tipp.pointsTeam1 == match.pointsTeam1
|
||||
and tipp.pointsTeam2 == match.pointsTeam2):
|
||||
if goals_total >= 5:
|
||||
score = scores.exact_high
|
||||
else:
|
||||
@@ -266,29 +262,29 @@ def update(request, ls, season, cur_md):
|
||||
tipp.score = score
|
||||
tipp.save()
|
||||
continue
|
||||
|
||||
|
||||
# who won?
|
||||
winnerReal = None
|
||||
if match.pointsTeam1 > match.pointsTeam2:
|
||||
winnerReal = 1
|
||||
elif (match.pointsTeam2 > match.pointsTeam1):
|
||||
elif match.pointsTeam2 > match.pointsTeam1:
|
||||
winnerReal = 2
|
||||
elif (match.pointsTeam1 == match.pointsTeam2):
|
||||
elif match.pointsTeam1 == match.pointsTeam2:
|
||||
winnerReal = 0
|
||||
|
||||
winnerTipp = None
|
||||
if tipp.pointsTeam1 > tipp.pointsTeam2:
|
||||
winnerTipp = 1
|
||||
elif (tipp.pointsTeam2 > tipp.pointsTeam1):
|
||||
elif tipp.pointsTeam2 > tipp.pointsTeam1:
|
||||
winnerTipp = 2
|
||||
elif (tipp.pointsTeam1 == tipp.pointsTeam2):
|
||||
elif tipp.pointsTeam1 == tipp.pointsTeam2:
|
||||
winnerTipp = 0
|
||||
|
||||
|
||||
diffReal = match.pointsTeam1 - match.pointsTeam2
|
||||
diffTipp = tipp.pointsTeam1 - tipp.pointsTeam2
|
||||
|
||||
if ( winnerTipp == winnerReal ):
|
||||
if ( diffTipp == diffReal ):
|
||||
if winnerTipp == winnerReal:
|
||||
if diffTipp == diffReal:
|
||||
score = scores.diff
|
||||
else:
|
||||
score = scores.tendency
|
||||
@@ -301,11 +297,12 @@ def update(request, ls, season, cur_md):
|
||||
tipp.save()
|
||||
continue
|
||||
|
||||
return redirect("matchday",
|
||||
ls=ls,
|
||||
season=season,
|
||||
matchday=cur_md
|
||||
)
|
||||
return django.shortcuts.redirect("matchday",
|
||||
ls=ls,
|
||||
season=season,
|
||||
matchday=cur_md
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
def matchday(request, ls, season, matchday, template_name='md.html'):
|
||||
@@ -314,8 +311,8 @@ def matchday(request, ls, season, matchday, template_name='md.html'):
|
||||
has_refresh = True
|
||||
|
||||
""" get matches of the matchday """
|
||||
md_matches = Match.objects.filter(leagueShortcut=ls, season=season,
|
||||
matchday=int(matchday)).order_by('matchDateTime')
|
||||
md_matches = Match.objects.filter(leagueShortcut=ls, season=season,
|
||||
matchday=int(matchday)).order_by('matchDateTime')
|
||||
|
||||
""" get the current user """
|
||||
user = User.objects.get(username=request.user.username)
|
||||
@@ -323,7 +320,7 @@ def matchday(request, ls, season, matchday, template_name='md.html'):
|
||||
""" get mandants current user is related """
|
||||
mandants = RelUserMandant.objects.filter(user=request.user). \
|
||||
values_list('mandant', flat=True)
|
||||
|
||||
|
||||
""" get mates """
|
||||
mandant_dict = {}
|
||||
for m in mandants:
|
||||
@@ -332,8 +329,8 @@ def matchday(request, ls, season, matchday, template_name='md.html'):
|
||||
rs = RelUserMandant.objects.filter(mandant=m)
|
||||
|
||||
for r in rs:
|
||||
mandant_dict[mandant.description].append({
|
||||
'mate': r.user,
|
||||
mandant_dict[mandant.description].append({
|
||||
'mate': r.user,
|
||||
'tipps': [],
|
||||
'sum_score': 0
|
||||
})
|
||||
@@ -350,7 +347,7 @@ def matchday(request, ls, season, matchday, template_name='md.html'):
|
||||
"""
|
||||
try:
|
||||
tipp = Tipp.objects.get(tipperID=user.id,
|
||||
matchID=match.matchID)
|
||||
matchID=match.matchID)
|
||||
except ObjectDoesNotExist:
|
||||
tipp = None
|
||||
|
||||
@@ -363,52 +360,52 @@ def matchday(request, ls, season, matchday, template_name='md.html'):
|
||||
if tipp is not None:
|
||||
""" Tipp exists an needs an UPDATE"""
|
||||
tipp = Tipp(id=tipp.pk,
|
||||
matchID=match,
|
||||
tipperID=user,
|
||||
pointsTeam1=data['tippTeam1'],
|
||||
pointsTeam2=data['tippTeam2']
|
||||
)
|
||||
matchID=match,
|
||||
tipperID=user,
|
||||
pointsTeam1=data['tippTeam1'],
|
||||
pointsTeam2=data['tippTeam2']
|
||||
)
|
||||
tipp.save()
|
||||
|
||||
else:
|
||||
else:
|
||||
""" do INSERT """
|
||||
tipp = Tipp( matchID=match,
|
||||
tipperID=user,
|
||||
pointsTeam1=data['tippTeam1'],
|
||||
pointsTeam2=data['tippTeam2'])
|
||||
tipp = Tipp(matchID=match,
|
||||
tipperID=user,
|
||||
pointsTeam1=data['tippTeam1'],
|
||||
pointsTeam2=data['tippTeam2'])
|
||||
tipp.save()
|
||||
|
||||
match_started = timezone.now() >= match.matchDateTime
|
||||
|
||||
if tipp is not None:
|
||||
f = TippForm(prefix=str(match.matchID),
|
||||
initial={'tippTeam1': tipp.pointsTeam1,
|
||||
'tippTeam2': tipp.pointsTeam2}
|
||||
)
|
||||
initial={'tippTeam1': tipp.pointsTeam1,
|
||||
'tippTeam2': tipp.pointsTeam2}
|
||||
)
|
||||
else:
|
||||
f = TippForm(prefix=str(match.matchID))
|
||||
|
||||
item = {}
|
||||
item['matchDateTime'] = (match.matchDateTime. \
|
||||
astimezone(timezoneLocal).strftime('%a %d.%m, %H:%M'))
|
||||
item['matchDateTime'] = (match.matchDateTime.
|
||||
astimezone(timezoneLocal).strftime('%a %d.%m, %H:%M'))
|
||||
|
||||
item['iconTeam1']= str(match.idTeam1.icon)
|
||||
item['iconTeam1'] = str(match.idTeam1.icon)
|
||||
item['iconURLTeam1'] = str(match.idTeam1.iconURL)
|
||||
item['nameTeam1']= str(match.idTeam1)
|
||||
item['abbrTeam1']= str(match.idTeam1.abbr)
|
||||
item['nameTeam1'] = str(match.idTeam1.name)
|
||||
item['abbrTeam1'] = str(match.idTeam1.abbr)
|
||||
item['iconTeam2'] = str(match.idTeam2.icon)
|
||||
item['iconURLTeam2'] = str(match.idTeam2.iconURL)
|
||||
item['nameTeam2'] = str(match.idTeam2)
|
||||
item['abbrTeam2']= str(match.idTeam2.abbr)
|
||||
item['nameTeam2'] = str(match.idTeam2.name)
|
||||
item['abbrTeam2'] = str(match.idTeam2.abbr)
|
||||
item['started'] = match_started
|
||||
item['finished'] = match.finished
|
||||
item['md_name'] = match.matchday_name
|
||||
|
||||
if (match.pointsTeam1 == -1 or match.pointsTeam2 == -1):
|
||||
if (match.pointsTeam1 == -1 or match.pointsTeam2 == -1):
|
||||
item['matchResult'] = "-:-"
|
||||
else:
|
||||
item['matchResult'] = str(match.pointsTeam1) + ":" \
|
||||
+ str(match.pointsTeam2)
|
||||
+ str(match.pointsTeam2)
|
||||
|
||||
item['tippFormTeam1'] = f['tippTeam1']
|
||||
item['tippFormTeam2'] = f['tippTeam2']
|
||||
@@ -422,27 +419,26 @@ def matchday(request, ls, season, matchday, template_name='md.html'):
|
||||
for mate in mandant_dict[mandant]:
|
||||
try:
|
||||
matetipp = Tipp.objects.get(tipperID=mate['mate'],
|
||||
matchID=match.matchID)
|
||||
matchID=match.matchID)
|
||||
if match_started is True:
|
||||
if matetipp.score is None:
|
||||
score = u'\u231B'
|
||||
else:
|
||||
score = matetipp.score
|
||||
|
||||
mate['tipps'].append(str(matetipp.pointsTeam1)
|
||||
+ ":" + str(matetipp.pointsTeam2)
|
||||
+ "(" + str(score) + ")")
|
||||
mate['tipps'].append(str(matetipp.pointsTeam1)
|
||||
+ ":" + str(matetipp.pointsTeam2)
|
||||
+ "(" + str(score) + ")")
|
||||
if match.finished is True:
|
||||
mate['sum_score'] += matetipp.score
|
||||
else:
|
||||
mate['tipps'].append(u'\u2714')
|
||||
mate['tipps'].append(u'\u2714')
|
||||
except Exception as e:
|
||||
mate['tipps'].append(u'\u2717')
|
||||
|
||||
""" sort items in mandant_dict by score """
|
||||
mandant_dict[mandant] = sorted(mandant_dict[mandant],
|
||||
key=lambda k: k['sum_score'], reverse=True)
|
||||
mate['tipps'].append(u'\u2717')
|
||||
|
||||
""" sort items in mandant_dict by score """
|
||||
mandant_dict[mandant] = sorted(mandant_dict[mandant],
|
||||
key=lambda k: k['sum_score'], reverse=True)
|
||||
|
||||
matches.append(item)
|
||||
|
||||
@@ -454,54 +450,54 @@ def matchday(request, ls, season, matchday, template_name='md.html'):
|
||||
try:
|
||||
avatar = UserProfile.objects.get(user_id=post.author_id). \
|
||||
avatar.name
|
||||
except:
|
||||
except:
|
||||
avatar = None
|
||||
posts.append( (post, avatar) )
|
||||
posts.append((post, avatar))
|
||||
|
||||
try:
|
||||
cur_group = matches[0]['md_name']
|
||||
cur_group = matches[0]['md_name']
|
||||
except:
|
||||
cur_group = "N/A"
|
||||
|
||||
debug += str(mandant_dict)
|
||||
|
||||
return render(request, 'md.html', {
|
||||
'debug': debug,
|
||||
#'mds_season': mds_in_season,
|
||||
return django.shortcuts.render(request, 'md.html', {
|
||||
'debug': debug,
|
||||
# 'mds_season': mds_in_season,
|
||||
'cur_md': matchday,
|
||||
'md_name': cur_group,
|
||||
'matches': matches,
|
||||
'matches': matches,
|
||||
'ls': ls,
|
||||
'season': season,
|
||||
'username': user,
|
||||
'mandant_dict': mandant_dict,
|
||||
#'tipp_mates': sorted(tipp_mates, key=lambda k: k['sum_score'],
|
||||
#reverse=True),
|
||||
# 'tipp_mates': sorted(tipp_mates, key=lambda k: k['sum_score'],
|
||||
# reverse=True),
|
||||
'posts': posts,
|
||||
'has_refresh': has_refresh
|
||||
})
|
||||
|
||||
@login_required
|
||||
def charts(request, ls, season, pos='default',
|
||||
template_name='charts.html'):
|
||||
|
||||
@login_required
|
||||
def charts(request, ls, season, pos='default',
|
||||
template_name='charts.html'):
|
||||
debug = ''
|
||||
|
||||
if pos == 'default':
|
||||
if get_current_md(ls) < 18:
|
||||
if get_current_md(ls) < 18:
|
||||
pos = 'hin'
|
||||
matches = Match.objects.filter(leagueShortcut=ls,
|
||||
season=season, matchday__lte=17)
|
||||
else:
|
||||
matches = Match.objects.filter(leagueShortcut=ls,
|
||||
season=season, matchday__lte=17)
|
||||
else:
|
||||
pos = 'rueck'
|
||||
matches = Match.objects.filter(leagueShortcut=ls,
|
||||
season=season, matchday__gte=18)
|
||||
matches = Match.objects.filter(leagueShortcut=ls,
|
||||
season=season, matchday__gte=18)
|
||||
elif pos == 'hin':
|
||||
matches = Match.objects.filter(leagueShortcut=ls,
|
||||
season=season, matchday__lte=17)
|
||||
matches = Match.objects.filter(leagueShortcut=ls,
|
||||
season=season, matchday__lte=17)
|
||||
elif pos == 'rueck':
|
||||
matches = Match.objects.filter(leagueShortcut=ls,
|
||||
season=season, matchday__gte=18)
|
||||
matches = Match.objects.filter(leagueShortcut=ls,
|
||||
season=season, matchday__gte=18)
|
||||
else:
|
||||
matches = Match.objects.filter(leagueShortcut=ls, season=season)
|
||||
|
||||
@@ -522,25 +518,26 @@ def charts(request, ls, season, pos='default',
|
||||
for userid in users:
|
||||
# get user object, then fetch matches for user
|
||||
user = User.objects.get(id=userid)
|
||||
score = Tipp.objects.filter(matchID__in=matches, \
|
||||
tipperID=user).aggregate(Sum('score'))
|
||||
|
||||
score = Tipp.objects.filter(matchID__in=matches,
|
||||
tipperID=user).aggregate(Sum('score'))
|
||||
|
||||
if score['score__sum'] is None:
|
||||
user_dict[user.first_name] = 0
|
||||
else:
|
||||
user_dict[user.first_name] = score['score__sum']
|
||||
|
||||
mandant_dict[mandant.name] = sorted(user_dict.items(),
|
||||
key=lambda x: x[1], reverse=True)
|
||||
mandant_dict[mandant.name] = sorted(user_dict.items(),
|
||||
key=lambda x: x[1], reverse=True)
|
||||
|
||||
return render(request, template_name, {
|
||||
#'debug': debug,
|
||||
return django.shortcuts.render(request, template_name, {
|
||||
# 'debug': debug,
|
||||
'mandant_dict': mandant_dict,
|
||||
'season': season,
|
||||
'ls': ls,
|
||||
'pos': pos
|
||||
})
|
||||
|
||||
|
||||
@login_required
|
||||
def blogindex(request, page):
|
||||
# get the blog posts that are published
|
||||
@@ -555,9 +552,9 @@ def blogindex(request, page):
|
||||
try:
|
||||
avatar = UserProfile.objects.get(user_id=post.author_id). \
|
||||
avatar.name
|
||||
except:
|
||||
except:
|
||||
avatar = None
|
||||
post_list.append( (post, avatar) )
|
||||
post_list.append((post, avatar))
|
||||
|
||||
p = Paginator(post_list, 5)
|
||||
|
||||
@@ -568,13 +565,14 @@ def blogindex(request, page):
|
||||
except EmptyPage:
|
||||
posts = p.page(p.num_pages)
|
||||
|
||||
return render(request, 'blogindex.html', {
|
||||
return django.shortcuts.render(request, 'blogindex.html', {
|
||||
'posts': posts,
|
||||
'ls': get_current_ls(),
|
||||
'season': get_current_season(),
|
||||
'page': page
|
||||
})
|
||||
|
||||
|
||||
|
||||
@login_required
|
||||
def newBlogpost(request):
|
||||
debug = ''
|
||||
@@ -583,10 +581,10 @@ def newBlogpost(request):
|
||||
mandants = RelUserMandant.objects.filter(user=request.user). \
|
||||
values_list('mandant', flat=True)
|
||||
choices = []
|
||||
if mandants.count > 1:
|
||||
if mandants.count() > 1:
|
||||
has_mandant = True
|
||||
for m in Mandant.objects.filter(id__in=mandants):
|
||||
choices.append( (m.name, m.description) )
|
||||
choices.append((m.name, m.description))
|
||||
|
||||
if request.method == 'POST':
|
||||
form_bp = BlogpostForm(request.POST)
|
||||
@@ -602,31 +600,32 @@ def newBlogpost(request):
|
||||
if has_mandant is True and form_mandant.is_valid():
|
||||
data = form_mandant.cleaned_data
|
||||
for m in data['mandants']:
|
||||
debug += str(m) + " "
|
||||
m = RelPostMandant(post=p,
|
||||
mandant=Mandant.objects.get(name=m))
|
||||
debug += str(m) + " "
|
||||
m = RelPostMandant(post=p,
|
||||
mandant=Mandant.objects.get(name=m))
|
||||
m.save()
|
||||
else:
|
||||
m = RelPostMandant(post=p,
|
||||
mandant=Mandant.objects.get(id=mandants[0]))
|
||||
m = RelPostMandant(post=p,
|
||||
mandant=Mandant.objects.get(id=mandants[0]))
|
||||
m.save()
|
||||
|
||||
return redirect( '/blog/1')
|
||||
return django.shortcuts.redirect('/blog/1')
|
||||
|
||||
blogpostForm = BlogpostForm()
|
||||
form_mandant = MandantsForm()
|
||||
form_mandant.fields["mandants"].choices = choices
|
||||
cnt_mandants = len(choices)
|
||||
|
||||
return render(request, 'newblogpost.html', {
|
||||
'debug': debug,
|
||||
return django.shortcuts.render(request, 'newblogpost.html', {
|
||||
'debug': debug,
|
||||
'form_bp': blogpostForm,
|
||||
'form_mandants': form_mandant,
|
||||
'cnt_mandants' : cnt_mandants,
|
||||
'cnt_mandants': cnt_mandants,
|
||||
'ls': get_current_ls(),
|
||||
'season': get_current_season()
|
||||
})
|
||||
|
||||
|
||||
def logout(request):
|
||||
"""
|
||||
Log users out and re-direct them to the main page.
|
||||
@@ -634,6 +633,7 @@ def logout(request):
|
||||
logout(request)
|
||||
return HttpResponseRedirect('/')
|
||||
|
||||
|
||||
def get_current_md(ls):
|
||||
"""
|
||||
get current matchday()
|
||||
@@ -646,14 +646,15 @@ def get_current_md(ls):
|
||||
try:
|
||||
ol = OpenLiga()
|
||||
cur_md = ol.getCurrentGroup(ls)
|
||||
except:
|
||||
except:
|
||||
cur_md = {'groupName': u"1. Spieltag", 'groupOrderID': 1}
|
||||
|
||||
return(cur_md['groupOrderID'])
|
||||
return (cur_md['groupOrderID'])
|
||||
|
||||
|
||||
def get_current_ls():
|
||||
return("bl1")
|
||||
return ("bl1")
|
||||
|
||||
|
||||
def get_current_season():
|
||||
return("2016")
|
||||
|
||||
return ("2022")
|
||||
|
||||
Reference in New Issue
Block a user