Skip to content

Commit

Permalink
v0.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Couapy committed Jul 28, 2020
1 parent 5dfea26 commit ec34775
Show file tree
Hide file tree
Showing 15 changed files with 275 additions and 89 deletions.
120 changes: 68 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,53 @@ Install the app in settings.py :
```python
INSTALLED_APPS = [
[...]
'social_django',
'crispy_forms',
'user_app',
]
```

Then add the settings for `social-auth-app-django` package, for example :
Specify static files and media files in settings :

```python
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'var/static/')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'var/media/')
```

Then add the settings for `social-auth-app-django` and 'django-crispy-froms' packages, for example :

```python
CRISPY_TEMPLATE_PACK = 'bootstrap4'

USER_APP_PROVIDERS = [
{
"provider": "google-oauth2",
"name": "Google",
"link": None,
"username": None,
},
{
"provider": "github",
"name": "Github",
"link": "https://github.com/{{ data.login }}",
"username": "{{ data.login }}",
},
{
"provider": "twitter",
"name": "Twitter",
"link": "https://twitter.com/{{ data.access_token.screen_name }}/",
"username": "@{{ data.access_token.screen_name }}",
},
{
"provider": "facebook",
"name": "Facebook",
"link": None,
"username": None,
},
]

AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'social_core.backends.google.GoogleOAuth2',
Expand All @@ -50,7 +90,7 @@ SOCIAL_AUTH_FACEBOOK_SECRET = ""

LOGIN_REDIRECT_URL = "/accounts/profile/"
SOCIAL_AUTH_URL_NAMESPACE = 'social'
AUTH_PROFILE_MODULE = 'accounts.Profile'
AUTH_PROFILE_MODULE = 'user_app.Profile'

SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
Expand All @@ -69,56 +109,32 @@ SOCIAL_AUTH_NEW_USER_REDIRECT_URL = '/accounts/profile/'
SOCIAL_AUTH_DISCONNECT_REDIRECT_URL = '/'
```

And finally use the `base.html` template like :

```html
<!doctype html>
<html lang="fr">

<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>{% block title %}{% endblock title %}</title>
{% block head %}{% endblock head %}

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"
integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"
crossorigin="anonymous"></script>
</head>

<body>
{% include 'nav.html' %}
<div class="container pt-4 pd-4">
{% if success is True %}
<div class="alert alert-success alert-dismissible fade show" role="alert">
Les modifications ont bien été enregistrées.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% elif success is False %}
<div class="alert alert-danger alert-dismissible fade show" role="alert">
Une erreur est survenue, les modifications n'ont pas été enregistrées.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% endif %}
{% block content %}{% endblock content %}
</div>
</body>

</html>
Then add the urls to the urlpattern of the project :

```python
from django.conf import settings
from django.conf.urls.static import static
#from django.contrib import admin
from django.urls import path, include

urlpatterns = [
#path('admin/', admin.site.urls),
path('accounts/', include('user_app.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('', include("social_django.urls", namespace="social")),
] + static(
settings.STATIC_URL,
document_root=settings.STATIC_ROOT
) + static(
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT
)
```

And finally add the context_processor :

```python
user_app.context_processors.providers_settings
```

Then you have to migrate the database with `python3 manage.py migrate`
Expand Down
6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Django>=3.0.8
Pillow>=7.2.0
Django==3.0.8
Pillow==7.2.0
social-auth-app-django==4.0.0
django-crispy-forms==1.9.2
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name="django-user-app",
version="0.0.5",
version="0.0.6",
license="MIT",

author="Couapy",
Expand Down
7 changes: 7 additions & 0 deletions user_app/context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.conf import settings


def providers_settings(request):
return {
'USER_APP_PROVIDERS': settings.USER_APP_PROVIDERS,
}
4 changes: 2 additions & 2 deletions user_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated by Django 3.0.4 on 2020-05-21 17:34

import accounts.models
import user_app.models
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
Expand All @@ -21,7 +21,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('biography', models.TextField(blank=True, help_text='300 caractères maximum.', max_length=300, null=True, verbose_name='Biographie')),
('website', models.URLField(blank=True, null=True, verbose_name='Site web')),
('avatar', models.ImageField(blank=True, null=True, upload_to=accounts.models.user_directory_path, verbose_name='Photo de profil')),
('avatar', models.ImageField(blank=True, null=True, upload_to=user_app.models.user_directory_path, verbose_name='Photo de profil')),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
Expand Down
47 changes: 47 additions & 0 deletions user_app/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!doctype html>
<html lang="fr">

<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>{% block title %}{% endblock title %}</title>
{% block head %}{% endblock head %}

<!-- Bootstrap -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"
integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"
crossorigin="anonymous"></script>
</head>

<body>
{% include 'navbar.html' %}
<div class="container pt-4 pd-4">
{% if success is True %}
<div class="alert alert-success alert-dismissible fade show" role="alert">
Les modifications ont bien été enregistrées.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% elif success is False %}
<div class="alert alert-danger alert-dismissible fade show" role="alert">
Une erreur est survenue, les modifications n'ont pas été enregistrées.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% endif %}
{% block content %}{% endblock content %}
</div>
</body>

</html>
30 changes: 30 additions & 0 deletions user_app/templates/navbar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% load static %}
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="/">Django-Template</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
<ul class="navbar-nav">
{% if user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="{% url 'user_app:index' %}">Mon compte</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'logout' %}">Se déconnecter</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{% url 'login' %}">Se connecter</a>
</li>
{% endif %}
</ul>
</div>
</nav>
16 changes: 16 additions & 0 deletions user_app/templates/registration/logged_out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends 'base.html' %}
{% load crispy_forms_tags %}


{% block title %}Déconnecté{% endblock title %}

{% block content %}
<h1>Vous avez été déconnecté</h1>
<p>
Merci du temps que vous avez accordé à ce site. A bientôt !
</p>
<p>
Vous pouvez aussi vous <a href="{% url 'login' %}">reconnecter ici</a>.
Ou simplement <a href="/">rester sur le site</a>.
</p>
{% endblock content %}
27 changes: 27 additions & 0 deletions user_app/templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% extends 'base.html' %}
{% load crispy_forms_tags %}


{% block title %}Connexion{% endblock title %}

{% block content %}
<h1>Connexion</h1>
<h2>Se connecter avec un compte utilisateur</h2>
<div class="mb-3">
<form method="POST">
{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="Enregistrer" class="btn btn-primary">
<input type="reset" value="Annuler" class="btn btn-secondary">
</form>
<div class="mt-3">
<a href="{% url 'user_app:register' %}">Créer un compte</a> - <a href="{% url 'password_reset' %}">Mot de passe oublié</a>
</div>
</div>
<h2>Se connecter avec un service tiers</h2>
<div class="mb-3">
{% for provider in USER_APP_PROVIDERS %}
<a href="{% url 'social:begin' provider.provider %}">Connectez-vous avec {{ provider.name }}</a><br>
{% endfor %}
</div>
{% endblock content %}
11 changes: 11 additions & 0 deletions user_app/templates/registration/password_reset_complete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'base.html' %}


{% block title %}Changement de mot de passe effectué{% endblock title %}

{% block content %}
<h1>Changement de mot de passe effectué</h1>
<p>
Votre mot de passe a bien été mis à jour. Vous pouvez vous <a href="{% url 'login' %}">connecter ici</a>.
</p>
{% endblock content %}
22 changes: 22 additions & 0 deletions user_app/templates/registration/password_reset_confirm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% extends 'base.html' %}
{% load crispy_forms_tags%}


{% block title %}Mot de passe oublié{% endblock title %}

{% block content %}
{% if validlink %}
<h3>Changement de mot de passe</h3>
<form method="POST">
{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="Enregistrer" class="btn btn-primary">
<input type="reset" value="Annuler" class="btn btn-secondary">
</form>
{% else %}
<p>
Le lien de réinitialisation du mot de passe n'était pas valide, peut-être parce qu'il a déjà été utilisé.
Veuillez demander une nouvelle réinitialisation du mot de passe.
</p>
{% endif %}
{% endblock %}
16 changes: 16 additions & 0 deletions user_app/templates/registration/password_reset_done.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends 'base.html' %}


{% block title %}Message de réinitialisation du mot de passe envoyé{% endblock title %}

{% block content %}
<h2>Message de réinitialisation du mot de passe envoyé</h2>
<p>
Nous vous avons envoyé par courriel les instructions pour changer de mot de passe, pour autant qu’un compte existe avec
l’adresse que vous avez indiquée. Vous devriez recevoir rapidement ce message.
</p>
<p>
Si vous ne recevez pas de message, vérifiez que vous avez saisi l’adresse avec laquelle vous vous êtes enregistré et
contrôlez votre dossier de pourriels.
</p>
{% endblock %}
18 changes: 18 additions & 0 deletions user_app/templates/registration/password_reset_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends 'base.html' %}
{% load crispy_forms_tags %}


{% block title %}Mot de passe oublié{% endblock title %}

{% block content %}
<h1>Réinitialiser son mot de passe</h1>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="Enregistrer" class="btn btn-primary">
<input type="reset" value="Annuler" class="btn btn-secondary">
</form>
<div class="mt-3">
<a href="{% url 'login' %}">Se connecter</a>
</div>
{% endblock %}
Loading

0 comments on commit ec34775

Please sign in to comment.