order_data = write_serializer.validated_data order_lines_data = order_data.pop('order_lines') begin = order_data.pop('begin') end = order_data.pop('end') order = Order(**order_data) order_lines = [ OrderLine(order=order, **data) for data in order_lines_data ] # store the OrderLine objects in the Order object so that we can use # those when calculating prices order._in_memory_order_lines = order_lines # order line price calculations need a dummy reservation from which they # get begin and end times from reservation = Reservation(begin=begin, end=end) order.reservation = reservation # serialize the in-memory objects read_serializer = PriceEndpointOrderSerializer(order) order_data = read_serializer.data order_data['order_lines'] = [ OrderLineSerializer(ol).data for ol in order_lines ] order_data.update({'begin': begin, 'end': end}) return Response(order_data, status=200) register_view(OrderViewSet, 'order', 'order')
class CateringProviderFilter(django_filters.rest_framework.FilterSet): unit = django_filters.CharFilter(name='units') class Meta: model = CateringProvider fields = ('unit',) class CateringProvider(viewsets.ReadOnlyModelViewSet): queryset = CateringProvider.objects.prefetch_related('units') serializer_class = CateringProviderSerializer filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) filter_class = CateringProviderFilter register_view(CateringProvider, 'catering_provider') class CateringProductCategorySerializer(TranslatedModelSerializer): class Meta: model = CateringProductCategory fields = ('id', 'name', 'products', 'provider') class CateringProductCategoryFilter(django_filters.rest_framework.FilterSet): class Meta: model = CateringProductCategory fields = ('provider',) class CateringProductCategoryViewSet(viewsets.ReadOnlyModelViewSet):
if api_name == value) except StopIteration: return queryset.none() content_type = ContentType.objects.get_for_model(model) return queryset.filter(content_type=content_type) class CommentViewSet(mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet): queryset = Comment.objects.select_related('created_by').prefetch_related( 'content_type') serializer_class = CommentSerializer filter_backends = (django_filters.rest_framework.DjangoFilterBackend, ) filterset_class = CommentFilter def get_queryset(self): user = self.request.user queryset = super().get_queryset() return queryset.filter( content_type__in=get_commentable_content_types()).can_view(user) def perform_create(self, serializer): obj = serializer.save(created_by=self.request.user, created_at=timezone.now()) obj.send_created_notification(self.request) return obj register_view(CommentViewSet, 'comment')
request = self.context.get('request') user = request.user if request else None if user and obj.is_admin(user): return None else: return obj.get_reservable_before() def get_reservable_after(self, obj): request = self.context.get('request') user = request.user if request else None if user and obj.is_admin(user): return None else: return obj.get_reservable_after() class Meta: model = Unit fields = '__all__' class UnitViewSet(munigeo_api.GeoModelAPIView, viewsets.ReadOnlyModelViewSet): queryset = Unit.objects.all() serializer_class = UnitSerializer filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) filter_class = UnitFilterSet register_view(UnitViewSet, 'unit')
def filter_target_type(self, queryset, name, value): try: model = next(model for api_name, model in COMMENTABLE_MODELS.items() if api_name == value) except StopIteration: return queryset.none() content_type = ContentType.objects.get_for_model(model) return queryset.filter(content_type=content_type) class CommentViewSet(mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet): queryset = Comment.objects.select_related('created_by').prefetch_related('content_type') serializer_class = CommentSerializer filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) filterset_class = CommentFilter def get_queryset(self): user = self.request.user queryset = super().get_queryset() return queryset.filter(content_type__in=get_commentable_content_types()).can_view(user) def perform_create(self, serializer): obj = serializer.save(created_by=self.request.user, created_at=timezone.now()) obj.send_created_notification(self.request) return obj register_view(CommentViewSet, 'comment')
class CateringProviderFilter(django_filters.rest_framework.FilterSet): unit = django_filters.CharFilter(field_name='units') class Meta: model = CateringProvider fields = ('unit',) class CateringProvider(viewsets.ReadOnlyModelViewSet): queryset = CateringProvider.objects.prefetch_related('units') serializer_class = CateringProviderSerializer filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) filterset_class = CateringProviderFilter register_view(CateringProvider, 'catering_provider') class CateringProductCategorySerializer(TranslatedModelSerializer): class Meta: model = CateringProductCategory fields = ('id', 'name', 'products', 'provider') class CateringProductCategoryFilter(django_filters.rest_framework.FilterSet): class Meta: model = CateringProductCategory fields = ('provider',) class CateringProductCategoryViewSet(viewsets.ReadOnlyModelViewSet):
from rest_framework import serializers, viewsets from munigeo import api as munigeo_api from resources.api.base import NullableDateTimeField, TranslatedModelSerializer, register_view from resources.models import Unit class UnitSerializer(TranslatedModelSerializer, munigeo_api.GeoModelSerializer): opening_hours_today = serializers.DictField( source='get_opening_hours', child=serializers.ListField( child=serializers.DictField( child=NullableDateTimeField()) ) ) class Meta: model = Unit class UnitViewSet(munigeo_api.GeoModelAPIView, viewsets.ReadOnlyModelViewSet): queryset = Unit.objects.all() serializer_class = UnitSerializer register_view(UnitViewSet, 'unit')