Esempio n. 1
def validate_username(form, user_id):

        cleaned_data = form.cleaned_data

        username = cleaned_data.get("username")
        # validate username string
        if username:
            if len(username) < 5:
                form._errors["username"] = form.error_class(["'Username' must contain at least 5 characters."])
            elif len(username) >30:
                form._errors["username"] = form.error_class(["'Username' must not exceed 30 characters."])
            elif, username):
                form._errors["username"] = form.error_class(["'Username' can only contain letters, digits and @/./+/-/_"])
            if settings.ESGF_CONFIG:
                # check that the corresponding OpenID is available in the local CoG database
                if user_id is None: # do not check when instance is updated
                    openid = esgfDatabaseManager.buildOpenid(username)
                    if esgfDatabaseManager.checkOpenid(openid):
                        form._errors["username"] = form.error_class(["Username/OpenID already taken in database."])
                        # save this openid in the form data so it can be used by the view POST method
                        form.cleaned_data['openid'] = openid      
                        # once the openid is validated, choose the closest possible username
                        _username = createUsername(username)
                        print 'Created username=%s from=%s' % (_username, username)
                        cleaned_data['username'] = _username # override form data
                # django will automatically check that the username is unique in the CoG database
Esempio n. 2
File: Progetto: soay/COG
    def _createObjects(self):
        '''Method to populate the database with some initial objects.'''
        # Site: reuse default site ''
        site = Site.objects.get(pk=1)"Updating site: %s" % site) = self.siteManager.get('SITE_NAME')
        site.domain = self.siteManager.get('SITE_DOMAIN')
        # Test project
        #if not Project.objects.filter(short_name=DEFAULT_PROJECT_SHORT_NAME).exists():
        if Project.objects.count() == 0:
  "Creating project: %s" % DEFAULT_PROJECT_SHORT_NAME)
            project = Project.objects.create(short_name=DEFAULT_PROJECT_SHORT_NAME, 
                                             long_name='Test Project', 
                                             description='This is a test project',
                                             site=site, active=True)
        # create Administrator user - one time only
        if User.objects.count() == 0:
            # create User object
  "Creating admin user")
            user = User(first_name='Admin', last_name='User', 
                        email=self.siteManager.get('EMAIL_SENDER', section=SECTION_EMAIL), 
                        is_staff=True, is_superuser=True)
            if settings.ESGF_CONFIG:
                password = self._getRootAdminPassword()
                password = DEFAULT_ROOTADMIN_PASSWORD
            # create UserProfile object
            userp = UserProfile(user=user, institution='Institution', city='City', state='State', country='Country',
            userp.clearTextPassword=password # needed by esgfDatabaseManager, NOT saved as clear text in any database
            # ESGF database setup
            if settings.ESGF_CONFIG:
                # create rootAdmin openid: https://<ESGF_HOSTNAME>/esgf-idp/openid/rootAdmin
                openid = esgfDatabaseManager.buildOpenid(ROOTADMIN_USERNAME)
                UserOpenID.objects.create(user=user, claimed_id=openid, display_id=openid)
      "Created openid:%s for CoG administrator: %s" % (openid, user.username) )
                # insert rootAdmin user in ESGF database
      "Inserting CoG administrator: %s in ESGF database" % user.username)

        # must create and enable 'esgf.idp.peer" as federated CoG peer
        if settings.IDP_REDIRECT is not None and settings.IDP_REDIRECT.strip()  != '':
            idpHostname = settings.IDP_REDIRECT.lower().replace('http://','').replace('https://','')
                idpPeerSite = PeerSite.objects.get(site__domain=idpHostname)
            except ObjectDoesNotExist:
                site = Site.objects.create(name=idpHostname, domain=idpHostname)
                idpPeerSite = PeerSite.objects.create(site=site, enabled=True)
            print '\tCreated IdP Peer site: %s with enabled=%s' % (idpPeerSite, idpPeerSite.enabled)