def update_popularity(): """ Update the popularity score of all the projects Simply loops over all the tenants, and updates the scores """ logger.info("Updating projects popularity using Celery") for tenant in Client.objects.all(): with LocalTenant(tenant, clear_tenant=True): Project.update_popularity() logger.info("Finished updating projects popularity using Celery")
def update_popularity(): """ Update the popularity score of all the projects Simply loops over all the tenants, and updates the scores """ logger.info("Updating projects popularity using Celery") for tenant in Client.objects.all(): connection.set_tenant(tenant) with LocalTenant(tenant, clear_tenant=True): Project.update_popularity() logger.info("Finished updating projects popularity using Celery")
def setUp(self): super(MonthlyDonationAdminTest, self).setUp() self.app.extra_environ['HTTP_HOST'] = str(self.tenant.domain_url) self.superuser = BlueBottleUserFactory.create(is_staff=True, is_superuser=True) self.init_projects() self.phase_campaign = ProjectPhase.objects.get(slug='campaign') self.country = CountryFactory() self.projects = [] for amount in [500, 100, 1500, 300, 200]: self.projects.append( ProjectFactory.create(amount_asked=amount, status=self.phase_campaign)) # Some donations to get the popularity going # Top 3 after this should be projects 4, 3, 0 order = OrderFactory() DonationFactory(order=order, project=self.projects[3], amount=10) DonationFactory(order=order, project=self.projects[3], amount=100) DonationFactory(order=order, project=self.projects[3], amount=20) DonationFactory(order=order, project=self.projects[4], amount=10) DonationFactory(order=order, project=self.projects[4], amount=70) DonationFactory(order=order, project=self.projects[0], amount=10) order.locked() order.save() order.success() order.save() # Since we force the transitions update_amounts isn't triggered by # signal, so we run it manually here. for project in self.projects: project.update_amounts() self.user1 = BlueBottleUserFactory.create() self.user2 = BlueBottleUserFactory.create() # Create a monthly donor with a preferred project self.monthly_donor1 = MonthlyDonorFactory(user=self.user1, amount=25) self.monthly_donor1_project = MonthlyDonorProjectFactory( donor=self.monthly_donor1, project=self.projects[0]) # Create a monthly donor without preferred projects self.monthly_donor2 = MonthlyDonorFactory(user=self.user2, amount=100) Project.update_popularity()
def update_project_status_stats(): """ Calculates the no. of projects per status for a tenant """ for tenant in Client.objects.all(): with LocalTenant(tenant, clear_tenant=True): Project.update_status_stats(tenant=tenant)
def test_update_popularity(self): Project.update_popularity() self.assertEqual(Project.objects.get(id=self.project.id).popularity, 11)
def _handle_projects(self, data): """Expected fields for Projects import: slug (string) title (string) deadline (string<date>) created (string<date>) user (string<email>) description (string) pitch (string) goal (int) video (string<url>) image (string<url>) categories (array<category-slug>) """ try: project = Project.objects.get(slug=data['slug']) except Project.DoesNotExist: project = Project(slug=data['slug']) try: project.owner = Member.objects.get(email=data['user']) except Member.DoesNotExist: project.owner, new = Member.objects.get_or_create( email='*****@*****.**') if new: project.owner.is_active = True project.owner.username = '******' project.owner.first_name = 'Admin' project.owner.last_name = 'Example' project.owner.save() try: project.status = ProjectPhase.objects.get(slug=data['status']) except (ProjectPhase.DoesNotExist, KeyError): # If we don't have a status, then it should be set in admin, so plan-new seems best. project.status = ProjectPhase.objects.get(slug='plan-new') deadline = data['deadline'] if deadline: deadline = parser.parse(deadline) project.title = data['title'] or data['slug'] project.created = data['created'] project.campaign_started = data['created'] goal = data['goal'] or 0.0 project.amount_asked = Money(goal, 'EUR') project.deadline = deadline project.video_url = data['video'] if data.get('country'): project.country = Country.objects.get(alpha2_code=data['country']) else: project.country = Country.objects.get(alpha2_code='NL') self._generic_import(project, data, excludes=[ 'deadline', 'slug', 'user', 'created', 'theme', 'country', 'status', 'goal', 'categories', 'image', 'video' ]) if data.get('theme'): try: project.theme = ProjectTheme.objects.get(slug=data['theme']) except ProjectTheme.DoesNotExist: logger.warn("Couldn't find theme {}".format(data['theme'])) try: project.save() except IntegrityError: project.title = project.title + '*' project.save() project.categories = Category.objects.filter( slug__in=data['categories']) if 'image' in data: parts = data['image'].split('/') name = "/".join(parts[5:]) if not self.upload: logger.warn( "Upload path not set, can't store project image. Please use -u" ) file_name = u"{}{}".format(self.upload, name) try: file = open(file_name, 'rb') project.image.save(name, File(file), save=True) except IOError: logger.warn("Couldn't find file {}".format(file_name))
def test_update_popularity(self): Project.update_popularity() self.assertEqual( Project.objects.get(id=self.project.id).popularity, 11)
def test_object_permission_non_owner(self): other_user = BlueBottleUserFactory.create() self.assertFalse( self.permission.has_object_action_permission( 'GET', self.user, Project(owner=other_user)))
def test_object_permission(self): self.assertTrue( self.permission.has_object_action_permission( 'GET', self.user, Project(owner=self.user)))