Fakultas Ilmu Komputer UI

Commit 047ebb4d authored by Muhammad Ilham Peruzzi's avatar Muhammad Ilham Peruzzi
Browse files

complete exercise 7

parent 761820bd
# Generated by Django 2.2.5 on 2019-11-12 05:55
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('email', models.EmailField(max_length=254, unique=True)),
],
),
migrations.RemoveField(
model_name='listuser',
name='groups',
),
migrations.RemoveField(
model_name='listuser',
name='user_permissions',
),
migrations.DeleteModel(
name='Token',
),
migrations.DeleteModel(
name='ListUser',
),
]
# Generated by Django 2.2.5 on 2019-11-12 06:00
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0002_auto_20191112_1255'),
]
operations = [
migrations.RemoveField(
model_name='user',
name='id',
),
migrations.AlterField(
model_name='user',
name='email',
field=models.EmailField(max_length=254, primary_key=True, serialize=False),
),
]
# Generated by Django 2.2.5 on 2019-11-12 06:01
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0003_auto_20191112_1300'),
]
operations = [
migrations.CreateModel(
name='Token',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('email', models.EmailField(max_length=254)),
('uid', models.CharField(max_length=255)),
],
),
]
from django.db import models
from django.contrib.auth.models import (
AbstractBaseUser, BaseUserManager, PermissionsMixin
)
import uuid
# Create your models here.
class Token(models.Model):
email = models.EmailField()
uid = models.CharField(max_length=255)
class ListUserManager(BaseUserManager):
def create_user(self, email):
ListUser.objects.create(email=email)
def create_superuser(self, email, password):
self.create_user(email)
class ListUser(AbstractBaseUser, PermissionsMixin):
class User(models.Model):
email = models.EmailField(primary_key=True)
USERNAME_FIELD = 'email'
#REQUIRED_FIELDS = ['email', 'height']
objects = ListUserManager()
@property
def is_staff(self):
return self.email == 'harry.percival@example.com'
@property
def is_active(self):
return True
REQUIRED_FIELDS = []
USERNAME_FIELD = 'email'
is_anonymous = False
is_authenticated = True
class Token(models.Model):
email = models.EmailField()
uid = models.CharField(default=uuid.uuid4, max_length=40)
\ No newline at end of file
from django.test import TestCase
# Create your tests here.
from django.test import TestCase
from django.contrib.auth import get_user_model
from accounts.models import Token
User = get_user_model()
class UserModelTest(TestCase):
def test_user_is_valid_with_email_only(self):
user = User(email='a@b.com')
user.full_clean() # should not raise
def test_email_is_primary_key(self):
user = User(email='a@b.com')
self.assertEqual(user.pk, 'a@b.com')
class TokenModelTest(TestCase):
def test_links_user_with_auto_generated_uid(self):
token1 = Token.objects.create(email='a@b.com')
token2 = Token.objects.create(email='a@b.com')
self.assertNotEqual(token1.uid, token2.uid)
\ No newline at end of file
......@@ -2,7 +2,5 @@ from django.conf.urls import url
from accounts import views
urlpatterns = [
url(r'^send_email$', views.send_login_email, name='send_login_email'),
url(r'^login$', views.login, name='login'),
url(r'^logout$', views.logout, name='logout'),
]
\ No newline at end of file
......@@ -5,32 +5,4 @@ from django.contrib.auth import login as auth_login, logout as auth_logout
from django.core.mail import send_mail
from django.shortcuts import redirect, render
from accounts.models import Token
def send_login_email(request):
email = request.POST['email']
uid = str(uuid.uuid4())
Token.objects.create(email=email, uid=uid)
print('saving uid', uid, 'for email', email, file=sys.stderr)
url = request.build_absolute_uri(f'/accounts/login?uid={uid}')
print(url)
send_mail(
'Your login link for Superlists',
f'Use this link to log in:\n\n{url}',
'noreply@superlists',
[email],
)
return render(request, 'login_email_sent.html')
def login(request):
print('login view', file=sys.stderr)
uid = request.GET.get('uid')
user = authenticate(uid=uid)
if user is not None:
auth_login(request, user)
return redirect('/')
def logout(request):
auth_logout(request)
return redirect('/')
\ No newline at end of file
from django.core import mail
""" from django.core import mail
from selenium.webdriver.common.keys import Keys
import re
......@@ -45,4 +45,4 @@ class LoginTest(FunctionalTest):
lambda: self.browser.find_element_by_link_text('Log out')
)
navbar = self.browser.find_element_by_css_selector('.navbar')
self.assertIn(TEST_EMAIL, navbar.text)
\ No newline at end of file
self.assertIn(TEST_EMAIL, navbar.text) """
\ No newline at end of file
......@@ -14,17 +14,16 @@
<body>
<div class="container">
<div class="navbar">
{% if user.is_authenticated %}
<p>Logged in as {{ user.email }}</p>
<p><a id="id_logout" href="{% url 'logout' %}">Log out</a></p>
{% else %}
<form method="POST" action ="{% url 'send_login_email' %}">
Enter email to log in: <input name="email" type="text" />
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<a class="navbar-brand" href="/">Superlists</a>
<form class="navbar-form navbar-right" method="POST" action="#">
<span>Enter email to log in:</span>
<input class="form-control" name="email" type="text" />
{% csrf_token %}
</form>
{% endif %}
</div>
</div>
</nav>
<div class="row">
<div class="col-md-6 col-md-offset-3 jumbotron card-body">
......
......@@ -32,11 +32,13 @@ DEBUG = True
ALLOWED_HOSTS = ['*']
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'ilhamperuzzi@gmail.com'
EMAIL_HOST_USER = 'ilhamperuzzi'
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASSWORD')
EMAIL_PORT = 587
EMAIL_USE_TLS = True
AUTH_USER_MODEL = 'accounts.User'
# Application definition
......@@ -53,7 +55,6 @@ INSTALLED_APPS = [
'django_mutpy'
]
AUTH_USER_MODEL = 'accounts.ListUser'
AUTHENTICATION_BACKENDS = [
'accounts.authentication.PasswordlessAuthenticationBackend',
]
......
{
"version": 3,
"file": "home.css",
"sources": [
"home.scss"
],
"names": [],
"mappings": "AAAA,AACE,IADE,CACF,UAAU,CAAC;EACT,OAAO,EAAE,GAAG,GACb;;AAHH,AAKE,IALE,CAKF,UAAU,CAAA;EACN,gBAAgB,EAAE,KAAK,GACxB"
}
\ No newline at end of file
{
"version": 3,
"file": "list.css",
"sources": [
"list.scss"
],
"names": [],
"mappings": "AAAA,AACI,IADA,CACA,UAAU,CAAC;EACT,OAAO,EAAE,GAAG,GACb;;AAHL,AAKI,IALA,CAKA,UAAU,CAAA;EACN,gBAAgB,EAAE,KAAK,GACxB"
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment