Skip to content

Commit 01eba5a

Browse files
authored
Merge pull request #93 from Vauhtijuoksu/feature/discord-login
Feature/discord login
2 parents 11e76e5 + 8223d91 commit 01eba5a

File tree

17 files changed

+2301
-686
lines changed

17 files changed

+2301
-686
lines changed

Pipfile

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ name = "pypi"
55

66
[packages]
77
Django = "~=3.2"
8+
django-cms = "~=3.11"
89
django-filer = "*"
910
djangocms-text-ckeditor = "*"
1011
djangocms-link = "*"
@@ -24,6 +25,7 @@ django-storages = {extras = ["azure"], version = "~=1.13"}
2425
python-dateutil = "*"
2526
requests-cache = "*"
2627
gunicorn = "*"
28+
django-allauth = "*"
2729

2830
[requires]
2931
python_version = "3.9"

Pipfile.lock

+612-625
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

marathon/cms_plugins.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from .forms import SubmissionForm, PlayerForm
55
from .models import Event, Submission, MarathonPlugin
6+
from .utils import get_player_info_for_user
67

78
@plugin_pool.register_plugin
89
class SubmissionListPlugin(CMSPluginBase):
@@ -36,8 +37,15 @@ def render(self, context, instance, placeholder):
3637
previous_data = context['request'].session.get('previous_form')
3738

3839
form = SubmissionForm(previous_data)
39-
player_form = PlayerForm(previous_data, prefix='player')
40-
40+
if previous_data:
41+
player_form = PlayerForm(previous_data, prefix='player')
42+
elif context['request'].user.is_authenticated:
43+
print('got user')
44+
player_info = get_player_info_for_user(context['request'].user)
45+
player_form = PlayerForm(initial=player_info, prefix='player')
46+
else:
47+
player_form = PlayerForm(prefix='player')
48+
context['require_authentication'] = True
4149
context['form'] = form
4250
context['player_form'] = player_form
4351
context['event'] = instance.event

marathon/utils.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from django.conf import settings
2+
from django.core.exceptions import ObjectDoesNotExist
3+
from django.forms.models import model_to_dict
4+
from allauth.account.models import EmailAddress
5+
from .models import Player
6+
7+
8+
def get_user_discord(user: settings.AUTH_USER_MODEL):
9+
if hasattr(user, 'socialaccount_set'):
10+
for account in user.socialaccount_set.all():
11+
if account.provider == 'discord':
12+
username = account.extra_data.get('username', None)
13+
nickname = account.extra_data.get('global_name', None)
14+
return {
15+
'discord': username,
16+
'nickname': nickname
17+
}
18+
return None
19+
20+
def get_user_primary_email(user: settings.AUTH_USER_MODEL):
21+
return EmailAddress.objects.get_primary_email(user)
22+
23+
def get_player_info_for_user(user: settings.AUTH_USER_MODEL):
24+
try:
25+
player = user.player
26+
return model_to_dict(player)
27+
except ObjectDoesNotExist:
28+
pass
29+
30+
player_info = {}
31+
32+
discord_info = get_user_discord(user) or {}
33+
player_info.update(discord_info)
34+
35+
email = get_user_primary_email(user)
36+
player_info['gmail'] = email
37+
38+
return player_info

marathon/views.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ def event_detail(request, event):
2525
def new_submission(request, event):
2626
if request.method == 'POST':
2727
event = get_object_or_404(Event, slug=event)
28-
player_form = PlayerForm(request.POST, prefix='player')
28+
29+
player = None
30+
if request.user.is_authenticated:
31+
player, created = Player.objects.get_or_create(user=request.user)
32+
player_form = PlayerForm(request.POST, prefix='player', instance=player)
33+
2934
form = SubmissionForm(request.POST)
3035

3136
if form.is_valid() and player_form.is_valid():

requirements.txt

+53-50
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,72 @@
11
-i https://pypi.org/simple
2-
asgiref==3.6.0 ; python_version >= '3.7'
3-
attrs==22.2.0 ; python_version >= '3.6'
4-
azure-core==1.26.3 ; python_version >= '3.7'
5-
azure-storage-blob==12.15.0
6-
beautifulsoup4==4.12.0 ; python_full_version >= '3.6.0'
7-
brotli==1.0.9
8-
cattrs==22.2.0 ; python_version >= '3.7'
9-
certifi==2022.12.7 ; python_version >= '3.6'
10-
cffi==1.15.1
11-
charset-normalizer==3.1.0 ; python_full_version >= '3.7.0'
12-
cryptography==40.0.1 ; python_version >= '3.6'
2+
asgiref==3.7.2 ; python_version >= '3.7'
3+
attrs==23.2.0 ; python_version >= '3.7'
4+
azure-core==1.29.7 ; python_version >= '3.7'
5+
azure-storage-blob==12.19.0
6+
beautifulsoup4==4.12.3 ; python_full_version >= '3.6.0'
7+
brotli==1.1.0
8+
cattrs==23.2.3 ; python_version >= '3.8'
9+
certifi==2023.11.17 ; python_version >= '3.6'
10+
cffi==1.16.0 ; python_version >= '3.8'
11+
chardet==5.2.0 ; python_version >= '3.7'
12+
charset-normalizer==3.3.2 ; python_full_version >= '3.7.0'
13+
cryptography==41.0.7 ; python_version >= '3.7'
1314
cssselect2==0.7.0 ; python_version >= '3.7'
14-
django==3.2.18
15-
django-appconf==1.0.5 ; python_version >= '3.6'
15+
defusedxml==0.7.1 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
16+
django==3.2.23
17+
django-allauth==0.60.1
18+
django-appconf==1.0.6 ; python_version >= '3.7'
1619
django-bootstrap-v5==1.0.11
17-
django-classy-tags==4.0.0 ; python_version >= '3.7'
18-
django-cms==3.11.1 ; python_version >= '3.7'
19-
django-compressor==4.3.1
20-
django-filer==2.2.4
21-
django-formtools==2.4 ; python_version >= '3.6'
22-
django-js-asset==2.0.0 ; python_version >= '3.6'
23-
django-mptt==0.14.0 ; python_version >= '3.6'
20+
django-classy-tags==4.1.0 ; python_version >= '3.8'
21+
django-cms==3.11.4
22+
django-compressor==4.4
23+
django-filer==3.1.1
24+
django-formtools==2.5.1 ; python_version >= '3.8'
2425
django-polymorphic==3.1.0
2526
django-sass-processor==1.1
26-
django-sekizai==4.0.0 ; python_version >= '3.8'
27-
django-storages[azure]==1.13.2
28-
django-treebeard==4.4 ; python_version >= '3.6'
29-
djangocms-admin-style==3.2.3 ; python_version >= '3.7'
30-
djangocms-attributes-field==2.1.0
31-
djangocms-file==3.0.0
27+
django-sekizai==4.1.0 ; python_version >= '3.8'
28+
django-storages[azure]==1.14.2
29+
django-treebeard==4.7 ; python_version >= '3.8'
30+
djangocms-admin-style==3.2.7 ; python_version >= '3.7'
31+
djangocms-attributes-field==3.0.0
32+
djangocms-file==3.0.1
3233
djangocms-link==3.1.1
33-
djangocms-picture==4.0.0
34-
djangocms-snippet==3.1.0
35-
djangocms-style==3.0.0
36-
djangocms-text-ckeditor==5.1.2
34+
djangocms-picture==4.1.1
35+
djangocms-snippet==3.1.1
36+
djangocms-style==3.1.0
37+
djangocms-text-ckeditor==5.1.5
3738
easy-thumbnails[svg]==2.8.5 ; python_version >= '3.6'
38-
exceptiongroup==1.1.1 ; python_version < '3.11'
39-
gunicorn==20.1.0
39+
exceptiongroup==1.2.0 ; python_version < '3.11'
40+
gunicorn==21.2.0
4041
html5lib==1.1 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
41-
idna==3.4 ; python_version >= '3.5'
42+
idna==3.6 ; python_version >= '3.5'
4243
isodate==0.6.1
43-
libsass==0.22.0
44-
lxml==4.9.2 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
45-
packaging==23.0 ; python_version >= '3.7'
46-
pillow==9.4.0 ; python_version >= '3.7'
47-
platformdirs==3.2.0 ; python_version >= '3.7'
48-
psycopg2==2.9.5
44+
libsass==0.23.0
45+
lxml==5.1.0 ; python_version >= '3.6'
46+
oauthlib==3.2.2 ; python_version >= '3.6'
47+
packaging==23.2 ; python_version >= '3.7'
48+
pillow==10.2.0 ; python_version >= '3.8'
49+
platformdirs==4.1.0 ; python_version >= '3.8'
50+
psycopg2==2.9.9
4951
pycparser==2.21 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
52+
pyjwt[crypto]==2.8.0 ; python_version >= '3.7'
5053
python-dateutil==2.8.2
5154
python-decouple==3.8
52-
pytz==2023.3
55+
python3-openid==3.2.0
56+
pytz==2023.3.post1
5357
rcssmin==1.1.1
54-
reportlab==3.6.12 ; python_version >= '3.7' and python_version < '4.0'
55-
requests==2.28.2
56-
requests-cache==1.0.1
58+
reportlab==4.0.9 ; python_version >= '3.7' and python_version < '4.0'
59+
requests==2.31.0
60+
requests-cache==1.1.1
61+
requests-oauthlib==1.3.1 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
5762
rjsmin==1.2.1
58-
setuptools==67.6.1 ; python_version >= '3.7'
5963
six==1.16.0 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
60-
soupsieve==2.4 ; python_version >= '3.7'
61-
sqlparse==0.4.3 ; python_version >= '3.5'
64+
soupsieve==2.5 ; python_version >= '3.8'
65+
sqlparse==0.4.4 ; python_version >= '3.5'
6266
svglib==1.5.1 ; python_version >= '3.7'
6367
tinycss2==1.2.1 ; python_version >= '3.7'
64-
typing-extensions==4.5.0 ; python_version >= '3.7'
65-
unidecode==1.1.2
68+
typing-extensions==4.9.0 ; python_version < '3.11'
6669
url-normalize==1.4.3 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5'
67-
urllib3==1.26.15 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5'
70+
urllib3==2.1.0 ; python_version >= '3.8'
6871
webencodings==0.5.1
69-
whitenoise[brotli]==6.4.0
72+
whitenoise[brotli]==6.6.0

scripts/az_postbuild.sh

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env bash
22

3+
python manage.py compilemessages
34
python manage.py compilescss --use-storage
45
python manage.py collectstatic --noinput --ignore=*.scss
56
python manage.py migrate --noinput

scripts/docker_entrypoint_gunicorn.sh

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ done
99

1010
echo "PostgreSQL started"
1111

12+
python manage.py compilemessages
1213
python manage.py compilescss --use-storage
1314
python manage.py collectstatic --noinput --ignore=*.scss
1415

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% load allauth %}
2+
{% comment %} djlint:off {% endcomment %}
3+
<{% if attrs.href %}a href="{{ attrs.href }}"{% else %}button{% endif %}
4+
class="btn {% if attrs.classes %}{{ attrs.classes }}{% else %}btn-primary{% endif %}"
5+
{% if attrs.form %}form="{{ attrs.form }}"{% endif %}
6+
{% if attrs.id %}id="{{ attrs.id }}"{% endif %}
7+
{% if attrs.name %}name="{{ attrs.name }}"{% endif %}
8+
{% if attrs.type %}type="{{ attrs.type }}"{% endif %}
9+
>
10+
{% slot %}
11+
{% endslot %}
12+
</{% if attrs.href %}a{% else %}button{% endif %}>

templates/allauth/layouts/base.html

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{% extends "vauhtijuoksu/vauhtijuoksu.html" %}
2+
{% load i18n %}
3+
{% block override %}
4+
{% if messages %}
5+
<div>
6+
<strong>{% trans "Messages:" %}</strong>
7+
<ul>
8+
{% for message in messages %}<li>{{ message }}</li>{% endfor %}
9+
</ul>
10+
</div>
11+
{% endif %}
12+
<div>
13+
<ul>
14+
{% if user.is_authenticated %}
15+
<li>
16+
<a href="{% url 'account_email' %}">{% trans "Change Email" %}</a>
17+
</li>
18+
<li>
19+
<a href="{% url 'account_logout' %}">{% trans "Sign Out" %}</a>
20+
</li>
21+
{% endif %}
22+
</ul>
23+
</div>
24+
{% block content %}
25+
{% endblock content %}
26+
{% endblock override %}
27+
{% block extra_body %}
28+
{% endblock extra_body %}

templates/generic_base.html

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% load static sekizai_tags cms_tags menu_tags %}
1+
{% load static sekizai_tags cms_tags menu_tags account socialaccount %}
22
<!DOCTYPE html>
33
<html>
44

@@ -16,7 +16,20 @@
1616

1717
{% block body %}{% endblock %}
1818

19-
{% render_block "js" %}
2019
</body>
21-
20+
<footer>
21+
{% block footer %}
22+
<div class="footer d-flex justify-content-center my-2">
23+
<div>
24+
{% if user.is_authenticated %}
25+
Hei, {% user_display user %}.
26+
<a href="{% url 'account_logout' %}">Kirjaudu ulos</a>
27+
{% else %}
28+
<a href="{% provider_login_url "discord" next=request.path %}">Kirjaudu sisään</a>
29+
{% endif %}
30+
</div>
31+
</div>
32+
{% endblock %}
33+
</footer>
34+
{% render_block "js" %}
2235
</html>

templates/marathon/plugins/submission_form.html

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
{% load bootstrap5 sekizai_tags %}
1+
{% load bootstrap5 sekizai_tags socialaccount %}
2+
{% bootstrap_messages %}
3+
{% if require_authentication and not user.is_authenticated %}
4+
{% provider_login_url "discord" next=request.path as href %}
5+
{% bootstrap_button "Kirjaudu Discord-tunnuksilla" button_type="link" href=href %}
6+
{% else %}
27
<form class="form" action="{% url 'new-submission' event=event.slug %}?next={{ request.path }}" method="post">
38
{% csrf_token %}
4-
{% bootstrap_messages %}
59
<!-- Player forms -->
610
<div id="player-form">
711
{% bootstrap_field player_form.nickname placeholder="Nimimerkki pelaajalistaukseen" layout="horizontal" form_group_class="mb-1" %}
@@ -37,4 +41,5 @@
3741
{% bootstrap_button "Lähetä" button_type="submit" button_class="btn-primary" %}
3842
{% endif %}
3943
</div>
40-
</form>
44+
</form>
45+
{% endif %}

templates/socialaccount/login.html

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{% extends "socialaccount/base_entrance.html" %}
2+
{% load i18n %}
3+
{% load allauth %}
4+
{% block head_title %}
5+
{% trans "Sign In" %}
6+
{% endblock head_title %}
7+
{% block content %}
8+
{% if process == "connect" %}
9+
{% element h1 %}
10+
{% blocktrans with provider.name as provider %}Connect {{ provider }}{% endblocktrans %}
11+
{% endelement %}
12+
<p>
13+
{% blocktrans with provider.name as provider %}You are about to connect a new third-party account from {{ provider }}.{% endblocktrans %}
14+
</p>
15+
{% else %}
16+
{% element h1 %}
17+
{% blocktrans with provider.name as provider %}Sign In Via {{ provider }}{% endblocktrans %}
18+
{% endelement %}
19+
<p>
20+
{% blocktrans with provider.name as provider %}You are about to sign in using a third-party account from {{ provider }}.{% endblocktrans %}
21+
</p>
22+
{% endif %}
23+
{% element form method="post" no_visible_fields=True %}
24+
{% slot actions %}
25+
{% csrf_token %}
26+
{% element button type="submit" classes="btn-primary btn-lg" %}
27+
{% trans "Continue" %}
28+
{% endelement %}
29+
{% endslot %}
30+
{% endelement %}
31+
{% endblock content %}

templates/vauhtijuoksu/vauhtijuoksu.html

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
</div>
3232
</div>
33+
{% block override %}{% endblock override %}
3334
{% placeholder "content" %}
3435
</div>
3536
</div>

0 commit comments

Comments
 (0)