Example #1
0
  def create_accounts(self, accounts):
    for acct_info in accounts:
      account, account_created = models.Account.objects.get_or_create( 
                                      email         = acct_info['email'], 
                                      full_name     = acct_info['full_name'], 
                                      contact_email = acct_info['contact_email'])
      if account_created:
        account.set_username_and_password(  username = acct_info['username'], 
                                            password = acct_info['password'])
        if acct_info.has_key(self.LABELS):
          for label, data_profile in acct_info[self.LABELS]:
            record, record_created = models.Record.objects.get_or_create(owner = account, label = label)
            if record_created:
              record.create_default_carenets()

              if data_profile:
                if self.verbosity:
                  print "\tLoading data profile %s for record %s"%(data_profile, label)
                loader = IndivoDataLoader(models.NoUser.objects.get_or_create(email="", 
                                                                              type='NoUser')[0])
                try:
                  loader.load_profile(record, data_profile)
                except Exception, e:
                  if self.verbosity:
                    print "\t\tError loading profile: %s"%str(e)
Example #2
0
    def test_loader_construction(self):

        # class should construct with valid args
        try:
            loader = IndivoDataLoader(self.creator)

        except Exception, e:
            self.fail('Could not create with standard args: %s' % str(e))
Example #3
0
class SampleDataUnitTests(InternalTests):
    def setUp(self):
        sampleDataSetUp(self)

    def tearDown(self):
        sampleDataTearDown(self)

    def test_loader_construction(self):

        # class should construct with valid args
        try:
            loader = IndivoDataLoader(self.creator)

        except Exception, e:
            self.fail('Could not create with standard args: %s' % str(e))

        # and with a custom data_dir
        try:
            loader = IndivoDataLoader(self.creator, self.old_sampledata_dir)
        except Exception, e:
            self.fail('Could not create with custom data dir: %s' % str(e))
Example #4
0
def account_create(request):
    """ Create a new account, and send out initialization emails.
    
    ``request.POST`` holds the creation arguments. 

    In Demo Mode, this call
    automatically creates new records for the account, populated with
    sample data. See :doc:`/sample-data` for details.
    
    Required Parameters:
    
    * *account_id*: an identifier for the new address. Must be formatted
      as an email address.
    
    Optional Parameters:
    
    * *full_name*: The full name to associate with the account. Defaults
      to the empty string.
    
    * *contact_email*: A valid email at which the account holder can 
      be reached. Defaults to the *account_id* parameter.
    
    * *primary_secret_p*: ``0`` or ``1``. Whether or not to associate 
      a primary secret with the account. Defaults to ``1``.
    
    * *secondary_secret_p*: ``0`` or ``1``. Whether or not to associate
      a secondary secret with the account. Defaults to ``0``.
    
    After creating the new account, this call generates secrets for it,
    and then emails the user (at *contact_email*) with their activation
    link, which contains the primary secret.
    
    This call will return :http:statuscode:`200` with info about the new
    account on success, :http:statuscode:`400` if *account_id* isn't 
    provided or isn't a valid email address, or if an account already
    exists with an id matching *account_id*.
      
    """

    account_id = request.POST.get('account_id', None)
    if not account_id or not utils.is_valid_email(account_id):
        return HttpResponseBadRequest("Account ID not valid")

    contact_email = request.POST.get('contact_email', account_id)
    if not contact_email or not utils.is_valid_email(contact_email):
        return HttpResponseBadRequest("Contact email not valid")

    new_account, create_p = Account.objects.get_or_create(
        email=urllib.unquote(account_id).lower().strip())
    if create_p:

        # generate a secondary secret or not? Requestor can say no.
        # trust model makes sense: the admin app requestor only decides whether or not
        # they control the additional interaction or if it's not necessary. They never
        # see the primary secret.

        new_account.full_name = request.POST.get('full_name', '')
        new_account.contact_email = contact_email

        new_account.creator = request.principal

        password = request.POST.get('password', None)
        primary_secret_p = (request.POST.get('primary_secret_p', "1") == "1")
        secondary_secret_p = (request.POST.get('secondary_secret_p',
                                               "0") == "1")

        # we don't allow setting the password here anymore
        new_account.save()

        if settings.DEMO_MODE:
            loader = IndivoDataLoader(request.principal)

            # Create new records for the account, populated by sample data.
            for record_label, data_profile in settings.DEMO_PROFILES.iteritems(
            ):

                # Create the record
                record = Record.objects.create(creator=request.principal,
                                               label=record_label,
                                               owner=new_account)

                try:
                    # Load the data: no transactions, as we're already managing them above
                    loader.load_profile(record,
                                        data_profile,
                                        transaction=False)
                except Exception, e:  # Something went wrong: roll everything back and fail
                    logging.exception(e)
                    raise

        if primary_secret_p:
            new_account.generate_secrets(secondary_secret_p=secondary_secret_p)
            try:
                new_account.send_secret()
            except Exception, e:
                logging.exception(e)
Example #5
0
        d.delete()
        _self.assertEqual(
            Document.objects.filter(record=_self.record).count(), 0)
    except Exception, e:
        pass

    # Use test sample_data
    _self.old_sampledata_dir = settings.SAMPLE_DATA_DIR
    settings.SAMPLE_DATA_DIR = TEST_SAMPLEDATA_DIR

    # Some common test profiles
    _self.normal_profile_dir = settings.SAMPLE_DATA_DIR + '/patient_1'
    _self.no_demo_profile_dir = settings.SAMPLE_DATA_DIR + '/nodemo'

    # create a loader for use in tests
    _self.loader = IndivoDataLoader(_self.creator)


def sampleDataTearDown(test_cases_instance):
    _self = test_cases_instance
    settings.SAMPLE_DATA_DIR = _self.old_sampledata_dir
    super(_self.__class__, _self).tearDown()


class SampleDataUnitTests(InternalTests):
    def setUp(self):
        sampleDataSetUp(self)

    def tearDown(self):
        sampleDataTearDown(self)
Example #6
0
def account_create(request):
    """ Create a new account, and send out initialization emails.
    
    ``request.POST`` holds the creation arguments. 

    In Demo Mode, this call
    automatically creates new records for the account, populated with
    sample data. See :doc:`/sample-data` for details.
    
    Required Parameters:
    
    * *account_id*: an identifier for the new address. Must be formatted
      as an email address.
    
    Optional Parameters:
    
    * *full_name*: The full name to associate with the account. Defaults
      to the empty string.
    
    * *contact_email*: A valid email at which the account holder can 
      be reached. Defaults to the *account_id* parameter.
    
    * *primary_secret_p*: ``0`` or ``1``. Whether or not to associate 
      a primary secret with the account. Defaults to ``1``.
    
    * *secondary_secret_p*: ``0`` or ``1``. Whether or not to associate
      a secondary secret with the account. Defaults to ``0``.
    
    After creating the new account, this call generates secrets for it,
    and then emails the user (at *contact_email*) with their activation
    link, which contains the primary secret.
    
    This call will return :http:statuscode:`200` with info about the new
    account on success, :http:statuscode:`400` if *account_id* isn't 
    provided or isn't a valid email address, or if an account already
    exists with an id matching *account_id*.
      
    """
    
    account_id = request.POST.get('account_id', None)
    if not account_id or not utils.is_valid_email(account_id):
        return HttpResponseBadRequest("Account ID not valid")
    
    contact_email = request.POST.get('contact_email', account_id)
    if not contact_email or not utils.is_valid_email(contact_email):
        return HttpResponseBadRequest("Contact email not valid")
    
    new_account, create_p = Account.objects.get_or_create(email=urllib.unquote(account_id).lower().strip())
    if create_p:
        
        # generate a secondary secret or not? Requestor can say no.
        # trust model makes sense: the admin app requestor only decides whether or not 
        # they control the additional interaction or if it's not necessary. They never
        # see the primary secret.
        
        new_account.full_name = request.POST.get('full_name', '')
        new_account.contact_email = contact_email
        
        new_account.creator = request.principal
        
        password            = request.POST.get('password', None)
        primary_secret_p    = (request.POST.get('primary_secret_p', "1") == "1")
        secondary_secret_p  = (request.POST.get('secondary_secret_p', "0") == "1")
        
        # we don't allow setting the password here anymore
        new_account.save()
            
        if settings.DEMO_MODE:
            loader = IndivoDataLoader(request.principal)
            
            # Create new records for the account, populated by sample data.
            for record_label, data_profile in settings.DEMO_PROFILES.iteritems():
                
                # Create the record
                record = Record.objects.create(creator=request.principal,
                                               label=record_label,
                                               owner=new_account)

                try:
                    # Load the data: no transactions, as we're already managing them above
                    loader.load_profile(record, data_profile, transaction=False)
                except Exception, e: # Something went wrong: roll everything back and fail
                    logging.exception(e)
                    raise

        if primary_secret_p:
            new_account.generate_secrets(secondary_secret_p = secondary_secret_p)
            try:
                new_account.send_secret()
            except Exception, e:
                logging.exception(e)