Пример #1
0
 def testRegistry(self):
     """Tests the registration functionality."""
     # not registered at first
     self.assertFalse(xmlrouter.is_registered(inc_xmlns, increment))
     xmlrouter.register(inc_xmlns, increment)
     # now it is
     self.assertTrue(xmlrouter.is_registered(inc_xmlns, increment))
     # but not with other namespaces
     self.assertFalse(xmlrouter.is_registered(dec_xmlns, increment))
     # or other methods
     self.assertFalse(xmlrouter.is_registered(inc_xmlns, noop))
     # unless it is registered
     xmlrouter.register(inc_xmlns, noop)
     # in which case both should work
     self.assertTrue(xmlrouter.is_registered(inc_xmlns, increment))
     self.assertTrue(xmlrouter.is_registered(inc_xmlns, noop))
     # but others still shouldn't
     self.assertFalse(xmlrouter.is_registered(inc_xmlns, decrement))
Пример #2
0
            return self.recipient_user.domain
        return None
           
    def __unicode__(self):
        return unicode(self.name + " - " + self.report_frequency)
        
class BlacklistedUser(models.Model):
    '''Model for a blacklisted user.  Blacklisted users should be excluded from 
       most views of the data, including, but not limited to, charts, reports,
       submission logs(?), data/tabular views, etc.'''
    # this is a temporary solution until we get real reporters for everyone 
    # we care about.
    domains = models.ManyToManyField(Domain)
    # could use reporters here, but this will keep things simple, which is 
    # desirable for a short-term solution
    username = models.CharField(max_length=64)
    # allow temporary enabling/disabling of blacklist at a global level.
    active = models.BooleanField(default=True)

    def __unicode__(self):
        return "%s in %s" %\
                  (self.username, 
                   ",".join([domain.name for domain in self.domains.all()]))
                  
                  
# register our registration method, like a signal, in the models file
# to make sure this always gets bootstrapped.  
# TODO: it's unclear what app reg should belong to, for now stick it in
# the blanket "hq"
xmlrouter.register(REGISTRATION_XMLNS, create_phone_user)
Пример #3
0
from backups.processor import create_backup, BACKUP_XMLNS

class BackupUser(models.Model):
    """Someone who backs things up.  They have a string identifier 
       and a domain."""
    # Note: really these should be CHWs but until we have a working
    # registration workflow we'll just make them these arbitrary 
    # things
    domain = models.ForeignKey(Domain, related_name="backup_users")
    username = models.CharField(max_length=32)
    
    def __unicode__(self):
        return self.username


class Backup(models.Model):
    """An instance of a commcare backup.  Points to a specific device
       as well as a set of users.  Additionally, has information about
       the original attachment that created the backup"""
    attachment = models.ForeignKey(Attachment)    
    device_id = models.CharField(max_length="32")
    users = models.ManyToManyField(BackupUser, null=True, blank=True)
    
    def __unicode__(self):
        return "Id: %s, Device: %s, Users: %s" % (self.id, self.device_id,
                                                  self.users.count())
    
# register our backup method, like a signal, in the models file
# to make sure this always gets bootstrapped.
xmlrouter.register(BACKUP_XMLNS, create_backup)
Пример #4
0
    def testRouting(self):
        """Tests the creation of a form group from a single form."""
        # nothing registered.  assure that nothing happens
        self.assertEqual(0, counter)
        xmlrouter.process(attachment, inc_xmlns, 0)
        self.assertEqual(0, counter)
        xmlrouter.process(attachment, dec_xmlns, 0)
        self.assertEqual(0, counter)
        xmlrouter.process(attachment, unused_xmlns, 0)
        self.assertEqual(0, counter)

        # register the incrementer and the decrementer
        xmlrouter.register(inc_xmlns, increment)
        xmlrouter.register(dec_xmlns, decrement)

        # make sure we are incrementing correctly
        xmlrouter.process(attachment, inc_xmlns, 0)
        self.assertEqual(1, counter)
        xmlrouter.process(attachment, inc_xmlns, 0)
        self.assertEqual(2, counter)

        # and decrementing correctly
        xmlrouter.process(attachment, dec_xmlns, 0)
        self.assertEqual(1, counter)

        # and ignoring what should be ignored
        xmlrouter.process(attachment, unused_xmlns, 0)
        self.assertEqual(1, counter)

        # reregistering should not "double up"
        xmlrouter.register(inc_xmlns, increment)
        xmlrouter.register(inc_xmlns, increment)
        xmlrouter.register(inc_xmlns, increment)
        xmlrouter.register(dec_xmlns, decrement)
        xmlrouter.process(attachment, inc_xmlns, 0)
        self.assertEqual(2, counter)
        xmlrouter.process(attachment, dec_xmlns, 0)
        self.assertEqual(1, counter)

        # but registering with a second method should
        xmlrouter.register(inc_xmlns, increment_again)
        xmlrouter.process(attachment, inc_xmlns, 0)
        self.assertEqual(3, counter)
        xmlrouter.process(attachment, inc_xmlns, 0)
        self.assertEqual(5, counter)
        xmlrouter.process(attachment, dec_xmlns, 0)
        self.assertEqual(4, counter)
Пример #5
0
    def __unicode__(self):
        return "Id: %s, Device: %s, Users: %s" % (self.id, self.phone,
                                                  self.device.users.count())


def create_phone_and_user(sender, instance, created, **kwargs):
    """
    Create a phone from a metadata submission if its a device we've
    not seen.
    """
    
    if not created:             return
    if not instance.deviceid:   return
    
    phone = Phone.objects.get_or_create\
                (device_id = instance.deviceid,
                 domain = instance.attachment.submission.domain)[0]
    
    try:
        PhoneUserInfo.objects.get(phone=phone, username="******" % instance.username)
    except PhoneUserInfo.DoesNotExist:
        # create it if we couldn't find it 
        PhoneUserInfo.objects.create(phone=phone,username="******" % instance.username,
                                     status="auto_created")

post_save.connect(create_phone_and_user, sender=Metadata)
    
# register our backup and registration methods, like a signal, 
# in the models file to make sure this always gets bootstrapped.
xmlrouter.register(BACKUP_XMLNS, create_backup)
xmlrouter.register(REGISTRATION_XMLNS, create_phone_user)