The Dashboard Accounts Django Source
VIEWS
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.forms import inlineformset_factory
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
from django.contrib.auth.decorators import login_required
# Create your views here.
from .models import *
from .forms import OrderForm, CreateUserForm
from .filters import OrderFilter
def registerPage(request):
if request.user.is_authenticated:
return redirect('home')
else:
form = CreateUserForm()
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
form.save()
user = form.cleaned_data.get('username')
messages.success(request, 'Account was created for ' + user)
return redirect('login')
context = {'form':form}
return render(request, 'accounts/register.html', context)
def loginPage(request):
if request.user.is_authenticated:
return redirect('home')
else:
if request.method == 'POST':
username = request.POST.get('username')
password =request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
messages.info(request, 'Username OR password is incorrect')
context = {}
return render(request, 'accounts/login.html', context)
def logoutUser(request):
logout(request)
return redirect('login')
@login_required(login_url='login')
def home(request):
orders = Order.objects.all()
customers = Customer.objects.all()
total_customers = customers.count()
total_orders = orders.count()
delivered = orders.filter(status='Delivered').count()
pending = orders.filter(status='Pending').count()
context = {'orders':orders, 'customers':customers,
'total_orders':total_orders,'delivered':delivered,
'pending':pending }
return render(request, 'accounts/dashboard.html', context)
@login_required(login_url='login')
def products(request):
products = Product.objects.all()
return render(request, 'accounts/products.html', {'products':products})
@login_required(login_url='login')
def customer(request, pk_test):
customer = Customer.objects.get(id=pk_test)
orders = customer.order_set.all()
order_count = orders.count()
myFilter = OrderFilter(request.GET, queryset=orders)
orders = myFilter.qs
context = {'customer':customer, 'orders':orders, 'order_count':order_count,
'myFilter':myFilter}
return render(request, 'accounts/customer.html',context)
@login_required(login_url='login')
def createOrder(request, pk):
OrderFormSet = inlineformset_factory(Customer, Order, fields=('product', 'status'), extra=10 )
customer = Customer.objects.get(id=pk)
formset = OrderFormSet(queryset=Order.objects.none(),instance=customer)
#form = OrderForm(initial={'customer':customer})
if request.method == 'POST':
#print('Printing POST:', request.POST)
form = OrderForm(request.POST)
formset = OrderFormSet(request.POST, instance=customer)
if formset.is_valid():
formset.save()
return redirect('/')
context = {'form':formset}
return render(request, 'accounts/order_form.html', context)
@login_required(login_url='login')
def updateOrder(request, pk):
order = Order.objects.get(id=pk)
form = OrderForm(instance=order)
if request.method == 'POST':
form = OrderForm(request.POST, instance=order)
if form.is_valid():
form.save()
return redirect('/')
context = {'form':form}
return render(request, 'accounts/order_form.html', context)
@login_required(login_url='login')
def deleteOrder(request, pk):
order = Order.objects.get(id=pk)
if request.method == "POST":
order.delete()
return redirect('/')
context = {'item':order}
return render(request, 'accounts/delete.html', context)
MODEL
from django.db import models
# Create your models here.
class Customer(models.Model):
name = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200, null=True)
date_created = models.DateTimeField(auto_now_add=True, null=True)
def __str__(self):
return self.name
class Tag(models.Model):
name = models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
class Product(models.Model):
CATEGORY = (
('Indoor', 'Indoor'),
('Out Door', 'Out Door'),
)
name = models.CharField(max_length=200, null=True)
price = models.FloatField(null=True)
category= models.CharField(max_length=200, null=True, choices=CATEGORY)
date_created = models.DateTimeField(auto_now_add=True, null=True)
tags = models.ManyToManyField(Tag)
description = models.CharField(max_length = 500, null = True, blank = True)
def __str__(self):
return self.name
class Order(models.Model):
STATUS = (
('Pending', 'Pending'),
('Out for delivery', 'Out for delivery'),
('Delivered', 'Delivered')
)
customer = models.ForeignKey(Customer, null = True, on_delete = models.SET_NULL)
product = models.ForeignKey(Product, null = True, on_delete = models.SET_NULL)
date_created = models.DateTimeField(auto_now_add=True,null=True)
status = models.CharField(max_length=200, null=True, choices=STATUS)
def __str__(self):
return self.status
Comments
Post a Comment