|
| 1 | +from django.contrib.auth.models import User |
| 2 | +from rest_framework import serializers |
| 3 | + |
| 4 | +from api.models import Submission, Hackathon, HackathonRegistration |
| 5 | + |
| 6 | + |
| 7 | +class UserRegistrationSerializer(serializers.ModelSerializer): |
| 8 | + password = serializers.CharField(write_only=True, required=True, style={'input_type': 'password'}) |
| 9 | + |
| 10 | + class Meta: |
| 11 | + model = User |
| 12 | + fields = ['username', 'password'] |
| 13 | + |
| 14 | + def create(self, validated_data): |
| 15 | + user = User.objects.create_user( |
| 16 | + username=validated_data['username'], |
| 17 | + password=validated_data['password'] |
| 18 | + ) |
| 19 | + return user |
| 20 | + |
| 21 | + |
| 22 | +class SubmissionSerializer(serializers.ModelSerializer): |
| 23 | + class Meta: |
| 24 | + model = Submission |
| 25 | + fields = '__all__' |
| 26 | + extra_kwargs = { |
| 27 | + 'user': { |
| 28 | + 'read_only': True, |
| 29 | + }, |
| 30 | + 'hackathon': { |
| 31 | + 'read_only': True, |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + def create(self, validated_data): |
| 36 | + user = self.context['request'].user |
| 37 | + hackathon = validated_data['hackathon'] |
| 38 | + |
| 39 | + # Check if the user is registered for the hackathon |
| 40 | + if not HackathonRegistration.objects.filter(user=user, hackathon=hackathon).exists(): |
| 41 | + raise serializers.ValidationError("You are not registered for this hackathon.") |
| 42 | + |
| 43 | + submission = Submission.objects.create(**validated_data) |
| 44 | + return submission |
| 45 | + |
| 46 | + def to_representation(self, instance): |
| 47 | + representation = super().to_representation(instance) |
| 48 | + hackathon = instance.hackathon |
| 49 | + submission_type = hackathon.submission_type |
| 50 | + representation['user'] = instance.user.username |
| 51 | + representation['hackathon'] = instance.hackathon.title |
| 52 | + |
| 53 | + if submission_type == 'image': |
| 54 | + representation.pop('file_submission', None) |
| 55 | + representation.pop('link_submission', None) |
| 56 | + elif submission_type == 'file': |
| 57 | + representation.pop('image_submission', None) |
| 58 | + representation.pop('link_submission', None) |
| 59 | + elif submission_type == 'link': |
| 60 | + representation.pop('image_submission', None) |
| 61 | + representation.pop('file_submission', None) |
| 62 | + |
| 63 | + return representation |
| 64 | + |
| 65 | + def validate(self, data): |
| 66 | + hackathon = self.context['view'].get_object() |
| 67 | + submission_type = hackathon.submission_type |
| 68 | + |
| 69 | + if submission_type == 'image': |
| 70 | + if not data.get('image_submission'): |
| 71 | + raise serializers.ValidationError("Image submission is required.") |
| 72 | + data.pop('file_submission', None) |
| 73 | + data.pop('link_submission', None) |
| 74 | + elif submission_type == 'file': |
| 75 | + if not data.get('file_submission'): |
| 76 | + raise serializers.ValidationError("File submission is required.") |
| 77 | + data.pop('image_submission', None) |
| 78 | + data.pop('link_submission', None) |
| 79 | + elif submission_type == 'link': |
| 80 | + if not data.get('link_submission'): |
| 81 | + raise serializers.ValidationError("Link submission is required.") |
| 82 | + data.pop('image_submission', None) |
| 83 | + data.pop('file_submission', None) |
| 84 | + |
| 85 | + return data |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +class UserSerializer(serializers.ModelSerializer): |
| 90 | + class Meta: |
| 91 | + model = User |
| 92 | + fields = ['id', 'username'] |
| 93 | + |
| 94 | + |
| 95 | +class HackathonSerializer(serializers.ModelSerializer): |
| 96 | + submissions = SubmissionSerializer(many=True, read_only=True) |
| 97 | + registrations = serializers.SerializerMethodField() |
| 98 | + submission_count = serializers.SerializerMethodField() |
| 99 | + |
| 100 | + class Meta: |
| 101 | + model = Hackathon |
| 102 | + fields = '__all__' |
| 103 | + |
| 104 | + def get_registrations(self, obj): |
| 105 | + return obj.hackathonregistration_set.count() |
| 106 | + |
| 107 | + def get_submission_count(self, obj): |
| 108 | + user = self.context['request'].user |
| 109 | + return Submission.objects.filter(user=user, hackathon=obj).count() |
| 110 | + |
| 111 | + |
| 112 | +class HackathonRegistrationSerializer(serializers.ModelSerializer): |
| 113 | + class Meta: |
| 114 | + model = HackathonRegistration |
| 115 | + fields = '__all__' |
| 116 | + extra_kwargs = { |
| 117 | + 'user': { |
| 118 | + 'read_only': True, |
| 119 | + }, |
| 120 | + } |
0 commit comments