Beispiel #1
0
 def update(self):
     intids = practice_service_intids(self.context)
     pps = self.context.restrictedTraverse('@@plone_portal_state')
     memberid = pps.member().getId()
     dao = MemberServicesDataAccess(self.context)
     self.memberservices = dao.get_memberservices(intids, memberid)
     # grab any errors from the request, just in case we want to display
     # them later.
     self.errors = self.request.get('errors', [])
Beispiel #2
0
def get_display_items_from_order(order):
    memberid = order.userid
    orderitems = dict(
        (item.related_item.to_object, item.quantity)
        for item in order.order_items()
    )

    dao = MemberServicesDataAccess(order)
    ids = [dao.intids.getId(service) for service in orderitems.keys()]
    display_items = dao.get_memberservices(ids, memberid)
    return display_items 
Beispiel #3
0
def memberServiceAdded(obj, event):
    service = obj.related_service.to_object
    intids = queryUtility(IIntIds, context=service)
    related_service_id = intids.getId(service)
    memberid = obj.memberid
    dao = MemberServicesDataAccess(obj)
    memberservices = dao.get_memberservices([related_service_id], memberid)

    if memberservices is not None and len(memberservices) > 0:
        raise RuntimeError(
            'Only one memberservice is allowed per purchasable service.'
            'This member (%s) already has a memberservice for '
            '%s.' %(obj.userid, service.Title())
        )
Beispiel #4
0
def onOrderPaid(order, event):
    """ Once the order is paid we create the memberservices.
    """
    if event.action == 'pay':
        portal = getToolByName(order, 'portal_url').getPortalObject()
        intids = queryUtility(IIntIds, context=portal)
        dao = MemberServicesDataAccess(portal)
        # we cannot use the authenticated user since an admin user might
        # trigger the workflow.
        memberid = order.userid 

        now = datetime.datetime.now().date()
        # grab the services from the orderitems
        for item in order.order_items():
            # try to find the memberservices based on the orderitem
            # related services.
            order_related_service = item.related_item.to_object
            related_service_ids = [intids.getId(order_related_service)]

            # filter out services that don't have subject or grade set,
            # eg. discounts
            service_purchased = item.related_item.to_object
            if not (service_purchased.grade and service_purchased.subject):
                continue

            # add the service siyavula.memberservices uses for trial access
            related_service_ids.append(
                service_subject_ids[order_related_service.subject][0]
            )
            memberservices = dao.get_memberservices(
                related_service_ids, memberid
            )

            tmpservices = []

            for ms in memberservices:
                related_service = ms.related_service(order)
                if (related_service.subject == service_purchased.subject and
                    related_service.access_path == \
                        service_purchased.access_path):
                    tmpservices.append(ms)

            # create a new memberservice if it doesn't exist
            if len(memberservices) == 0:
                mstitle = '%s for %s' % (order_related_service.title, memberid)
                props = {'memberid': memberid,
                         'title': mstitle,
                         'related_service_id': intids.getId(order_related_service),
                         'expiry_date': now,
                         'service_type': order_related_service.service_type}
                ms = dao.add_memberservice(**props)
                tmpservices.append(ms)

            # update the memberservices with info from the orderitem
            for ms in tmpservices:
                # NOTE: remove this when we remove siyavula.what
                if order_related_service.service_type == 'credit':
                    credits = ms.credits
                    credits += order_related_service.amount_of_credits
                    ms.credits = credits
                elif order_related_service.service_type == 'subscription':
                    # Always use the current expiry date if it is greater than
                    # 'now', since that gives the user everything he paid for.
                    # Only use 'now' if the related_service has already expired, so we
                    # don't give the user more than he paid for.
                    if now > ms.expiry_date:
                        ms.expiry_date = now
                    expiry_date = ms.expiry_date + datetime.timedelta(
                        order_related_service.subscription_period
                    )
                    ms.expiry_date = expiry_date
                dao.update_memberservice(ms)
            
            # if we have specific access groups add the user to those here.
            access_group = order_related_service.access_group
            if access_group:
                gt = getToolByName(order, 'portal_groups')
                # now add the member to the correct group
                gt.addPrincipalToGroup(memberid, access_group)
Beispiel #5
0
class TestPracticeBrowserView(unittest.TestCase):
    
    layer = INTEGRATION_TESTING

    def setUp(self):
        super(TestPracticeBrowserView, self).setUp()
        self.portal = self.layer['portal']
        settings = queryUtility(IRegistry).forInterface(IEmasSettings)
        settings.practiceurl = u'http://*****:*****@@practice')
        self.assertRaises(Unauthorized, view,
                         'The system should force a login.')

    def test_logged_in_memberservices_expired(self):
        self.deactivate_services()

        view = self.portal.restrictedTraverse('maths/@@practice')
        self.update_request(view) 
        result = view()

        self.assertGreater(len(result), 0,
                           'No result found.')
        self.assertEqual(view.services_active(), False,
                         'The service should not be active.')
        doc = lxml.html.fromstring(result)
        elements = doc.xpath('//a[contains(.,"orders")]')
        self.assertGreater(len(elements), 0,
                           'No order links found.')

    def test_logged_in_memberservices_active(self):
        view = self.portal.restrictedTraverse('maths/@@practice')
        self.update_request(view)
        result = view()

        self.assertEqual(view.services_active(), True,
                         'The service should be active.')
        doc = lxml.html.fromstring(result)
        elements = doc.xpath('//a[contains(.,"orders")]')
        self.assertEqual(len(elements), 0,
                           'There should be no order links.')
    
    def test_logged_in_grade10_maths(self):
        view = self.portal.restrictedTraverse('maths/@@practice')
        self.update_request(view)
        result = view()

        assert 'maths-grade-10' in view.accessto, \
               'Grade 10 Maths service not in memberservices.'
    
    def test_practice_service_info_on_first_login(self):
        portal_state = self.portal.restrictedTraverse('@@plone_portal_state')
        member = portal_state.member()
        default = DateTime('2000/01/01')
        member.setProperties(login_time=default,
                             last_login_time=default)

        view = self.portal.restrictedTraverse('maths/@@practice')
        self.update_request(view)
        # The call to loginUser is the one that fires the required event.
        # The testcase's login method simly creates a new security manager, it
        # does not lead to the IUserLoggedInEvent. 
        self.portal.portal_membership.loginUser(view.request)
        result = view()
        doc = lxml.html.fromstring(result)
        elements = doc.xpath('//a[contains(.,"@@practice")]')
        self.assertEqual(len(elements), 0,
                         'No link to @@practice found.')
    
    def test_expiry_warning(self):
        """
        Memberservice are only valid for 30 days and the expiry warning for
        monthly services is 7 days before expiry. So they should not show now.
        """
        view = self.portal.restrictedTraverse('maths/@@practice')
        self.update_request(view)
        result = view()
        self.assertEqual(view.show_expirywarning(), False,
                         'Trial service should all expire withing NUM_DAYS days.')

    def test_expiry_warning_for_manager(self):
        self.setRoles(('Manager',))
        view = self.portal.restrictedTraverse('maths/@@practice')
        self.update_request(view)
        result = view()
        self.assertEqual(view.show_expirywarning(), False,
                         "We don't show expiry warnings to manager users.")

    def test_get_services(self):
        view = self.portal.restrictedTraverse('maths/@@practice')
        self.update_request(view)
        result = view()

        self.assertEqual(len(view.memberservices), NUM_SERVICES,
                         'There should be %s practice memberservices.')

        for ms in view.memberservices:
            assert (IMemberService.providedBy(ms),
                'The memberservice does not provide the correct interface.')

        for service in view.practice_services:
            assert (IService.providedBy(service),
                'The practice_service does not provide the correct interface.')

    def test_get_services_with_empty_access_path(self):
        view = self.portal.restrictedTraverse('maths/@@practice')
        memberservices, services = view.get_services(self.portal)

        self.assertEqual(len(memberservices), NUM_SERVICES,
                         'There should be %s memberservices.' % NUM_SERVICES)

        self.assertEqual(len(services), NUM_SERVICES,
                         'There should be %s practice services.' % NUM_SERVICES)

        self.clear_access_path()

        memberservices, services = view.get_services(self.portal)

        self.assertEqual(len(memberservices), 0,
                         'There should be 0 memberservices.')

        self.assertEqual(len(services), 0,
                         'There should be 0 practice services.')
    
    def test_filtered_services_for_maths_grade10(self):
        view = self.portal.restrictedTraverse('maths/@@practice')
        self.update_request(view)
        view()

        memberservices, services = view.get_services(self.portal)
        filteredservices = view.filtered(memberservices, 'maths', 'grade-10')
        
        self.assertEqual(len(filteredservices), 1,
                         'There can be onle one!')

    def test_filtered_services_for_maths(self):
        view = self.portal.restrictedTraverse('maths/@@practice')
        self.update_request(view)
        view()

        memberservices, services = view.get_services(self.portal)
        filteredservices = view.filtered(memberservices, 'maths', None)
        
        self.assertEqual(len(filteredservices), 3,
                         'We should find 3  member services for maths.')

    def test_filtered_services_for_None_subject_None_grade(self):
        view = self.portal.restrictedTraverse('maths/@@practice')
        self.update_request(view)
        view()

        memberservices, services = view.get_services(self.portal)
        filteredservices = view.filtered(memberservices, None, None)
        self.assertEqual(len(filteredservices), 6,
                         'We should find 6 member services.')

    def test_manager_user_access(self):
        self.setRoles(('Manager',))
        self.deactivate_services()
        view = self.portal.maths.restrictedTraverse('maths/@@practice')
        self.update_request(view)
        view()
        
        self.assertEqual(len(view.memberservices), 0,
                         'All member services should be expired.')

        self.assertEqual(len(view.accessto.split(',')), NUM_SERVICES,
                         '"accesto" should be %s long.' % NUM_SERVICES)
        
        self.assertEqual(view.services_active(), True,
                         'Manager always has access.')
    
    def test_days_to_expiry_date_calculation(self):
        view = self.portal.restrictedTraverse('maths/@@practice')
        self.update_request(view)
        view()
        
        numdays = Practice.NUM_DAYS
        for offset in [0, 10, 10]:
            numdays = numdays - offset
            print 'Testing %s days.' % numdays
            self.change_service_expiry_date(view, offset)
            self.assertEqual(view.get_days_to_expiry_date(), numdays,
                             'Expiry date should be %s days away.' % numdays)
    
    def test_subject_and_grade_computing_no_subject_no_grade(self):
        view = self.portal.restrictedTraverse('maths/@@practice')
        view()
        self.assertEqual(view.get_days_to_expiry_date(), Practice.NUM_DAYS,
                         'Expiry date should be %s days away.' % Practice.NUM_DAYS)

    def clear_access_path(self):
        memberservices = self.portal._getOb('memberservices')
        for ms in memberservices.objectValues():
            ms.related_service.to_object.access_path = u''

    def change_service_expiry_date(self, view, days):
        for service in view.memberservices:
            newdate = service.expiry_date - timedelta(days)
            service.expiry_date = newdate
            service.reindexObject(idxs=['expiry_date'])

    def deactivate_services(self):
        service_uids = [self.intids.getId(service) \
                        for service in self.services.objectValues()]
        memberservices = self.dao.get_memberservices(service_uids, TEST_USER_ID)
        expired = date(1970, 01, 01)
        for service in memberservices:
            service.expiry_date = expired
            self.dao.update_memberservice(service)

    def update_request(self, view):
        cookie = ''
        path = 'maths/@@practice/grade-10'
        host = 'localhost:8080'
        serverurl = 'http://%s' % host
        referer = '%s%s' % (serverurl, path)
        props = {'REQUEST_METHOD':    'GET',
                 'PATH_INFO':          path,
                 'HTTP_HOST':          host,
                 'HTTP_COOKIE':        cookie,
                 'HTTP_REFERER':       referer,
                 'SERVER_URL':         serverurl,
                 'CONTENT_LENGTH':     '0',
                 'GATEWAY_INTERFACE':  'TestFooInterface/1.0',}
        
        view.request.environ.update(props)
        return view.request
class TestMemberServicesIntegration(unittest.TestCase):
    """Unit test for the MemberService type
    """

    layer = INTEGRATION_TESTING

    def setUp(self):
        self.portal = self.layer['portal']
        self.services = self.portal._getOb('products_and_services')
        self.intids = queryUtility(IIntIds, context=self.portal)
        self.dao = MemberServicesDataAccess(self.portal)
        setRoles(self.portal, TEST_USER_ID, ['Member'])
        self.session = SESSION()

    def tearDown(self):
        count = self.session.query(MemberService).filter(
            MemberService.memberid == TEST_USER_ID
        ).delete()
        print 'Deleted %s' % count
        transaction.commit()
    
    def test_get_all_memberservices(self):
        db_ms = self.session.query(MemberService).all()
        all_ms = self.dao.get_all_memberservices()
        self.assertEquals(len(db_ms), len(all_ms))
        self.assertEquals(all_ms, db_ms)

    def test_get_memberservices(self):
        service = self.services.objectValues()[0]
        ms_args = self.get_ms_args(service, TEST_USER_ID)
        ms1 = self.dao.add_memberservice(**ms_args) 
        self.session.flush()
        ms1_db = self.get_memberservice(ms1.id)
        service_uids = [self.intids.getId(service),]
        memberid = TEST_USER_ID
        memberservices = \
            self.dao.get_memberservices(service_uids, memberid)
        self.assertEquals(memberservices, [ms1_db])

    def test_get_memberservices_by_subject(self):
        maths_services = \
            [s for s in self.services.objectValues() if 'maths' in s.subject]
        for count in range(0,3):
            service = maths_services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        science_services = \
            [s for s in self.services.objectValues() if 'science' in s.subject]
        for count in range(0,3):
            service = science_services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        memberservices = \
            self.dao.get_memberservices_by_subject(TEST_USER_ID, 'maths')
        self.assertEqual(len(memberservices), 3)
        memberservices = \
            self.dao.get_memberservices_by_subject(TEST_USER_ID, 'science')
        self.assertEqual(len(memberservices), 3)

    def test_get_memberservices_by_grade(self):
        grade_10_services = \
            [s for s in self.services.objectValues() if s.grade == 'grade-10']
        for count in range(0,3):
            service = grade_10_services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        memberservices = \
            self.dao.get_memberservices_by_grade(TEST_USER_ID, 'grade-10')
        self.assertEqual(len(memberservices), 3)

    def test_get_memberservices_by_subject_and_grade(self):
        grade_10_maths_services = \
            [s for s in self.services.objectValues() if s.grade == 'grade-10']
        grade_10_maths_services = \
            [s for s in grade_10_maths_services if 'maths' in s.subject]
        for count in range(0,3):
            service = grade_10_maths_services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        memberservices = \
            self.dao.get_memberservices_by_subject_and_grade(TEST_USER_ID,
                                                             'maths',
                                                             'grade-10')
        self.assertEqual(len(memberservices), 3)

        memberservices = \
            self.dao.get_memberservices_by_subject_and_grade(TEST_USER_ID,
                                                             'science',
                                                             'grade-10')
        self.assertEqual(len(memberservices), 0)

    def test_get_active_memberservices(self):
        for count in range(0,3):
            service = self.services.objectValues()[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            self.dao.add_memberservice(**ms_args)
        self.session.flush()
        
        now = datetime.now().date()
        td = timedelta(1)
        yesterday = now - td
        for count in range(3,6):
            service = self.services.objectValues()[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            ms_args['expiry_date'] = yesterday
            self.dao.add_memberservice(**ms_args)
        self.session.flush()
        
        active_memberservices = self.dao.get_active_memberservices(TEST_USER_ID)
        self.assertEquals(len(active_memberservices), 3)
        for ms in active_memberservices:
            self.assertEquals(ms.expiry_date, now)

    def test_get_active_memberservices_by_subject(self):
        now = datetime.now().date()
        td = timedelta(1)
        yesterday = now - td
        services = \
            [s for s in self.services.objectValues() if 'maths' in s.subject]
        for count in range(0,3):
            service = services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            ms_args['expiry_date'] = now
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        services = \
            [s for s in self.services.objectValues() if 'science' in s.subject]
        for count in range(0,3):
            service = services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            ms_args['expiry_date'] = yesterday
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        memberservices = \
            self.dao.get_active_memberservices_by_subject(TEST_USER_ID, 'maths')
        self.assertEqual(len(memberservices), 3)
        for ms in memberservices:
            self.assertEquals(ms.expiry_date, now)

    def test_get_active_memberservices_by_grade(self):
        now = datetime.now().date()
        td = timedelta(1)
        yesterday = now - td

        services = \
            [s for s in self.services.objectValues() if s.grade == 'grade-10']
        for count in range(0,3):
            service = services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            ms_args['expiry_date'] = now
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        services = \
            [s for s in self.services.objectValues() if s.grade == 'grade-11']
        for count in range(0,3):
            service = services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            ms_args['expiry_date'] = yesterday
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        memberservices = \
            self.dao.get_active_memberservices_by_grade(TEST_USER_ID, 'grade-10')
        self.assertEqual(len(memberservices), 3)
        for ms in memberservices:
            self.assertEquals(ms.expiry_date, now)

    def test_get_active_memberservices_by_subject_and_grade(self):
        now = datetime.now().date()
        td = timedelta(1)
        yesterday = now - td

        grade_10_maths_services = \
            [s for s in self.services.objectValues() if \
             s.grade == 'grade-10' and 'maths' in s.subject]
        for count in range(0,3):
            service = grade_10_maths_services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            ms_args['expiry_date'] = now
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        grade_10_science_services = \
            [s for s in self.services.objectValues() if \
             s.grade == 'grade-10' and 'science' in s.subject]
        for count in range(0,3):
            service = grade_10_science_services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            ms_args['expiry_date'] = yesterday
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        memberservices = \
            self.dao.get_memberservices_by_subject_and_grade(TEST_USER_ID,
                                                             'maths',
                                                             'grade-10')
        self.assertEqual(len(memberservices), 3)
        for ms in memberservices:
            self.assertEquals(ms.expiry_date, now)

    def test_get_expired_memberservices(self):
        now = datetime.now().date()
        for count in range(0,3):
            service = self.services.objectValues()[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            ms_args['expiry_date'] = now
            self.dao.add_memberservice(**ms_args)
        self.session.flush()
        
        td = timedelta(1)
        yesterday = now - td
        for count in range(3,6):
            service = self.services.objectValues()[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            ms_args['expiry_date'] = yesterday
            self.dao.add_memberservice(**ms_args)
        self.session.flush()
        
        expired_memberservices = self.dao.get_expired_memberservices(TEST_USER_ID)
        self.assertEquals(len(expired_memberservices), 3)
        for ms in expired_memberservices:
            self.assertEquals(ms.expiry_date, yesterday)

    def test_get_expired_memberservices_by_subject(self):
        now = datetime.now().date()
        td = timedelta(1)
        yesterday = now - td
        services = \
            [s for s in self.services.objectValues() if 'maths' in s.subject]
        for count in range(0,3):
            service = services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            ms_args['expiry_date'] = now
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        services = \
            [s for s in self.services.objectValues() if 'science' in s.subject]
        for count in range(0,3):
            service = services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            ms_args['expiry_date'] = yesterday
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        memberservices = \
            self.dao.get_expired_memberservices_by_subject(TEST_USER_ID, 'science')
        self.assertEqual(len(memberservices), 3)
        for ms in memberservices:
            self.assertEquals(ms.expiry_date, yesterday)

    def test_get_expired_memberservices_by_grade(self):
        now = datetime.now().date()
        td = timedelta(1)
        yesterday = now - td

        services = \
            [s for s in self.services.objectValues() if s.grade == 'grade-10']
        for count in range(0,3):
            service = services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            ms_args['expiry_date'] = now
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        services = \
            [s for s in self.services.objectValues() if s.grade == 'grade-11']
        for count in range(0,3):
            service = services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            ms_args['expiry_date'] = yesterday
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        memberservices = \
            self.dao.get_expired_memberservices_by_grade(TEST_USER_ID,
                                                         'grade-11')
        self.assertEqual(len(memberservices), 3)
        for ms in memberservices:
            self.assertEquals(ms.expiry_date, yesterday)

    def test_get_expired_memberservices_by_subject_and_grade(self):
        now = datetime.now().date()
        td = timedelta(1)
        yesterday = now - td

        grade_10_maths_services = \
            [s for s in self.services.objectValues() if \
             s.grade == 'grade-10' and 'maths' in s.subject]
        for count in range(0,3):
            service = grade_10_maths_services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            ms_args['expiry_date'] = now
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        grade_10_science_services = \
            [s for s in self.services.objectValues() if \
             s.grade == 'grade-10' and 'science' in s.subject]
        for count in range(0,3):
            service = grade_10_science_services[count]
            ms_args = self.get_ms_args(service, TEST_USER_ID)
            ms_args['expiry_date'] = yesterday
            self.dao.add_memberservice(**ms_args)
        self.session.flush()

        memberservices = \
            self.dao.get_expired_memberservices_by_subject_and_grade(
                TEST_USER_ID, 'science', 'grade-10')
        self.assertEqual(len(memberservices), 3)
        for ms in memberservices:
            self.assertEquals(ms.expiry_date, yesterday)

    def test_get_memberservice_by_primary_key(self):
        service = self.services.objectValues()[0]
        ms_args = self.get_ms_args(service, TEST_USER_ID)
        ms1 = self.dao.add_memberservice(**ms_args) 
        self.session.flush()
        ms1_db = self.dao.get_memberservice_by_primary_key(ms1.id)
        self.assertEquals(ms1_db, self.get_memberservice(ms1.id))

    def test_add_memberservice(self):
        service = self.services.objectValues()[0]
        ms_args = self.get_ms_args(service, TEST_USER_ID)
        ms1 = self.dao.add_memberservice(**ms_args) 
        self.session.flush()
        ms1_db = self.get_memberservice(ms1.id)
        self.failUnless(IMemberService.providedBy(ms1_db))

    def test_adding_duplicate_memberservices(self):
        service = self.services.objectValues()[0]
        ms_args = self.get_ms_args(service, TEST_USER_ID)
        ms1 = self.dao.add_memberservice(**ms_args) 
        ms2 = self.dao.add_memberservice(**ms_args) 
        self.session.flush()
        ms1_db = self.get_memberservice(ms1.id)
        ms2_db = self.get_memberservice(ms2.id)
        self.failUnless(IMemberService.providedBy(ms1_db))

    def test_update_memberservice(self):
        service = self.services.objectValues()[0]
        ms_args = self.get_ms_args(service, TEST_USER_ID)
        ms1 = self.dao.add_memberservice(**ms_args) 
        self.session.flush()
        ms1_db = self.get_memberservice(ms1.id)
        ms1_db.title = 'new title'
        self.dao.update_memberservice(ms1_db)
        self.session.flush()
        ms1_db = self.get_memberservice(ms1.id)
        self.assertEquals(ms1_db.title, 'new title')

    def test_delete_memberservice(self):
        service = self.services.objectValues()[0]
        ms_args = self.get_ms_args(service, TEST_USER_ID)
        ms1 = self.dao.add_memberservice(**ms_args) 
        self.session.flush()
        ms1_db = self.get_memberservice(ms1.id)
        self.dao.delete_memberservice(ms1_db)
        ms1_db = self.get_memberservice(ms1.id)
        self.assertEquals(ms1_db, None)

    def test_related_service(self):
        service = self.services.objectValues()[0]
        ms_args = self.get_ms_args(service, TEST_USER_ID)
        ms1 = self.dao.add_memberservice(**ms_args) 
        self.session.flush()
        ms1_db = self.get_memberservice(ms1.id)
        self.failUnless(self.dao.related_service(ms1_db), service)

    def test_fti(self):
        fti = queryUtility(IDexterityFTI, name='emas.app.memberservice')
        self.assertEquals(None, fti)

    def test_schema(self):
        fti = queryUtility(IDexterityFTI, name='emas.app.memberservice')
        schema = fti.lookupSchema()
        self.assertEquals(None, schema)

    def test_factory(self):
        fti = queryUtility(IDexterityFTI, name='emas.app.memberservice')
        factory = fti.factory
        self.assertEquals(factory, None)
    
    def get_memberservice(self, memberservice_id):
        memberservices = self.session.query(MemberService).filter_by(
            id = memberservice_id).all()
        return memberservices and memberservices[0] or None

    def create_services(self):
        subjects = ['maths', 'science']
        grades = ['10', '11', '12']
        for subject in subjects:
            for count, grade in grades:
                props = {'service_type' : 'subscription',
                         'grade'        : grade,
                         'subject'      : subject,
                         'price'        : '111'}
                s_id = self.services.invokeFactory('emas.app.service',
                                                 '%s-%s' % (subject, grade),
                                                 **props)
                service = self.services._getOb(s_id)
                service.subject = props['subject']

    def get_ms_args(self, service, memberid):
        ms_args = {
            'memberid': memberid,
            'title': '%s for %s' % (service.title, memberid),
            'related_service_id': self.intids.getId(service),
            'expiry_date': datetime.now(),
        }
        return ms_args
Beispiel #7
0
    def update(self):
        pps = self.context.restrictedTraverse('@@plone_portal_state')
        self.products_and_services = pps.portal()._getOb('products_and_services')
        self.navroot = pps.navigation_root()
        self.base_url = 'http://billing.internal.mxit.com/Transaction/PaymentRequest'
        self.action = self.context.absolute_url() + '/@@mxitpaymentrequest'

        pmt = getToolByName(self.context, 'portal_membership')
        memberid = member_id(
            self.request.get(USER_ID_TOKEN.lower(),
                             self.request.get(USER_ID_TOKEN)
                            )
        )
        if not memberid:
            raise ValueError('No member id supplied.')
        member = pmt.getMemberById(memberid)
        if not member:
            password = password_hash(self.context, memberid)
            pmt.addMember(memberid, password, 'Member', '')
            member = pmt.getMemberById(memberid)
            # login as new member
            newSecurityManager(self.request, member)

        registry = queryUtility(IRegistry)
        self.emas_settings = registry.forInterface(IEmasSettings)
        self.transaction_reference = self.emas_settings.order_sequence_number +1
        self.emas_settings.order_sequence_number = self.transaction_reference
        self.transaction_reference = '%04d' % self.transaction_reference

        self.product_id = self.request.get('productId')
        try:
            self.product = self.products_and_services._getOb(self.product_id)
        except AttributeError:
            raise AttributeError(
                'Product with id %s not found' % self.product_id
                )
            
        self.product_name = self.product.title
        self.product_description = self.product.description
        
        # create an order for this member : product combination
        if self.product:
            portal = pps.portal()
            member_orders = portal['orders']

            props = {'id'            : self.transaction_reference, 
                     'title'         : self.transaction_reference,
                     'userid'        : memberid,
                     'payment_method': MOOLA,
                     }
            self.order= createContentInContainer(
                member_orders,
                'emas.app.order',
                False,
                **props
            )
            self.order = member_orders._getOb(self.transaction_reference)
            self.order.verification_code = generate_verification_code(self.order)
            self.order.manage_setLocalRoles(self.order.userid, ('Owner',))
                
            relation = create_relation(self.product.getPhysicalPath())
            item_id = self.order.generateUniqueId(type_name='orderitem')
            props = {'id'           : item_id,
                     'title'        : item_id,
                     'related_item' : relation,
                     'quantity'     : 1}
            order_item = createContentInContainer(
                self.order,
                'emas.app.orderitem',
                False,
                **props
            )
            order_item.manage_setLocalRoles(self.order.userid, ('Owner',))

            self.callback_url = \
                '%s/mxitpaymentresponse?productId=%s&order_number=%s&verification_code=%s' %(
                    self.context.absolute_url(),
                    self.product_id,
                    self.order.getId(),
                    self.order.verification_code
            )

        self.cost_settings = registry.forInterface(IEmasServiceCost)
        self.vendor_id = self.cost_settings.MXitVendorId
        self.moola_amount = self.product.amount_of_moola
        self.currency_amount = self.product.price
        url = '%s/%s' %(self.navroot.absolute_url(), self.product.access_path)
        
        # get all active services for this user
        service_intids = practice_service_intids(self.context)
        dao = MemberServicesDataAccess(self.context)
        memberservices = dao.get_memberservices(service_intids, memberid)
        active_services = [m.related_service(self.context) for m in memberservices]

        # check if the currently requested one is in the list
        if self.product in active_services:
            return self.request.response.redirect(url)
        else:
            return self.render()