Skip to content

Commit

Permalink
Adjust API to better handle teams
Browse files Browse the repository at this point in the history
  • Loading branch information
TeoTN committed Feb 13, 2017
1 parent e3ff09c commit 026f934
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
15 changes: 10 additions & 5 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
MemberViewSet,
MatchViewSet,
CountPointsView,
UserLatestMatchesView
UserLatestMatchesView,
TeamViewSet,
)
from rest_framework.routers import DefaultRouter

team_router = DefaultRouter()
team_router.register(r'users', MemberViewSet, base_name='member')
team_router.register(r'matches', MatchViewSet, base_name='match')

router = DefaultRouter()
router.register(r'users', MemberViewSet, base_name='member')
router.register(r'matches', MatchViewSet, base_name='match')
router.register(r'teams', TeamViewSet, base_name='team')

urlpatterns = [
url(r'^matches/count-points/?$', CountPointsView.as_view(), name='count_points'),
url(r'^users/(?P<username>[A-Za-z0-9_\-\.]+)/matches/?$', UserLatestMatchesView.as_view(), name='user_matches'),
url(r'^(?P<team>[0-9a-zA-Z]+)/matches/count-points/?$', CountPointsView.as_view(), name='count_points'),
url(r'^(?P<team>[0-9a-zA-Z]+)/users/(?P<username>[A-Za-z0-9_\-\.]+)/matches/?$', UserLatestMatchesView.as_view(), name='user_matches'),
url(r'^', include(router.urls, namespace='api')),
url(r'^(?P<team>[0-9a-zA-Z]+)/', include(team_router.urls, namespace='team-api')),
]
6 changes: 4 additions & 2 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ class StandardPagination(PageNumberPagination):
max_page_size = 50


class TeamView(ModelViewSet):
class TeamViewSet(ModelViewSet):
serializer_class = TeamSerializer
queryset = Team.objects.all()
allowed_methods = [u'GET', u'POST', u'OPTIONS']

def get_queryset(self):
return self.request.user.teams


class MemberViewSet(ModelViewSet):
serializer_class = MemberSerializer
Expand Down
4 changes: 1 addition & 3 deletions tfoosball/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ class Player(AbstractUser):
lowest_exp = models.IntegerField(default=1000)
highest_exp = models.IntegerField(default=1000)
hidden = models.BooleanField(default=False)

def get_teams(self):
return list(self.member_set.all().values_list('team__domain', 'team__name'))
teams = models.ManyToManyField(Team, through='Member')

@property
def won(self):
Expand Down
7 changes: 1 addition & 6 deletions tfoosball/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@
CallbackView,
GoogleLoginView,
)
from api.views import TeamView

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('allauth.urls')),
url(r'^(?P<team>[0-9a-zA-Z]+)/rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/google/$', GoogleLoginView.as_view(), name='g_login'),
url(r'^auth/callback/?$', CallbackView.as_view(), name='auth_callback'),
url(r'^api/teams/$', TeamView.as_view({
'get': 'list',
'post': 'create'
}), name='teams'),
url(r'^api/(?P<team>[0-9a-zA-Z]+)/', include('api.urls')),
url(r'^api/', include('api.urls')),
url(r'^$', TemplateView.as_view(template_name='index.html'), name='index'),
]
2 changes: 1 addition & 1 deletion tfoosball/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class GoogleLoginView(SocialLoginView):

def get_response(self):
response = super(GoogleLoginView, self).get_response()
teams = self.user.get_teams()
teams = list(self.user.teams.values_list('domain', 'name'))
response.data['teams'] = teams
return response

Expand Down

0 comments on commit 026f934

Please sign in to comment.