We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Review items in #770 need to be addressed
Please address the comments in this PR: https://github.com/kartoza/projecta/pull/770

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `django_project/base/views/organisation.py`
Content:
```
1 # coding=utf-8
2 from braces.views import LoginRequiredMixin, StaffuserRequiredMixin
3 from django.db import IntegrityError
4 from django.core.exceptions import ValidationError
5 from django.core.urlresolvers import reverse
6 from django.http import Http404
7 from django.views.generic import (
8     CreateView,
9     ListView,
10     RedirectView,
11     DeleteView,
12     UpdateView)
13 from django.shortcuts import get_object_or_404
14 from pure_pagination.mixins import PaginationMixin
15 from base.models import Organisation
16 from base.forms import OrganisationForm
17 
18 
19 class OrganisationMixin(object):
20     model = Organisation
21     form_class = OrganisationForm
22 
23 
24 class CreateOrganisationView(
25         LoginRequiredMixin, OrganisationMixin, CreateView):
26     """Create view for organisation."""
27 
28     context_object_name = 'organisation'
29     template_name = 'organisation/create.html'
30 
31     def get_success_url(self):
32         return reverse('list-organisation')
33 
34     def get_context_data(self, **kwargs):
35         context = super(
36             CreateOrganisationView, self).get_context_data(**kwargs)
37         return context
38 
39     def get_form_kwargs(self):
40         kwargs = super(CreateOrganisationView, self).get_form_kwargs()
41         kwargs.update({'user': self.request.user})
42         return kwargs
43 
44 
45 class OrganisationListView(OrganisationMixin, PaginationMixin, ListView):
46     """List view for organisation."""
47 
48     context_object_name = 'organisations'
49     template_name = 'organisation/list.html'
50     paginate_by = 1000
51 
52     def get_context_data(self, **kwargs):
53         """Get the context data which is passed to a template.
54 
55         :param kwargs: Any arguments to pass to the superclass.
56         :type kwargs: dict
57 
58         :returns: Context data which will be passed to the template.
59         :rtype: dict
60         """
61 
62         context = super(OrganisationListView, self).get_context_data(**kwargs)
63         context['num_organisations'] = context['organisations'].count()
64         context['unapproved'] = False
65         return context
66 
67     def get_queryset(self, queryset=None):
68         """Get the queryset for this view.
69 
70         :param queryset: A query set
71         :type queryset: QuerySet
72 
73         :returns: CertifyingOrganisation Queryset which is filtered by project.
74         :rtype: QuerySet
75         :raises: Http404
76         """
77 
78         if self.queryset is None:
79             queryset = Organisation.objects.all()
80             return queryset
81 
82         return self.queryset
83 
84 
85 class ApproveOrganisationView(
86         StaffuserRequiredMixin, OrganisationMixin, RedirectView):
87     """Redirect view for approving organisation."""
88 
89     permanent = False
90     query_string = True
91     pattern_name = 'list-organisation'
92 
93     def get_redirect_url(self, pk):
94         """Save Certifying Organisation as approved and redirect.
95 
96         :param project_slug: The slug of the parent
97                             Certifying Organisation's parent Project.
98         :type project_slug: str
99 
100         :param slug: The slug of the Certifying Organisation.
101         :type slug: str
102 
103         :returns: URL
104         :rtype: str
105         """
106 
107         organisation_qs = \
108             Organisation.objects.filter(approved=False)
109         organisation = \
110             get_object_or_404(organisation_qs, pk=pk)
111         organisation.approved = True
112         organisation.save()
113 
114         return reverse(self.pattern_name)
115 
116 
117 class PendingOrganisationListView(
118         OrganisationMixin, PaginationMixin, ListView):
119     """List view for pending organisation."""
120 
121     context_object_name = 'organisations'
122     template_name = 'organisation/list.html'
123     paginate_by = 1000
124 
125     def get_context_data(self, **kwargs):
126         """Get the context data which is passed to a template.
127 
128         :param kwargs: Any arguments to pass to the superclass.
129         :type kwargs: dict
130 
131         :returns: Context data which will be passed to the template.
132         :rtype: dict
133         """
134 
135         context = super(
136             PendingOrganisationListView, self).get_context_data(**kwargs)
137         context['num_organisations'] = context['organisations'].count()
138         context['unapproved'] = True
139         return context
140 
141     def get_queryset(self, queryset=None):
142         """Get the queryset for this view.
143 
144         :param queryset: A query set
145         :type queryset: QuerySet
146 
147         :returns: CertifyingOrganisation Queryset which is filtered by project.
148         :rtype: QuerySet
149         :raises: Http404
150         """
151 
152         if self.queryset is None:
153             queryset = Organisation.objects.filter(approved=False)
154             return queryset
155 
156         return self.queryset
157 
158 
159 class OrganisationDeleteView(
160         StaffuserRequiredMixin, OrganisationMixin, DeleteView):
161     context_object_name = 'organisation'
162     template_name = 'organisation/delete.html'
163 
164     def get_success_url(self):
165         return reverse('list-organisation')
166 
167     def get_queryset(self):
168         qs = Organisation.objects.all()
169         if self.request.user.is_staff:
170             return qs
171         else:
172             raise Http404
173 
174 
175 class OrganisationUpdateView(
176         LoginRequiredMixin, OrganisationMixin, UpdateView):
177     context_object_name = 'organisation'
178     template_name = 'organisation/update.html'
179 
180     def get_form_kwargs(self):
181         kwargs = super(OrganisationUpdateView, self).get_form_kwargs()
182         kwargs.update({'user': self.request.user})
183         return kwargs
184 
185     def get_queryset(self):
186         qs = Organisation.objects
187         if self.request.user.is_staff:
188             return qs
189         else:
190             return qs.filter(owner=self.request.user)
191 
192     def get_context_data(self, **kwargs):
193         context = super(
194             OrganisationUpdateView, self).get_context_data(**kwargs)
195         return context
196 
197     def get_success_url(self):
198         if self.object.approved:
199             return reverse('list-organisation')
200         else:
201             return reverse('pending-list-organisation')
202 
203     def form_valid(self, form):
204         """Check that there is no referential integrity error when saving."""
205 
206         try:
207             return super(OrganisationUpdateView, self).form_valid(form)
208         except IntegrityError:
209             return ValidationError(
210                 'ERROR: Organisation by this name is already exists!')
211
```

Path: `django_project/base/views/custom_domain.py`
Content:
```
1 # coding=utf-8
2 from braces.views import LoginRequiredMixin, StaffuserRequiredMixin
3 from django.db import IntegrityError
4 from django.core.exceptions import ValidationError
5 from django.core.urlresolvers import reverse
6 from django.http import Http404
7 from django.views.generic import (
8     TemplateView,
9     CreateView,
10     ListView,
11     RedirectView,
12     DeleteView,
13     UpdateView)
14 from django.shortcuts import get_object_or_404
15 from pure_pagination.mixins import PaginationMixin
16 from base.models.custom_domain import Domain
17 from base.forms import RegisterDomainForm
18 
19 
20 class DomainNotFound(TemplateView):
21     template_name = 'custom_domain/domain-not-found.html'
22 
23 
24 class DomainMixin(object):
25     model = Domain
26     form_class = RegisterDomainForm
27 
28 
29 class RegisterDomainView(LoginRequiredMixin, DomainMixin, CreateView):
30     """View for domain registration."""
31 
32     context_object_name = 'domain'
33     template_name = 'custom_domain/register.html'
34 
35     def get_success_url(self):
36         return reverse('domain-registered')
37 
38     def get_context_data(self, **kwargs):
39         context = super(RegisterDomainView, self).get_context_data(**kwargs)
40         return context
41 
42     def get_form_kwargs(self):
43         kwargs = super(RegisterDomainView, self).get_form_kwargs()
44         kwargs.update({'user': self.request.user})
45         return kwargs
46 
47 
48 class DomainThankYouView(TemplateView):
49     template_name = 'custom_domain/thankyou.html'
50 
51 
52 class DomainListView(
53         StaffuserRequiredMixin, DomainMixin, PaginationMixin, ListView):
54     """List view for Domain."""
55 
56     context_object_name = 'domains'
57     template_name = 'custom_domain/list.html'
58     paginate_by = 1000
59 
60     def get_context_data(self, **kwargs):
61         """Get the context data which is passed to a template.
62 
63         :param kwargs: Any arguments to pass to the superclass.
64         :type kwargs: dict
65 
66         :returns: Context data which will be passed to the template.
67         :rtype: dict
68         """
69 
70         context = super(DomainListView, self).get_context_data(**kwargs)
71         context['num_domains'] = context['domains'].count()
72         context['unapproved'] = False
73         return context
74 
75     def get_queryset(self, queryset=None):
76         """Get the queryset for this view.
77 
78         :param queryset: A query set
79         :type queryset: QuerySet
80 
81         :returns: CertifyingOrganisation Queryset which is filtered by project.
82         :rtype: QuerySet
83         :raises: Http404
84         """
85 
86         if self.queryset is None:
87             queryset = Domain.objects.all()
88             return queryset
89 
90         return self.queryset
91 
92 
93 class ApproveDomainView(StaffuserRequiredMixin, DomainMixin, RedirectView):
94     """Redirect view for approving domain."""
95 
96     permanent = False
97     query_string = True
98     pattern_name = 'domain-list'
99 
100     def get_redirect_url(self, pk):
101         """Save Certifying Organisation as approved and redirect.
102 
103         :param project_slug: The slug of the parent
104                             Certifying Organisation's parent Project.
105         :type project_slug: str
106 
107         :param slug: The slug of the Certifying Organisation.
108         :type slug: str
109 
110         :returns: URL
111         :rtype: str
112         """
113 
114         domain_qs = \
115             Domain.objects.filter(approved=False)
116         domain = \
117             get_object_or_404(domain_qs, pk=pk)
118         domain.approved = True
119         domain.save()
120 
121         return reverse(self.pattern_name)
122 
123 
124 class PendingDomainListView(
125         StaffuserRequiredMixin, DomainMixin, PaginationMixin, ListView):
126     """Pending list view for domain."""
127 
128     context_object_name = 'domains'
129     template_name = 'custom_domain/list.html'
130     paginate_by = 1000
131 
132     def get_context_data(self, **kwargs):
133         """Get the context data which is passed to a template.
134 
135         :param kwargs: Any arguments to pass to the superclass.
136         :type kwargs: dict
137 
138         :returns: Context data which will be passed to the template.
139         :rtype: dict
140         """
141 
142         context = super(PendingDomainListView, self).get_context_data(**kwargs)
143         context['num_domains'] = context['domains'].count()
144         context['unapproved'] = True
145         return context
146 
147     def get_queryset(self, queryset=None):
148         """Get the queryset for this view.
149 
150         :param queryset: A query set
151         :type queryset: QuerySet
152 
153         :returns: CertifyingOrganisation Queryset which is filtered by project.
154         :rtype: QuerySet
155         :raises: Http404
156         """
157 
158         if self.queryset is None:
159             queryset = Domain.objects.filter(approved=False)
160             return queryset
161 
162         return self.queryset
163 
164 
165 class DomainDeleteView(StaffuserRequiredMixin, DomainMixin, DeleteView):
166     context_object_name = 'domain'
167     template_name = 'custom_domain/delete.html'
168 
169     def get_success_url(self):
170         return reverse('domain-list')
171 
172     def get_queryset(self):
173         qs = Domain.objects.all()
174         if self.request.user.is_staff:
175             return qs
176         else:
177             raise Http404
178 
179 
180 class DomainUpdateView(LoginRequiredMixin, DomainMixin, UpdateView):
181     context_object_name = 'domain'
182     template_name = 'custom_domain/update.html'
183 
184     def get_form_kwargs(self):
185         kwargs = super(DomainUpdateView, self).get_form_kwargs()
186         kwargs.update({'user': self.request.user})
187         return kwargs
188 
189     def get_queryset(self):
190         qs = Domain.objects
191         if self.request.user.is_staff:
192             return qs
193         else:
194             return qs.filter(user=self.request.user)
195 
196     def get_context_data(self, **kwargs):
197         context = super(DomainUpdateView, self).get_context_data(**kwargs)
198         return context
199 
200     def get_success_url(self):
201         if self.object.approved:
202             return reverse('domain-list')
203         else:
204             return reverse('domain-pending-list')
205 
206     def form_valid(self, form):
207         """Check that there is no referential integrity error when saving."""
208 
209         try:
210             return super(DomainUpdateView, self).form_valid(form)
211         except IntegrityError:
212             return ValidationError(
213                 'ERROR: Domain by this name is already exists!')
214
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/django_project/base/views/custom_domain.py b/django_project/base/views/custom_domain.py
--- a/django_project/base/views/custom_domain.py
+++ b/django_project/base/views/custom_domain.py
@@ -1,7 +1,5 @@
 # coding=utf-8
 from braces.views import LoginRequiredMixin, StaffuserRequiredMixin
-from django.db import IntegrityError
-from django.core.exceptions import ValidationError
 from django.core.urlresolvers import reverse
 from django.http import Http404
 from django.views.generic import (
@@ -193,21 +191,8 @@
         else:
             return qs.filter(user=self.request.user)
 
-    def get_context_data(self, **kwargs):
-        context = super(DomainUpdateView, self).get_context_data(**kwargs)
-        return context
-
     def get_success_url(self):
         if self.object.approved:
             return reverse('domain-list')
         else:
             return reverse('domain-pending-list')
-
-    def form_valid(self, form):
-        """Check that there is no referential integrity error when saving."""
-
-        try:
-            return super(DomainUpdateView, self).form_valid(form)
-        except IntegrityError:
-            return ValidationError(
-                'ERROR: Domain by this name is already exists!')
diff --git a/django_project/base/views/organisation.py b/django_project/base/views/organisation.py
--- a/django_project/base/views/organisation.py
+++ b/django_project/base/views/organisation.py
@@ -1,7 +1,5 @@
 # coding=utf-8
 from braces.views import LoginRequiredMixin, StaffuserRequiredMixin
-from django.db import IntegrityError
-from django.core.exceptions import ValidationError
 from django.core.urlresolvers import reverse
 from django.http import Http404
 from django.views.generic import (
@@ -189,22 +187,8 @@
         else:
             return qs.filter(owner=self.request.user)
 
-    def get_context_data(self, **kwargs):
-        context = super(
-            OrganisationUpdateView, self).get_context_data(**kwargs)
-        return context
-
     def get_success_url(self):
         if self.object.approved:
             return reverse('list-organisation')
         else:
             return reverse('pending-list-organisation')
-
-    def form_valid(self, form):
-        """Check that there is no referential integrity error when saving."""
-
-        try:
-            return super(OrganisationUpdateView, self).form_valid(form)
-        except IntegrityError:
-            return ValidationError(
-                'ERROR: Organisation by this name is already exists!')

{"golden_diff": "diff --git a/django_project/base/views/custom_domain.py b/django_project/base/views/custom_domain.py\n--- a/django_project/base/views/custom_domain.py\n+++ b/django_project/base/views/custom_domain.py\n@@ -1,7 +1,5 @@\n # coding=utf-8\n from braces.views import LoginRequiredMixin, StaffuserRequiredMixin\n-from django.db import IntegrityError\n-from django.core.exceptions import ValidationError\n from django.core.urlresolvers import reverse\n from django.http import Http404\n from django.views.generic import (\n@@ -193,21 +191,8 @@\n         else:\n             return qs.filter(user=self.request.user)\n \n-    def get_context_data(self, **kwargs):\n-        context = super(DomainUpdateView, self).get_context_data(**kwargs)\n-        return context\n-\n     def get_success_url(self):\n         if self.object.approved:\n             return reverse('domain-list')\n         else:\n             return reverse('domain-pending-list')\n-\n-    def form_valid(self, form):\n-        \"\"\"Check that there is no referential integrity error when saving.\"\"\"\n-\n-        try:\n-            return super(DomainUpdateView, self).form_valid(form)\n-        except IntegrityError:\n-            return ValidationError(\n-                'ERROR: Domain by this name is already exists!')\ndiff --git a/django_project/base/views/organisation.py b/django_project/base/views/organisation.py\n--- a/django_project/base/views/organisation.py\n+++ b/django_project/base/views/organisation.py\n@@ -1,7 +1,5 @@\n # coding=utf-8\n from braces.views import LoginRequiredMixin, StaffuserRequiredMixin\n-from django.db import IntegrityError\n-from django.core.exceptions import ValidationError\n from django.core.urlresolvers import reverse\n from django.http import Http404\n from django.views.generic import (\n@@ -189,22 +187,8 @@\n         else:\n             return qs.filter(owner=self.request.user)\n \n-    def get_context_data(self, **kwargs):\n-        context = super(\n-            OrganisationUpdateView, self).get_context_data(**kwargs)\n-        return context\n-\n     def get_success_url(self):\n         if self.object.approved:\n             return reverse('list-organisation')\n         else:\n             return reverse('pending-list-organisation')\n-\n-    def form_valid(self, form):\n-        \"\"\"Check that there is no referential integrity error when saving.\"\"\"\n-\n-        try:\n-            return super(OrganisationUpdateView, self).form_valid(form)\n-        except IntegrityError:\n-            return ValidationError(\n-                'ERROR: Organisation by this name is already exists!')\n", "issue": "Review items in #770 need to be addressed\nPlease address the comments in this PR: https://github.com/kartoza/projecta/pull/770\n", "before_files": [{"content": "# coding=utf-8\nfrom braces.views import LoginRequiredMixin, StaffuserRequiredMixin\nfrom django.db import IntegrityError\nfrom django.core.exceptions import ValidationError\nfrom django.core.urlresolvers import reverse\nfrom django.http import Http404\nfrom django.views.generic import (\n    CreateView,\n    ListView,\n    RedirectView,\n    DeleteView,\n    UpdateView)\nfrom django.shortcuts import get_object_or_404\nfrom pure_pagination.mixins import PaginationMixin\nfrom base.models import Organisation\nfrom base.forms import OrganisationForm\n\n\nclass OrganisationMixin(object):\n    model = Organisation\n    form_class = OrganisationForm\n\n\nclass CreateOrganisationView(\n        LoginRequiredMixin, OrganisationMixin, CreateView):\n    \"\"\"Create view for organisation.\"\"\"\n\n    context_object_name = 'organisation'\n    template_name = 'organisation/create.html'\n\n    def get_success_url(self):\n        return reverse('list-organisation')\n\n    def get_context_data(self, **kwargs):\n        context = super(\n            CreateOrganisationView, self).get_context_data(**kwargs)\n        return context\n\n    def get_form_kwargs(self):\n        kwargs = super(CreateOrganisationView, self).get_form_kwargs()\n        kwargs.update({'user': self.request.user})\n        return kwargs\n\n\nclass OrganisationListView(OrganisationMixin, PaginationMixin, ListView):\n    \"\"\"List view for organisation.\"\"\"\n\n    context_object_name = 'organisations'\n    template_name = 'organisation/list.html'\n    paginate_by = 1000\n\n    def get_context_data(self, **kwargs):\n        \"\"\"Get the context data which is passed to a template.\n\n        :param kwargs: Any arguments to pass to the superclass.\n        :type kwargs: dict\n\n        :returns: Context data which will be passed to the template.\n        :rtype: dict\n        \"\"\"\n\n        context = super(OrganisationListView, self).get_context_data(**kwargs)\n        context['num_organisations'] = context['organisations'].count()\n        context['unapproved'] = False\n        return context\n\n    def get_queryset(self, queryset=None):\n        \"\"\"Get the queryset for this view.\n\n        :param queryset: A query set\n        :type queryset: QuerySet\n\n        :returns: CertifyingOrganisation Queryset which is filtered by project.\n        :rtype: QuerySet\n        :raises: Http404\n        \"\"\"\n\n        if self.queryset is None:\n            queryset = Organisation.objects.all()\n            return queryset\n\n        return self.queryset\n\n\nclass ApproveOrganisationView(\n        StaffuserRequiredMixin, OrganisationMixin, RedirectView):\n    \"\"\"Redirect view for approving organisation.\"\"\"\n\n    permanent = False\n    query_string = True\n    pattern_name = 'list-organisation'\n\n    def get_redirect_url(self, pk):\n        \"\"\"Save Certifying Organisation as approved and redirect.\n\n        :param project_slug: The slug of the parent\n                            Certifying Organisation's parent Project.\n        :type project_slug: str\n\n        :param slug: The slug of the Certifying Organisation.\n        :type slug: str\n\n        :returns: URL\n        :rtype: str\n        \"\"\"\n\n        organisation_qs = \\\n            Organisation.objects.filter(approved=False)\n        organisation = \\\n            get_object_or_404(organisation_qs, pk=pk)\n        organisation.approved = True\n        organisation.save()\n\n        return reverse(self.pattern_name)\n\n\nclass PendingOrganisationListView(\n        OrganisationMixin, PaginationMixin, ListView):\n    \"\"\"List view for pending organisation.\"\"\"\n\n    context_object_name = 'organisations'\n    template_name = 'organisation/list.html'\n    paginate_by = 1000\n\n    def get_context_data(self, **kwargs):\n        \"\"\"Get the context data which is passed to a template.\n\n        :param kwargs: Any arguments to pass to the superclass.\n        :type kwargs: dict\n\n        :returns: Context data which will be passed to the template.\n        :rtype: dict\n        \"\"\"\n\n        context = super(\n            PendingOrganisationListView, self).get_context_data(**kwargs)\n        context['num_organisations'] = context['organisations'].count()\n        context['unapproved'] = True\n        return context\n\n    def get_queryset(self, queryset=None):\n        \"\"\"Get the queryset for this view.\n\n        :param queryset: A query set\n        :type queryset: QuerySet\n\n        :returns: CertifyingOrganisation Queryset which is filtered by project.\n        :rtype: QuerySet\n        :raises: Http404\n        \"\"\"\n\n        if self.queryset is None:\n            queryset = Organisation.objects.filter(approved=False)\n            return queryset\n\n        return self.queryset\n\n\nclass OrganisationDeleteView(\n        StaffuserRequiredMixin, OrganisationMixin, DeleteView):\n    context_object_name = 'organisation'\n    template_name = 'organisation/delete.html'\n\n    def get_success_url(self):\n        return reverse('list-organisation')\n\n    def get_queryset(self):\n        qs = Organisation.objects.all()\n        if self.request.user.is_staff:\n            return qs\n        else:\n            raise Http404\n\n\nclass OrganisationUpdateView(\n        LoginRequiredMixin, OrganisationMixin, UpdateView):\n    context_object_name = 'organisation'\n    template_name = 'organisation/update.html'\n\n    def get_form_kwargs(self):\n        kwargs = super(OrganisationUpdateView, self).get_form_kwargs()\n        kwargs.update({'user': self.request.user})\n        return kwargs\n\n    def get_queryset(self):\n        qs = Organisation.objects\n        if self.request.user.is_staff:\n            return qs\n        else:\n            return qs.filter(owner=self.request.user)\n\n    def get_context_data(self, **kwargs):\n        context = super(\n            OrganisationUpdateView, self).get_context_data(**kwargs)\n        return context\n\n    def get_success_url(self):\n        if self.object.approved:\n            return reverse('list-organisation')\n        else:\n            return reverse('pending-list-organisation')\n\n    def form_valid(self, form):\n        \"\"\"Check that there is no referential integrity error when saving.\"\"\"\n\n        try:\n            return super(OrganisationUpdateView, self).form_valid(form)\n        except IntegrityError:\n            return ValidationError(\n                'ERROR: Organisation by this name is already exists!')\n", "path": "django_project/base/views/organisation.py"}, {"content": "# coding=utf-8\nfrom braces.views import LoginRequiredMixin, StaffuserRequiredMixin\nfrom django.db import IntegrityError\nfrom django.core.exceptions import ValidationError\nfrom django.core.urlresolvers import reverse\nfrom django.http import Http404\nfrom django.views.generic import (\n    TemplateView,\n    CreateView,\n    ListView,\n    RedirectView,\n    DeleteView,\n    UpdateView)\nfrom django.shortcuts import get_object_or_404\nfrom pure_pagination.mixins import PaginationMixin\nfrom base.models.custom_domain import Domain\nfrom base.forms import RegisterDomainForm\n\n\nclass DomainNotFound(TemplateView):\n    template_name = 'custom_domain/domain-not-found.html'\n\n\nclass DomainMixin(object):\n    model = Domain\n    form_class = RegisterDomainForm\n\n\nclass RegisterDomainView(LoginRequiredMixin, DomainMixin, CreateView):\n    \"\"\"View for domain registration.\"\"\"\n\n    context_object_name = 'domain'\n    template_name = 'custom_domain/register.html'\n\n    def get_success_url(self):\n        return reverse('domain-registered')\n\n    def get_context_data(self, **kwargs):\n        context = super(RegisterDomainView, self).get_context_data(**kwargs)\n        return context\n\n    def get_form_kwargs(self):\n        kwargs = super(RegisterDomainView, self).get_form_kwargs()\n        kwargs.update({'user': self.request.user})\n        return kwargs\n\n\nclass DomainThankYouView(TemplateView):\n    template_name = 'custom_domain/thankyou.html'\n\n\nclass DomainListView(\n        StaffuserRequiredMixin, DomainMixin, PaginationMixin, ListView):\n    \"\"\"List view for Domain.\"\"\"\n\n    context_object_name = 'domains'\n    template_name = 'custom_domain/list.html'\n    paginate_by = 1000\n\n    def get_context_data(self, **kwargs):\n        \"\"\"Get the context data which is passed to a template.\n\n        :param kwargs: Any arguments to pass to the superclass.\n        :type kwargs: dict\n\n        :returns: Context data which will be passed to the template.\n        :rtype: dict\n        \"\"\"\n\n        context = super(DomainListView, self).get_context_data(**kwargs)\n        context['num_domains'] = context['domains'].count()\n        context['unapproved'] = False\n        return context\n\n    def get_queryset(self, queryset=None):\n        \"\"\"Get the queryset for this view.\n\n        :param queryset: A query set\n        :type queryset: QuerySet\n\n        :returns: CertifyingOrganisation Queryset which is filtered by project.\n        :rtype: QuerySet\n        :raises: Http404\n        \"\"\"\n\n        if self.queryset is None:\n            queryset = Domain.objects.all()\n            return queryset\n\n        return self.queryset\n\n\nclass ApproveDomainView(StaffuserRequiredMixin, DomainMixin, RedirectView):\n    \"\"\"Redirect view for approving domain.\"\"\"\n\n    permanent = False\n    query_string = True\n    pattern_name = 'domain-list'\n\n    def get_redirect_url(self, pk):\n        \"\"\"Save Certifying Organisation as approved and redirect.\n\n        :param project_slug: The slug of the parent\n                            Certifying Organisation's parent Project.\n        :type project_slug: str\n\n        :param slug: The slug of the Certifying Organisation.\n        :type slug: str\n\n        :returns: URL\n        :rtype: str\n        \"\"\"\n\n        domain_qs = \\\n            Domain.objects.filter(approved=False)\n        domain = \\\n            get_object_or_404(domain_qs, pk=pk)\n        domain.approved = True\n        domain.save()\n\n        return reverse(self.pattern_name)\n\n\nclass PendingDomainListView(\n        StaffuserRequiredMixin, DomainMixin, PaginationMixin, ListView):\n    \"\"\"Pending list view for domain.\"\"\"\n\n    context_object_name = 'domains'\n    template_name = 'custom_domain/list.html'\n    paginate_by = 1000\n\n    def get_context_data(self, **kwargs):\n        \"\"\"Get the context data which is passed to a template.\n\n        :param kwargs: Any arguments to pass to the superclass.\n        :type kwargs: dict\n\n        :returns: Context data which will be passed to the template.\n        :rtype: dict\n        \"\"\"\n\n        context = super(PendingDomainListView, self).get_context_data(**kwargs)\n        context['num_domains'] = context['domains'].count()\n        context['unapproved'] = True\n        return context\n\n    def get_queryset(self, queryset=None):\n        \"\"\"Get the queryset for this view.\n\n        :param queryset: A query set\n        :type queryset: QuerySet\n\n        :returns: CertifyingOrganisation Queryset which is filtered by project.\n        :rtype: QuerySet\n        :raises: Http404\n        \"\"\"\n\n        if self.queryset is None:\n            queryset = Domain.objects.filter(approved=False)\n            return queryset\n\n        return self.queryset\n\n\nclass DomainDeleteView(StaffuserRequiredMixin, DomainMixin, DeleteView):\n    context_object_name = 'domain'\n    template_name = 'custom_domain/delete.html'\n\n    def get_success_url(self):\n        return reverse('domain-list')\n\n    def get_queryset(self):\n        qs = Domain.objects.all()\n        if self.request.user.is_staff:\n            return qs\n        else:\n            raise Http404\n\n\nclass DomainUpdateView(LoginRequiredMixin, DomainMixin, UpdateView):\n    context_object_name = 'domain'\n    template_name = 'custom_domain/update.html'\n\n    def get_form_kwargs(self):\n        kwargs = super(DomainUpdateView, self).get_form_kwargs()\n        kwargs.update({'user': self.request.user})\n        return kwargs\n\n    def get_queryset(self):\n        qs = Domain.objects\n        if self.request.user.is_staff:\n            return qs\n        else:\n            return qs.filter(user=self.request.user)\n\n    def get_context_data(self, **kwargs):\n        context = super(DomainUpdateView, self).get_context_data(**kwargs)\n        return context\n\n    def get_success_url(self):\n        if self.object.approved:\n            return reverse('domain-list')\n        else:\n            return reverse('domain-pending-list')\n\n    def form_valid(self, form):\n        \"\"\"Check that there is no referential integrity error when saving.\"\"\"\n\n        try:\n            return super(DomainUpdateView, self).form_valid(form)\n        except IntegrityError:\n            return ValidationError(\n                'ERROR: Domain by this name is already exists!')\n", "path": "django_project/base/views/custom_domain.py"}], "after_files": [{"content": "# coding=utf-8\nfrom braces.views import LoginRequiredMixin, StaffuserRequiredMixin\nfrom django.core.urlresolvers import reverse\nfrom django.http import Http404\nfrom django.views.generic import (\n    CreateView,\n    ListView,\n    RedirectView,\n    DeleteView,\n    UpdateView)\nfrom django.shortcuts import get_object_or_404\nfrom pure_pagination.mixins import PaginationMixin\nfrom base.models import Organisation\nfrom base.forms import OrganisationForm\n\n\nclass OrganisationMixin(object):\n    model = Organisation\n    form_class = OrganisationForm\n\n\nclass CreateOrganisationView(\n        LoginRequiredMixin, OrganisationMixin, CreateView):\n    \"\"\"Create view for organisation.\"\"\"\n\n    context_object_name = 'organisation'\n    template_name = 'organisation/create.html'\n\n    def get_success_url(self):\n        return reverse('list-organisation')\n\n    def get_context_data(self, **kwargs):\n        context = super(\n            CreateOrganisationView, self).get_context_data(**kwargs)\n        return context\n\n    def get_form_kwargs(self):\n        kwargs = super(CreateOrganisationView, self).get_form_kwargs()\n        kwargs.update({'user': self.request.user})\n        return kwargs\n\n\nclass OrganisationListView(OrganisationMixin, PaginationMixin, ListView):\n    \"\"\"List view for organisation.\"\"\"\n\n    context_object_name = 'organisations'\n    template_name = 'organisation/list.html'\n    paginate_by = 1000\n\n    def get_context_data(self, **kwargs):\n        \"\"\"Get the context data which is passed to a template.\n\n        :param kwargs: Any arguments to pass to the superclass.\n        :type kwargs: dict\n\n        :returns: Context data which will be passed to the template.\n        :rtype: dict\n        \"\"\"\n\n        context = super(OrganisationListView, self).get_context_data(**kwargs)\n        context['num_organisations'] = context['organisations'].count()\n        context['unapproved'] = False\n        return context\n\n    def get_queryset(self, queryset=None):\n        \"\"\"Get the queryset for this view.\n\n        :param queryset: A query set\n        :type queryset: QuerySet\n\n        :returns: CertifyingOrganisation Queryset which is filtered by project.\n        :rtype: QuerySet\n        :raises: Http404\n        \"\"\"\n\n        if self.queryset is None:\n            queryset = Organisation.objects.all()\n            return queryset\n\n        return self.queryset\n\n\nclass ApproveOrganisationView(\n        StaffuserRequiredMixin, OrganisationMixin, RedirectView):\n    \"\"\"Redirect view for approving organisation.\"\"\"\n\n    permanent = False\n    query_string = True\n    pattern_name = 'list-organisation'\n\n    def get_redirect_url(self, pk):\n        \"\"\"Save Certifying Organisation as approved and redirect.\n\n        :param project_slug: The slug of the parent\n                            Certifying Organisation's parent Project.\n        :type project_slug: str\n\n        :param slug: The slug of the Certifying Organisation.\n        :type slug: str\n\n        :returns: URL\n        :rtype: str\n        \"\"\"\n\n        organisation_qs = \\\n            Organisation.objects.filter(approved=False)\n        organisation = \\\n            get_object_or_404(organisation_qs, pk=pk)\n        organisation.approved = True\n        organisation.save()\n\n        return reverse(self.pattern_name)\n\n\nclass PendingOrganisationListView(\n        OrganisationMixin, PaginationMixin, ListView):\n    \"\"\"List view for pending organisation.\"\"\"\n\n    context_object_name = 'organisations'\n    template_name = 'organisation/list.html'\n    paginate_by = 1000\n\n    def get_context_data(self, **kwargs):\n        \"\"\"Get the context data which is passed to a template.\n\n        :param kwargs: Any arguments to pass to the superclass.\n        :type kwargs: dict\n\n        :returns: Context data which will be passed to the template.\n        :rtype: dict\n        \"\"\"\n\n        context = super(\n            PendingOrganisationListView, self).get_context_data(**kwargs)\n        context['num_organisations'] = context['organisations'].count()\n        context['unapproved'] = True\n        return context\n\n    def get_queryset(self, queryset=None):\n        \"\"\"Get the queryset for this view.\n\n        :param queryset: A query set\n        :type queryset: QuerySet\n\n        :returns: CertifyingOrganisation Queryset which is filtered by project.\n        :rtype: QuerySet\n        :raises: Http404\n        \"\"\"\n\n        if self.queryset is None:\n            queryset = Organisation.objects.filter(approved=False)\n            return queryset\n\n        return self.queryset\n\n\nclass OrganisationDeleteView(\n        StaffuserRequiredMixin, OrganisationMixin, DeleteView):\n    context_object_name = 'organisation'\n    template_name = 'organisation/delete.html'\n\n    def get_success_url(self):\n        return reverse('list-organisation')\n\n    def get_queryset(self):\n        qs = Organisation.objects.all()\n        if self.request.user.is_staff:\n            return qs\n        else:\n            raise Http404\n\n\nclass OrganisationUpdateView(\n        LoginRequiredMixin, OrganisationMixin, UpdateView):\n    context_object_name = 'organisation'\n    template_name = 'organisation/update.html'\n\n    def get_form_kwargs(self):\n        kwargs = super(OrganisationUpdateView, self).get_form_kwargs()\n        kwargs.update({'user': self.request.user})\n        return kwargs\n\n    def get_queryset(self):\n        qs = Organisation.objects\n        if self.request.user.is_staff:\n            return qs\n        else:\n            return qs.filter(owner=self.request.user)\n\n    def get_success_url(self):\n        if self.object.approved:\n            return reverse('list-organisation')\n        else:\n            return reverse('pending-list-organisation')\n", "path": "django_project/base/views/organisation.py"}, {"content": "# coding=utf-8\nfrom braces.views import LoginRequiredMixin, StaffuserRequiredMixin\nfrom django.core.urlresolvers import reverse\nfrom django.http import Http404\nfrom django.views.generic import (\n    TemplateView,\n    CreateView,\n    ListView,\n    RedirectView,\n    DeleteView,\n    UpdateView)\nfrom django.shortcuts import get_object_or_404\nfrom pure_pagination.mixins import PaginationMixin\nfrom base.models.custom_domain import Domain\nfrom base.forms import RegisterDomainForm\n\n\nclass DomainNotFound(TemplateView):\n    template_name = 'custom_domain/domain-not-found.html'\n\n\nclass DomainMixin(object):\n    model = Domain\n    form_class = RegisterDomainForm\n\n\nclass RegisterDomainView(LoginRequiredMixin, DomainMixin, CreateView):\n    \"\"\"View for domain registration.\"\"\"\n\n    context_object_name = 'domain'\n    template_name = 'custom_domain/register.html'\n\n    def get_success_url(self):\n        return reverse('domain-registered')\n\n    def get_context_data(self, **kwargs):\n        context = super(RegisterDomainView, self).get_context_data(**kwargs)\n        return context\n\n    def get_form_kwargs(self):\n        kwargs = super(RegisterDomainView, self).get_form_kwargs()\n        kwargs.update({'user': self.request.user})\n        return kwargs\n\n\nclass DomainThankYouView(TemplateView):\n    template_name = 'custom_domain/thankyou.html'\n\n\nclass DomainListView(\n        StaffuserRequiredMixin, DomainMixin, PaginationMixin, ListView):\n    \"\"\"List view for Domain.\"\"\"\n\n    context_object_name = 'domains'\n    template_name = 'custom_domain/list.html'\n    paginate_by = 1000\n\n    def get_context_data(self, **kwargs):\n        \"\"\"Get the context data which is passed to a template.\n\n        :param kwargs: Any arguments to pass to the superclass.\n        :type kwargs: dict\n\n        :returns: Context data which will be passed to the template.\n        :rtype: dict\n        \"\"\"\n\n        context = super(DomainListView, self).get_context_data(**kwargs)\n        context['num_domains'] = context['domains'].count()\n        context['unapproved'] = False\n        return context\n\n    def get_queryset(self, queryset=None):\n        \"\"\"Get the queryset for this view.\n\n        :param queryset: A query set\n        :type queryset: QuerySet\n\n        :returns: CertifyingOrganisation Queryset which is filtered by project.\n        :rtype: QuerySet\n        :raises: Http404\n        \"\"\"\n\n        if self.queryset is None:\n            queryset = Domain.objects.all()\n            return queryset\n\n        return self.queryset\n\n\nclass ApproveDomainView(StaffuserRequiredMixin, DomainMixin, RedirectView):\n    \"\"\"Redirect view for approving domain.\"\"\"\n\n    permanent = False\n    query_string = True\n    pattern_name = 'domain-list'\n\n    def get_redirect_url(self, pk):\n        \"\"\"Save Certifying Organisation as approved and redirect.\n\n        :param project_slug: The slug of the parent\n                            Certifying Organisation's parent Project.\n        :type project_slug: str\n\n        :param slug: The slug of the Certifying Organisation.\n        :type slug: str\n\n        :returns: URL\n        :rtype: str\n        \"\"\"\n\n        domain_qs = \\\n            Domain.objects.filter(approved=False)\n        domain = \\\n            get_object_or_404(domain_qs, pk=pk)\n        domain.approved = True\n        domain.save()\n\n        return reverse(self.pattern_name)\n\n\nclass PendingDomainListView(\n        StaffuserRequiredMixin, DomainMixin, PaginationMixin, ListView):\n    \"\"\"Pending list view for domain.\"\"\"\n\n    context_object_name = 'domains'\n    template_name = 'custom_domain/list.html'\n    paginate_by = 1000\n\n    def get_context_data(self, **kwargs):\n        \"\"\"Get the context data which is passed to a template.\n\n        :param kwargs: Any arguments to pass to the superclass.\n        :type kwargs: dict\n\n        :returns: Context data which will be passed to the template.\n        :rtype: dict\n        \"\"\"\n\n        context = super(PendingDomainListView, self).get_context_data(**kwargs)\n        context['num_domains'] = context['domains'].count()\n        context['unapproved'] = True\n        return context\n\n    def get_queryset(self, queryset=None):\n        \"\"\"Get the queryset for this view.\n\n        :param queryset: A query set\n        :type queryset: QuerySet\n\n        :returns: CertifyingOrganisation Queryset which is filtered by project.\n        :rtype: QuerySet\n        :raises: Http404\n        \"\"\"\n\n        if self.queryset is None:\n            queryset = Domain.objects.filter(approved=False)\n            return queryset\n\n        return self.queryset\n\n\nclass DomainDeleteView(StaffuserRequiredMixin, DomainMixin, DeleteView):\n    context_object_name = 'domain'\n    template_name = 'custom_domain/delete.html'\n\n    def get_success_url(self):\n        return reverse('domain-list')\n\n    def get_queryset(self):\n        qs = Domain.objects.all()\n        if self.request.user.is_staff:\n            return qs\n        else:\n            raise Http404\n\n\nclass DomainUpdateView(LoginRequiredMixin, DomainMixin, UpdateView):\n    context_object_name = 'domain'\n    template_name = 'custom_domain/update.html'\n\n    def get_form_kwargs(self):\n        kwargs = super(DomainUpdateView, self).get_form_kwargs()\n        kwargs.update({'user': self.request.user})\n        return kwargs\n\n    def get_queryset(self):\n        qs = Domain.objects\n        if self.request.user.is_staff:\n            return qs\n        else:\n            return qs.filter(user=self.request.user)\n\n    def get_success_url(self):\n        if self.object.approved:\n            return reverse('domain-list')\n        else:\n            return reverse('domain-pending-list')\n", "path": "django_project/base/views/custom_domain.py"}]}