oneBet/onebet/mainapp/views.py

130 lines
3.6 KiB
Python

import json
import django.shortcuts
import django.db.models
import django.http
import mainapp.models
class BreakerException(Exception):
pass
def view_index(request, sport=None, league=None, search=None, tag=None):
try:
if sport is not None:
sport = mainapp.models.Sport.objects.get(clean_name=sport)
assert sport is not None, 'sport not found'
if league is not None:
league = mainapp.models.League.objects.get(sport_id=sport.id, clean_name=league)
assert league is not None, 'league not found'
navbar_sports = mainapp.models.League.navbar_sports()
assert navbar_sports, 'navbar sports not found'
except BaseException:
raise django.http.Http404()
else:
return django.shortcuts.render(
request=request,
template_name='mainapp/index.html',
status=200,
context={
'sport': sport,
'league': league,
'search': search,
'tag': tag,
'navbar_sports': navbar_sports,
}
)
def view_news(request, title):
try:
navbar_sports = mainapp.models.League.navbar_sports()
assert navbar_sports, 'navbar sports not found'
news = mainapp.models.News.objects.get(clean_title=title)
assert news is not None
except BaseException:
raise django.http.Http404()
else:
return django.shortcuts.render(
request=request,
template_name='mainapp/news.html',
status=200,
context={
'navbar_sports': navbar_sports,
'news': news
}
)
def api_news_list(request):
try:
json_body = json.loads(request.body)
news_filter = list()
if json_body.get('tag'):
news_filter.append(django.db.models.Q(clean_tags__contains=[json_body['tag']]))
elif json_body.get('search'):
news_filter.append(django.db.models.Q(haystack__contains=json_body['search']))
else:
if json_body.get('sportId'):
news_filter.append(django.db.models.Q(sport_id=json_body['sportId']))
if json_body.get('leagueId'):
news_filter.append(django.db.models.Q(league_id=json_body['leagueId']))
if json_body.get('offsetId'):
news_filter.append(django.db.models.Q(id__lt=json_body['offsetId']))
news = mainapp.models.News.objects.filter(*news_filter).order_by('-pub_date')[:json_body.get('quantity', 10)]
except BaseException as e:
return django.http.JsonResponse(
data={
'success': False,
'error': str(e)
},
status=400
)
else:
return django.http.JsonResponse(
data={
'success': True,
'error': None,
'news': [ns.to_json for ns in news]
},
status=200
)
def api_user_session(request):
try:
json_body = json.loads(request.body)
if 'seenBuildingModal' in json_body:
request.session['seenBuildingModal'] = json_body['seenBuildingModal']
except BaseException as e:
return django.http.JsonResponse(
data={
'success': False,
'error': str(e)
},
status=400
)
else:
return django.http.JsonResponse(
data={
'success': True,
'error': None,
},
status=200
)