コード例 #1
0
ファイル: models.py プロジェクト: praekelt/jmbo-neo
def load_consumer(sender, *args, **kwargs):
    instance = kwargs['instance']
    # if the object being instantiated has a pk, i.e. has been saved to the db
    if instance.id:
        cache_key = 'neo_consumer_%s' % instance.id
        if USE_MCAL:
            '''
            All member fields are in our database
            '''
            if not cache.has_key(cache_key):
                member_dict = dict((k, getattr(instance, k)) for k in NEO_ATTR.union(ADDRESS_FIELDS))
                cache.set(cache_key, member_dict, 1200)
        else:
            '''
            Members with a corresponding consumer in CIDB
            won't have all fields stored in our database
            '''
            try:
                member_dict = cache.get(cache_key, None)
                if not member_dict:
                    neoprofile = instance.neoprofile
                    if neoprofile:
                         # retrieve consumer from Neo
                        consumer = api.get_consumer(instance.neoprofile.consumer_id)
                        wrapper = ConsumerWrapper(consumer=consumer)
                        member_dict = dict((k, getattr(wrapper, k)) for k in NEO_ATTR)
                        member_dict.update(wrapper.address)
                # update instance with Neo attributes
                if member_dict:
                    for key, val in member_dict.iteritems():
                        setattr(instance, key, val)

            except NeoProfile.DoesNotExist:
                pass
コード例 #2
0
ファイル: tests.py プロジェクト: praekelt/jmbo-neo
    def test_update_member(self):
        member = self.create_member()
        new_dob = timezone.now().date() - timedelta(days=24 * 365)
        new_country = Country.objects.create(
            title="South Africa",
            slug="south-africa",
            country_code="ZA",
        )
        new_gender = 'M'
        # change the member attributes
        for key, val in self.member_attrs.iteritems():
            if key == 'dob':
                new_val = new_dob
            elif key == 'country':
                new_val = new_country
            elif key == 'gender':
                new_val = new_gender
            else:
                new_val = "new_" + val
            setattr(member, key, new_val)
        member.save()
        if settings.NEO.get('USE_MCAL', False):
            consumer = api.get_consumer(member.neoprofile.consumer_id, username=member.username, password=member.neoprofile.password)
            wrapper = ConsumerWrapper(consumer=consumer)
            fields_to_check = self.member_attrs.copy()
            for key in ('address', 'city', 'province', 'zipcode'):
                del fields_to_check[key]
            for field, val in fields_to_check.iteritems():
                new_attr = getattr(wrapper, field)
                if isinstance(new_attr, dict):
                    for k, v in new_attr.iteritems():
                        self.assertEqual(getattr(member, k), v)
                else:
                    self.assertEqual(getattr(member, field), new_attr)

        else:
            cache.clear()
            # retrieve the member from db + Neo
            member = Member.objects.all()[0]
            # check that updated values had been stored on Neo
            for key, val in self.member_attrs.iteritems():
                if key == 'dob':
                    new_val = new_dob
                elif key == 'country':
                    new_val = new_country
                elif key == 'gender':
                    new_val = new_gender
                else:
                    new_val = "new_" + val
                self.assertEqual(getattr(member, key), new_val)
コード例 #3
0
ファイル: backends.py プロジェクト: pjdelport/jmbo-neo
    def authenticate(self, username=None, password=None):
        obj = None

        for klass, fieldnames in self._authentication_chain:
            for fieldname in fieldnames:
                try:
                    obj = klass.objects.get(**{fieldname: username})
                except klass.DoesNotExist:
                    pass
                else:
                    break
            if obj is not None:
                break

        if obj is None:
            # try to log in via Neo
            consumer_id = authenticate_neo(username, password)
            if consumer_id:
                # create the member using data from Neo
                consumer = api.get_consumer(consumer_id, username, password)
                wrapper = ConsumerWrapper(consumer=consumer)
                attrs = dict((k, getattr(wrapper, k)) for k in NEO_ATTR)
                attrs.update(wrapper.address)
                member = Member(**attrs)
                # don't want save method to attempt to create a consumer
                member.need_to_clean_member = False
                member.consumer_id = consumer_id
                member.save()
                return member
            return None

        # Obj is an instance of either user or a subclass of user, or else a
        # profile.
        if isinstance(obj, User):
            user = obj
        else:
            user = obj.user

        # Authenticate via Neo instead of Django
        consumer_id = authenticate_neo(user.username, password)
        if consumer_id:
            assert NeoProfile.objects.get(consumer_id=consumer_id).user.id == user.id
            return user

        return None
コード例 #4
0
ファイル: models.py プロジェクト: ontiyonke/jmbo-neo
def load_consumer(sender, *args, **kwargs):
    instance = kwargs['instance']
    # if the object being instantiated has a pk, i.e. has been saved to the db
    if instance.id:
        pk = instance.id
        cache_key = 'neo_consumer_%s' % pk
        member = cache.get(cache_key, None)
        if member is None:
            consumer_id = NeoProfile.objects.get(user=pk).consumer_id
            # retrieve consumer from Neo
            consumer = api.get_consumer(consumer_id)
            wrapper = ConsumerWrapper(consumer=consumer)
            member=dict((k, getattr(wrapper, k)) for k in NEO_ATTR)
            member.update(wrapper.address) # special case
            # cache the neo member dictionary
            cache.set(cache_key, member, 1200)

        # update instance with Neo attributes
        for key, val in member.iteritems():
            setattr(instance, key, val)