def get_queryset(self): """Return only those readers belonging to the current user. A coach can see his own readers, other users can't see any. """ user = self.request.user if is_coach(user): return Reader.objects.filter(coach=user.coach) else: return Reader.objects.none()
def get_queryset(self): """Coaches can view all splits from sessions they create. Athletes can view all of their own splits. Anonymous users have read access to all splits belonging to public sessions. """ user = self.request.user if is_athlete(user): return Split.objects.filter(athlete__user=user).distinct() elif is_coach(user): return Split.objects.filter(timingsession__coach=user.coach) else: return Split.objects.filter(timingsession__private=False)
def register(request): """Register a new coach or athlete.""" team_name = request.data.pop('organization', None) utype = request.data.get('user_type') if utype not in ('athlete', 'coach'): return Response({'errors': ['Invalid user type']}, status=status.HTTP_400_BAD_REQUEST) new_user = create_user(request.data, utype) if team_name is not None and is_coach(new_user.user): Team.objects.create(name=team_name, coach=new_user, primary_team=True) return Response(_serializer_lookup[utype](new_user).data, status=status.HTTP_201_CREATED)
def filter_registered_athletes(self, queryset): """Only show athletes belonging to the current coach.""" if 'request' in self.context: user = self.context['request'].user if is_coach(user): queryset = queryset.filter(team__in=user.coach.team_set.all(), team__isnull=False) elif is_athlete(user): queryset = queryset.filter(user=user) else: queryset = queryset.none() return queryset
def filter_team(self, queryset): # Only allow teams owned by the current user. Note that this # is for validation only. if 'request' in self.context: user = self.context['request'].user if is_coach(user): queryset = queryset.filter(coach__user=user) elif is_athlete(user): queryset = queryset.filter(athlete__user=user) else: queryset = queryset.none() return queryset
def get_queryset(self): """Filter teams based on current user. A coach can see all of the teams he owns, an athlete can see all of the teams he belongs to, an anonymous user can see teams that are designated public and primary. """ user = self.request.user if is_coach(user): return user.coach.team_set.all() elif is_athlete(user): return Team.objects.filter(athlete__in=[user.athlete.pk], primary_team=True) else: return Team.objects.filter(public_team=True, primary_team=True)
def filter_athlete(self, queryset): """Only show athletes belonging to the current coach.""" if 'request' in self.context: user = self.context['request'].user if is_coach(user): athletes = Athlete.objects.filter( team__in=user.coach.team_set.all()) queryset = queryset.filter( pk__in=[athlete.pk for athlete in athletes]) elif is_athlete(user): queryset = queryset.filter(pk=user.athlete.id) else: queryset = queryset.none() return queryset
def get_queryset(self): """Filter sessions by user. If the user is an athlete, list the sessions he has completed. If the user is a coach, list the sessions he owns. Otherwise, list all public sessions. """ user = self.request.user if is_athlete(user): return TimingSession.objects.filter( splits__athlete=user.athlete).distinct() elif is_coach(user): return TimingSession.objects.filter(coach=user.coach) else: return TimingSession.objects.filter(private=False)
def get_queryset(self): """ Get athletes associated with a coach. """ user = self.request.user if is_coach(user): coach = Coach.objects.get(user=user) return Athlete.objects.filter(team__in=coach.team_set.all()) elif is_athlete(user): return Athlete.objects.filter(user=user) else: return Athlete.objects.filter( Q(split__timingsession__private=False) | Q(timingsession__private=False)).distinct()
def get_queryset(self): """Filter tags by requesting user. Athletes can see the tags assigned to them, coaches can see all tags owned by their athletes, anonymous users cannot see any tags. """ user = self.request.user if is_athlete(user): tags = Tag.objects.filter(athlete_id=user.athlete.id) elif is_coach(user): tags = Tag.objects.filter( athlete__team__in=user.coach.team_set.all()) else: tags = Tag.objects.none() return tags
def upload_workouts(request): """ Create a complete workout through CSV file upload. --- parameters: - name: title description: workout title paramType: form - name: start_time description: start date of workout in ISO string format paramType: form - name: track_size description: size of track paramType: form - name: interval_distance description: distance for each split paramType: form - name: results description: workout results object paramType: body """ #- username: athlete username #- first_name: athlete first name (used to create new athlete if doesn't exist) #- last_name: athlete last name (used to create new athlete if doesn't exist) #- splits: list of split times #Note: The created workout will be automatically set to filter splits and private. data = json.loads(request.body) user = request.user if not is_coach(user): return Response({}, status.HTTP_403_FORBIDDEN) coach = user.coach start_time = dateutil.parser.parse(data['start_time']) #stop_time = dateutil.parser.parse(data['start_time']) ts = TimingSession(name=data['title'], coach=coach, start_time=start_time, stop_time=start_time, track_size=data['track_size'], interval_distance=data['interval_distance'], filter_choice=False, private=True) # set start button time in milliseconds since epoch timestamp = (start_time.replace(tzinfo=None) - EPOCH).total_seconds() ts.start_button_time = int(round(timestamp * 10**3)) ts.save() results = data['results'] if results: reader, created = Reader.objects.get_or_create(id_str='ArchivedReader', defaults={ 'name': 'Archived Reader', 'coach': coach }) ts.readers.add(reader.pk) for runner in results: new_user, created = User.objects.get_or_create( username=runner['username'], defaults={ 'first_name': runner['first_name'], 'last_name': runner['last_name'], 'last_login': timezone.now() }) if created: # Register new athlete. athlete = Athlete() athlete.user = new_user athlete.save() # add coach's team to new athlete's team if coach.team_set.all(): team = coach.team_set.all()[0] athlete.team = team athlete.save() # register tag to the timing session ts.registered_athletes.add(new_user.athlete.pk) # init reference timestamp time = ts.start_button_time for split in runner['splits']: try: #x = timezone.datetime.strptime(split, "%M:%S.%f") mins, secs = split.split(':') diff = int(round((int(mins) * 60 + float(secs)) * 10**3)) except: diff = int(round(float(secs) * 10**3)) time += diff tt = Split.objects.create(athlete_id=new_user.athlete.id, time=time, reader_id=reader.id) SplitFilter.objects.create(split=tt, timingsession=ts) return Response({}, status=status.HTTP_201_CREATED)