From 4271d9153df348e710a65174a4c1ad63fad5e008 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 17 Feb 2018 16:51:44 +0400 Subject: [PATCH] Cup matches --- static/js/api/root.js | 10 +++++ static/js/components/pages/cupDetail.js | 57 +++++++++++++++++++++++++ static/js/main.js | 6 +++ teamlogic/api/serializers.py | 16 +++++++ teamlogic/api/urls.py | 1 + teamlogic/api/views.py | 8 ++++ teamlogic/models.py | 9 +++- templates/teamlogic/cup.html | 14 +++++- 8 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 static/js/components/pages/cupDetail.js diff --git a/static/js/api/root.js b/static/js/api/root.js index 8f75968..c47e531 100644 --- a/static/js/api/root.js +++ b/static/js/api/root.js @@ -19,6 +19,16 @@ const api = { }); }, + getCupMatches(cupId, callback) { + $.get(`/logic/api/v1/cup/${cupId}/`, (data, result) => { + if (result === 'success') { + callback(data); + } else { + console.log('ERROR: fetch cup matches'); + } + }); + }, + createStadionTime(stadionId, params, callback) { let selectedTimeStart = params.selectedTimeStart.split(':'); let selectedTimeEnd = params.selectedTimeEnd.split(':'); diff --git a/static/js/components/pages/cupDetail.js b/static/js/components/pages/cupDetail.js new file mode 100644 index 0000000..118cb1e --- /dev/null +++ b/static/js/components/pages/cupDetail.js @@ -0,0 +1,57 @@ +import React from 'react'; +import ReactDOM from 'react-dom'; +import api from '../../api/root'; +import { Spin } from 'antd'; + +class App extends React.Component { + constructor(props) { + super(props); + this.state = { loading: true, matches: [], cupId: props.cupId }; + this.loadMatches(); + } + + loadMatches() { + api.getCupMatches(this.state.cupId, (data) => { + setTimeout(() => this.setState(prevState => Object.assign({}, prevState, { loading: false, matches: data })), 2000); + }); + } + + render() { + if (this.state.loading) { + return ( +
+
+ +

Загружаем матчи специально для вас!

+
+
+ ) + } + + return ( +
+ { this.state.matches.map((matchPair) => { + return ( +
+
+
+ {matchPair.first_match.home.name} +
+
+ {matchPair.first_match.away.name} +
+
+
+ ) + }) } +
+ ) + } +} + +export const renderCupDetail = (id) => { + ReactDOM.render( + , + document.getElementById('current_cup'), + ); +}; diff --git a/static/js/main.js b/static/js/main.js index 41f86bc..c1cd945 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -4,6 +4,7 @@ import 'antd/dist/antd.css'; import { renderSurvey } from './survey'; import { renderStadionForm } from './components/pages/stadionReservation'; import { renderProfileForm } from './components/pages/profile'; +import { renderCupDetail } from './components/pages/cupDetail'; import { renderLoginForm } from './login'; import store from './store'; import Alert from 'react-s-alert'; @@ -30,6 +31,11 @@ $(document).ready(() => { if (window.location.pathname === '/survey') renderSurvey(); if (window.location.pathname === '/logic/stadion/get/') renderStadionForm(); if (window.location.pathname === '/login/') renderProfileForm(); + if (window.location.pathname.match('/logic/cup/[0-1]+/')) { + const id = Number(window.location.pathname.split('/')[3]); + renderCupDetail(id); + } + renderLoginForm(); }); diff --git a/teamlogic/api/serializers.py b/teamlogic/api/serializers.py index a7d0032..65c44f4 100644 --- a/teamlogic/api/serializers.py +++ b/teamlogic/api/serializers.py @@ -28,3 +28,19 @@ class TimeBoardSerializer(serializers.ModelSerializer): class Meta: model = models.TimeBoard fields = ('id', 'date', 'stadion', 'time1', 'time2', 'match') + + +class MatchSerializer(serializers.ModelSerializer): + home = TeamSerializer(many=False, read_only=True) + away = TeamSerializer(many=False, read_only=True) + class Meta: + model = models.Match + fields = ('id', 'home_goal', 'away_goal', 'home', 'away') + + +class CupDetailSerialiser(serializers.ModelSerializer): + first_match = MatchSerializer(many=False, read_only=True) + second_match = MatchSerializer(many=False, read_only=True) + class Meta: + model = models.MatchPair + fields = ('id', 'first_match', 'second_match', 'next_pair', 'only_one_match') diff --git a/teamlogic/api/urls.py b/teamlogic/api/urls.py index fee55b3..c84e835 100644 --- a/teamlogic/api/urls.py +++ b/teamlogic/api/urls.py @@ -15,4 +15,5 @@ url(r'^api/v1/match/(?P[0-9]+)', views.set_date, name='api_set_date'), url(r'^api/v1/stadion/(?P[0-9]+)/times', views.stadion_times, name='api_stadion_times'), url(r'^api/v1/stadion', views.stadions, name='api_stadion'), + url(r'^api/v1/cup/(?P[0-9]+)', views.cup_detail, name='cup_detail'), ] diff --git a/teamlogic/api/views.py b/teamlogic/api/views.py index 6a6a85a..7527f46 100644 --- a/teamlogic/api/views.py +++ b/teamlogic/api/views.py @@ -325,3 +325,11 @@ def stadion_times(request, id): new_time.save() return Response({'status': 'ok'}, status=status.HTTP_201_CREATED) + + +@api_view(['GET']) +@permission_classes((permissions.AllowAny,)) +def cup_detail(request, id): + entities = models.MatchPair.objects.filter(cup_id=id) + serializer = serializers.CupDetailSerialiser(entities, many=True) + return Response(serializer.data) diff --git a/teamlogic/models.py b/teamlogic/models.py index 8c95ff9..f99bb0b 100644 --- a/teamlogic/models.py +++ b/teamlogic/models.py @@ -149,7 +149,7 @@ class Match(models.Model): """ Simple model of match. """ - league = models.ForeignKey('Tournament', default=1, null=True) + league = models.ForeignKey('Tournament', default=1, **NULLABLE) home = models.ForeignKey(Team, related_name='+') away = models.ForeignKey(Team, related_name='+') home_goal = models.IntegerField(default=0) @@ -429,6 +429,9 @@ def get_season(self): def __unicode__(self): return unicode("%s %s" % (self.name, self.get_season())) + def __str__(self): + return "%s %s" % (self.name, self.get_season()) + def get_absolute_url(self): return reverse("cup", args=(self.id,)) @@ -463,6 +466,10 @@ def __unicode__(self): return unicode('%s - %s' % (self.first_match.home.name, self.first_match.away.name)) + def __str__(self): + return '%s - %s' % (self.first_match.home.name, + self.first_match.away.name) + def _getWinnerByPenalty(self): if not (self.first_penalty == 0 and self.second_penalty == 0): if self.first_penalty > self.second_penalty: diff --git a/templates/teamlogic/cup.html b/templates/teamlogic/cup.html index ba0cd7d..d6ebe3d 100644 --- a/templates/teamlogic/cup.html +++ b/templates/teamlogic/cup.html @@ -5,7 +5,19 @@
  • АДФС
  • Кубки
  • {{ cup }}
  • + - +

    {{ cup }}

    +
    +
    {% endblock %}