def post(self, request, format=None): serializer_class = ProgramSerializer serializer = ProgramSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, format=None): program = self.get_object(pk) serializer = ProgramSerializer(program, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def program(request): """ List all programs, or create a new programs. """ if request.method == 'GET': programs = Program.objects.all() serializer = ProgramSerializer(programs, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = ProgramSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response( serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class ActivityRetrieveSerializer(ActivitySerializer): maps = ActivityMapSerializer(read_only=True, many=True, source='activitymap_set') sponsors = serializers.SerializerMethodField() programs = ProgramSerializer(read_only=True, many=True, source='program_set') speakers = SpeakerRetrieveSerializer(read_only=True, many=True) gallery = GallerySerializer(read_only=True, many=True, source='gallery_set') class Meta: model = Activity fields = ('id', 'year', 'email', 'is_active', 'short_description', 'description', 'register_url', 'sponsor_document', 'address', 'transportation', 'accommodation', 'contact_info', 'social_accounts', 'sponsors', 'programs', 'speakers', 'maps', 'gallery') def get_sponsors(self, obj): request = self.context.get('request') activity_sponsors = collections.OrderedDict() sponsors = obj.activitysponsor_set \ .filter(is_active=True, sponsor_type__is_active=True) \ .order_by('sponsor_type', 'order_id') for sponsor in sponsors: data = collections.OrderedDict() data.update({ 'name': sponsor.sponsor.name, 'url': sponsor.sponsor.url, 'logo': request.build_absolute_uri(sponsor.sponsor.logo.url) }) activity_sponsors.setdefault(sponsor.sponsor_type.name, []) \ .append(data) return activity_sponsors