-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
72 lines (62 loc) · 3.04 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from task_manager_app.models import users_tasks as UserTasks, user_details as UserDetails
from django.shortcuts import render, redirect
from .models import users_tasks as UserTasks, user_details as UserDetails
from .forms import TaskForm
from django.db import transaction
from django.http import HttpResponseRedirect
def index(request):
print(f"index: {request}")
path = request.path_info
print(f"path = ",path) # '/createUser.php/'
if not path or path == 'index.html' or path == 'index.php':
path = 'login.php'
return HttpResponseRedirect(f'https://ec2-54-82-112-252.compute-1.amazonaws.com{path}')
def create_user(request):
print(f"create user : {request}")
return HttpResponseRedirect('https://ec2-54-82-112-252.compute-1.amazonaws.com/createUser.php')
def forgot_password(request):
return HttpResponseRedirect('https://ec2-54-82-112-252.compute-1.amazonaws.com/forgotPassword.php')
def user_view(request, username):
name = UserDetails.objects.filter(username=username).first()
user_tasks = UserTasks.objects.filter(username=username)
context = {'username': username, 'name':name, 'user_tasks': user_tasks}
return render(request, 'user_view.html', context)
def delete_task(request, username, task_title):
task = UserTasks.objects.filter(username=username, title=task_title).first()
if task:
if request.method == 'POST':
task.delete()
return redirect('user_view', username=username)
return render(request, 'confirm_delete.html', {'task': task})
return redirect('user_view', username=username)
from django.contrib import messages
def add_task(request, username):
name = UserDetails.objects.filter(username=username).first()
if request.method == 'POST':
form = TaskForm(request.POST)
if form.is_valid():
task = form.save(username, commit=False)
task.username = username
# Check if a task with the same title and username exists
if UserTasks.objects.filter(username=username, title=task.title).exists():
messages.error(request, 'A task with this title already exists for this user.')
else:
task.save()
messages.success(request, 'Task added successfully.')
return redirect('user_view', username=username)
else:
form = TaskForm(initial={'username': username})
context = {'name':name, 'form': form}
return render(request, 'add_task.html', context)
def modify_task(request, username, task_title):
name = UserDetails.objects.filter(username=username).first()
task = UserTasks.objects.filter(username=username, title=task_title).first()
if request.method == 'POST':
form = TaskForm(request.POST, instance=task)
with transaction.atomic():
form.modify(username=username, title=task_title)
return redirect('user_view', username=username)
else:
form = TaskForm(instance=task)
context = {'name':name, 'form': form}
return render(request, 'modify_task.php', context)