File size: 8,423 Bytes
0a08d4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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
228
229
from django.shortcuts import render, redirect, HttpResponse
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
import json
from .models import Userdata, ip_address
import threading
from django.core.files.storage import FileSystemStorage
from django.conf import settings
from django.views.decorators.csrf import csrf_exempt, csrf_protect
from django.core.mail import send_mail
import os
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import requests
import geocoder
import folium

gauth = GoogleAuth()
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
    # Authenticate if they're not there
    gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
    # Refresh them if expired
    gauth.Refresh()
else:
    # Initialize the saved creds
    gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("mycreds.txt")
DRIVE = GoogleDrive(gauth)


def GoogleDriveUpload(filename, folder):
    file1 = DRIVE.CreateFile({'title': filename, 'parents': [{'id': folder}]})
    file1.SetContentFile("files/"+filename)
    file1.Upload()


@csrf_exempt
def index(request):
    context = {
        'user': request.user
    }
    return render(request, 'index.html', context=context)


@csrf_exempt
def user_login(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            messages.success(request, ('You have been logged in!'))
            # print('logged in')
            return redirect('index')
        else:
            messages.success(request, ('Error logging in - please try again.'))
            # print('error logging in')
            return render(request, 'login.html')
    return render(request, 'login.html')


@csrf_exempt
def sign_up(request):
    if request.method == 'POST':
        name = request.POST['name']
        email = request.POST['email']
        password = request.POST['password']
        password2 = request.POST['password1']
        user = authenticate(request, email=email, password=password)
        if user is not None and password == password2:
            login(request, user)
            messages.success(request, ('You have been logged in!'))
            # print('logged in')
            return redirect('index')
        elif user is None and password == password2:
            user = User.objects.create_user(username=email, email=email, password=password, first_name=name)
            user.save()
            messages.success(request, ('Created new user!'))
            login(request, user)
            folder = '1N3uD81zuXY_23esOFWRvN2Fp4zEbCrzy'
            new_folder = DRIVE.CreateFile({'title': name, 'mimeType': 'application/vnd.google-apps.folder', 'parents': [{'id': folder}]})
            new_folder.Upload()
            new_folder.InsertPermission({'type': 'user', 'value': email, 'role': 'writer'})
            dataObj = Userdata(user_id=request.user, folder=new_folder['id'])
            dataObj.save()
            # print('new user created')
            return redirect('index')
        else:
            pass
    return render(request, 'signup.html')


@csrf_exempt
@login_required
def user_logout(request):
    logout(request)
    messages.success(request, ('You have been logged out!'))
    # print('logged out')
    return redirect('index')


@csrf_exempt
@login_required
def upload(request):
    return render(request, 'upload.html')


@csrf_exempt
@login_required
def uploader(request):
    if request.method == 'POST':
        folder = 'files/'
        file = request.FILES['file']
        title = request.POST['title']
        if title == '':
            title = file.name
        fs = FileSystemStorage(location=folder)
        filename = fs.save(title, file)
        # print(filename)
        if file.name == '':
            return redirect('../upload/')
        folder_id = Userdata.objects.get(user_id=request.user).folder
        thread = threading.Thread(target=GoogleDriveUpload, args=(title, folder_id))
        thread.start()
        thread.join()
        os.remove(folder+"/"+filename)
        return redirect('../upload/')


@csrf_exempt
@login_required
def list(request):
    files = []
    folders = []
    folder_id = Userdata.objects.get(user_id=request.user).folder
    list_files = DRIVE.ListFile({'q': "'%s' in parents and trashed=false" % folder_id}).GetList()
    for file in list_files:
        if file['title'] != 'Deleted' and file['title'] != 'DeletedFile' and file['mimeType'] != 'application/vnd.google-apps.folder':
            files.append([file['title'], file['embedLink']])
        elif file['mimeType'] == 'application/vnd.google-apps.folder':
            folders.append([file['title'], "/list/"+file['id'][::-1]])
        else:
            pass
    context = {'files': files, 'folders': folders}
    return render(request, 'list.html', context=context)


@csrf_exempt
@login_required
def folder_list(request, id):
    files = []
    folders = []
    folder_id = id[::-1]  # Userdata.objects.get(user_id=request.user).folder
    list_files = DRIVE.ListFile({'q': "'%s' in parents and trashed=false" % folder_id}).GetList()
    for file in list_files:
        if file['title'] != 'Deleted' and file['mimeType'] != 'application/vnd.google-apps.folder':
            files.append([file['title'], file['embedLink']])
        elif file['mimeType'] == 'application/vnd.google-apps.folder':
            folders.append([file['title'], "/list/"+file['id'][::-1]])
        else:
            pass
    context = {'files': files, 'folders': folders}
    return render(request, 'folder_list.html', context=context)


@csrf_exempt
@login_required
def deleteFile(request):
    if request.method == 'POST':
        file_id = json.loads(request.body)['file_id']
        print(file_id)
        file = DRIVE.CreateFile({'id': file_id})
        file.FetchMetadata(fields="title")
        file['title'] = 'Deleted'
        file.SetContentString('')
        file.Upload()
        return HttpResponse(json.dumps({'status': 'success'}), content_type='application/json')


def emailSender(request):
    name = request.GET.get('name', '')
    email = request.GET.get('email', '')
    subject = request.GET.get('subject', '')
    message = request.GET.get('message', '')
    print(name, email, subject, message)
    subject = subject + ' - ' + name + ' - ' + email
    response = requests.get('https://script.google.com/macros/s/AKfycbwQxoJPZYttDmLEym7btUB2F-KIbvITr9EWSdfC5TaYJZybnA-s/exec?email=tempmailpatel@gmail.com&subject='+subject+'&body='+message+'&html=false')
    print(response.json())
    '''message = message

    from_email = settings.EMAIL_HOST_USER

    to_email = settings.EMAIL_HOST_USER

    send_mail(subject, message, from_email, [to_email], fail_silently=True)'''
    return redirect('index')

# http://127.0.0.1:8000/emailSender/?name=jagrat%20patel&email=thejagstudio@gmail.com&subject=sdsd&message=sdsfs
# https://script.google.com/macros/s/AKfycbwQxoJPZYttDmLEym7btUB2F-KIbvITr9EWSdfC5TaYJZybnA-s/exec?email=jagratpatel99@gmail.com&subject=hello&body=world&html=false


def ipGetter(request):
    ip = request.GET.get('ip', '')
    ipIn = ip_address.objects.filter(ip=ip).first()
    if ipIn is None:
        new_entry = ip_address(ip=ip)
        new_entry.save()
        print(ip)
    return HttpResponse(json.dumps({'status': 'success'}), content_type='application/json')


@login_required
def godMode(request):
    if request.user.is_superuser:
        ips = ip_address.objects.all()
        ipF = ips[0].ip
        g = geocoder.ip(ipF)
        myAddress = g.latlng
        my_map1 = folium.Map(location=myAddress, zoom_start=12)
        for ip in ips:
            g = geocoder.ip(ip.ip)
            myAddress = g.latlng
            folium.Marker(myAddress).add_to(my_map1)
        my_map1.save('templates/godMode.html')
        return render(request, 'godMode.html')