Commit 361ec44d authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

astakos: Reset user sign flags when adding new terms

When new terms are added, set has_signed_terms=False for all users, forcing
them to accept the new terms. If there are no terms, a new user is created
with has_signed_terms=True.

With this change we can skip querying the terms when authenticating a user,
speeding up POST /tokens.
parent a2c6c210
......@@ -191,6 +191,7 @@ class LocalUserCreationForm(UserCreationForm, StoreUserMixin):
save behavior is complete.
"""
user = super(LocalUserCreationForm, self).save(commit=False, **kwargs)
user.has_signed_terms = True
user.date_signed_terms = datetime.now()
user.renew_token()
if commit:
......@@ -303,6 +304,7 @@ class ThirdPartyUserCreationForm(forms.ModelForm, StoreUserMixin):
**kwargs)
user.set_unusable_password()
user.renew_token()
user.has_signed_terms = True
user.date_signed_terms = datetime.now()
if commit:
user.save(**kwargs)
......
......@@ -34,14 +34,16 @@
from os.path import abspath
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
from astakos.im.models import ApprovalTerms
from astakos.im.models import ApprovalTerms, AstakosUser
class Command(BaseCommand):
args = "<location>"
help = "Insert approval terms"
@transaction.commit_on_success
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError("Invalid number of arguments")
......@@ -54,6 +56,9 @@ class Command(BaseCommand):
terms = ApprovalTerms(location=location)
terms.save()
AstakosUser.objects.select_for_update().\
filter(has_signed_terms=True).\
update(has_signed_terms=False, date_signed_terms=None)
msg = "Created term id %d" % (terms.id,)
self.stdout.write(msg + '\n')
......@@ -32,13 +32,14 @@
# or implied, of GRNET S.A.
from optparse import make_option
from datetime import datetime
from django.db import transaction
from django.core.management.base import BaseCommand, CommandError
from django.core.validators import validate_email
from django.core.exceptions import ValidationError
from astakos.im.models import AstakosUser
from astakos.im.models import AstakosUser, get_latest_terms
class Command(BaseCommand):
......@@ -83,10 +84,19 @@ class Command(BaseCommand):
except ValidationError:
raise CommandError("Invalid email")
if get_latest_terms() is not None:
has_signed_terms = False
date_signed_terms = None
else:
has_signed_terms = True
date_signed_terms = datetime.now()
try:
u = AstakosUser(email=email,
first_name=first_name,
last_name=last_name,
has_signed_terms=has_signed_terms,
date_signed_terms=date_signed_terms,
is_superuser=options['is_superuser'])
u.set_password(password)
u.save()
......
......@@ -32,6 +32,7 @@
# or implied, of GRNET S.A.
import string
from datetime import datetime
from optparse import make_option
......@@ -138,6 +139,10 @@ class Command(BaseCommand):
make_option('--reject-reason',
dest='reject_reason',
help="Reason user got rejected"),
make_option('--sign-terms',
default=False,
action='store_true',
help="Sign terms"),
make_option('--base-quota',
dest='set_base_quota',
metavar='<resource> <capacity>',
......@@ -311,6 +316,10 @@ class Command(BaseCommand):
if options['renew_token']:
user.renew_token()
if options['sign_terms']:
user.has_signed_terms = True
user.date_signed_terms = datetime.now()
try:
user.save()
except ValidationError, e:
......
This diff is collapsed.
......@@ -631,19 +631,7 @@ class AstakosUser(User):
@property
def signed_terms(self):
term = get_latest_terms()
if not term:
return True
if not self.has_signed_terms:
return False
if not self.date_signed_terms:
return False
if self.date_signed_terms < term.date:
self.has_signed_terms = False
self.date_signed_terms = None
self.save()
return False
return True
return self.has_signed_terms
def set_invitations_level(self):
"""
......
......@@ -409,12 +409,14 @@ class TokensApiTest(TestCase):
self.user1 = AstakosUser.objects.create(
email='test1', email_verified=True, moderated=True,
has_signed_terms=True,
is_rejected=False)
backend.activate_user(self.user1)
assert self.user1.is_active is True
self.user2 = AstakosUser.objects.create(
email='test2', email_verified=True, moderated=True,
has_signed_terms=True,
is_rejected=False)
backend.activate_user(self.user2)
assert self.user2.is_active is True
......@@ -610,6 +612,7 @@ class UserCatalogsTest(TestCase):
def test_get_uuid_displayname_catalogs(self):
self.user = AstakosUser.objects.create(
email='test1', email_verified=True, moderated=True,
has_signed_terms=True,
is_rejected=False)
client = Client()
......
......@@ -152,6 +152,8 @@ def get_local_user(username, **kwargs):
'email': username,
'is_active': True,
'activation_sent': datetime.now(),
'has_signed_terms': True,
'date_signed_terms': datetime.now(),
'email_verified': True
}
user_params.update(kwargs)
......
......@@ -80,7 +80,9 @@ class TestApprovalTerms(TestCase):
terms = ApprovalTerms(location='terms')
terms.save()
self.user = get_local_user('user@synnefo.org')
self.user = get_local_user('user@synnefo.org',
has_signed_terms=False,
date_signed_terms=None)
self.assertTrue(not self.user.signed_terms)
self.assertTrue(self.user.date_signed_terms is None)
self.user_client = get_user_client(self.user.username)
......
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