from piston.handler import BaseHandler
from piston.utils import rc

from competition.models import Team
from .models import TeamClient, TeamSubmission
from .forms import AuthForm, PathForm, TagListForm

import json


class RepoAuthHandler(BaseHandler):
    methods_allowed = ('GET',)

    def read(self, request):
        form = AuthForm(request.GET)
        if not form.is_valid():
            return rc.BAD_REQUEST

        try:
            team = Team.objects.get(pk=form.cleaned_data['teamid'])
            if team.teamclient.git_password == form.cleaned_data['password']:
                return {
                    'team': {
                        'id': team.id,
                        'name': team.name,
                        'slug': team.slug,
                    },
                    'repository': {
                        'name': team.teamclient.repository.name,
                        'path': team.teamclient.repository.path,
                        'description': team.teamclient.repository.description,
                        'base': team.teamclient.base.repository.name,
                    },
                }
            else:
                # Bad password
                return rc.FORBIDDEN

        except Team.DoesNotExist:
            return rc.NOT_FOUND
        except TeamClient.DoesNotExist:
            return rc.NOT_FOUND

        return rc.NOT_FOUND


class RepoPathHandler(BaseHandler):
    methods_allowed = ('GET',)

    def read(self, request):
        form = PathForm(request.GET)
        if not form.is_valid():
            return rc.BAD_REQUEST

        try:
            team = Team.objects.get(pk=form.cleaned_data['teamid'])
            return {
                'repository': {
                    'name': team.teamclient.repository.name,
                    'path': team.teamclient.repository.path,
                    'description': team.teamclient.repository.description,
                    'base': team.teamclient.base.repository.name,
                }
            }
        except Team.DoesNotExist:
            return rc.NOT_FOUND
        except TeamClient.DoesNotExist:
            return rc.NOT_FOUND

        return rc.NOT_FOUND


class RepoTagListHandler(BaseHandler):
    methods_allowed = ('GET',)

    def read(self, request):
        form = TagListForm(request.GET)
        if not form.is_valid():
            return rc.BAD_REQUEST

        try:
            slug = form.cleaned_data['competition']
            teams = Team.objects.filter(competition__slug=slug)

            def make_dict(team):
                try:
                    try:
                        tag = team.teamsubmission_set.latest().name
                    except TeamSubmission.DoesNotExist, e:
                        tag = None
                    return {
                        'id': team.pk,
                        'name': team.name,
                        'path': team.teamclient.repository.repo.path,
                        'tag': tag
                    }
                except TeamClient.DoesNotExist:
                    return None

            return [x for x in [make_dict(t) for t in teams] if x is not None]

        except Team.DoesNotExist:
            return rc.NOT_FOUND

        return rc.NOT_FOUND

@@ -2638,16 +2638,23 @@
 eam.team
+client.
 submissi
@@ -2659,12 +2659,9 @@
 sion
-_set
+s
 .lat

Fix API tags endpoint