Skip to content

Commit

Permalink
auth by email only, select field added
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronik22 committed Jul 25, 2022
1 parent a9d0def commit b8e4d69
Show file tree
Hide file tree
Showing 20 changed files with 251 additions and 55 deletions.
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
SECRET_KEY='django-insecure-k!a&*tc7pew2di&tm_3ky87c2m!h9m+zb-_knabm9ao!biqmd*'
DEBUG=True
EMAIL_HOST_USER='your_email'
EMAIL_HOST_PASSWORD='your_password'
Expand Down
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ ignore =
B007,
B950,

max-line-length = 200
max-line-length = 119
3 changes: 3 additions & 0 deletions accounts/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from django.contrib import admin

# Register your models here.

from .models import User
admin.site.register(User)
12 changes: 5 additions & 7 deletions accounts/forms.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
from django.contrib.auth.models import User
from accounts.models import User
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm


class LoginForm(forms.Form):
username = forms.CharField(max_length=63)
password = forms.CharField(max_length=63, widget=forms.PasswordInput)
email = forms.EmailField(max_length=100)
password = forms.CharField(max_length=63, widget=forms.PasswordInput)


class EditProfileForm(UserChangeForm):
password = forms.CharField(label="", widget=forms.TextInput(attrs={'type':'hidden'}))
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email','password',)
fields = ('email','name','password')




class SignUpForm(UserCreationForm):
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2',)
fields = ('email', 'password1', 'password2',)
31 changes: 31 additions & 0 deletions accounts/managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.contrib.auth.base_user import BaseUserManager


class UserManager(BaseUserManager):
use_in_migrations = True

def _create_user(self, email, password, **extra_fields):
"""
Creates and saves a User with the given email and password.
"""
if not email:
raise ValueError('Users must have an email address')
# username = self.normalize_email(username)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user

def create_user(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)

def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)

if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')

return self._create_user(email, password, **extra_fields)
42 changes: 42 additions & 0 deletions accounts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generated by Django 4.0.5 on 2022-07-25 13:49

import accounts.managers
from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

initial = True

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
]

operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('email', models.EmailField(help_text='Provide an email for registration', max_length=320, unique=True, verbose_name='Email')),
('name', models.CharField(max_length=70, verbose_name='Name')),
('date_confirmation_mail_sent', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date confirmation mail sent')),
('date_joined', models.DateTimeField(auto_now_add=True, verbose_name='date joined')),
('is_active', models.BooleanField(default=True, verbose_name='active')),
('is_verified', models.BooleanField(default=False, verbose_name='verified')),
('is_staff', models.BooleanField(default=False, verbose_name='staff')),
('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')),
('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')),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
},
managers=[
('objects', accounts.managers.UserManager()),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.0.5 on 2022-07-25 14:06

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('accounts', '0001_initial'),
]

operations = [
migrations.RemoveField(
model_name='user',
name='date_confirmation_mail_sent',
),
]
27 changes: 26 additions & 1 deletion accounts/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
# from django.utils import timezone
from django.contrib.auth.models import PermissionsMixin
from django.db import models
from django.contrib.auth.base_user import AbstractBaseUser
from .managers import UserManager
from django.utils.translation import gettext_lazy as _

# Create your models here.
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('Email'), unique=True, max_length=320, help_text='Provide an email for registration')
name = models.CharField(_('Name'), max_length=70)

# date_confirmation_mail_sent = models.DateTimeField(_('date confirmation mail sent'), default=timezone.now)
date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)
is_active = models.BooleanField(_('active'), default=True)
is_verified = models.BooleanField(_('verified'), default=False)
is_staff = models.BooleanField(_('staff'), default=False)

objects = UserManager()

USERNAME_FIELD = 'email'
EMAIL_FIELD = 'email'

class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')

def __str__(self):
return self.email
6 changes: 3 additions & 3 deletions accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def register_user(request):
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data['username']
email = form.cleaned_data['email']
password = form.cleaned_data['password1']
user = authenticate(username=username, password=password)
user = authenticate(email=email, password=password)
login(request,user)
messages.success(request, ('You are now registered'))
return redirect('home')
Expand All @@ -32,7 +32,7 @@ def login_user(request):
form = LoginForm(request.POST)
if form.is_valid():
user = authenticate(
username=form.cleaned_data['username'],
email=form.cleaned_data['email'],
password=form.cleaned_data['password'],
)
if user is not None:
Expand Down
23 changes: 22 additions & 1 deletion templates/wcd_mainapp/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
<link href="https://fonts.googleapis.com/css2?family=Kanit:ital,wght@0,400;1,400&amp;display=swap" rel="stylesheet" />
<!-- Core CSS (includes Bootstrap)-->
<link href="{% static 'css/styles.css' %}" rel="stylesheet" />
<style>
.msgmessages{
height: fit-content;
margin: 90px auto 0px auto;
z-index: 9998;
max-width: 800px;
}
</style>
{% block extra_head %} {% endblock extra_head %}
</head>
<body id="page-top">
Expand All @@ -26,7 +34,7 @@
<div class="container px-5">
<a class="navbar-brand fw-bold" href="/">
{% if user.is_authenticated %}
Hello {{user.username}} !
Hello {{user.name}} !
{% else %}
WCD
{% endif %}
Expand Down Expand Up @@ -61,6 +69,19 @@
</div>
</div>
</nav>

<!-- Messages from backend -->
{% if messages %}
<div class="msgmessages">
{% for message in messages %}
<div class="alert alert-dismissible fade show {% if message.tags %} {{message.tags}} {% else %} alert-warning {% endif %} ">
{{ message }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endfor %}
</div>
{% endif %}

{% block content %} {% endblock content %}
<!-- Footer-->
<footer class="bg-black text-center py-5">
Expand Down
23 changes: 19 additions & 4 deletions templates/wcd_mainapp/tasks.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,19 @@
<tr id="{{task.id}}">
<td contenteditable="true">{{task.web_url}}</td>
<td contenteditable="true">{{task.partOf}}</td>
<td contenteditable="true">{{task.detection_type}}</td>
<td>
<select name="detection_type" id="detection_type">
<option value="1" {% if task.detection_type == 1 %}selected="selected"{% endif %}>
Image
</option>
<option value="2" {% if task.detection_type == 2 %}selected="selected"{% endif %}>
HTML
</option>
<option value="3" {% if task.detection_type == 3 %}selected="selected"{% endif %}>
Text
</option>
</select>
</td>
<td contenteditable="true">{{task.threshold}}</td>
<td>{{task.date_added}}</td>
<td>{{task.date_updated}}</td>
Expand Down Expand Up @@ -81,11 +93,14 @@ <h2 class ="text-center"> You dont have any task. <a href="{% url 'add_tasks' %}
{% block extra_scripts %}
<script>
function editTask(idTask) {
targetElement = document.getElementById(idTask).querySelectorAll("td")
const targetElement = document.getElementById(idTask).querySelectorAll("td")
const detection_type = document.getElementById('detection_type');
const detection_type_option = detection_type.options[detection_type.selectedIndex];

const dataToBackEnd = new FormData()
dataToBackEnd.append("web_url", targetElement[0].innerText)
dataToBackEnd.append("partOf", targetElement[1].innerText)
dataToBackEnd.append("detection_type", targetElement[2].innerText)
dataToBackEnd.append("detection_type", detection_type_option.value)
dataToBackEnd.append("threshold", targetElement[3].innerText)

const request = new Request(`./${idTask}/update/`,
Expand All @@ -103,7 +118,7 @@ <h2 class ="text-center"> You dont have any task. <a href="{% url 'add_tasks' %}
if (response.status === 200) {
location.reload()
} else {
alert("error")
alert("error: " + response.statusText)
}
}
)
Expand Down
11 changes: 9 additions & 2 deletions wcd_mainapp/image_wcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ def __init__(self, id, url, css, threshold):
(async () => {
const browser = await puppeteer.launch({ headless: true });
const browser = await puppeteer.launch({
headless: true,
// these args are for heroku
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
],
});
const page = await browser.newPage();
await page.setRequestInterception(true)
Expand All @@ -65,7 +72,7 @@ def __init__(self, id, url, css, threshold):
page.setViewport({ width: 1000, height: 1000, deviceScaleFactor: 1 });
await page.goto(process.argv[2], { waitUntil: 'networkidle2' });
// await page.waitFor(5000);
await page.waitForTimeout(3000);
if (process.argv[4] == 'full') {
await page.screenshot({
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.0.5 on 2022-07-24 09:44
# Generated by Django 4.0.5 on 2022-07-25 13:50

from django.conf import settings
from django.db import migrations, models
Expand All @@ -9,10 +9,19 @@ class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('wcd_mainapp', '0003_alter_tasks_partof'),
('wcd_mainapp', '0001_initial'),
]

operations = [
migrations.RemoveField(
model_name='tasks',
name='hasChanged',
),
migrations.AddField(
model_name='tasks',
name='threshold',
field=models.FloatField(default=1.0),
),
migrations.AddField(
model_name='tasks',
name='user',
Expand All @@ -24,4 +33,9 @@ class Migration(migrations.Migration):
name='detection_type',
field=models.IntegerField(choices=[(1, 'Image'), (2, 'HTML'), (3, 'Text')], default=3),
),
migrations.AlterField(
model_name='tasks',
name='partOf',
field=models.CharField(default='full', max_length=1000),
),
]
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# Generated by Django 4.0.5 on 2022-07-04 16:09
# Generated by Django 4.0.5 on 2022-07-25 14:17

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('wcd_mainapp', '0002_remove_tasks_haschanged_tasks_threshold'),
('wcd_mainapp', '0002_remove_tasks_haschanged_tasks_threshold_tasks_user_and_more'),
]

operations = [
migrations.AlterField(
migrations.AddConstraint(
model_name='tasks',
name='partOf',
field=models.CharField(default='full', max_length=1000),
constraint=models.UniqueConstraint(fields=('user', 'web_url'), name='wcd_mainapp_tasks_unique'),
),
]
Loading

0 comments on commit b8e4d69

Please sign in to comment.