
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required

from .models import PredictDataset

class PredictMixin(object):
    """The baseline predict view"""
    slug_field = 'md5'

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        """Only allow a logged in users to view"""
        return super(PredictMixin, self).dispatch(request, *args, **kwargs)

    def get_queryset(self):
        """Limit queryset to the user's own predictions only"""
        qs = PredictDataset.objects.all()
        if 'slug' not in self.kwargs:
            # Limit to my own predictions unless I have the md5
            qs = qs.filter(user_id=self.request.user.pk)
        return qs



from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required

from .models import PredictDataset

class PredictMixin(object):
    """The baseline predict view"""
    slug_field = 'md5'

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        """Only allow a logged in users to view"""
        return super(PredictMixin, self).dispatch(request, *args, **kwargs)

    def get_queryset(self):
        """Limit queryset to the user's own predictions only"""
        qset = PredictDataset.objects.all()
        if 'slug' not in self.kwargs:
            # Limit to my own predictions unless I have the md5
            qset = qset.filter(user_id=self.request.user.pk)
        return qset.prefetch_related('strains', 'strains__piperun', 'strains__piperun__programs')


Improve prefetch speed in predict listing pages
Improve prefetch speed in predict listing pages

  
  from django.utils.decorators import method_decorator
  from django.contrib.auth.decorators import login_required
  
  from .models import PredictDataset
  
  class PredictMixin(object):
      """The baseline predict view"""
      slug_field = 'md5'
  
      @method_decorator(login_required)
      def dispatch(self, request, *args, **kwargs):
          """Only allow a logged in users to view"""
          return super(PredictMixin, self).dispatch(request, *args, **kwargs)
  
      def get_queryset(self):
          """Limit queryset to the user's own predictions only"""
-         qs = PredictDataset.objects.all()
+         qset = PredictDataset.objects.all()
          if 'slug' not in self.kwargs:
              # Limit to my own predictions unless I have the md5
-             qs = qs.filter(user_id=self.request.user.pk)
+             qset = qset.filter(user_id=self.request.user.pk)
-         return qs
+         return qset.prefetch_related('strains', 'strains__piperun', 'strains__piperun__programs')
  
  
Improve prefetch speed in predict listing pages
## Code Before:

from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required

from .models import PredictDataset

class PredictMixin(object):
    """The baseline predict view"""
    slug_field = 'md5'

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        """Only allow a logged in users to view"""
        return super(PredictMixin, self).dispatch(request, *args, **kwargs)

    def get_queryset(self):
        """Limit queryset to the user's own predictions only"""
        qs = PredictDataset.objects.all()
        if 'slug' not in self.kwargs:
            # Limit to my own predictions unless I have the md5
            qs = qs.filter(user_id=self.request.user.pk)
        return qs


## Instruction:
Improve prefetch speed in predict listing pages
## Code After:

from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required

from .models import PredictDataset

class PredictMixin(object):
    """The baseline predict view"""
    slug_field = 'md5'

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        """Only allow a logged in users to view"""
        return super(PredictMixin, self).dispatch(request, *args, **kwargs)

    def get_queryset(self):
        """Limit queryset to the user's own predictions only"""
        qset = PredictDataset.objects.all()
        if 'slug' not in self.kwargs:
            # Limit to my own predictions unless I have the md5
            qset = qset.filter(user_id=self.request.user.pk)
        return qset.prefetch_related('strains', 'strains__piperun', 'strains__piperun__programs')

