Skip to content

Instantly share code, notes, and snippets.

@hseritt
Created February 25, 2017 13:05
Show Gist options
  • Save hseritt/1b10fbdedd5d0298ca5898817a075ba4 to your computer and use it in GitHub Desktop.
Save hseritt/1b10fbdedd5d0298ca5898817a075ba4 to your computer and use it in GitHub Desktop.
Django urls - examples of how to write them (permissions, class views, etc.)
"""
URL patterns for common views
"""
from django.conf.urls import url
from django.contrib.auth.decorators import permission_required
from django.contrib.auth.views import login, logout
from django.views.generic.edit import DeleteView
from apps.business.models import SalesPhase, SeverityLevel
from apps.cases.models import CaseCategory, CaseStatus
from apps.common.views import Index, Start
from apps.common.view.console import Console
from apps.common.view.add_views import (
ArticleAdd, ArticleAttachmentAdd, CaseAdd, CaseAttachmentAdd,
CaseCommentAdd, ContactAdd, InteractionAdd, IssueAdd, IssueCommentAdd,
IssueAttachmentAdd, ProjectComponentAdd, ProjectAdd, ProjectVersionAdd,
OrganizationAdd, SloAdd, SubscriptionAdd)
from apps.common.view.delete_views import ComponentDelete, ProjectVersionDelete
from apps.common.view.detail_views import (
ArticleDetail, CaseDetail, ContactDetail, InteractionDetail, IssueDetail,
OrganizationDetail, ProjectDetail, SubscriptionDetail)
from apps.common.view.list_views import (
Activities, Articles, Cases, Contacts, Issues, Organizations,
ProjectComponents, Projects, ProjectVersions, Subscriptions)
from apps.common.view.search_views import Search
from apps.common.view.update_views import (
ArticleUpdate, CaseCommentUpdate, CaseUpdate, ContactUpdate, IssueUpdate, OrganizationUpdate,
ProjectUpdate, ProjectComponentUpdate, ProjectVersionUpdate, SubscriptionUpdate)
from apps.common.views import (
ArticleAttachmentDownload, CaseAttachmentDownload, IssueAttachmentDownload
)
from apps.contacts.models import Region
from apps.issues.models import IssueType, Priority, Status
urlpatterns = [
url(
regex=r'^activities/$',
view=permission_required('is_superuser')(Activities.as_view()),
name='activities'
),
url(
regex=r'^article/add/$',
view=ArticleAdd.as_view(),
name='article_add'
),
url(
regex=r'^article/(?P<article_id>\d+)/attachment/add/$',
view=ArticleAttachmentAdd.as_view(),
name='article_attachment_add'
),
url(
regex=r'^article/attachment/(?P<attachment_id>\d+)/download',
view=ArticleAttachmentDownload.as_view(),
name='article_attachment_download'
),
url(
regex=r'^article/(?P<article_id>\d+)/$',
view=ArticleDetail.as_view(),
name='article_detail'
),
url(
regex=r'^article/(?P<article_id>\d+)/update/$',
view=ArticleUpdate.as_view(),
name='article_update'
),
url(
regex=r'^articles/$',
view=Articles.as_view(),
name='articles'
),
url(
regex=r'^case/add/$',
view=CaseAdd.as_view(),
name='case_add'
),
url(
regex=r'^case/attachment/(?P<attachment_id>\d+)/download',
view=CaseAttachmentDownload.as_view(),
name='case_attachment_download'
),
url(
regex=r'^case/(?P<case_id>\d+)/$',
view=CaseDetail.as_view(),
name='case_detail'
),
url(
regex=r'^case/(?P<case_id>\d+)/attachment/add/$',
view=CaseAttachmentAdd.as_view(),
name='case_attachment_add'
),
url(
regex=r'^case/(?P<case_id>\d+)/comment/add/$',
view=CaseCommentAdd.as_view(),
name='case_comment_add'
),
url(
regex=r'^case/(?P<case_id>\d+)/comment/(?P<comment_id>\d+)/update/$',
view=CaseCommentUpdate.as_view(),
name='case_comment_update'
),
url(
regex=r'^case/(?P<case_id>\d+)/update/$',
view=CaseUpdate.as_view(),
name='case_update'
),
url(
regex=r'^cases/$',
view=Cases.as_view(),
name='cases'
),
url(
regex=r'^component/(?P<component_id>\d+)/delete/$',
view=ComponentDelete.as_view(),
name='component_delete',
),
url(
regex=r'^console/$',
view=Console.as_view(),
name='console'
),
url(
regex=r'^console/(?P<topic>[\w\-]+)/$',
view=Console.as_view(),
name='console'
),
url(
regex=r'^contact/add/$',
view=ContactAdd.as_view(),
name='contact_add'
),
url(
regex=r'^contacts/$',
view=Contacts.as_view(),
name='contacts'
),
url(
regex=r'^$',
view=Index.as_view(),
name='index'
),
url(
regex=r'^interaction/(?P<interaction_id>\d+)/$',
view=InteractionDetail.as_view(),
name='interaction_detail'
),
url(
regex=r'^issues/$',
view=Issues.as_view(),
name='issues'
),
url(
regex=r'^issue/add/$',
view=IssueAdd.as_view(),
name='issue_add'
),
url(
regex=r'^issue/attachment/(?P<attachment_id>\d+)/download',
view=IssueAttachmentDownload.as_view(),
name='issue_attachment_download'
),
url(
regex=r'^issue/(?P<issue_id>\d+)/$',
view=IssueDetail.as_view(),
name='issue_detail'
),
url(
regex=r'^issue/(?P<issue_id>\d+)/attachment/add/$',
view=IssueAttachmentAdd.as_view(),
name='issue_attachment_add'
),
url(
regex=r'^issue/(?P<issue_id>\d+)/comment/add/$',
view=IssueCommentAdd.as_view(),
name='issue_comment_add'
),
url(
regex=r'^issue/(?P<issue_id>\d+)/update/$',
view=IssueUpdate.as_view(),
name='issue_update'
),
url(
regex=r'^login/$',
view=login,
kwargs={'template_name': 'login.html'},
name='login'
),
url(
regex=r'^logout/$',
view=logout,
kwargs={'next_page': '/activo/login/'},
name='logout'
),
url(
regex=r'^organization/add/$',
view=OrganizationAdd.as_view(),
name='organization_add'
),
url(
regex=r'^organization/(?P<organization_id>\d+)/$',
view=OrganizationDetail.as_view(),
name='organization_detail'
),
url(
regex=r'^organization/(?P<organization_id>\d+)/interaction/add/$',
view=InteractionAdd.as_view(),
name='interaction_add'
),
url(
regex=r'^organizations/$',
view=Organizations.as_view(),
name='organizations'
),
url(
regex=r'^organization/(?P<organization_id>\d+)/update/$',
view=OrganizationUpdate.as_view(),
name='organization_update'
),
url(
regex=r'^project/add/$',
view=ProjectAdd.as_view(),
name='project_add'
),
url(
regex=r'^project/(?P<project_key>[\w\-]+)/$',
view=ProjectDetail.as_view(),
name='project_detail'
),
url(
regex=r'^project/(?P<project_key>[\w\-]+)/components/$',
view=ProjectComponents.as_view(),
name='project_components'
),
url(
regex=r'^project/(?P<project_key>[\w\-]+)/component/add/$',
view=ProjectComponentAdd.as_view(),
name='project_component_add'
),
url(
regex=r'^component/(?P<component_id>\d+)/update/$',
view=ProjectComponentUpdate.as_view(),
name='project_component_update'
),
url(
regex=r'^project/(?P<project_key>[\w\-]+)/version/add/$',
view=ProjectVersionAdd.as_view(),
name='project_version_add',
),
url(
regex=r'^project/(?P<project_key>[\w\-]+)/update/$',
view=ProjectUpdate.as_view(),
name='project_update'
),
url(
regex=r'^project/(?P<project_key>[\w\-]+)/versions/$',
view=ProjectVersions.as_view(),
name='project_versions',
),
url(
regex=r'^projects/$',
view=Projects.as_view(),
name='projects'
),
url(
regex=r'^projectversion/(?P<projectversion_id>\d+)/delete/$',
view=ProjectVersionDelete.as_view(),
name='project_version_delete',
),
url(
regex=r'^projectversion/(?P<projectversion_id>\d+)/update/$',
view=ProjectVersionUpdate.as_view(),
name='project_version_update',
),
url(
regex=r'^search/$',
view=Search.as_view(),
name='search'
),
url(
regex=r'^start/$',
view=Start.as_view(),
name='start'
),
url(
regex=r'^subscriptions/$',
view=Subscriptions.as_view(),
name='subscriptions'
),
url(
regex=r'^subscription/add/$',
view=SubscriptionAdd.as_view(),
name='subscription_add'
),
url(
regex=r'^subscription/(?P<subscription_id>\d+)/$',
view=SubscriptionDetail.as_view(),
name='subscription_detail'
),
url(
regex=r'^subscription/(?P<subscription_id>\d+)/slo/add/$',
view=SloAdd.as_view(),
name='slo_add'
),
url(
regex=r'^subscription/(?P<subscription_id>\d+)/update/$',
view=SubscriptionUpdate.as_view(),
name='subscription_update'
),
url(
regex=r'^contact/(?P<user_id>\d+)/$',
view=ContactDetail.as_view(),
name='contact_detail'
),
url(
regex=r'^contact/(?P<user_id>\d+)/update/$',
view=ContactUpdate.as_view(),
name='contact_update'
),
url(
regex=r'^casecategory/(?P<pk>[0-9]+)/delete/$',
view=DeleteView.as_view(
model=CaseCategory,
success_url="/activo/console/casecategories/"),
name='casecategory_delete'
),
url(
regex=r'^casestatus/(?P<pk>[0-9]+)/delete/$',
view=DeleteView.as_view(
model=CaseStatus,
success_url="/activo/console/casestatuses/"),
name='casestatus_delete'),
url(
regex=r'^issuetype/(?P<pk>[0-9]+)/delete/$',
view=DeleteView.as_view(
model=IssueType,
success_url="/activo/console/issuetypes/"),
name='issuetype_delete'
),
url(
regex=r'^priority/(?P<pk>[0-9]+)/delete/$',
view=DeleteView.as_view(
model=Priority,
success_url="/activo/console/priorities/"),
name='priority_delete'
),
url(
regex=r'^region/(?P<pk>[0-9]+)/delete/$',
view=DeleteView.as_view(
model=Region,
success_url="/activo/console/regions/"),
name='region_delete'
),
url(
regex=r'^salesphase/(?P<pk>[0-9]+)/delete/$',
view=DeleteView.as_view(
model=SalesPhase,
success_url="/activo/console/salesphases/"),
name='salesphase_delete'
),
url(
regex=r'^severitylevel/(?P<pk>[0-9]+)/delete/$',
view=DeleteView.as_view(
model=SeverityLevel,
success_url="/activo/console/severitylevels/"),
name='severitylevel_delete'
),
url(
regex=r'^status/(?P<pk>[0-9]+)/delete/$',
view=DeleteView.as_view(
model=Status,
success_url="/activo/console/statuses/"),
name='status_delete'
),
]
"""
Add model views for the common app.
"""
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import UserPassesTestMixin
from django.contrib.auth.models import Group, User
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.views import View
from apps.articles.models import Article
from apps.articles.services import add_article, add_article_attachment
from apps.business.models import Subscription
from apps.business.services import add_slo, add_subscription
from apps.cases.models import Case
from apps.cases.services import add_case, add_case_attachment, add_case_comment
from apps.common.forms import (
AddressAddForm, ArticleAddForm, ArticleAttachmentAddForm, CaseAddForm,
CaseAttachmentAddForm, CaseCommentAddForm, ContactAddForm, InteractionAddForm,
IssueAddForm, IssueCommentAddForm, IssueAttachmentAddForm, OrganizationAddForm,
ProjectAddForm, ProjectComponentAddForm, ProjectVersionAddForm,
SloAddForm, SubscriptionAddForm)
from apps.common.utils import get_activity_list
from apps.common.view.utils import activity_context, page_template
from apps.contacts.models import Contact, Organization
from apps.contacts.services import add_contact, add_interaction, add_organization
from apps.issues.models import Issue, Project
from apps.issues.services import (
add_component, add_issue, add_issue_attachment, add_issue_comment,
add_project, add_project_version
)
MODEL_ADD_TEMPLATE = 'add_model.html'
JS_CLOSE_AND_REFRESH = '<script type="text/javascript">window.close(); ' \
+ 'window.opener.location.reload(false); window.parent.location.href = "/";</script>'
@method_decorator(login_required, name='dispatch')
class ArticleAdd(UserPassesTestMixin, View):
"""
View class for "/article/add/" view url.
"""
article_add_form = ArticleAddForm
article_add_context = {
'activity_list' : get_activity_list(),
'page_template' : 'page/article_add.html',
'page_title' : 'Create Article',
}
def test_func(self):
"""
Tests if user is in proper group before allowing add.
"""
contact = Contact.objects.get(user=self.request.user)
return contact.organization.org_type == 'Owner' or self.request.user.is_superuser
def get(self, request):
"""
GET method for ArticleAdd class view.
"""
article_add_form = self.article_add_form
context = activity_context
context.update(self.article_add_context)
context.update({'article_add_form': article_add_form})
return render(request, page_template, context)
def post(self, request):
"""
POST method for ArticleAdd class view.
"""
article_add_form = self.article_add_form(request.POST, request.FILES)
if article_add_form.is_valid():
add_article(request, article_add_form)
return HttpResponseRedirect(reverse('articles'))
else:
context = activity_context
context.update(self.article_add_context)
context.update({'article_add_form': article_add_form})
return render(request, page_template, context)
@method_decorator(login_required, name='dispatch')
class ArticleAttachmentAdd(View):
"""
View class for "/article/attachment/add/"
"""
article_att_add_form = ArticleAttachmentAddForm
def get(self, request, article_id):
"""
GET method for ArticleAttachmentAdd class view.
"""
article_att_add_form = self.article_att_add_form()
return render(
request, 'article_attachment_add.html', {
'article_att_add_form': article_att_add_form})
def post(self, request, article_id):
"""
POST method for ArticleAttachmentAdd class view.
"""
article = Article.objects.get(pk=article_id)
form = self.article_att_add_form(request.POST, request.FILES)
if form.is_valid():
add_article_attachment(form, article)
return HttpResponse(JS_CLOSE_AND_REFRESH)
return render(
request, 'article_attachment_add.html', {
'article_att_add_form': form, 'article': article})
@method_decorator(login_required, name='dispatch')
class CaseAdd(View):
"""
View class for "/case/add/" view url.
"""
case_add_form = CaseAddForm
def get(self, request):
"""
GET method for CaseAdd class view.
"""
if not hasattr(request.user, 'contact') or \
not hasattr(request.user.contact, 'organization') or \
request.user.contact.organization is None:
msg = 'Your user account must have a company or organization ' + \
'associated with it.</p><p id="warn-message">Click Ok to ' + \
'redirect to your user profile page.'
return render(
request, 'component/warn.html',
{
'msg' : msg,
'request' : request,
'redirect_url' : 'contact_update',
'redirect_url_arg' : request.user.contact.id,
}
)
elif not Project.objects.filter(is_case_project=True):
msg = 'There are no available projects to create a case.' + \
' Set the associated project as a case project in order to create a case.'
return render(
request, 'component/warn.html',
{
'msg' : msg,
'request' : request,
'redirect_url' : 'projects',
'redirect_url_arg' : None,
}
)
else:
case_add_form = self.case_add_form(request=request)
return render(
request, MODEL_ADD_TEMPLATE,
{
'model_add_form': case_add_form,
'model_name': 'Case',
'request': request
}
)
def post(self, request, *args):
"""
POST method for CaseAdd class view.
"""
form = self.case_add_form(request.POST, request=request)
if form.is_valid():
add_case(request, form)
return HttpResponse(JS_CLOSE_AND_REFRESH)
return render(
request, MODEL_ADD_TEMPLATE,
{
'model_add_form': form,
'model_name': 'Case'
}
)
@method_decorator(login_required, name='dispatch')
class CaseAttachmentAdd(View):
"""
View class for "/case/id/attachment/add/"
"""
case_att_add_form = CaseAttachmentAddForm
def get(self, request, case_id):
"""
GET method for CaseAttachmentAdd class view.
"""
case = Case.objects.get(pk=case_id)
case_att_add_form = self.case_att_add_form()
return render(
request, 'case_attachment_add.html', {
'case_att_add_form': case_att_add_form, 'case': case})
def post(self, request, case_id):
"""
POST method for CaseAttachmentAdd class view.
"""
case = Case.objects.get(pk=case_id)
form = self.case_att_add_form(request.POST, request.FILES)
if form.is_valid():
add_case_attachment(request, form, case_id)
return HttpResponse(JS_CLOSE_AND_REFRESH)
return render(
request, 'case_attachment_add.html', {
'case_att_add_form': form, 'case': case})
@method_decorator(login_required, name='dispatch')
class CaseCommentAdd(View):
"""
View class for "/case/id/comment/add/" view url.
"""
case_comment_add_form = CaseCommentAddForm
def get(self, request, case_id):
"""
GET method for CaseCommentAdd class view.
"""
case = Case.objects.get(pk=case_id)
initial_status = None
if request.user == case.assignee:
initial_status = {'status': case.status}
case_comment_add_form = self.case_comment_add_form(
case=case, initial=initial_status, request=request)
return render(
request, MODEL_ADD_TEMPLATE,
{'model_add_form': case_comment_add_form, 'model_name': 'Case Comment'})
def post(self, request, case_id, *args):
"""
POST method for CaseCommentAdd class view.
"""
case = Case.objects.get(pk=case_id)
form = self.case_comment_add_form(request.POST, case=case, request=request)
if form.is_valid():
add_case_comment(request, form, case)
return HttpResponse(JS_CLOSE_AND_REFRESH)
return render(
request, MODEL_ADD_TEMPLATE,
{'model_add_form': form, 'model_name': 'Case Comment'})
@method_decorator(login_required, name='dispatch')
class ContactAdd(UserPassesTestMixin, View):
"""
View class for "/contact/add/" view url.
"""
contact_add_form = ContactAddForm
def test_func(self):
"""
Tests if user is in proper group before allowing add.
"""
group = Group.objects.get(name='contacts_admin')
return group in self.request.user.groups.all() or self.request.user.is_superuser
def get(self, request):
"""
GET method for ContactAdd class view.
"""
unattached_users = User.objects.filter(contact=None)
if not unattached_users:
if request.user.is_superuser:
msg = 'There are no system users to create a contact. Go to the ' \
+ 'Django Admin console to create more system users.'
else:
msg = 'There are no system users to create a contact. Please see ' \
+ 'your system administrator to add more system users.'
return render(
request, 'component/warn.html',
{
'msg': msg,
'request': request,
'redirect_url': 'contacts',
'redirect_url_arg': None,
}
)
contact_add_form = self.contact_add_form(request=request)
return render(
request, MODEL_ADD_TEMPLATE,
{
'contact_add_form' : contact_add_form,
'model_add_form' : contact_add_form,
'model_name' : 'Contact',
'upload' : True
}
)
def post(self, request, *args):
"""
POST method for ContactAdd class view.
"""
form = self.contact_add_form(request.POST, request.FILES, request=request)
if form.is_valid():
add_contact(request, form)
return HttpResponse(JS_CLOSE_AND_REFRESH)
return render(
request, MODEL_ADD_TEMPLATE, {
'model_add_form': form, 'model_name': 'Contact'})
@method_decorator(login_required, name='dispatch')
class InteractionAdd(View):
"""
View class for adding interactions for organizations.
"""
interaction_add_form = InteractionAddForm
interaction_add_context = {
'activity_list' : get_activity_list(),
'page_template' : 'page/interaction_add.html',
'page_title' : 'New Interaction',
}
def get(self, request, organization_id):
"""
GET method for InteractionAdd class view.
"""
organization = Organization.objects.get(pk=organization_id)
interaction_add_form = self.interaction_add_form(organization=organization)
context = activity_context
context.update(self.interaction_add_context)
context.update(
{
'interaction_add_form': interaction_add_form,
'organization': organization,
}
)
return render(request, page_template, context)
def post(self, request, organization_id):
"""
POST method for InteractionAdd class view.
"""
organization = Organization.objects.get(pk=organization_id)
form = self.interaction_add_form(request.POST, organization=organization)
if form.is_valid():
add_interaction(request, form, organization)
return HttpResponseRedirect(reverse('organization_detail', args=[organization.id]))
context = activity_context
context.update(self.interaction_add_context)
context.update(
{
'interaction_add_form': form,
'organization': organization,
}
)
return render(request, page_template, context)
@method_decorator(login_required, name='dispatch')
class IssueAdd(View):
"""
View class for "/issue/add/" view url.
"""
issue_add_form = IssueAddForm
def get(self, request):
"""
GET method for IssueAdd class view.
"""
issue_add_form = self.issue_add_form(
request=request, initial={'assignee': request.user})
return render(
request, MODEL_ADD_TEMPLATE,
{
'model_add_form': issue_add_form,
'model_name': 'Issue'
}
)
def post(self, request, *args):
"""
POST method for IssueAdd class view.
"""
form = self.issue_add_form(request.POST, request=request)
if form.is_valid():
add_issue(request, form)
return HttpResponse(JS_CLOSE_AND_REFRESH)
return render(
request, MODEL_ADD_TEMPLATE,
{
'model_add_form': form,
'model_name': 'Issue'
}
)
@method_decorator(login_required, name='dispatch')
class IssueAttachmentAdd(View):
"""
View class for "/issue/id/attachment/add/"
"""
issue_att_add_form = IssueAttachmentAddForm
def get(self, request, issue_id):
"""
GET method for IssueAttachmentAdd class view.
"""
issue = Issue.objects.get(pk=issue_id)
issue_att_add_form = self.issue_att_add_form()
return render(
request, 'issue_attachment_add.html', {
'issue_att_add_form': issue_att_add_form, 'issue': issue})
def post(self, request, issue_id):
"""
POST method for IssueAttachmentAdd class view.
"""
form = self.issue_att_add_form(request.POST, request.FILES)
issue = Issue.objects.get(pk=issue_id)
if form.is_valid():
add_issue_attachment(request, form, issue)
return HttpResponse(JS_CLOSE_AND_REFRESH)
return render(
request, 'issue_attachment_add.html', {
'issue_att_add_form': form, 'issue': issue})
@method_decorator(login_required, name='dispatch')
class IssueCommentAdd(View):
"""
View class for "/issue/id/comment/add/" view url.
"""
issue_comment_add_form = IssueCommentAddForm
def get(self, request, issue_id):
"""
GET method for IssueCommentAdd class view.
"""
issue = Issue.objects.get(pk=issue_id)
initial_status = None
if request.user == issue.assignee:
initial_status = {'status': issue.status}
issue_comment_add_form = self.issue_comment_add_form(
initial=initial_status, issue=issue, request=request
)
return render(
request, MODEL_ADD_TEMPLATE,
{
'model_add_form': issue_comment_add_form,
'model_name': 'Issue Comment'
}
)
def post(self, request, issue_id, *args):
"""
POST method for IssueCommentAdd class view.
"""
issue = Issue.objects.get(pk=issue_id)
form = self.issue_comment_add_form(request.POST, issue=issue, request=request)
if form.is_valid():
add_issue_comment(request, form, issue)
return HttpResponse(JS_CLOSE_AND_REFRESH)
return render(
request, MODEL_ADD_TEMPLATE,
{'model_add_form': form, 'model_name': 'Issue Comment'})
@method_decorator(login_required, name='dispatch')
class ProjectAdd(UserPassesTestMixin, View):
"""
View class for "/project/add/" view url.
"""
project_add_form = ProjectAddForm
def test_func(self):
"""
Tests if user is in proper group before allowing add.
"""
group = Group.objects.get(name='issues_admin')
return group in self.request.user.groups.all() or self.request.user.is_superuser
def get(self, request):
"""
GET method for ProjectAdd class view.
"""
project_add_form = self.project_add_form(request=request)
return render(
request, MODEL_ADD_TEMPLATE,
{
'project_add_form' : project_add_form,
'model_add_form' : project_add_form,
'model_name' : 'Project'
}
)
def post(self, request, *args):
"""
POST method for ProjectAdd class view.
"""
form = self.project_add_form(request.POST, request=request)
if form.is_valid():
add_project(request, form)
return HttpResponse(JS_CLOSE_AND_REFRESH)
return render(
request, MODEL_ADD_TEMPLATE,
{
'model_add_form': form,
'model_name': 'Project'
}
)
@method_decorator(login_required, name='dispatch')
class ProjectComponentAdd(View):
"""
View class for "/project/key/component/add/" view url.
"""
project_component_add_form = ProjectComponentAddForm
def get(self, request, project_key):
"""
GET method for ProjectComponentAdd class view.
"""
project_component_add_form = self.project_component_add_form()
return render(
request, MODEL_ADD_TEMPLATE,
{
'model_add_form' : project_component_add_form,
'model_name' : 'Project Component'
}
)
def post(self, request, project_key):
"""
POST method for ProjectComponentAdd class view.
"""
project = Project.objects.get(key=project_key)
form = self.project_component_add_form(request.POST)
if form.is_valid():
add_component(request, form, project)
return HttpResponse(JS_CLOSE_AND_REFRESH)
return render(
request, MODEL_ADD_TEMPLATE,
{
'model_add_form': form,
'model_name': 'Project Component',
}
)
@method_decorator(login_required, name='dispatch')
class ProjectVersionAdd(View):
"""
View class for "/project/key/version/add/" view url.
"""
project_version_add_form = ProjectVersionAddForm
project_version_add_context = {
'activity_list' : get_activity_list(),
'page_template' : 'page/project_version_add.html',
'page_title' : 'Create Project Version',
}
def get(self, request, project_key):
"""
GET method for ProjectVersionAdd class view.
"""
project = Project.objects.get(key=project_key)
project_version_add_form = self.project_version_add_form()
context = activity_context
context.update(self.project_version_add_context)
context.update(
{
'project' : project,
'project_version_add_form' : project_version_add_form
}
)
return render(request, page_template, context)
def post(self, request, project_key):
"""
POST method for ProjectVersionAdd class view.
"""
project = Project.objects.get(key=project_key)
form = self.project_version_add_form(request.POST)
if form.is_valid():
add_project_version(request, form, project)
return HttpResponseRedirect(
reverse('project_versions', args=[project.key])
)
context = activity_context
context.update(self.project_version_add_context)
context.update({'project_version_add_form': form})
return render(request, page_template, context)
@method_decorator(login_required, name='dispatch')
class OrganizationAdd(UserPassesTestMixin, View):
"""
View class for "/organization/add/" view url.
"""
organization_add_form = OrganizationAddForm
address_add_form = AddressAddForm
add_organization_context = {
'activity_list' : get_activity_list(),
'page_template' : 'page/organization_add.html',
'page_title' : 'Create Organization',
}
def test_func(self):
"""
Tests if user is in proper group before allowing add.
"""
group = Group.objects.get(name='contacts_admin')
return group in self.request.user.groups.all() or self.request.user.is_superuser
def get(self, request):
"""
GET method for Organization class view.
"""
organization_add_form = self.organization_add_form
address_add_form = self.address_add_form
context = activity_context
context.update(self.add_organization_context)
context.update(
{
'address_add_form' : address_add_form,
'organization_add_form' : organization_add_form,
}
)
return render(request, page_template, context)
def post(self, request):
"""
POST method for Organization class view.
"""
organization_add_form = self.organization_add_form(request.POST)
address_add_form = self.address_add_form(request.POST)
if address_add_form.is_valid() and organization_add_form.is_valid():
add_organization(request, address_add_form, organization_add_form)
return HttpResponseRedirect(reverse('organizations'))
else:
context = activity_context
context.update(self.add_organization_context)
context.update(
{
'address_add_form' : address_add_form,
'organization_add_form' : organization_add_form,
}
)
return render(request, page_template, context)
@method_decorator(login_required, name='dispatch')
class SloAdd(View):
"""
View class for "/subscription/id/slo/add/" view url.
"""
slo_add_form = SloAddForm
def get(self, request, subscription_id):
"""
GET method for SloAdd class view.
"""
slo_add_form = self.slo_add_form()
return render(
request, MODEL_ADD_TEMPLATE,
{
'model_add_form' : slo_add_form,
'model_name' : 'Severity Level SLO'
}
)
def post(self, request, subscription_id):
"""
POST method for SloAdd class view.
"""
subscription = Subscription.objects.get(pk=subscription_id)
form = self.slo_add_form(request.POST)
if form.is_valid():
add_slo(form, subscription)
return HttpResponse(JS_CLOSE_AND_REFRESH)
return render(
request, MODEL_ADD_TEMPLATE,
{'model_add_form': form, 'model_name': 'Severity Level SLO'}
)
@method_decorator(login_required, name='dispatch')
class SubscriptionAdd(UserPassesTestMixin, View):
"""
View class for "/subscription/add/" view url.
"""
subscription_add_form = SubscriptionAddForm
def test_func(self):
"""
Tests if user is in proper group before allowing add.
"""
group = Group.objects.get(name='business_admin')
return group in self.request.user.groups.all() or self.request.user.is_superuser
def get(self, request):
"""
GET method for SubscriptionAdd class view.
"""
subscription_add_form = self.subscription_add_form()
return render(
request, MODEL_ADD_TEMPLATE,
{
'model_add_form' : subscription_add_form,
'model_name' : 'Subscription'
}
)
def post(self, request, *args):
"""
POST method for SubscriptionAdd class view.
"""
form = self.subscription_add_form(request.POST)
if form.is_valid():
add_subscription(form)
return HttpResponse(JS_CLOSE_AND_REFRESH)
return render(
request, MODEL_ADD_TEMPLATE,
{
'model_add_form': form,
'model_name': 'Subscription'
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment