Commit eeeef6c6 authored by Michal Čihař's avatar Michal Čihař

Registration using social-auth

Registration process is now handled by social-auth.

Missing:

- still relies on forms from django-registration
- validation code processing is broken
parent de1ae946
...@@ -166,8 +166,9 @@ def notify_new_comment(unit, comment, user, report_source_bugs): ...@@ -166,8 +166,9 @@ def notify_new_comment(unit, comment, user, report_source_bugs):
) )
def send_notification_email(language, email, notification, translation_obj, def send_notification_email(language, email, notification,
context=None, headers=None, user=None): translation_obj=None, context=None, headers=None,
user=None, info=None):
''' '''
Renders and sends notification email. Renders and sends notification email.
''' '''
...@@ -177,10 +178,12 @@ def send_notification_email(language, email, notification, translation_obj, ...@@ -177,10 +178,12 @@ def send_notification_email(language, email, notification, translation_obj,
if headers is None: if headers is None:
headers = {} headers = {}
try: try:
if info is None:
info = translation_obj.__unicode__()
weblate.logger.info( weblate.logger.info(
'sending notification %s on %s to %s', 'sending notification %s on %s to %s',
notification, notification,
translation_obj.__unicode__(), info,
email email
) )
...@@ -194,9 +197,10 @@ def send_notification_email(language, email, notification, translation_obj, ...@@ -194,9 +197,10 @@ def send_notification_email(language, email, notification, translation_obj,
# Adjust context # Adjust context
site = Site.objects.get_current() site = Site.objects.get_current()
context['translation'] = translation_obj
context['current_site'] = site.domain context['current_site'] = site.domain
context['site'] = site context['site'] = site
if translation_obj is not None:
context['translation'] = translation_obj
context['translation_url'] = get_site_url( context['translation_url'] = get_site_url(
translation_obj.get_absolute_url() translation_obj.get_absolute_url()
) )
......
...@@ -19,10 +19,13 @@ ...@@ -19,10 +19,13 @@
# #
from django.shortcuts import redirect from django.shortcuts import redirect
from django.core.urlresolvers import reverse
from social.pipeline.partial import partial from social.pipeline.partial import partial
from social.exceptions import AuthException from social.exceptions import AuthException
from accounts.models import send_notification_email
@partial @partial
def require_email(strategy, details, user=None, is_new=False, def require_email(strategy, details, user=None, is_new=False,
...@@ -52,3 +55,23 @@ def user_password(strategy, user, is_new=False, *args, **kwargs): ...@@ -52,3 +55,23 @@ def user_password(strategy, user, is_new=False, *args, **kwargs):
user.save() user.save()
elif not user.check_password(password): elif not user.check_password(password):
raise AuthException(strategy.backend) raise AuthException(strategy.backend)
def send_validation(strategy, code):
'''
Sends verification email.
'''
url = '%s?verification_code=%s' % (
reverse('social:complete', args=(strategy.backend_name,)),
code.code
)
send_notification_email(
'en', # FIXME: should probably stay same
code.email,
'activation',
info=code.code,
context={
'url': url
}
)
...@@ -57,7 +57,7 @@ REGISTRATION_DATA = { ...@@ -57,7 +57,7 @@ REGISTRATION_DATA = {
class RegistrationTest(TestCase): class RegistrationTest(TestCase):
def test_register(self): def test_register(self):
response = self.client.post( response = self.client.post(
reverse('registration_register'), reverse('register'),
REGISTRATION_DATA REGISTRATION_DATA
) )
# Check we did succeed # Check we did succeed
...@@ -94,7 +94,7 @@ class RegistrationTest(TestCase): ...@@ -94,7 +94,7 @@ class RegistrationTest(TestCase):
data = REGISTRATION_DATA.copy() data = REGISTRATION_DATA.copy()
data['username'] = 'u' data['username'] = 'u'
response = self.client.post( response = self.client.post(
reverse('registration_register'), reverse('register'),
data data
) )
self.assertContains( self.assertContains(
...@@ -106,7 +106,7 @@ class RegistrationTest(TestCase): ...@@ -106,7 +106,7 @@ class RegistrationTest(TestCase):
data = REGISTRATION_DATA.copy() data = REGISTRATION_DATA.copy()
data['email'] = 'x' data['email'] = 'x'
response = self.client.post( response = self.client.post(
reverse('registration_register'), reverse('register'),
data data
) )
self.assertContains( self.assertContains(
...@@ -123,7 +123,7 @@ class RegistrationTest(TestCase): ...@@ -123,7 +123,7 @@ class RegistrationTest(TestCase):
data = REGISTRATION_DATA.copy() data = REGISTRATION_DATA.copy()
data['content'] = 'x' data['content'] = 'x'
response = self.client.post( response = self.client.post(
reverse('registration_register'), reverse('register'),
data data
) )
self.assertContains( self.assertContains(
......
...@@ -25,27 +25,11 @@ from django.conf import settings ...@@ -25,27 +25,11 @@ from django.conf import settings
from registration.views import activate, register from registration.views import activate, register
from accounts.forms import RegistrationForm
from accounts.views import RegistrationTemplateView from accounts.views import RegistrationTemplateView
urlpatterns = patterns( urlpatterns = patterns(
'', '',
url(
r'^register/$', register, {
'backend': 'registration.backends.default.DefaultBackend',
'form_class': RegistrationForm,
'extra_context': {'title': _('User registration')}
},
name='registration_register'
),
url(
r'^register/complete/$',
RegistrationTemplateView.as_view(
template_name='registration/registration_complete.html'
),
name='registration_complete'
),
url( url(
r'^register/closed/$', r'^register/closed/$',
RegistrationTemplateView.as_view( RegistrationTemplateView.as_view(
...@@ -118,7 +102,15 @@ urlpatterns = patterns( ...@@ -118,7 +102,15 @@ urlpatterns = patterns(
name='auth_password_reset_done' name='auth_password_reset_done'
), ),
url(
r'^email-sent/$',
RegistrationTemplateView.as_view(
template_name='registration/registration_complete.html'
),
name='email-sent'
),
url(r'^profile/', 'accounts.views.user_profile', name='profile'), url(r'^profile/', 'accounts.views.user_profile', name='profile'),
url(r'^login/$', 'accounts.views.weblate_login', name='login'), url(r'^login/$', 'accounts.views.weblate_login', name='login'),
url(r'^register/$', 'accounts.views.register', name='register'),
url(r'', include('social.apps.django_app.urls', namespace='social')), url(r'', include('social.apps.django_app.urls', namespace='social')),
) )
...@@ -31,8 +31,10 @@ from django.contrib.auth.views import login ...@@ -31,8 +31,10 @@ from django.contrib.auth.views import login
from django.views.generic import TemplateView from django.views.generic import TemplateView
from urllib import urlencode from urllib import urlencode
from accounts.forms import RegistrationForm
from social.backends.utils import load_backends from social.backends.utils import load_backends
from social.apps.django_app.utils import BACKENDS from social.apps.django_app.utils import BACKENDS
from social.apps.django_app.views import complete
from accounts.models import set_lang, Profile from accounts.models import set_lang, Profile
from trans.models import Change, Project from trans.models import Change, Project
...@@ -235,3 +237,29 @@ def weblate_login(request): ...@@ -235,3 +237,29 @@ def weblate_login(request):
'title': _('Login'), 'title': _('Login'),
} }
) )
def register(request):
'''
Registration form.
'''
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
return complete(request, 'email')
else:
form = RegistrationForm()
return render_to_response(
'registration/registration_form.html',
RequestContext(
request,
{
'registration_backends': [
x for x in load_backends(BACKENDS).keys() if x != 'email'
],
'title': _('User registration'),
'form': form,
}
)
)
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
<li><a class="button" href="{% url 'auth_logout' %}">{% trans "Logout" %}</a></li> <li><a class="button" href="{% url 'auth_logout' %}">{% trans "Logout" %}</a></li>
{% else %} {% else %}
{% if registration_open %} {% if registration_open %}
<li><a class="button" href="{% url 'registration_register' %}">{% trans "Register" %}</a></li> <li><a class="button" href="{% url 'register' %}">{% trans "Register" %}</a></li>
{% endif %} {% endif %}
<li><a class="button" href="{% url 'login' %}{% if not skip_next %}?next={{ current_url }}{% endif %}">{% trans "Login" %}</a></li> <li><a class="button" href="{% url 'login' %}{% if not skip_next %}?next={{ current_url }}{% endif %}">{% trans "Login" %}</a></li>
{% endif %} {% endif %}
......
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
{{ status_text }} {{ status_text }}
</p> </p>
<p> <p>
{% url 'registration_register' as reg_url %} {% url 'register' as reg_url %}
{% blocktrans %}If you would like to contribute to translation of {{ project }}, you need to <a href="{{ reg_url }}">register on this server</a>.{% endblocktrans %} {% blocktrans %}If you would like to contribute to translation of {{ project }}, you need to <a href="{{ reg_url }}">register on this server</a>.{% endblocktrans %}
</p> </p>
<p> <p>
......
{% extends "mail/base.html" %}
{% load url from future %}
{% load i18n %}{% load translations %}
{% block content %}
<p>
{% trans "Hi,"%}
</p>
<p>
{% blocktrans with site|site_title as site_title %}This is an automatic email to help you complete your registration with {{ site_title }}.{% endblocktrans %}
</p>
<p>
{% blocktrans %}Please open the following link in your web browser. If the link is split over several lines, you may need to copy it in the address bar.{% endblocktrans %}
</p>
<p>
<a href="http://{{ site.domain }}{{ url }}">http://{{ site.domain }}{{ url }}</a>
</p>
<p>
{% blocktrans %}If there is a problem with your registration, please contact us:{% endblocktrans %}
</p>
<p>
<a href="http://{{ site.domain }}{% url 'contact' %}?subject=Registration">http://{{ site.domain }}{% url 'contact' %}?subject=Registration</a>
</p>
{% endblock %}
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
{% blocktrans %}Please open the following link in your web browser. If the link is split over several lines, you may need to copy it in the address bar.{% endblocktrans %} {% blocktrans %}Please open the following link in your web browser. If the link is split over several lines, you may need to copy it in the address bar.{% endblocktrans %}
http://{{ site.domain }}{% url 'registration.views.activate' activation_key=activation_key %} http://{{ site.domain }}{{ url }}
{% blocktrans %}If there is a problem with your registration, please contact us:{% endblocktrans %} {% blocktrans %}If there is a problem with your registration, please contact us:{% endblocktrans %}
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<h2>{% trans "Your account could not be activated" %}</h2> <h2>{% trans "Your account could not be activated" %}</h2>
<p> <p>
{% blocktrans count expiration_days as days %}This may be because it is already active or because you waited over {{ days }} day to activate it.{% plural %}This may be because it is already active or because you waited over {{ days }} days to activate it.{% endblocktrans %} {% blocktrans count expiration_days as days %}This may be because it is already active or because you waited over {{ days }} day to activate it.{% plural %}This may be because it is already active or because you waited over {{ days }} days to activate it.{% endblocktrans %}
{% url 'registration_register' as reg_url %} {% url 'register' as reg_url %}
{% url 'contact' as contact_url %} {% url 'contact' as contact_url %}
{% blocktrans %}If this is not the case, please <a href="{{ contact_url }}">contact</a> the website administrator. Otherwise, you may <a href="{{ reg_url }}">register again</a>.{% endblocktrans %} {% blocktrans %}If this is not the case, please <a href="{{ contact_url }}">contact</a> the website administrator. Otherwise, you may <a href="{{ reg_url }}">register again</a>.{% endblocktrans %}
</p> </p>
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
</div> </div>
{% endif %} {% endif %}
<form action="{% url 'registration_register' %}" method="post" accept-charset="utf-8"> <form action="{% url 'register' %}" method="post" accept-charset="utf-8">
{% csrf_token %} {% csrf_token %}
<table> <table>
{{ form.as_table }} {{ form.as_table }}
......
...@@ -196,6 +196,10 @@ SOCIAL_AUTH_PIPELINE = ( ...@@ -196,6 +196,10 @@ SOCIAL_AUTH_PIPELINE = (
'social.pipeline.user.user_details' 'social.pipeline.user.user_details'
) )
SOCIAL_AUTH_EMAIL_VALIDATION_FUNCTION = 'accounts.pipeline.send_validation'
SOCIAL_AUTH_EMAIL_VALIDATION_URL = '%s/accounts/email-sent/' % URL_PREFIX
# Middleware # Middleware
MIDDLEWARE_CLASSES = ( MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',
......
Markdown is supported
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