<filename>wedding/views.py
import json
from django.contrib.auth import authenticate, login, logout
from django.http.response import JsonResponse
from django.shortcuts import render
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.db import IntegrityError
from django.core.management.base import CommandError
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt

from .models import User, Task, CookerProfile, WeddingPlannerProfile


def index(request):
    return render(request, "wedding/index.html")


# List of tasks
@login_required
def tasks(request):
    taskList = Task.objects.all()
    return render(request, "wedding/tasks.html", {
        "tasks" : taskList
    })

#Save task
@csrf_exempt
@login_required
def createTask(request):
    if (request.method != 'POST'):
        return JsonResponse({"error": "POST request required."}, status=400)

    data = json.loads(request.body)
    content = data["content"]
    
    try:
        idTask = content['id']
        if idTask is not None:
            task = Task.objects.get(pk=idTask)
            if (task is not None):
                task.Title = content['title']
                task.Content = content['content']
                task.Budget = content['budget']
                task.save()
        else:
            task = Task(
                Title = content['title'],
                Content = content['content'],
                Budget = content['budget']
            )

            task.save()
    except IntegrityError:
            # already been created because we got IntegrityError
            raise CommandError("Task not saved")

    return JsonResponse({'message': 'Task created successfully'}, status=200)


@csrf_exempt
@login_required
def deleteTask(request, task_id):
    if (request.method != 'POST'):
        return JsonResponse({'error', 'POST request required'}, status=400)
    
    try: 
        task = Task.objects.get(id=task_id)
        task.delete()
    except IntegrityError:
            # already been created because we got IntegrityError
            raise CommandError("Task not saved")

    return JsonResponse({'message': 'Task deleted successfully'}, status=200)


@login_required
def getTask(request, task_id):

    if (request.method != 'GET'):
        return JsonResponse({'error', 'GET request required'}, status=400)
    
    try:
        task = Task.objects.get(pk=task_id)
    except Task.DoesNotExist:
        # already been created because we got DoesNotExist
        raise CommandError('Task not exist')

    return JsonResponse(task.serialize())


def calendar(request):
    return render(request, "wedding/calendar.html")


def getSettingsView(request):
    return render(request, "wedding/settings.html")


def getContactList(request):
    return render(request, "wedding/contact.html")


def search(request):
    return render(request, "wedding/search.html")


def board(request):
    return render(request, "wedding/board.html")


def login_view(request):
    if request.method == "POST":

        # Attempt to sign user in
        username = request.POST["username"]
        password = request.POST["password"]
        user = authenticate(request, username=username, password=password)

        # Check if authentication successful
        if user is not None:
            login(request, user)

            if user.is_cooker or user.is_wedding_planner:
                return HttpResponseRedirect(reverse("profile_settings"))
            else:
                return HttpResponseRedirect(reverse("tasks"))
        else:
            return render(request, "wedding/login.html", {
                "message": "Invalid username and/or password."
            })
    else:
        return render(request, "wedding/login.html")


def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse("index"))


def register(request):
    if request.method == "POST":
        username = request.POST["username"]
        email = request.POST["email"]

        # Ensure password matches confirmation
        password = request.POST["password"]
        confirmation = request.POST["confirmation"]
        if password != confirmation:
            return render(request, "wedding/registration/register.html", {
                "message": "Passwords must match."
            })

        # Attempt to create new user
        try:
            user = User.objects.create_user(username, email, password)
            user.save()
        except IntegrityError:
            return render(request, "wedding/registration/register.html", {
                "message": "Username already taken."
            })
        login(request, user)
        return HttpResponseRedirect(reverse("tasks"))
    else:
        return render(request, "wedding/registration/register.html")


def registerCooker(request):
    if request.method == "POST":
        username = request.POST['username']
        email = request.POST['email']
        phone = request.POST['phone']
        specialties = request.POST['specialties']
        password = request.POST["password"]
        confirmation = request.POST["confirmation"]
        if password != confirmation:
            return render(request, "wedding/registration/register.html", {
                "message": "Passwords must match."
            })
        
        # print(f'username: {username} \n email: {email} \n specialties: {specialties} \n password: {password} \n phone {phone}')

        try:
            user = User.objects.create_user(username=username, email=email, password=password, phone=phone, is_cooker=True)
            user.save()
            cooker = CookerProfile.objects.create(user=user, specialty=specialties)
            cooker.save()
        except IntegrityError:
            return render(request, "wedding/registration/registerCooker.html", {
                "message": "Username already taken."
            })
        
        login(request, user)
        return HttpResponseRedirect(reverse("profile_contacts"))

    else:
        return render(request, "wedding/registration/registerCooker.html")


def registerWeddingPlanner(request):
    if request.method == "POST":
        username = request.POST['username']
        email = request.POST['email']
        phone = request.POST['phone']
        password = request.POST["password"]
        confirmation = request.POST["confirmation"]
        address =  request.POST["address"]
        description =  request.POST["description"]
        if password != confirmation:
            return render(request, "wedding/registration/registerWeddingPlanner.html", {
                "message": "Passwords must match."
            })
    
        # print(f'username: {username} \n email: {email} \n specialties: {specialties} \n password: {password} \n phone {phone}')

        try:
            user = User.objects.create_user(username=username, email=email, password=password, phone=phone, is_wedding_planner=True)
            user.save()
            weddingPlanner = WeddingPlannerProfile.objects.create(user=user, address=address, description=description)
            weddingPlanner.save()
        except IntegrityError:
            return render(request, "wedding/registration/registerWeddingPlanner.html", {
                "message": "Username already taken."
            })
        
        login(request, user)
        return HttpResponseRedirect(reverse("profile_contacts"))

    else:
        return render(request, "wedding/registration/registerWeddingPlanner.html")

def selectViewRegistrationType(request):
    return render(request, "wedding/registration/selectUserTypeView.html") 
    

def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse("index"))
<filename>wedding/views.py
import json
from django.contrib.auth import authenticate, login, logout
from django.http.response import JsonResponse
from django.shortcuts import render
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.db import IntegrityError
from django.core.management.base import CommandError
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt

from .models import User, Task, CookerProfile, WeddingPlannerProfile


def index(request):
    return render(request, "wedding/index.html")


# List of tasks
@login_required
def tasks(request):
    taskList = Task.objects.all()
    return render(request, "wedding/tasks.html", {
        "tasks" : taskList
    })

#Save task
@csrf_exempt
@login_required
def createTask(request):
    if (request.method != 'POST'):
        return JsonResponse({"error": "POST request required."}, status=400)

    data = json.loads(request.body)
    content = data["content"]
    
    try:
        idTask = content['id']
        if idTask is not None:
            task = Task.objects.get(pk=idTask)
            if (task is not None):
                task.Title = content['title']
                task.Content = content['content']
                task.Budget = content['budget']
                task.save()
        else:
            task = Task(
                Title = content['title'],
                Content = content['content'],
                Budget = content['budget']
            )

            task.save()
    except IntegrityError:
            # already been created because we got IntegrityError
            raise CommandError("Task not saved")

    return JsonResponse({'message': 'Task created successfully'}, status=200)


@csrf_exempt
@login_required
def deleteTask(request, task_id):
    if (request.method != 'POST'):
        return JsonResponse({'error', 'POST request required'}, status=400)
    
    try: 
        task = Task.objects.get(id=task_id)
        task.delete()
    except IntegrityError:
            # already been created because we got IntegrityError
            raise CommandError("Task not saved")

    return JsonResponse({'message': 'Task deleted successfully'}, status=200)


@login_required
def getTask(request, task_id):

    if (request.method != 'GET'):
        return JsonResponse({'error', 'GET request required'}, status=400)
    
    try:
        task = Task.objects.get(pk=task_id)
    except Task.DoesNotExist:
        # already been created because we got DoesNotExist
        raise CommandError('Task not exist')

    return JsonResponse(task.serialize())


def calendar(request):
    return render(request, "wedding/calendar.html")


def getSettingsView(request):
    return render(request, "wedding/settings.html")


def getContactList(request):
    return render(request, "wedding/contact.html")


def search(request):
    return render(request, "wedding/search.html")


def board(request):
    return render(request, "wedding/board.html")


def login_view(request):
    if request.method == "POST":

        # Attempt to sign user in
        username = request.POST["username"]
        password = request.POST["password"]
        user = authenticate(request, username=username, password=password)

        # Check if authentication successful
        if user is not None:
            login(request, user)

            if user.is_cooker or user.is_wedding_planner:
                return HttpResponseRedirect(reverse("profile_settings"))
            else:
                return HttpResponseRedirect(reverse("tasks"))
        else:
            return render(request, "wedding/login.html", {
                "message": "Invalid username and/or password."
            })
    else:
        return render(request, "wedding/login.html")


def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse("index"))


def register(request):
    if request.method == "POST":
        username = request.POST["username"]
        email = request.POST["email"]

        # Ensure password matches confirmation
        password = request.POST["password"]
        confirmation = request.POST["confirmation"]
        if password != confirmation:
            return render(request, "wedding/registration/register.html", {
                "message": "Passwords must match."
            })

        # Attempt to create new user
        try:
            user = User.objects.create_user(username, email, password)
            user.save()
        except IntegrityError:
            return render(request, "wedding/registration/register.html", {
                "message": "Username already taken."
            })
        login(request, user)
        return HttpResponseRedirect(reverse("tasks"))
    else:
        return render(request, "wedding/registration/register.html")


def registerCooker(request):
    if request.method == "POST":
        username = request.POST['username']
        email = request.POST['email']
        phone = request.POST['phone']
        specialties = request.POST['specialties']
        password = request.POST["password"]
        confirmation = request.POST["confirmation"]
        if password != confirmation:
            return render(request, "wedding/registration/register.html", {
                "message": "Passwords must match."
            })
        
        # print(f'username: {username} \n email: {email} \n specialties: {specialties} \n password: {password} \n phone {phone}')

        try:
            user = User.objects.create_user(username=username, email=email, password=password, phone=phone, is_cooker=True)
            user.save()
            cooker = CookerProfile.objects.create(user=user, specialty=specialties)
            cooker.save()
        except IntegrityError:
            return render(request, "wedding/registration/registerCooker.html", {
                "message": "Username already taken."
            })
        
        login(request, user)
        return HttpResponseRedirect(reverse("profile_contacts"))

    else:
        return render(request, "wedding/registration/registerCooker.html")


def registerWeddingPlanner(request):
    if request.method == "POST":
        username = request.POST['username']
        email = request.POST['email']
        phone = request.POST['phone']
        password = request.POST["password"]
        confirmation = request.POST["confirmation"]
        address =  request.POST["address"]
        description =  request.POST["description"]
        if password != confirmation:
            return render(request, "wedding/registration/registerWeddingPlanner.html", {
                "message": "Passwords must match."
            })
    
        # print(f'username: {username} \n email: {email} \n specialties: {specialties} \n password: {password} \n phone {phone}')

        try:
            user = User.objects.create_user(username=username, email=email, password=password, phone=phone, is_wedding_planner=True)
            user.save()
            weddingPlanner = WeddingPlannerProfile.objects.create(user=user, address=address, description=description)
            weddingPlanner.save()
        except IntegrityError:
            return render(request, "wedding/registration/registerWeddingPlanner.html", {
                "message": "Username already taken."
            })
        
        login(request, user)
        return HttpResponseRedirect(reverse("profile_contacts"))

    else:
        return render(request, "wedding/registration/registerWeddingPlanner.html")

def selectViewRegistrationType(request):
    return render(request, "wedding/registration/selectUserTypeView.html") 
    

def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse("index"))
# List of tasks #Save task # already been created because we got IntegrityError # already been created because we got IntegrityError # already been created because we got DoesNotExist # Attempt to sign user in # Check if authentication successful # Ensure password matches confirmation # Attempt to create new user # print(f'username: {username} \n email: {email} \n specialties: {specialties} \n password: {password} \n phone {phone}') # print(f'username: {username} \n email: {email} \n specialties: {specialties} \n password: {password} \n phone {phone}')