Commit d733629f29588ca2f60691f696a7b76be33fd898

Authored by Sergio Oliveira
2 parents 44419239 8fb93ee6

Merge branch 'incomplete_register_redirect' into 'master'

Incomplete register redirect

Let browserid handle the new users registration.
colab/accounts/auth.py
1 1
2 from django_browserid.auth import BrowserIDBackend 2 from django_browserid.auth import BrowserIDBackend
3 3
  4 +
4 class ColabBrowserIDBackend(BrowserIDBackend): 5 class ColabBrowserIDBackend(BrowserIDBackend):
5 def filter_users_by_email(self, email): 6 def filter_users_by_email(self, email):
6 return self.User.objects.filter(emails__address=email) 7 return self.User.objects.filter(emails__address=email)
colab/accounts/forms.py
@@ -32,7 +32,7 @@ class UserForm(forms.ModelForm): @@ -32,7 +32,7 @@ class UserForm(forms.ModelForm):
32 # Forces username to be lowercase always 32 # Forces username to be lowercase always
33 widget=forms.TextInput(attrs={'style' : 'text-transform: lowercase;'}), 33 widget=forms.TextInput(attrs={'style' : 'text-transform: lowercase;'}),
34 ) 34 )
35 - required = ('first_name', 'last_name', 'email', 'username') 35 + required = ('first_name', 'last_name', 'username')
36 36
37 class Meta: 37 class Meta:
38 model = User 38 model = User
@@ -49,9 +49,17 @@ class UserForm(forms.ModelForm): @@ -49,9 +49,17 @@ class UserForm(forms.ModelForm):
49 49
50 50
51 class UserCreationForm(UserForm): 51 class UserCreationForm(UserForm):
  52 +
  53 + def clean_username(self):
  54 + username = self.cleaned_data['username']
  55 + username = username.strip()
  56 + if not username:
  57 + raise forms.ValidationError(_('This field cannot be blank.'))
  58 + return username
  59 +
52 class Meta: 60 class Meta:
53 model = User 61 model = User
54 - fields = ('first_name', 'last_name', 'email', 'username') 62 + fields = ('first_name', 'last_name', 'username')
55 63
56 64
57 class UserUpdateForm(UserForm): 65 class UserUpdateForm(UserForm):
colab/accounts/management/__init__.py
colab/accounts/management/commands/__init__.py
colab/accounts/management/commands/delete_invalid.py
@@ -1,42 +0,0 @@ @@ -1,42 +0,0 @@
1 -  
2 -  
3 -from django.db.models import F  
4 -from django.utils import timezone  
5 -from django.utils.translation import ugettext as _  
6 -from django.core.management.base import BaseCommand, CommandError  
7 -  
8 -  
9 -from ...models import User  
10 -  
11 -  
12 -class Command(BaseCommand):  
13 - """Delete user accounts that have never logged in.  
14 -  
15 - Delete from database user accounts that have never logged in  
16 - and are at least 24h older.  
17 -  
18 - """  
19 -  
20 - help = __doc__  
21 -  
22 - def handle(self, *args, **kwargs):  
23 - seconds = timezone.timedelta(seconds=1)  
24 - now = timezone.now()  
25 - one_day_ago = timezone.timedelta(days=1)  
26 -  
27 - # Query for users that have NEVER logged in  
28 - #  
29 - # By default django sets the last_login as auto_now and then  
30 - # last_login is pretty much the same than date_joined  
31 - # (instead of null as I expected). Because of that we query  
32 - # for users which last_login is between date_joined - N and  
33 - # date_joined + N, where N is a small constant in seconds.  
34 - users = User.objects.filter(last_login__gt=(F('date_joined') - seconds),  
35 - last_login__lt=(F('date_joined') + seconds),  
36 - date_joined__lt=now-one_day_ago)  
37 - count = 0  
38 - for user in users:  
39 - count += 1  
40 - user.delete()  
41 -  
42 - print _(u'%(count)s users deleted.') % {'count': count}  
colab/accounts/middleware.py 0 → 100644
@@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
  1 +
  2 +from django.shortcuts import redirect
  3 +
  4 +VIEW_NAMES_ALLOWED = ('signup', 'Logout')
  5 +
  6 +
  7 +class UserRegisterMiddleware(object):
  8 +
  9 + def process_view(self, request, view_func, view_args, view_kwargs):
  10 +
  11 + if request.is_ajax():
  12 + return
  13 +
  14 + if not request.user.is_authenticated():
  15 + return
  16 +
  17 + if not request.user.needs_update:
  18 + return
  19 +
  20 + if view_func.__name__ not in VIEW_NAMES_ALLOWED:
  21 + return redirect('signup')
colab/accounts/migrations/0002_user_needs_update.py 0 → 100644
@@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
  1 +# -*- coding: utf-8 -*-
  2 +from __future__ import unicode_literals
  3 +
  4 +from django.db import models, migrations
  5 +
  6 +
  7 +class Migration(migrations.Migration):
  8 +
  9 + dependencies = [
  10 + ('accounts', '0001_initial'),
  11 + ]
  12 +
  13 + operations = [
  14 + migrations.AddField(
  15 + model_name='user',
  16 + name='needs_update',
  17 + field=models.BooleanField(default=False),
  18 + preserve_default=True,
  19 + ),
  20 + ]
colab/accounts/migrations/0003_auto_20141218_1755.py 0 → 100644
@@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
  1 +# -*- coding: utf-8 -*-
  2 +from __future__ import unicode_literals
  3 +
  4 +from django.db import models, migrations
  5 +
  6 +
  7 +class Migration(migrations.Migration):
  8 +
  9 + dependencies = [
  10 + ('accounts', '0002_user_needs_update'),
  11 + ]
  12 +
  13 + operations = [
  14 + migrations.AlterField(
  15 + model_name='user',
  16 + name='needs_update',
  17 + field=models.BooleanField(default=True),
  18 + preserve_default=True,
  19 + ),
  20 + ]
colab/accounts/models.py
@@ -26,6 +26,7 @@ class User(AbstractUser): @@ -26,6 +26,7 @@ class User(AbstractUser):
26 verification_hash = models.CharField(max_length=32, null=True, blank=True) 26 verification_hash = models.CharField(max_length=32, null=True, blank=True)
27 modified = models.DateTimeField(auto_now=True) 27 modified = models.DateTimeField(auto_now=True)
28 bio = models.CharField(max_length=200, null=True, blank=True) 28 bio = models.CharField(max_length=200, null=True, blank=True)
  29 + needs_update = models.BooleanField(default=True)
29 30
30 def check_password(self, raw_password): 31 def check_password(self, raw_password):
31 32
colab/accounts/views.py
@@ -123,21 +123,34 @@ class UserProfileDetailView(UserProfileBaseMixin, DetailView): @@ -123,21 +123,34 @@ class UserProfileDetailView(UserProfileBaseMixin, DetailView):
123 123
124 124
125 def signup(request): 125 def signup(request):
  126 + user = request.user
  127 +
  128 + # If the user is not authenticated, redirect to login
  129 + if not user.is_authenticated():
  130 + return redirect('login')
  131 +
  132 + # If the user doesn't need to update its main data, redirect to its profile
  133 + if not user.needs_update:
  134 + return redirect('user_profile', username=user.username)
  135 +
126 # If the request method is GET just return the form 136 # If the request method is GET just return the form
127 if request.method == 'GET': 137 if request.method == 'GET':
128 user_form = UserCreationForm() 138 user_form = UserCreationForm()
129 lists_form = ListsForm() 139 lists_form = ListsForm()
  140 +
130 return render(request, 'accounts/user_create_form.html', 141 return render(request, 'accounts/user_create_form.html',
131 {'user_form': user_form, 'lists_form': lists_form}) 142 {'user_form': user_form, 'lists_form': lists_form})
132 143
133 - user_form = UserCreationForm(request.POST) 144 + user_form = UserCreationForm(request.POST, instance=user)
134 lists_form = ListsForm(request.POST) 145 lists_form = ListsForm(request.POST)
135 146
136 if not user_form.is_valid() or not lists_form.is_valid(): 147 if not user_form.is_valid() or not lists_form.is_valid():
137 return render(request, 'accounts/user_create_form.html', 148 return render(request, 'accounts/user_create_form.html',
138 {'user_form': user_form, 'lists_form': lists_form}) 149 {'user_form': user_form, 'lists_form': lists_form})
139 150
140 - user = user_form.save() 151 + user = user_form.save(commit=False)
  152 + user.needs_update = False
  153 + user.save()
141 154
142 # Check if the user's email have been used previously 155 # Check if the user's email have been used previously
143 # in the mainling lists to link the user to old messages 156 # in the mainling lists to link the user to old messages
@@ -152,8 +165,6 @@ def signup(request): @@ -152,8 +165,6 @@ def signup(request):
152 mailman.update_subscription(user.email, mailing_lists) 165 mailman.update_subscription(user.email, mailing_lists)
153 166
154 messages.success(request, _('Your profile has been created!')) 167 messages.success(request, _('Your profile has been created!'))
155 - messages.warning(request, _('You must login to validated your profile. '  
156 - 'Profiles not validated are deleted in 24h.'))  
157 168
158 return redirect('user_profile', username=user.username) 169 return redirect('user_profile', username=user.username)
159 170
colab/settings.py
@@ -214,6 +214,7 @@ MIDDLEWARE_CLASSES = ( @@ -214,6 +214,7 @@ MIDDLEWARE_CLASSES = (
214 'django_mobile.middleware.MobileDetectionMiddleware', 214 'django_mobile.middleware.MobileDetectionMiddleware',
215 'django_mobile.middleware.SetFlavourMiddleware', 215 'django_mobile.middleware.SetFlavourMiddleware',
216 'colab.tz.middleware.TimezoneMiddleware', 216 'colab.tz.middleware.TimezoneMiddleware',
  217 + 'colab.accounts.middleware.UserRegisterMiddleware',
217 ) 218 )
218 219
219 # Add the django_browserid authentication backend. 220 # Add the django_browserid authentication backend.
@@ -272,12 +273,13 @@ LOGIN_URL = '/user/login' @@ -272,12 +273,13 @@ LOGIN_URL = '/user/login'
272 LOGIN_REDIRECT_URL = '/' 273 LOGIN_REDIRECT_URL = '/'
273 LOGIN_REDIRECT_URL_FAILURE = '/?bid_login_failed=true' 274 LOGIN_REDIRECT_URL_FAILURE = '/?bid_login_failed=true'
274 LOGOUT_REDIRECT_URL = '/' 275 LOGOUT_REDIRECT_URL = '/'
275 -BROWSERID_CREATE_USER = False 276 +BROWSERID_CREATE_USER = True
276 277
277 REVPROXY_ADD_REMOTE_USER = True 278 REVPROXY_ADD_REMOTE_USER = True
278 279
279 # Converse.js settings 280 # Converse.js settings
280 # This URL must use SSL in order to keep chat sessions secure 281 # This URL must use SSL in order to keep chat sessions secure
  282 +CONVERSEJS_ENABLED = False
281 CONVERSEJS_BOSH_SERVICE_URL = SITE_URL + '/http-bind' 283 CONVERSEJS_BOSH_SERVICE_URL = SITE_URL + '/http-bind'
282 284
283 CONVERSEJS_ALLOW_CONTACT_REQUESTS = False 285 CONVERSEJS_ALLOW_CONTACT_REQUESTS = False
colab/templates/base.html
@@ -119,28 +119,28 @@ @@ -119,28 +119,28 @@
119 {% if proxy.noosfero %} 119 {% if proxy.noosfero %}
120 <li class="dropdown"> 120 <li class="dropdown">
121 <a href="#" class="dropdown-toggle" data-toggle="dropdown"> 121 <a href="#" class="dropdown-toggle" data-toggle="dropdown">
122 - {% trans "Social" %} <b class="caret"></b> 122 + {% trans "Social" %} <b class="caret"></b>
123 </a> 123 </a>
124 <ul class="dropdown-menu"> 124 <ul class="dropdown-menu">
125 <li> 125 <li>
126 <a href="/social/search/people"> 126 <a href="/social/search/people">
127 - {% trans "Users" %} 127 + {% trans "Users" %}
128 </a> 128 </a>
129 </li> 129 </li>
130 <li> 130 <li>
131 <a href="/social/search/communities"> 131 <a href="/social/search/communities">
132 - {% trans "Communities" %} 132 + {% trans "Communities" %}
133 </a> 133 </a>
134 </li> 134 </li>
135 {% if user.is_active %} 135 {% if user.is_active %}
136 <li> 136 <li>
137 <a href="/social/profile/{{ user.username }}"> 137 <a href="/social/profile/{{ user.username }}">
138 - {% trans "Profile" %} 138 + {% trans "Profile" %}
139 </a> 139 </a>
140 </li> 140 </li>
141 <li> 141 <li>
142 <a href="/social/myprofile/{{ user.username }}"> 142 <a href="/social/myprofile/{{ user.username }}">
143 - {% trans "Control panel" %} 143 + {% trans "Control panel" %}
144 </a> 144 </a>
145 </li> 145 </li>
146 {% endif %} 146 {% endif %}
@@ -180,12 +180,10 @@ @@ -180,12 +180,10 @@
180 <li class="dropdown hidden-xs hidden-lg"> 180 <li class="dropdown hidden-xs hidden-lg">
181 <a href="#" class="dropdown-toggle" data-toggle="dropdown">Acesso <b class="caret"></b></a> 181 <a href="#" class="dropdown-toggle" data-toggle="dropdown">Acesso <b class="caret"></b></a>
182 <ul class="dropdown-menu"> 182 <ul class="dropdown-menu">
183 - <li><a href="{% url 'signup' %}">{% trans "Register" %}</a></li>  
184 {% trans 'Login' as login_text %} 183 {% trans 'Login' as login_text %}
185 <li>{% browserid_login text=login_text %}</li> 184 <li>{% browserid_login text=login_text %}</li>
186 </ul> 185 </ul>
187 </li> 186 </li>
188 - <li class="visible-xs hidden-sm hidden-md"><a href="{% url 'signup' %}">{% trans "Register" %}</a></li>  
189 {% trans 'Login' as login_text %} 187 {% trans 'Login' as login_text %}
190 <li class="visible-xs hidden-sm hidden-md">{% browserid_login text=login_text %}</li> 188 <li class="visible-xs hidden-sm hidden-md">{% browserid_login text=login_text %}</li>
191 {% else %} 189 {% else %}