Revision e7cb4085 snf-astakos-app/astakos/im/models.py

b/snf-astakos-app/astakos/im/models.py
36 36
import logging
37 37
import json
38 38
import math
39
import copy
39 40

  
40 41
from time import asctime
41 42
from datetime import datetime, timedelta
......
349 350
        _('Token expiration date'), null=True)
350 351

  
351 352
    updated = models.DateTimeField(_('Update date'))
352
    is_verified = models.BooleanField(_('Is verified?'), default=False)
353 353

  
354
    email_verified = models.BooleanField(_('Email verified?'), default=False)
354
    # Arbitrary text to identify the reason user got deactivated.
355
    # To be used as a reference from administrators.
356
    deactivated_reason = models.TextField(
357
        _('Reason the user was disabled for'),
358
        default=None, null=True)
359
    deactivated_at = models.DateTimeField(_('User deactivated at'), null=True,
360
                                          blank=True)
355 361

  
356 362
    has_credits = models.BooleanField(_('Has credits?'), default=False)
357
    has_signed_terms = models.BooleanField(
358
        _('I agree with the terms'), default=False)
359
    date_signed_terms = models.DateTimeField(
360
        _('Signed terms date'), null=True, blank=True)
361 363

  
362
    activation_sent = models.DateTimeField(
363
        _('Activation sent data'), null=True, blank=True)
364
    # this is set to True when user profile gets updated for the first time
365
    is_verified = models.BooleanField(_('Is verified?'), default=False)
366

  
367
    # user email is verified
368
    email_verified = models.BooleanField(_('Email verified?'), default=False)
369

  
370
    # unique string used in user email verification url
371
    verification_code = models.CharField(max_length=255, null=True,
372
                                         blank=False, unique=True)
373

  
374
    # date user email verified
375
    verified_at = models.DateTimeField(_('User verified email at'), null=True,
376
                                       blank=True)
377

  
378
    # email verification notice was sent to the user at this time
379
    activation_sent = models.DateTimeField(_('Activation sent date'),
380
                                           null=True, blank=True)
381

  
382
    # user got rejected during moderation process
383
    is_rejected = models.BooleanField(_('Account rejected'),
384
                                      default=False)
385
    # reason user got rejected
386
    rejected_reason = models.TextField(_('User rejected reason'), null=True,
387
                                       blank=True)
388
    # moderation status
389
    moderated = models.BooleanField(_('User moderated'), default=False)
390
    # date user moderated (either accepted or rejected)
391
    moderated_at = models.DateTimeField(_('Date moderated'), default=None,
392
                                        blank=True, null=True)
393
    # a snapshot of user instance the time got moderated
394
    moderated_data = models.TextField(null=True, default=None, blank=True)
395
    # a string which identifies how the user got moderated
396
    accepted_policy = models.CharField(_('Accepted policy'), max_length=255,
397
                                       default=None, null=True, blank=True)
398
    # the email used to accept the user
399
    accepted_email = models.EmailField(null=True, default=None, blank=True)
400

  
401
    has_signed_terms = models.BooleanField(_('I agree with the terms'),
402
                                           default=False)
403
    date_signed_terms = models.DateTimeField(_('Signed terms date'),
404
                                             null=True, blank=True)
405
    # permanent unique user identifier
406
    uuid = models.CharField(max_length=255, null=True, blank=False,
407
                            unique=True)
364 408

  
365 409
    policy = models.ManyToManyField(
366 410
        Resource, null=True, through='AstakosUserQuota')
367 411

  
368
    uuid = models.CharField(max_length=255, null=True, blank=False, unique=True)
369

  
370
    __has_signed_terms = False
371 412
    disturbed_quota = models.BooleanField(_('Needs quotaholder syncing'),
372 413
                                           default=False, db_index=True)
373 414

  
374 415
    objects = AstakosUserManager()
375

  
376 416
    forupdate = ForUpdateManager()
377 417

  
378 418
    def __init__(self, *args, **kwargs):
379 419
        super(AstakosUser, self).__init__(*args, **kwargs)
380
        self.__has_signed_terms = self.has_signed_terms
381 420
        if not self.id:
382 421
            self.is_active = False
383 422

  
......
447 486

  
448 487
    def update_uuid(self):
449 488
        while not self.uuid:
450
            uuid_val =  str(uuid.uuid4())
489
            uuid_val = str(uuid.uuid4())
451 490
            try:
452 491
                AstakosUser.objects.get(uuid=uuid_val)
453 492
            except AstakosUser.DoesNotExist, e:
......
460 499
                self.date_joined = datetime.now()
461 500
            self.updated = datetime.now()
462 501

  
463
        # update date_signed_terms if necessary
464
        if self.__has_signed_terms != self.has_signed_terms:
465
            self.date_signed_terms = datetime.now()
466

  
467 502
        self.update_uuid()
468

  
503
        # username currently matches email
469 504
        if self.username != self.email.lower():
470
            # set username
471 505
            self.username = self.email.lower()
472 506

  
473 507
        super(AstakosUser, self).save(**kwargs)
474 508

  
509
    def renew_verification_code(self):
510
        self.verification_code = str(uuid.uuid4())
511
        logger.info("Verification code renewed for %s" % self.log_display)
512

  
475 513
    def renew_token(self, flush_sessions=False, current_key=None):
476 514
        md5 = hashlib.md5()
477 515
        md5.update(settings.SECRET_KEY)
......
485 523
                                  timedelta(hours=AUTH_TOKEN_DURATION)
486 524
        if flush_sessions:
487 525
            self.flush_sessions(current_key)
488
        msg = 'Token renewed for %s' % self.email
526
        msg = 'Token renewed for %s' % self.log_display
489 527
        logger.log(LOGGING_LEVEL, msg)
490 528

  
491 529
    def flush_sessions(self, current_key=None):
......
637 675

  
638 676
    def get_activation_url(self, nxt=False):
639 677
        url = "%s?auth=%s" % (reverse('astakos.im.views.activate'),
640
                                 quote(self.auth_token))
678
                                 quote(self.verification_code))
641 679
        if nxt:
642 680
            url += "&next=%s" % quote(nxt)
643 681
        return url
......
658 696
        msg_pending_help = _(astakos_messages.ACCOUNT_PENDING_ACTIVATION_HELP)
659 697
        #msg_resend_prompt = _(astakos_messages.ACCOUNT_RESEND_ACTIVATION)
660 698
        msg_pending_mod = provider.get_pending_moderation_msg
699
        msg_rejected = _(astakos_messages.ACCOUNT_REJECTED)
661 700
        msg_resend = _(astakos_messages.ACCOUNT_RESEND_ACTIVATION)
662 701

  
663
        if self.activation_sent:
664
            if self.email_verified:
665
                message = msg_inactive
666
            else:
667
                message = msg_pending
668
                url = self.get_resend_activation_url()
669
                msg_extra = msg_pending_help + \
670
                            u' ' + \
671
                            '<a href="%s">%s?</a>' % (url, msg_resend)
702
        if not self.email_verified:
703
            message = msg_pending
704
            url = self.get_resend_activation_url()
705
            msg_extra = msg_pending_help + \
706
                        u' ' + \
707
                        '<a href="%s">%s?</a>' % (url, msg_resend)
672 708
        else:
673
            if astakos_settings.MODERATION_ENABLED:
709
            if not self.moderated:
674 710
                message = msg_pending_mod
675 711
            else:
676
                message = msg_pending
677
                url = self.get_resend_activation_url()
678
                msg_extra = '<a href="%s">%s?</a>' % (url, \
679
                                msg_resend)
712
                if self.is_rejected:
713
                    message = msg_rejected
714
                else:
715
                    message = msg_inactive
680 716

  
681
        return mark_safe(message + u' '+ msg_extra)
717
        return mark_safe(message + u' ' + msg_extra)
682 718

  
683 719
    def owns_application(self, application):
684 720
        return application.owner == self
......
1104 1140
        unique_together = ("provider", "third_party_identifier")
1105 1141

  
1106 1142
    def get_user_instance(self):
1143
        """
1144
        Create a new AstakosUser instance based on details provided when user
1145
        initially signed up.
1146
        """
1107 1147
        d = copy.copy(self.__dict__)
1108 1148
        d.pop('_state', None)
1109 1149
        d.pop('id', None)

Also available in: Unified diff