Skip to content

views

ReportViewSet

Bases: viewsets.ModelViewSet

API endpoint that allows users to be viewed or edited.

Source code in reports/views.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class ReportViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = Report.objects.all()
    serializer_class = ReportSerializer
    pagination_class = LimitOffsetPagination
    filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
    search_fields = ['notebook']

    def perform_create(self, serializer):
        serializer.save(dataset_id=self.request.data['dataset'])

        instance: Report = serializer.instance
        instance.creator = self.request.user
        instance.save()

    def get_queryset(self):
        if self.request.user.is_superuser:
            return super().get_queryset()

        return super().get_queryset().filter(
            Q(creator=self.request.user) | (Q(discoverable=True) & ~Q(share_mode=Report.ShareModes.PRIVATE))
        )

    def get_permissions(self):
        permissions = super().get_permissions()

        if self.action in ['update', 'partial_update']:
            permissions.append(CanEditReport())

        if self.action in ['destroy']:
            permissions.append(IsOwner())

        if self.action in ['retrieve']:
            permissions.append(CanViewReport())

        return permissions

filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter] class-attribute

pagination_class = LimitOffsetPagination class-attribute

queryset = Report.objects.all() class-attribute

search_fields = ['notebook'] class-attribute

serializer_class = ReportSerializer class-attribute

get_permissions()

Source code in reports/views.py
46
47
48
49
50
51
52
53
54
55
56
57
58
def get_permissions(self):
    permissions = super().get_permissions()

    if self.action in ['update', 'partial_update']:
        permissions.append(CanEditReport())

    if self.action in ['destroy']:
        permissions.append(IsOwner())

    if self.action in ['retrieve']:
        permissions.append(CanViewReport())

    return permissions

get_queryset()

Source code in reports/views.py
38
39
40
41
42
43
44
def get_queryset(self):
    if self.request.user.is_superuser:
        return super().get_queryset()

    return super().get_queryset().filter(
        Q(creator=self.request.user) | (Q(discoverable=True) & ~Q(share_mode=Report.ShareModes.PRIVATE))
    )

perform_create(serializer)

Source code in reports/views.py
31
32
33
34
35
36
def perform_create(self, serializer):
    serializer.save(dataset_id=self.request.data['dataset'])

    instance: Report = serializer.instance
    instance.creator = self.request.user
    instance.save()

gpt_prompt(request)

Source code in reports/views.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
@swagger_auto_schema(methods=['post'], request_body=openapi.Schema(
    type=openapi.TYPE_OBJECT,
    properties={
        'prompt': openapi.Schema(type=openapi.TYPE_STRING, description="GPT prompt"),
    }
))
@api_view(['POST'])
def gpt_prompt(request: Request):
    if not request.data.get('prompt', None):
        raise Exception('No prompt provided')

    if not OPENAPI_KEY:
        raise Exception('OpenAI API key not set')

    openai.api_key = OPENAPI_KEY

    response = openai.Completion.create(
        model="code-davinci-002",
        prompt=request.data['prompt'],
        temperature=0,
        max_tokens=150,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
        stop=["#", ";"]
    )

    return JsonResponse(response.to_dict())