Repository: kartoza/prj.app

Issue:
500 error on category list
http://sentry.kartoza.com/kartoza/projecta-live/group/5227/

url = http://changelog.qgis.org/en/test/category/list/ 

```
Stacktrace (most recent call first):

  File "django/db/models/query.py", line 334, in get
    self.model._meta.object_name
  File "django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "changes/views/category.py", line 167, in get_queryset
    project = Project.objects.get(slug=project_slug)
  File "django/views/generic/list.py", line 159, in get
    self.object_list = self.get_queryset()
  File "django/views/generic/base.py", line 89, in dispatch
    return handler(request, *args, **kwargs)
  File "django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "django/core/handlers/base.py", line 132, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
```



Discussion:

Resolving patch:
diff --git a/django_project/changes/views/category.py b/django_project/changes/views/category.py
--- a/django_project/changes/views/category.py
+++ b/django_project/changes/views/category.py
@@ -154,22 +154,34 @@ def get_context_data(self, **kwargs):
     def get_queryset(self, queryset=None):
         """Get the queryset for this view.
 
-        :param queryset: A query set
-        :type queryset: QuerySet
+        :returns: A queryset which is filtered to only show approved
+            Categories.
 
-        :returns: Category Queryset which is filtered by project
+        :param queryset: Optional queryset.
         :rtype: QuerySet
         :raises: Http404
         """
-        if self.queryset is None:
+        if queryset is None:
             project_slug = self.kwargs.get('project_slug', None)
             if project_slug:
-                project = Project.objects.get(slug=project_slug)
-                queryset = Category.objects.filter(project=project)
+                try:
+                    project = Project.objects.get(slug=project_slug)
+                except Project.DoesNotExist:
+                    raise Http404(
+                        'Sorry! The project you are requesting a category for '
+                        'could not be found or you do not have permission to '
+                        'view the category. Also the version may not be '
+                        'approved yet. Try logging in as a staff member if '
+                        'you wish to view it.')
+                queryset = Category.approved_objects.filter(
+                    project=project)
                 return queryset
             else:
-                raise Http404('Sorry! We could not find your category!')
-        return self.queryset
+                raise Http404(
+                        'Sorry! We could not find the project for '
+                        'your category!')
+        else:
+            return queryset
 
 
 class CategoryDetailView(CategoryMixin, DetailView):
@@ -418,8 +430,8 @@ def form_valid(self, form):
                 'ERROR: Category by this name already exists!')
 
 
-class PendingCategoryListView(StaffuserRequiredMixin, CategoryMixin,
-                              PaginationMixin, ListView):
+class PendingCategoryListView(
+        StaffuserRequiredMixin, CategoryMixin, PaginationMixin, ListView):
     """List view for pending Category."""
     context_object_name = 'categories'
     template_name = 'category/list.html'
@@ -465,12 +477,22 @@ def get_queryset(self):
         if self.queryset is None:
             self.project_slug = self.kwargs.get('project_slug', None)
             if self.project_slug:
-                self.project = Project.objects.get(slug=self.project_slug)
-                queryset = Category.unapproved_objects.filter(
+                try:
+                    self.project = Project.objects.get(slug=self.project_slug)
+                except Project.DoesNotExist:
+                    raise Http404(
+                        'Sorry! The project you are requesting a category for '
+                        'could not be found or you do not have permission to '
+                        'view the category. Also the version may not be '
+                        'approved yet. Try logging in as a staff member if '
+                        'you wish to view it.')
+                self.queryset = Category.unapproved_objects.filter(
                     project=self.project)
-                return queryset
+                return self.queryset
             else:
-                raise Http404('Sorry! We could not find your category!')
+                raise Http404(
+                        'Sorry! We could not find the project for '
+                        'your category!')
         return self.queryset
 
 
diff --git a/django_project/changes/views/version.py b/django_project/changes/views/version.py
--- a/django_project/changes/views/version.py
+++ b/django_project/changes/views/version.py
@@ -130,11 +130,11 @@ def get_object(self, queryset=None):
                     return obj
                 except Version.DoesNotExist:
                     raise Http404(
-                        'Sorry! The version you are requesting could not '
-                        'be found or you do not have permission to view '
-                        'the version. Also the version may not be approved '
-                        'yet. Try logging in as a staff member if you wish '
-                        'to view it.')
+                        'Sorry! The project you are requesting a version for '
+                        'could not be found or you do not have permission to '
+                        'view the version. Also the version may not be '
+                        'approved yet. Try logging in as a staff member if '
+                        'you wish to view it.')
             else:
                 raise Http404('Sorry! We could not find your version!')
 
@@ -214,11 +214,11 @@ def get_object(self, queryset=None):
                     return obj
                 except Version.DoesNotExist:
                     raise Http404(
-                        'Sorry! The version you are requesting could not '
-                        'be found or you do not have permission to view '
-                        'the version. Also the version may not be approved '
-                        'yet. Try logging in as a staff member if you wish '
-                        'to view it.')
+                        'Sorry! The project you are requesting a version for '
+                        'could not be found or you do not have permission to '
+                        'view the version. Also the version may not be '
+                        'approved yet. Try logging in as a staff member if '
+                        'you wish to view it.')
             else:
                 raise Http404('Sorry! We could not find your version!')
 


Test patch: