Pastebin
Paste #25073: No description
< previous paste - next paste>
Pasted by Anonymous Coward
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from .models import Customer, Deal, Lead, Contact
# from .forms import CustomerForm, DealForm, LeadForm
from .forms import DealForm, LeadForm, ContactForm, CustomerForm
from django_tables2 import SingleTableView
from .tables import ContactTable, DealTable, LeadTable, CustomerTable
from django.urls import reverse_lazy
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.views import LoginView
from django.shortcuts import redirect
from django.urls import reverse_lazy
class MyLoginView(LoginView):
template_name = 'crm/login.html'
def logout_view(request):
logout(request)
return redirect(reverse_lazy('login'))
def user_is_consultant(user):
return user.userprofile.is_consultant()
def user_is_manager(user):
return user.userprofile.is_manager()
def user_is_sales(user):
return user.userprofile.is_sales()
def user_is_both_manager_and_consultant(user):
return user.userprofile.is_consultant() and user.userprofile.is_manager()
# ------------------- Contact ---------------------
@user_passes_test(lambda u: u.userprofile.is_sales())
def contact_edit(request, pk):
contact = get_object_or_404(Contact, pk=pk)
if request.method == "POST":
form = ContactForm(request.POST, instance=contact)
if form.is_valid():
contact = form.save(commit=False)
contact.save()
return redirect('contacts_view')
else:
form = ContactForm(instance=contact)
return render(request, 'crm/contact_edit.html', {'form': form})
def contact_new(request):
if request.method == "POST":
form = ContactForm(request.POST)
if form.is_valid():
contact = form.save(commit=False)
contact.save()
return redirect('contacts_view')
else:
form = ContactForm()
return render(request, 'crm/contact_edit.html', {'form': form})
class ContactsListView(SingleTableView):
model = Contact
table_class = ContactTable
template_name = "crm/contact_list.html"
class ContactDeleteView(DeleteView):
model = Contact
success_url = reverse_lazy("contacts_view")
class ContactDetailView(DetailView):
model = Contact
class ContactEditView(UpdateView):
model = Contact
fields = ('name',
'customer',
'email',
'phone',
'title',
'notes',
'created_on',
)
template_name_suffix = "_form"
# ------------------- Customer ---------------------
def customer_edit(request, pk):
customer = get_object_or_404(Customer, pk=pk)
if request.method == "POST":
form = CustomerForm(request.POST, instance=customer)
if form.is_valid():
customer = form.save(commit=False)
customer.save()
return redirect('customers_view')
else:
form = CustomerForm(instance=customer)
return render(request, 'crm/customer_edit.html', {'form': form})
def customer_new(request):
if request.method == "POST":
form = CustomerForm(request.POST)
if form.is_valid():
customer = form.save(commit=False)
customer.save()
return redirect('customers_view')
else:
form = CustomerForm()
return render(request, 'crm/customer_edit.html', {'form': form})
class CustomersListView(SingleTableView):
model = Customer
table_class = CustomerTable
class CustomerDeleteView(DeleteView):
model = Customer
success_url = reverse_lazy("customers_view")
class CustomerDetailView(DetailView):
model = Customer
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['deal_list'] = Deal.objects.all()
context['contact_list'] = Contact.objects.all()
return context
class CustomerListView(SingleTableView):
model = Customer
table_class = CustomerTable
# Not sure if this is used?
# template_name = "crm/customer_list.html"
class CustomerDetailView(DetailView):
model = Customer
class CustomerEditView(UpdateView):
model = Customer
fields = ("name",
"email",
"phone",
"address",
"city",
"country",
"website",
"notes",
"logo_path",
"created_on",
)
template_name_suffix = "_form"
# ------------------- Deal ---------------------
def deal_edit(request, pk):
deal = get_object_or_404(Deal, pk=pk)
if request.method == "POST":
form = DealForm(request.POST, instance=deal)
if form.is_valid():
deal = form.save(commit=False)
deal.save()
return redirect('deals_view')
else:
form = DealForm(instance=deal)
return render(request, 'crm/deal_edit.html', {'form': form})
def deal_detail(request, pk):
deal = get_object_or_404(Deal, pk=pk)
return render(request, 'crm/deal_detail.html', {'deal': deal})
def deal_new(request):
if request.method == "POST":
form = DealForm(request.POST)
if form.is_valid():
deal = form.save(commit=False)
deal.save()
return redirect('deals_view')
else:
form = DealForm()
return render(request, 'crm/deal_edit.html', {'form': form})
class DealListView(SingleTableView):
model = Deal
table_class = DealTable
template_name = "crm/deal_list.html"
class DealDeleteView(DeleteView):
model = Deal
success_url = reverse_lazy("deals_view")
class DealDetailView(DetailView):
model = Deal
# ------------------- Lead ---------------------
def lead_edit(request, pk):
lead = get_object_or_404(Lead, pk=pk)
if request.method == "POST":
form = LeadForm(request.POST, instance=lead)
if form.is_valid():
lead = form.save(commit=False)
lead.save()
return redirect('leads_view')
else:
form = LeadForm(instance=lead)
return render(request, 'crm/lead_edit.html', {'form': form})
def lead_new(request):
if request.method == "POST":
form = LeadForm(request.POST)
if form.is_valid():
lead = form.save(commit=False)
lead.save()
return redirect('leads_view')
else:
form = LeadForm()
return render(request, 'crm/lead_form.html', {'form': form})
class LeadsListView(SingleTableView):
model = Lead
table_class = LeadTable
template_name = "crm/lead_list.html"
class LeadDeleteView(DeleteView):
model = Lead
success_url = reverse_lazy("leads_view")
class LeadDetailView(DetailView):
model = Lead
class LeadEditView(UpdateView):
model = Lead
fields = ('title',
'company_name',
'contact_name',
'contact_email',
'contact_phone',
'contact_title',
'contact_other',
'company_address',
'company_country',
'notes',
'status',
'source',
'marked_spam',
'message')
template_name_suffix = "_form"
# ------------------- timesheets ---------------------
New Paste
Go to most recent paste.