from django import forms from django.utils import timezone from rest_framework import serializers from contest.models import Contest, Band, Frequency, QSO, EntryCategory, User, ShadowCall, Reference from contest.validators import CallLogValidator class ContestSerializer(serializers.ModelSerializer): class Meta: model = Contest # FIXME: add callQrg fields = ('id', 'shortName', 'deadline', 'qsoStartTime', 'qsoEndTime', 'callQrg') class BandSerializer(serializers.ModelSerializer): # contest = ContestSerializer() class Meta: model = Band fields = ('id', 'name', 'contest') class FrequencySerializer(serializers.ModelSerializer): # band = BandSerializer() class Meta: model = Frequency fields = ('id', 'channel', 'qrg', 'band', 'note') class EntryCategorySerializer(serializers.ModelSerializer): class Meta: model = EntryCategory fields = ('id', 'name', 'description') class ReferenceSerializer(serializers.ModelSerializer): class Meta: model = Reference fields = ('id', 'name', 'description') class UserSerializer(serializers.ModelSerializer): # ref = ReferenceSerializer() # cat = EntryCategorySerializer() class Meta: model = User fields = ('id', 'ref', 'cat', 'location', 'opName', 'regTime', 'dncall', 'qrv2m', 'qrv70cm', 'extra2m70cm') read_only_fields = ('ref', 'location', 'regTime') def validate(self, attrs): contest = Contest.get_current_contest() if contest.deadline < timezone.now(): raise serializers.ValidationError("The deadline for changing the entry category has passed") return attrs class QSOSerializer(serializers.ModelSerializer): class Meta: model = QSO fields = ('id', 'owner', 'time', 'ownNo', 'band', 'call', 'reportTX', 'reportRX', 'refStr', 'remarks') read_only_fields = ('owner',) def validate_call(self, value): val = value.strip().upper() try: CallLogValidator()(val) except forms.ValidationError as e: raise serializers.ValidationError({'errors': e.error_list}) return val def validate(self, attrs): ownNo = attrs['ownNo'] try: o = QSO.objects.get(owner=self.context['request'].user, ownNo=ownNo) if not (self.instance and self.instance.id and self.instance.id == o.id): raise serializers.ValidationError("You already logged a QSO with the number %s" % ownNo) except QSO.DoesNotExist: pass band = attrs.get('band') if band: if band.contest.deadline < timezone.now(): raise serializers.ValidationError("The deadline for logging and editing QSOs has passed") return attrs class ShadowCallSerializer(serializers.ModelSerializer): ref = ReferenceSerializer() class Meta: model = ShadowCall fields = ('id', 'username', 'ref', 'location', 'opName', 'regTime')