Spaces:
Runtime error
Runtime error
thejagstudio
commited on
Update home/views.py
Browse files- home/views.py +226 -228
home/views.py
CHANGED
@@ -1,228 +1,226 @@
|
|
1 |
-
from django.shortcuts import render, redirect, HttpResponse
|
2 |
-
from django.contrib.auth import authenticate, login, logout
|
3 |
-
from django.contrib import messages
|
4 |
-
from django.contrib.auth.decorators import login_required
|
5 |
-
from django.contrib.auth.models import User
|
6 |
-
import json
|
7 |
-
from .models import Userdata, ip_address
|
8 |
-
import threading
|
9 |
-
from django.core.files.storage import FileSystemStorage
|
10 |
-
from django.conf import settings
|
11 |
-
from django.views.decorators.csrf import csrf_exempt, csrf_protect
|
12 |
-
from django.core.mail import send_mail
|
13 |
-
import os
|
14 |
-
from pydrive.auth import GoogleAuth
|
15 |
-
from pydrive.drive import GoogleDrive
|
16 |
-
import requests
|
17 |
-
import geocoder
|
18 |
-
import folium
|
19 |
-
|
20 |
-
gauth = GoogleAuth()
|
21 |
-
|
22 |
-
if gauth.credentials is None:
|
23 |
-
# Authenticate if they're not there
|
24 |
-
gauth.LocalWebserverAuth()
|
25 |
-
elif gauth.access_token_expired:
|
26 |
-
# Refresh them if expired
|
27 |
-
gauth.Refresh()
|
28 |
-
else:
|
29 |
-
# Initialize the saved creds
|
30 |
-
gauth.Authorize()
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
file1
|
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 |
-
user
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
new_folder
|
88 |
-
new_folder
|
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 |
-
thread
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
file =
|
179 |
-
file.
|
180 |
-
file
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
from_email =
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
my_map1.save('templates/godMode.html')
|
228 |
-
return render(request, 'godMode.html')
|
|
|
1 |
+
from django.shortcuts import render, redirect, HttpResponse
|
2 |
+
from django.contrib.auth import authenticate, login, logout
|
3 |
+
from django.contrib import messages
|
4 |
+
from django.contrib.auth.decorators import login_required
|
5 |
+
from django.contrib.auth.models import User
|
6 |
+
import json
|
7 |
+
from .models import Userdata, ip_address
|
8 |
+
import threading
|
9 |
+
from django.core.files.storage import FileSystemStorage
|
10 |
+
from django.conf import settings
|
11 |
+
from django.views.decorators.csrf import csrf_exempt, csrf_protect
|
12 |
+
from django.core.mail import send_mail
|
13 |
+
import os
|
14 |
+
from pydrive.auth import GoogleAuth
|
15 |
+
from pydrive.drive import GoogleDrive
|
16 |
+
import requests
|
17 |
+
import geocoder
|
18 |
+
import folium
|
19 |
+
|
20 |
+
gauth = GoogleAuth()
|
21 |
+
|
22 |
+
if gauth.credentials is None:
|
23 |
+
# Authenticate if they're not there
|
24 |
+
gauth.LocalWebserverAuth()
|
25 |
+
elif gauth.access_token_expired:
|
26 |
+
# Refresh them if expired
|
27 |
+
gauth.Refresh()
|
28 |
+
else:
|
29 |
+
# Initialize the saved creds
|
30 |
+
gauth.Authorize()
|
31 |
+
DRIVE = GoogleDrive(gauth)
|
32 |
+
|
33 |
+
|
34 |
+
def GoogleDriveUpload(filename, folder):
|
35 |
+
file1 = DRIVE.CreateFile({'title': filename, 'parents': [{'id': folder}]})
|
36 |
+
file1.SetContentFile("files/"+filename)
|
37 |
+
file1.Upload()
|
38 |
+
|
39 |
+
|
40 |
+
@csrf_exempt
|
41 |
+
def index(request):
|
42 |
+
context = {
|
43 |
+
'user': request.user
|
44 |
+
}
|
45 |
+
return render(request, 'index.html', context=context)
|
46 |
+
|
47 |
+
|
48 |
+
@csrf_exempt
|
49 |
+
def user_login(request):
|
50 |
+
if request.method == 'POST':
|
51 |
+
username = request.POST['username']
|
52 |
+
password = request.POST['password']
|
53 |
+
user = authenticate(request, username=username, password=password)
|
54 |
+
if user is not None:
|
55 |
+
login(request, user)
|
56 |
+
messages.success(request, ('You have been logged in!'))
|
57 |
+
# print('logged in')
|
58 |
+
return redirect('index')
|
59 |
+
else:
|
60 |
+
messages.success(request, ('Error logging in - please try again.'))
|
61 |
+
# print('error logging in')
|
62 |
+
return render(request, 'login.html')
|
63 |
+
return render(request, 'login.html')
|
64 |
+
|
65 |
+
|
66 |
+
@csrf_exempt
|
67 |
+
def sign_up(request):
|
68 |
+
if request.method == 'POST':
|
69 |
+
name = request.POST['name']
|
70 |
+
email = request.POST['email']
|
71 |
+
password = request.POST['password']
|
72 |
+
password2 = request.POST['password1']
|
73 |
+
user = authenticate(request, email=email, password=password)
|
74 |
+
if user is not None and password == password2:
|
75 |
+
login(request, user)
|
76 |
+
messages.success(request, ('You have been logged in!'))
|
77 |
+
# print('logged in')
|
78 |
+
return redirect('index')
|
79 |
+
elif user is None and password == password2:
|
80 |
+
user = User.objects.create_user(username=email, email=email, password=password, first_name=name)
|
81 |
+
user.save()
|
82 |
+
messages.success(request, ('Created new user!'))
|
83 |
+
login(request, user)
|
84 |
+
folder = '1N3uD81zuXY_23esOFWRvN2Fp4zEbCrzy'
|
85 |
+
new_folder = DRIVE.CreateFile({'title': name, 'mimeType': 'application/vnd.google-apps.folder', 'parents': [{'id': folder}]})
|
86 |
+
new_folder.Upload()
|
87 |
+
new_folder.InsertPermission({'type': 'user', 'value': email, 'role': 'writer'})
|
88 |
+
dataObj = Userdata(user_id=request.user, folder=new_folder['id'])
|
89 |
+
dataObj.save()
|
90 |
+
# print('new user created')
|
91 |
+
return redirect('index')
|
92 |
+
else:
|
93 |
+
pass
|
94 |
+
return render(request, 'signup.html')
|
95 |
+
|
96 |
+
|
97 |
+
@csrf_exempt
|
98 |
+
@login_required
|
99 |
+
def user_logout(request):
|
100 |
+
logout(request)
|
101 |
+
messages.success(request, ('You have been logged out!'))
|
102 |
+
# print('logged out')
|
103 |
+
return redirect('index')
|
104 |
+
|
105 |
+
|
106 |
+
@csrf_exempt
|
107 |
+
@login_required
|
108 |
+
def upload(request):
|
109 |
+
return render(request, 'upload.html')
|
110 |
+
|
111 |
+
|
112 |
+
@csrf_exempt
|
113 |
+
@login_required
|
114 |
+
def uploader(request):
|
115 |
+
if request.method == 'POST':
|
116 |
+
folder = 'files/'
|
117 |
+
file = request.FILES['file']
|
118 |
+
title = request.POST['title']
|
119 |
+
if title == '':
|
120 |
+
title = file.name
|
121 |
+
fs = FileSystemStorage(location=folder)
|
122 |
+
filename = fs.save(title, file)
|
123 |
+
# print(filename)
|
124 |
+
if file.name == '':
|
125 |
+
return redirect('../upload/')
|
126 |
+
folder_id = Userdata.objects.get(user_id=request.user).folder
|
127 |
+
thread = threading.Thread(target=GoogleDriveUpload, args=(title, folder_id))
|
128 |
+
thread.start()
|
129 |
+
thread.join()
|
130 |
+
os.remove(folder+"/"+filename)
|
131 |
+
return redirect('../upload/')
|
132 |
+
|
133 |
+
|
134 |
+
@csrf_exempt
|
135 |
+
@login_required
|
136 |
+
def list(request):
|
137 |
+
files = []
|
138 |
+
folders = []
|
139 |
+
folder_id = Userdata.objects.get(user_id=request.user).folder
|
140 |
+
list_files = DRIVE.ListFile({'q': "'%s' in parents and trashed=false" % folder_id}).GetList()
|
141 |
+
for file in list_files:
|
142 |
+
if file['title'] != 'Deleted' and file['title'] != 'DeletedFile' and file['mimeType'] != 'application/vnd.google-apps.folder':
|
143 |
+
files.append([file['title'], file['embedLink']])
|
144 |
+
elif file['mimeType'] == 'application/vnd.google-apps.folder':
|
145 |
+
folders.append([file['title'], "/list/"+file['id'][::-1]])
|
146 |
+
else:
|
147 |
+
pass
|
148 |
+
context = {'files': files, 'folders': folders}
|
149 |
+
return render(request, 'list.html', context=context)
|
150 |
+
|
151 |
+
|
152 |
+
@csrf_exempt
|
153 |
+
@login_required
|
154 |
+
def folder_list(request, id):
|
155 |
+
files = []
|
156 |
+
folders = []
|
157 |
+
folder_id = id[::-1] # Userdata.objects.get(user_id=request.user).folder
|
158 |
+
list_files = DRIVE.ListFile({'q': "'%s' in parents and trashed=false" % folder_id}).GetList()
|
159 |
+
for file in list_files:
|
160 |
+
if file['title'] != 'Deleted' and file['mimeType'] != 'application/vnd.google-apps.folder':
|
161 |
+
files.append([file['title'], file['embedLink']])
|
162 |
+
elif file['mimeType'] == 'application/vnd.google-apps.folder':
|
163 |
+
folders.append([file['title'], "/list/"+file['id'][::-1]])
|
164 |
+
else:
|
165 |
+
pass
|
166 |
+
context = {'files': files, 'folders': folders}
|
167 |
+
return render(request, 'folder_list.html', context=context)
|
168 |
+
|
169 |
+
|
170 |
+
@csrf_exempt
|
171 |
+
@login_required
|
172 |
+
def deleteFile(request):
|
173 |
+
if request.method == 'POST':
|
174 |
+
file_id = json.loads(request.body)['file_id']
|
175 |
+
print(file_id)
|
176 |
+
file = DRIVE.CreateFile({'id': file_id})
|
177 |
+
file.FetchMetadata(fields="title")
|
178 |
+
file['title'] = 'Deleted'
|
179 |
+
file.SetContentString('')
|
180 |
+
file.Upload()
|
181 |
+
return HttpResponse(json.dumps({'status': 'success'}), content_type='application/json')
|
182 |
+
|
183 |
+
|
184 |
+
def emailSender(request):
|
185 |
+
name = request.GET.get('name', '')
|
186 |
+
email = request.GET.get('email', '')
|
187 |
+
subject = request.GET.get('subject', '')
|
188 |
+
message = request.GET.get('message', '')
|
189 |
+
print(name, email, subject, message)
|
190 |
+
subject = subject + ' - ' + name + ' - ' + email
|
191 |
+
response = requests.get('https://script.google.com/macros/s/AKfycbwQxoJPZYttDmLEym7btUB2F-KIbvITr9EWSdfC5TaYJZybnA-s/exec?email=tempmailpatel@gmail.com&subject='+subject+'&body='+message+'&html=false')
|
192 |
+
print(response.json())
|
193 |
+
'''message = message
|
194 |
+
from_email = settings.EMAIL_HOST_USER
|
195 |
+
to_email = settings.EMAIL_HOST_USER
|
196 |
+
send_mail(subject, message, from_email, [to_email], fail_silently=True)'''
|
197 |
+
return redirect('index')
|
198 |
+
|
199 |
+
# http://127.0.0.1:8000/emailSender/?name=jagrat%20patel&email=thejagstudio@gmail.com&subject=sdsd&message=sdsfs
|
200 |
+
# https://script.google.com/macros/s/AKfycbwQxoJPZYttDmLEym7btUB2F-KIbvITr9EWSdfC5TaYJZybnA-s/exec?email=jagratpatel99@gmail.com&subject=hello&body=world&html=false
|
201 |
+
|
202 |
+
|
203 |
+
def ipGetter(request):
|
204 |
+
ip = request.GET.get('ip', '')
|
205 |
+
ipIn = ip_address.objects.filter(ip=ip).first()
|
206 |
+
if ipIn is None:
|
207 |
+
new_entry = ip_address(ip=ip)
|
208 |
+
new_entry.save()
|
209 |
+
print(ip)
|
210 |
+
return HttpResponse(json.dumps({'status': 'success'}), content_type='application/json')
|
211 |
+
|
212 |
+
|
213 |
+
@login_required
|
214 |
+
def godMode(request):
|
215 |
+
if request.user.is_superuser:
|
216 |
+
ips = ip_address.objects.all()
|
217 |
+
ipF = ips[0].ip
|
218 |
+
g = geocoder.ip(ipF)
|
219 |
+
myAddress = g.latlng
|
220 |
+
my_map1 = folium.Map(location=myAddress, zoom_start=12)
|
221 |
+
for ip in ips:
|
222 |
+
g = geocoder.ip(ip.ip)
|
223 |
+
myAddress = g.latlng
|
224 |
+
folium.Marker(myAddress).add_to(my_map1)
|
225 |
+
my_map1.save('templates/godMode.html')
|
226 |
+
return render(request, 'godMode.html')
|
|
|
|