Advertisement
SensoHokatsugo

Untitled

Jan 28th, 2022
619
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ItemSerializer(serializers.ModelSerializer):
  2.     class Meta:
  3.         model = models.Item
  4.         exclude = ('modified',)
  5.         extra_kwargs = {'key': {'write_only': True}}
  6.  
  7.     robot_name = serializers.ReadOnlyField(source='run.robot.name')
  8.  
  9.  
  10. class ItemsFilterset(FilterSet):
  11.     class Meta:
  12.         model = Item
  13.         fields = {'run': ['exact']}
  14.  
  15.     item_id = CompoundRegexFilter(field_name='name', lookup_expr='regex')
  16.     additional_info = CompoundRegexFilter(field_name='reference', lookup_expr='regex')
  17.     start = DateTimeFromToRangeFilter(lookup_expr='exact')
  18.     end = DateTimeFromToRangeFilter(lookup_expr='exact')
  19.     status = MultipleChoiceFilter(choices=ITEM_STATUS_CHOICES, lookup_expr='exact')
  20.     robot = NumberInFilter(field_name='run__robot__id', lookup_expr='in')
  21.  
  22.  
  23. class PDItemViewSet(ReadOnlyModelViewSet):
  24.     """List all latest items reported by PD robots."""
  25.     permission_required = 'core.view_item'
  26.     permission_classes = [IsAuthenticated & (HasUnitRole | HasPermission)]
  27.     serializer_class = serializers.ItemSerializer
  28.     filter_backends = [DjangoFilterBackend]
  29.     filterset_class = ItemsFilterset
  30.  
  31.     def get_queryset(self):
  32.         """Construct queryset of latest unique items."""
  33.         items = models.Item.objects.order_by('key', '-modified').distinct('key')
  34.         if not (self.request.user.is_superuser or self.request.user.is_staff):
  35.             units = self.request.user.unit_roles.values('unit')
  36.             items = items.filter(run__robot__unit__in=units)
  37.  
  38.         items = items.annotate(item_id=F('name'), additional_info=F('reference'), robot_name=F('run__robot__name'),
  39.                                duration=Epoch(F('end') - F('start')))
  40.         items = self.filter_queryset(items)
  41.  
  42.         return items
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement