Revision 09e7393c

b/snf-astakos-app/astakos/im/api.py
48 48
from astakos.im.faults import BadRequest, Unauthorized, InternalServerError, Fault
49 49
from astakos.im.models import AstakosUser
50 50
from astakos.im.settings import CLOUD_SERVICES, INVITATIONS_ENABLED, COOKIE_NAME
51
from astakos.im.util import has_signed_terms, epoch
51
from astakos.im.util import epoch
52 52

  
53 53
logger = logging.getLogger(__name__)
54 54

  
......
112 112
    if (time() - mktime(user.auth_token_expires.timetuple())) > 0:
113 113
        raise Unauthorized('Authentication expired')
114 114
    
115
    if not has_signed_terms(user):
115
    if not user.signed_terms():
116 116
        raise Unauthorized('Pending approval terms')
117 117
    
118 118
    response = HttpResponse()
......
123 123
                 'auth_token_created':user.auth_token_created.isoformat(),
124 124
                 'auth_token_expires':user.auth_token_expires.isoformat(),
125 125
                 'has_credits':user.has_credits,
126
                 'has_signed_terms':has_signed_terms(user)}
126
                 'has_signed_terms':user.signed_terms()}
127 127
    response.content = json.dumps(user_info)
128 128
    response['Content-Type'] = 'application/json; charset=UTF-8'
129 129
    response['Content-Length'] = len(response.content)
......
146 146
    if (time() - mktime(user.auth_token_expires.timetuple())) > 0:
147 147
        raise Unauthorized('Authentication expired')
148 148
    
149
    if not has_signed_terms(user):
149
    if not user.signed_terms():
150 150
        raise Unauthorized('Pending approval terms')
151 151
    
152 152
    response = HttpResponse()
b/snf-astakos-app/astakos/im/forms.py
45 45
from django.utils.safestring import mark_safe
46 46
from django.contrib import messages
47 47

  
48
from astakos.im.models import AstakosUser, Invitation
48
from astakos.im.models import AstakosUser, Invitation, get_latest_terms
49 49
from astakos.im.settings import INVITATIONS_PER_LEVEL, DEFAULT_FROM_EMAIL, SITENAME, RECAPTCHA_PRIVATE_KEY, DEFAULT_CONTACT_EMAIL, RECAPTCHA_ENABLED
50 50
from astakos.im.widgets import DummyWidget, RecaptchaWidget, ApprovalTermsWidget
51 51

  
52 52
# since Django 1.4 use django.core.urlresolvers.reverse_lazy instead
53
from astakos.im.util import reverse_lazy, get_latest_terms, reserved_email, get_query
53
from astakos.im.util import reverse_lazy, reserved_email, get_query
54 54

  
55 55
import logging
56 56
import recaptcha.client.captcha as captcha
b/snf-astakos-app/astakos/im/models.py
181 181
        q = q.filter(is_active = self.is_active)
182 182
        if q.count() != 0:
183 183
            raise ValidationError({'__all__':[_('Another account with the same email & is_active combination found.')]})
184
        
184
    
185
    def signed_terms(self):
186
        term = get_latest_terms()
187
        if not term:
188
            return True
189
        if not self.has_signed_terms:
190
            return False
191
        if not self.date_signed_terms:
192
            return False
193
        if self.date_signed_terms < term.date:
194
            self.has_signed_terms = False
195
            self.save()
196
            return False
197
        return True
198

  
185 199
class ApprovalTerms(models.Model):
186 200
    """
187 201
    Model for approval terms
......
254 268
            Invitation.objects.get(code=code)
255 269
            # An invitation with this code already exists, try again
256 270
        except Invitation.DoesNotExist:
257
            return code
271
            return code
272

  
273
def get_latest_terms():
274
    try:
275
        term = ApprovalTerms.objects.order_by('-id')[0]
276
        return term
277
    except IndexError:
278
        pass
279
    return None
b/snf-astakos-app/astakos/im/target/redirect.py
43 43
from urlparse import urlunsplit, urlsplit, urlparse, parse_qsl
44 44

  
45 45
from astakos.im.settings import COOKIE_NAME, COOKIE_DOMAIN
46
from astakos.im.util import set_cookie, has_signed_terms
46
from astakos.im.util import set_cookie
47 47

  
48 48
import logging
49 49

  
......
51 51

  
52 52
def login(request):
53 53
    """
54
    If there is no `next` request parameter redirects to astakos index page displaying an error
55
    message.
56
    If the request user is authenticated and has signed the approval terms, redirects to `next` request parameter. If not, redirects to approval terms in order to return back here after agreeing with the terms.
54
    If there is no ``next`` request parameter redirects to astakos index page
55
    displaying an error message.
56
    If the request user is authenticated and has signed the approval terms,
57
    redirects to `next` request parameter. If not, redirects to approval terms
58
    in order to return back here after agreeing with the terms.
57 59
    Otherwise, redirects to login in order to return back here after successful login.
58 60
    """
59 61
    next = request.GET.get('next')
......
67 69
    if request.user.is_authenticated():
68 70
        # if user has not signed the approval terms
69 71
        # redirect to approval terms with next the request path
70
        if not has_signed_terms(request.user):
72
        if not request.user.signed_terms():
71 73
            # first build next parameter
72 74
            parts = list(urlsplit(request.build_absolute_uri()))
73 75
            params = dict(parse_qsl(parts[3], keep_blank_values=True))
b/snf-astakos-app/astakos/im/util.py
174 174
def reverse_lazy(*args, **kwargs):
175 175
    return lazy_string(reverse, *args, **kwargs)
176 176

  
177
def get_latest_terms():
178
    try:
179
        term = ApprovalTerms.objects.order_by('-id')[0]
180
        return term
181
    except IndexError:
182
        pass
183
    return None
184

  
185
def has_signed_terms(user):
186
    term = get_latest_terms()
187
    if not term:
188
        return True
189
    if not user.has_signed_terms:
190
        return False
191
    if not user.date_signed_terms:
192
        return False
193
    if user.date_signed_terms < term.date:
194
        user.has_signed_terms = False
195
        user.save()
196
        return False
197
    return True
198

  
199 177
def reserved_email(email):
200 178
    return AstakosUser.objects.filter(email = email).count() != 0
201 179

  

Also available in: Unified diff