Esempio n. 1
0
def admin_release_lead(request):
    if request.method == 'POST':
        lead_id = int(request.POST['lead_id'])
        wm = WorkManager.instance()
        wm.unlockLead(lead_id, request.user)
        return redirect('/locked_leads')
    else:
        logger.warning('GET /release_lead when POST is expected')
Esempio n. 2
0
def click_logout(request):
    wm = WorkManager.instance()
    if 'workitem' in request.session:
        wi = request.session['workitem']
        del request.session['workitem']
        wm.releaseCurrentWorkItem(wi)
    try:
        wm.signOut(request.user)
    except Exception, msg:
        logger.error(msg)
Esempio n. 3
0
def next_workitem(request):
    """Responds with lead and appropriate offers"""
    wm = WorkManager.instance()
    logger.info('wm instance %s' % wm)
    wi = None
    for attempt in range(1, 3):
        try:
            logger.info('Getting work item')
            if 'workitem' not in request.session:
                wi = wm.nextWorkItem(request.user)
                request.session['workitem'] = wi
            else:
                wi = request.session['workitem']
                if not wm.validateWorkItem(wi):
                    wi = wm.nextWorkItem(request.user)
                    request.session['workitem'] = wi
                    request.session['msg'] = 'Your previous work item was cancelled by administrator. Start with next.'
            logger.info('User %s: Found offers in views: %d' % (request.user, len(wi.offers)))
            TrafficHolder().processOffers(wi.offers)
            msg = None
            if 'msg' in request.session:
                msg = request.session['msg']
                del request.session['msg']

            return render(request, 'worker/showlead.html',
                                      {
                                          'randomMessage': '',
                                          'user': request.user, 'wi': wi,
                                          'message': msg,
                                          'remaining_leads': wi.get_remaining_leads()
                                      })
        except NoWorkException as exception:
            return render(request, 'worker/worker_goodbye.html',
                                      {'user': request.user,
                                       'message': str(exception)
                                      })
        except WorkInterceptedException:
            logger.warning('User %s got intercepted work exception' % request.user)
            continue
        except WorkerProfileDoesNotExistException:
            logger.error('User %s got unrecoverable error in getting next work item' % request.user)
            raise Http404
Esempio n. 4
0
def submit_workitem(request):
    logger.info('submit_workitem')
    if request.method == 'POST':
        wm = WorkManager.instance()
        logger.info(request.POST['user_action'])
        wi = request.session['workitem']
        del request.session['workitem']
        if request.POST['user_action'] == 'Next':
            try:
                wm.completeCurrentWorkItem(wi)
            except WorkInterceptedException, msg:
                logger.warning(msg)
            return redirect('/next')
        else:
            logger.info('Cancel job!')
            logger.info('User %s has canceled work item %s' % (request.user, wi))
            request.session['msg'] = 'Work item %s was canceled by %s' % (wi, request.user)
            wm.releaseCurrentWorkItem(wi)

            return redirect('/next')
Esempio n. 5
0
    def test_WorkStrategy(self):

        user_test1 = User.objects.create_user('test1', '*****@*****.**', 'test1')
        user_test2 = User.objects.create_user('test2', '*****@*****.**', 'test2')

        advA = self.createAdvertiser('AdvertiserA', self.ADV, self.test_account_net11)

        advB = self.createAdvertiser('AdvertiserB', self.ADV, self.test_account_net21)

        advC = self.createAdvertiser('AdvertiserC', self.ADV, self.test_account_net11)
        self.createAdvertiser('AdvertiserC', self.ADV, self.test_account_net21, advC)

        self.createTestCapacity(self.ACC, self.COM, self.OWN)

        # create offers for network1
        net1_offers = []
        for n in range(1, 10):
            if n % 2:
                offer = self.createTestOffer(self.POT, self.OFR, self.test_account_net11, self.test_network1)
                if n in (1, 5, 9):
                    offer.advertiser = advA
                elif n == 3:
                    offer.advertiser = advC
            else:
                offer = self.createTestOffer(self.POT, self.OFR, self.test_account_net12, self.test_network1)
            offer.save()
            net1_offers.append(offer)

            # create offers for network2
        net2_offers = []
        for n in range(1, 10):
            if n % 2:
                offer = self.createTestOffer(self.POT, self.OFR, self.test_account_net21, self.test_network2)
                if n in (1, 5, 9):
                    offer.advertiser = advB
                elif n == 3:
                    offer.advertiser = advC
            else:
                offer = self.createTestOffer(self.POT, self.OFR, self.test_account_net22, self.test_network2)
            offer.save()
            net2_offers.append(offer)

        ls = LeadSource.objects.get(pk=1)
        test_csv1 = CSVFile.objects.create(niche=self.test_niche, csv_headers="a,b,c", lead_source=ls, filename='test1.csv', uploaded_by='test_user', status='active')
        for n in range(1, 10):
            lead = Lead(csv=test_csv1, status='active', lead_data='a%d,b%d,c%s' % (n, n, n))
            lead.save()

        test_csv2 = CSVFile.objects.create(niche=self.test_niche, csv_headers="x,y,z", lead_source=ls, filename='test1.csv', uploaded_by='test_user', status='active')
        for n in range(1, 10):
            lead = Lead(csv=test_csv2, status='active', lead_data='x%d,y%d,z%s' % (n, n, n))
            lead.save()

        print 'DATA POPULATED'
        wm = WorkManager.instance()
        wm.workers_online.remove(self.w1) # hack: w1 was inherited from initiaL FIXTURE. we don't need it for this test
        wm.save()

        wm.checkOrCreateUserProfile(user_test1)
        wm.checkOrCreateUserProfile(user_test2)
        wm.signIn(user_test1)
        wm.signIn(user_test2)

        self.assertTrue(user_test1.get_profile().now_online)
        self.assertTrue(user_test2.get_profile().now_online)
        self.assertEquals(wm.getNumberOfOnlineWorkers, 2)

        try:
            # user_test1 is not added to CSV file, so expected NoWorkException
            workItem1 = wm.nextWorkItem(user_test1)
            self.assertTrue(False)
        except NoWorkException:
            test_csv1.workers.add(user_test1)
            test_csv1.workers.add(user_test2)
            test_csv1.save()
            test_csv2.workers.add(user_test1)
            test_csv2.save()
            test_csv1.status = 'disabled'
            test_csv1.save()
            workItem1 = wm.nextWorkItem(user_test1)

        self.assertEquals(len(workItem1.offers), 5)
        self.assertTrue(workItem1.lead.is_locked)
        self.assertEquals(workItem1.lead.locked_by, user_test1)

        try:
            # user_test2 is not added to test_csv2 file, but it is added to test_csv1 so we DONT  expect NoWorkException
            workItem2 = wm.nextWorkItem(user_test2)
            test_csv2.workers.add(user_test2)
            test_csv2.save()
            workItem2 = wm.nextWorkItem(user_test2)
        except NoWorkException:
            self.assertTrue(False)

        #        self.assertEquals(len(workItem2.offers), 5)
        self.assertTrue(workItem2.lead.is_locked)
        self.assertEquals(workItem2.lead.locked_by, user_test2)

        # Cancel work on items
        wm.releaseCurrentWorkItem(workItem1)
        print workItem1.lead.locked_by, workItem1.lead.locked_at
        self.assertFalse(workItem1.lead.is_locked)
        self.assertEquals(workItem1.lead.locked_by, None)
        self.assertEquals(workItem1.lead.worker, None)

        wm.releaseCurrentWorkItem(workItem2)
        self.assertFalse(workItem2.lead.is_locked)
        self.assertEquals(workItem2.lead.locked_by, None)
        self.assertEquals(workItem2.lead.worker, None)

        test_csv1.status = 'enabled'
        test_csv1.save()

        # Disable second CSV 
        test_csv2.status = 'disabled'
        test_csv2.save()

        # Two users get next lead and 5 offers and complete them
        # one account have 100 cap, so it can afford 10 offer shows (for payout 10)
        print 'Two users pick up work from one file'
        self.createTestCapacity(self.ACC, self.COM, self.OWN)
        acc_hash = {}
        while True:
            try:
                user1_wi = wm.nextWorkItem(user_test1)
                requested = user1_wi.lead.offers_requested.count()
                for offer in user1_wi.offers:
                    if offer.account not in acc_hash:
                        acc_hash[offer.account] = []
                    acc_hash[offer.account].append(offer)

                wm.completeCurrentWorkItem(user1_wi)
                self.assertEquals(user1_wi.lead.offers_completed.count(), requested)

                user2_wi = wm.nextWorkItem(user_test2)
                requested = user2_wi.lead.offers_requested.count()

                for offer in user2_wi.offers:
                    if offer.account not in acc_hash:
                        acc_hash[offer.account] = []
                    acc_hash[offer.account].append(offer)
                wm.completeCurrentWorkItem(user2_wi)
                self.assertEquals(user2_wi.lead.offers_completed.count(), requested)

            except NoWorkException:
                break

        for account, offers in acc_hash.items():
            account = Account.objects.get(pk=account.pk)
            print account
            for offer in offers:
                print '** ', offer.get_capacity_today