-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
133 lines (97 loc) · 5.4 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from django.shortcuts import render,redirect,get_object_or_404
from .models import Phone
from .models import Grp
from django.contrib.auth.models import User
from django.contrib import auth
from django.contrib.auth.decorators import login_required
@login_required(login_url="signup")
def contacts(request,grp_id = ''):
if grp_id:
group_id=get_object_or_404(Grp,pk=grp_id)
contacts=Phone.objects..raw('select * from phone_phone where user_id = {} and group_id = {}'.format(user.id,grp_id))
groups=Grp.objects.raw('select * from phone_grp where user_id = {}'.format(user.id))
return render(request,'phone/index.html',{'contacts':contacts,'groups':groups,'grp_id':group_id.id})
else:
contacts=Phone.objects.raw('select * from phone_phone where user_id = {} '.format(user.id))
groups=Grp.objects.raw('select * from phone_grp where user_id = {}'.format(user.id))
return render(request,'phone/index.html',{'contacts':contacts,'groups':groups})
@login_required(login_url="signup")
def delete(request,contact_id,grp_id = ''):
contact=get_object_or_404(Phone,pk=contact_id)
Phone.objects.raw('delete from phone_phone where user_id = {}'.format(contact.id))
if grp_id:
return redirect('/contacts/'+str(grp_id)+'/')
else:
return redirect('/contacts/')
@login_required(login_url="signup")
def update(request,contact_id):
if request.method=='POST':
contact=get_object_or_404(Phone,pk=contact_id)
if request.POST['name'] and request.POST['number'] and request.POST['group'] :
group=Grp.objects.raw('select * from phone_grp where user_id = {}'.format(user.id))
Phone.objects.raw('update phone_phone set name = {} and number = {} and group_id = {} where user_id = {} '.format(request.POST['name'],request.POST['number'],request.POST['group'] user.id))
return redirect('/contacts/')
else:
group=Grp.objects.raw('select * from phone_grp where user_id = {}'.format(user.id))
return render(request,'phone/add.html',{'error':'All field are required','groups':groups})
else:
contact=get_object_or_404(Phone,pk=contact_id)
group=Grp.objects.raw('select * from phone_grp where user_id = {}'.format(user.id))
return render(request,'phone/update.html',{'groups':groups,'contact':contact})
def signup(request):
if request.method=='POST':
if request.POST['password']==request.POST['confirm']:
try:
user=User.objects.raw('select * from auth_user where username = {}'.format(request.POST['name']))
return render(request,'phone/signup.html',{'error':'user alredy'})
except User.DoesNotExist:
user=User.objects.raw('insert into auth_user (username , email , password) values ({},{},{})'.format(request.POST['name'],request.POST['email'],Hash(request.POST['name'])))
execute(user)
return redirect('/contacts/')
else:
return render(request,'phone/signup.html',{'error':'password must be match'})
else:
return render(request,'phone/signup.html')
@login_required(login_url="signup")
def add(request):
if request.method=='POST':
group=Grp.objects.raw('select * from phone_grp where user_id = {}'.format(user.id))
if request.POST['name'] and request.POST['number'] and request.POST['group'] :
user=User.objects.raw('insert into phonr_phone (name , number , group_id , user_id) values ({},{},{},{})'.format(request.POST['name'],request.POST['number'],request.POST['group.id'],user.user_id ))
execute(user)
return redirect('/contacts/')
else:
group=Grp.objects.raw('select * from phone_grp where user_id = {}'.format(user.id))
return render(request,'phone/add.html',{'error':'All field are required','groups':group})
else:
group=Grp.objects.raw('select * from phone_grp where user_id = {}'.format(user.id))
return render(request,'phone/add.html',{'groups':group})
@login_required(login_url="signup")
def add_grp(request):
if request.method=='POST':
if request.POST['name'] :
group=Grp.objects.raw('insert into phonr_grp (name , user_id) values ({},{})'.format(request.POST['name'],user.user_id ))
execute(group)
return redirect('/contacts/')
else:
groups=Grp.objects.all().filter(user=request.user)
return render(request,'phone/add_grp.html',{'error':'All field are required','groups':groups})
else:
group=Grp.objects.raw('select * from phone_grp where user_id = {}'.format(user.id))
return render(request,'phone/add_grp.html',{'groups':group})
def login (request):
if request.method == 'POST':
user = auth.authenticate(username=request.POST['username'] , password=request.POST['password'])
if user is not None:
auth.login(request,user)
return redirect('/contacts/')
else:
return render(request,'phone/login.html',{'error':'username or password is incorrect'})
else:
return render(request,'phone/login.html')
def logout (request):
if request.method == 'POST':
auth.logout(request)
return redirect('login')
else:
return render(request,'phone/login.html')