django-pim / sitetracker / views.py

from django.contrib.auth.decorators import login_required
from django.views.generic.create_update import create_object, update_object
from django.contrib.contenttypes.models import ContentType
from django.views.generic.list_detail import object_detail
from sitetracker.models import HostingProvider, Domain
from django.shortcuts import get_object_or_404, redirect, render
from projects.models import Project
from django.contrib import messages

@login_required
def index(req):
    return render(req, "sitetracker/base.html")

@login_required
def create(req, *args, **kwargs):
    ct = ContentType.objects.get_for_model(kwargs['form_class'].Meta.model)
    kwargs.update({'template_name':'sitetracker/new_form.html', 'extra_context':{'type':ct.name.title()}})
    return create_object(req, *args, **kwargs)

@login_required
def details(req, *args, **kwargs):
    return object_detail(req, *args, **kwargs)

@login_required
def update(req, *args, **kwargs):
    ct = ContentType.objects.get_for_model(kwargs['form_class'].Meta.model)
    kwargs.update({'template_name':'sitetracker/edit_form.html', 'extra_context':{'type':ct.name.title()}})
    return update_object(req, *args, **kwargs)

@login_required
def server_details(req, slug, server_slug):
    provider = get_object_or_404(HostingProvider, slug=slug)
    server = get_object_or_404(provider.server_set, slug=server_slug)
    return render(req, "sitetracker/server_detail.html", {'object':server})

@login_required
def update_server(req, slug, server_slug, form_class):
    ct = ContentType.objects.get_for_model(form_class.Meta.model)
    kwargs = {'slug':server_slug, 'form_class':form_class}
    kwargs.update({'template_name':'sitetracker/edit_form.html', 'extra_context':{'type':ct.name.title()}})
    return update_object(req, *[], **kwargs)

@login_required
def domain_details(req, slug, server_slug, domain):
    provider = get_object_or_404(HostingProvider, slug=slug)
    server = get_object_or_404(provider.server_set, slug=server_slug)
    domain = get_object_or_404(server.domain_set, name=domain)
    return render(req, "sitetracker/domain_detail.html", {'object':domain})

@login_required
def update_domain(req, slug, server_slug, domain, form_class):
    ct = ContentType.objects.get_for_model(form_class.Meta.model)
    provider = get_object_or_404(HostingProvider, slug=slug)
    server = get_object_or_404(provider.server_set, slug=server_slug)
    domain = get_object_or_404(server.domain_set, name=domain)
    kwargs = {'object_id':domain.pk, 'form_class':form_class}
    kwargs.update({'template_name':'sitetracker/edit_form.html', 'extra_context':{'type':ct.name.title()}})
    return update_object(req, *[], **kwargs)

@login_required
def add_project(req, slug, server_slug, domain):
    if req.method == 'GET' or 'project_id' not in req.POST:
        return redirect('servers-home')
    provider = get_object_or_404(HostingProvider, slug=slug)
    server = get_object_or_404(provider.server_set, slug=server_slug)
    domain = get_object_or_404(server.domain_set, name=domain)
    project = Project.objects.get(pk=req.POST.get('project_id'))
    domain.projects.add(project)
    messages.success(req, 'Project <b>%s</b> added to domain <b>%s</b>.' % (project.title, domain.name))
    return redirect(domain)

@login_required
def remove_project(req, slug, server_slug, domain):
    if 'project_id' not in req.GET:
        return redirect('servers-home')
    provider = get_object_or_404(HostingProvider, slug=slug)
    server = get_object_or_404(provider.server_set, slug=server_slug)
    domain = get_object_or_404(server.domain_set, name=domain)
    project = Project.objects.get(pk=req.GET.get('project_id'))
    domain.projects.remove(project)
    messages.success(req, 'Project <b>%s</b> removed from domain <b>%s</b>.' % (project.title, domain.name))
    return redirect(domain)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.