Browse Source

backend google auth

dmr1725 3 years ago
parent
commit
57b5045e1a

+ 2
- 1
server/CompanionApp/admin.py View File

@@ -1,7 +1,8 @@
1 1
 from django.contrib import admin
2
-from .models import Curso, Matricula, Facultad
2
+from .models import Curso, Matricula, Facultad, User
3 3
 
4 4
 # Register your models here.
5 5
 admin.site.register(Curso)
6 6
 admin.site.register(Matricula)
7 7
 admin.site.register(Facultad)
8
+admin.site.register(User)

+ 59
- 1
server/CompanionApp/migrations/0001_initial.py View File

@@ -1,6 +1,10 @@
1
-# Generated by Django 3.1.2 on 2020-10-08 23:09
1
+# Generated by Django 3.0.8 on 2020-11-02 23:35
2 2
 
3
+import django.contrib.auth.models
4
+import django.contrib.auth.validators
3 5
 from django.db import migrations, models
6
+import django.db.models.deletion
7
+import django.utils.timezone
4 8
 
5 9
 
6 10
 class Migration(migrations.Migration):
@@ -8,6 +12,7 @@ class Migration(migrations.Migration):
8 12
     initial = True
9 13
 
10 14
     dependencies = [
15
+        ('auth', '0011_update_proxy_permissions'),
11 16
     ]
12 17
 
13 18
     operations = [
@@ -16,6 +21,59 @@ class Migration(migrations.Migration):
16 21
             fields=[
17 22
                 ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18 23
                 ('name', models.CharField(max_length=150)),
24
+                ('code', models.CharField(max_length=9)),
25
+                ('creditos', models.IntegerField(default=0)),
19 26
             ],
20 27
         ),
28
+        migrations.CreateModel(
29
+            name='Facultad',
30
+            fields=[
31
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
32
+                ('fname', models.CharField(max_length=150)),
33
+            ],
34
+        ),
35
+        migrations.CreateModel(
36
+            name='User',
37
+            fields=[
38
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
39
+                ('password', models.CharField(max_length=128, verbose_name='password')),
40
+                ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
41
+                ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
42
+                ('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
43
+                ('first_name', models.CharField(blank=True, max_length=30, verbose_name='first name')),
44
+                ('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
45
+                ('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
46
+                ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
47
+                ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
48
+                ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
49
+                ('gpa', models.DecimalField(decimal_places=2, default=0, max_digits=3)),
50
+                ('fac_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='CompanionApp.Facultad')),
51
+                ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
52
+                ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
53
+            ],
54
+            options={
55
+                'verbose_name': 'user',
56
+                'verbose_name_plural': 'users',
57
+                'abstract': False,
58
+            },
59
+            managers=[
60
+                ('objects', django.contrib.auth.models.UserManager()),
61
+            ],
62
+        ),
63
+        migrations.CreateModel(
64
+            name='Matricula',
65
+            fields=[
66
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
67
+                ('section', models.CharField(max_length=5)),
68
+                ('prof', models.CharField(max_length=150)),
69
+                ('grade', models.CharField(max_length=3)),
70
+                ('semestre', models.IntegerField(default=0)),
71
+                ('course_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='CompanionApp.Curso')),
72
+            ],
73
+        ),
74
+        migrations.AddField(
75
+            model_name='curso',
76
+            name='fac_id',
77
+            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='CompanionApp.Facultad'),
78
+        ),
21 79
     ]

+ 0
- 19
server/CompanionApp/migrations/0002_curso_code.py View File

@@ -1,19 +0,0 @@
1
-# Generated by Django 3.1.2 on 2020-10-08 23:11
2
-
3
-from django.db import migrations, models
4
-
5
-
6
-class Migration(migrations.Migration):
7
-
8
-    dependencies = [
9
-        ('CompanionApp', '0001_initial'),
10
-    ]
11
-
12
-    operations = [
13
-        migrations.AddField(
14
-            model_name='curso',
15
-            name='code',
16
-            field=models.CharField(default=0, max_length=9),
17
-            preserve_default=False,
18
-        ),
19
-    ]

+ 0
- 43
server/CompanionApp/migrations/0003_auto_20201015_1100.py View File

@@ -1,43 +0,0 @@
1
-# Generated by Django 3.1.1 on 2020-10-15 15:00
2
-
3
-from django.db import migrations, models
4
-import django.db.models.deletion
5
-
6
-
7
-class Migration(migrations.Migration):
8
-
9
-    dependencies = [
10
-        ('CompanionApp', '0002_curso_code'),
11
-    ]
12
-
13
-    operations = [
14
-        migrations.CreateModel(
15
-            name='Facultad',
16
-            fields=[
17
-                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18
-                ('fname', models.CharField(max_length=150)),
19
-            ],
20
-        ),
21
-        migrations.AddField(
22
-            model_name='curso',
23
-            name='creditos',
24
-            field=models.IntegerField(default=0),
25
-        ),
26
-        migrations.CreateModel(
27
-            name='Matricula',
28
-            fields=[
29
-                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
30
-                ('section', models.CharField(max_length=5)),
31
-                ('prof', models.CharField(max_length=150)),
32
-                ('grade', models.CharField(max_length=3)),
33
-                ('semestre', models.IntegerField(default=0)),
34
-                ('course_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='CompanionApp.curso')),
35
-            ],
36
-        ),
37
-        migrations.AddField(
38
-            model_name='curso',
39
-            name='fac_id',
40
-            field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to='CompanionApp.facultad'),
41
-            preserve_default=False,
42
-        ),
43
-    ]

+ 4
- 1
server/CompanionApp/models.py View File

@@ -1,5 +1,5 @@
1 1
 from django.db import models
2
-
2
+from django.contrib.auth.models import AbstractUser
3 3
 # Create your models here.
4 4
 # diego, Diego1999$
5 5
 
@@ -19,3 +19,6 @@ class Matricula(models.Model):
19 19
     grade = models.CharField(max_length=3)
20 20
     semestre = models.IntegerField(default=0)
21 21
 
22
+class User(AbstractUser):
23
+    gpa = models.DecimalField(default = 0, max_digits = 3, decimal_places = 2)
24
+    fac_id = models.ForeignKey(Facultad, on_delete=models.CASCADE, default=1)

+ 12
- 6
server/CompanionApp/views.py View File

@@ -5,7 +5,9 @@ sys.path.insert(1,'C:/Users/diego/Documents/companion_app/organizar/')
5 5
 from organizar import files3
6 6
 
7 7
 
8
-from django.shortcuts import render
8
+from rest_auth.registration.views import SocialLoginView
9
+from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter
10
+
9 11
 
10 12
 from django.http.response import JsonResponse
11 13
 from rest_framework.parsers import JSONParser 
@@ -15,10 +17,14 @@ from .models import Facultad, Curso
15 17
 from .serializers import FacultadSerializer, CursoSerializer 
16 18
 from rest_framework.decorators import api_view
17 19
 
18
-
20
+# clientID: 965339169610-8et02d4qpfk96vclngd0otths1rs8661.apps.googleusercontent.com
21
+# clientSecret: zeY8NoW6ORBHP8pDQLE2x_Z2
19 22
 
20 23
 # Create your views here.
21 24
 
25
+class GoogleLogin(SocialLoginView):
26
+    adapter_class = GoogleOAuth2Adapter
27
+
22 28
 @api_view(['POST',])
23 29
 def insertarFacultades(request):
24 30
     faculties = [ 'Administración de Empresas', 'Administración de Empresas graduado', 'Arquitectura', 'Arquitectura Graduado', 'Asuntos Académicos',
@@ -58,9 +64,9 @@ def insertarTodosLosCursos(request):
58 64
 
59 65
 @api_view(['GET', 'POST'])
60 66
 def hello_world(request):
61
-    if request.method == 'POST':
62
-        return JsonResponse({"message": "Got some data!", "data": request.data})
63
-    return JsonResponse({"message": "Hello, world!"})
64
-   
67
+    if request.user.is_authenticated:
68
+        if request.method == 'GET':
69
+            return JsonResponse({'msg': request.user.email}, status = status.HTTP_200_OK)
70
+    return JsonResponse({'msg': 'no'})
65 71
 
66 72
 

+ 28
- 2
server/restful/settings.py View File

@@ -26,7 +26,6 @@ SECRET_KEY = 'j(q!!tj1a0yae#js(^7h-@y(g%=no817zm_t5*mf@p0t53l3qf'
26 26
 # SECURITY WARNING: don't run with debug turned on in production!
27 27
 DEBUG = True
28 28
 
29
-ALLOWED_HOSTS = []
30 29
 
31 30
 import os
32 31
 
@@ -57,10 +56,25 @@ INSTALLED_APPS = [
57 56
     'django.contrib.messages',
58 57
     'django.contrib.staticfiles',
59 58
     'CompanionApp',
59
+   
60 60
     'rest_framework',
61
-    'corsheaders'
61
+    'rest_framework.authtoken',
62
+    'rest_auth',
63
+
64
+    'django.contrib.sites',
65
+    'allauth',
66
+    'allauth.account',
67
+    'rest_auth.registration',
68
+
69
+    'allauth.socialaccount',
70
+    'allauth.socialaccount.providers.facebook',
71
+    'allauth.socialaccount.providers.google',
72
+
73
+    'corsheaders',
62 74
 ]
63 75
 
76
+SITE_ID = 2
77
+
64 78
 MIDDLEWARE = [
65 79
     'django.middleware.security.SecurityMiddleware',
66 80
     'django.contrib.sessions.middleware.SessionMiddleware',
@@ -91,6 +105,18 @@ TEMPLATES = [
91 105
     },
92 106
 ]
93 107
 
108
+# REST_FRAMEWORK = {
109
+#     # 'DEFAULT_AUTHENTICATION_CLASSES': (
110
+#     #     'rest_framework.authentication.TokenAuthentication',
111
+#     # ),
112
+#     'DEFAULT_PERMISSION_CLASSES': (
113
+#         'rest_framework.permissions.IsAuthenticated', )
114
+# }
115
+
116
+ALLOWED_HOSTS = ['*']
117
+
118
+AUTH_USER_MODEL = 'CompanionApp.User' # new
119
+
94 120
 WSGI_APPLICATION = 'restful.wsgi.application'
95 121
 
96 122
 

+ 3
- 1
server/restful/urls.py View File

@@ -14,13 +14,15 @@ Including another URLconf
14 14
     2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
15 15
 """
16 16
 from django.contrib import admin
17
-# from django.urls import path, include, 
17
+from django.urls import path, include 
18 18
 from django.conf.urls import url, include 
19
+from CompanionApp.views import GoogleLogin
19 20
 
20 21
 
21 22
 
22 23
 urlpatterns = [
23 24
     url(r'^admin/', admin.site.urls),
25
+    url(r'^rest-auth/google', GoogleLogin.as_view(), name='google_login'),
24 26
     url(r'^', include('CompanionApp.urls'))
25 27
 ]
26 28