Esempio n. 1
0
 def form_valid(self, form):
     # multi-tenant support
     tenant = getattr(connection, 'tenant', None)
     # start actual export and render the template
     if settings.EXPORTDB_USE_CELERY:
         async_result = export.delay(self.get_exporter_class(),
                                     tenant=tenant,
                                     **form.cleaned_data)
         self.request.session[EXPORTDB_EXPORT_KEY] = async_result.id
         context = self.get_context_data(export_running=True)
         self.template_name = 'exportdb/in_progress.html'
         return self.render_to_response(context)
     else:
         result = plain_export(self.get_exporter_class(),
                               tenant=tenant,
                               **form.cleaned_data)
         filename = result.split('/')[-1]
         output = open(result, 'r')
         response = HttpResponse(
             output.read(),
             content_type=
             'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
         )
         response[
             'Content-Disposition'] = 'attachment; filename=%s' % filename
         return response
Esempio n. 2
0
    def test_export(self):
        from_date = now() - timedelta(weeks=2)
        to_date = now() + timedelta(weeks=1)
        data = {'from_date': from_date, 'to_date': to_date, '_save': 'Confirm'}
        tenant = connection.tenant
        initiatives = InitiativeFactory.create_batch(4)
        for initiative in initiatives:
            EventFactory.create_batch(3, initiative=initiative)
            AssignmentFactory.create_batch(2, initiative=initiative)
            FundingFactory.create_batch(1, initiative=initiative)

        result = plain_export(Exporter, tenant=tenant, **data)
        book = xlrd.open_workbook(result)
        self.assertEqual(book.sheet_by_name('Users').ncols, 11)
        self.assertEqual(book.sheet_by_name('Users').nrows, 41)
        self.assertEqual(book.sheet_by_name('Initiatives').nrows, 5)
        self.assertEqual(book.sheet_by_name('Funding activities').nrows, 5)
        self.assertEqual(book.sheet_by_name('Events').nrows, 13)
        self.assertEqual(
            book.sheet_by_name('Events').cell(0, 13).value, 'Start')
        self.assertEqual(
            book.sheet_by_name('Events').cell(0, 14).value, 'Time needed')

        self.assertEqual(book.sheet_by_name('Tasks').nrows, 9)
        self.assertEqual(
            book.sheet_by_name('Tasks').cell(0, 16).value, 'Preparation time')
        self.assertEqual(
            book.sheet_by_name('Tasks').cell(0, 17).value, 'Start time')
        self.assertEqual(
            book.sheet_by_name('Tasks').cell(0, 19).value, 'End date')
Esempio n. 3
0
    def test_export_impact(self):
        from_date = now() - timedelta(weeks=2)
        to_date = now() + timedelta(weeks=1)
        users = BlueBottleUserFactory.create_batch(5)

        co2 = ImpactType.objects.get(slug='co2')
        co2.active = True
        co2.save()
        water = ImpactType.objects.get(slug='water')
        water.active = True
        water.save()

        initiative = InitiativeFactory.create(owner=users[0])

        assignment = AssignmentFactory.create(owner=users[1],
                                              initiative=initiative)
        assignment.goals.create(type=co2, realized=300)
        assignment.goals.create(type=water, realized=750)

        data = {'from_date': from_date, 'to_date': to_date, '_save': 'Confirm'}
        tenant = connection.tenant
        result = plain_export(Exporter, tenant=tenant, **data)
        book = xlrd.open_workbook(result)

        self.assertEqual(
            book.sheet_by_name('Tasks').cell(0, 23).value,
            u'Reduce CO\u2082 emissions')
        self.assertEqual(book.sheet_by_name('Tasks').cell(1, 23).value, 300)
        self.assertEqual(
            book.sheet_by_name('Tasks').cell(0, 24).value, u'Save water')
        self.assertEqual(book.sheet_by_name('Tasks').cell(1, 24).value, 750)
Esempio n. 4
0
    def test_export_user_segments(self):
        from_date = now() - timedelta(weeks=2)
        to_date = now() + timedelta(weeks=1)
        users = BlueBottleUserFactory.create_batch(5)
        segment_type = SegmentTypeFactory.create(name='Department')
        engineering = SegmentFactory.create(type=segment_type,
                                            name='Engineering')
        rubbish = SegmentFactory.create(type=segment_type, name='Rubbish')
        users[0].segments.add(engineering)
        initiative = InitiativeFactory.create(owner=users[0])
        assignment = AssignmentFactory.create(owner=users[1],
                                              initiative=initiative)
        assignment.segments.add(engineering)
        assignment.segments.add(rubbish)
        ApplicantFactory.create(activity=assignment, user=users[2])

        data = {'from_date': from_date, 'to_date': to_date, '_save': 'Confirm'}
        tenant = connection.tenant
        result = plain_export(Exporter, tenant=tenant, **data)
        book = xlrd.open_workbook(result)

        self.assertEqual(book.sheet_by_name('Users').ncols, 12)
        self.assertEqual(
            book.sheet_by_name('Users').cell(0, 11).value, 'Department')

        t = 0
        while t < book.sheet_by_name('Users').nrows:
            if book.sheet_by_name('Users').cell(t, 5).value == users[0].email:
                self.assertEqual(
                    book.sheet_by_name('Users').cell(t, 11).value,
                    'Engineering')
            t += 1

        self.assertEqual(
            book.sheet_by_name('Tasks').cell(0, 23).value, 'Department')

        t = 0
        while t < book.sheet_by_name('Users').nrows:
            if book.sheet_by_name('Users').cell(t, 5).value == users[0].email:
                self.assertTrue(
                    book.sheet_by_name('Tasks').cell(t, 23).value in
                    ['Engineering, Rubbish', 'Rubbish, Engineering'])
            t += 1
Esempio n. 5
0
    def test_export_custom_user_fields(self):
        from_date = now() - timedelta(weeks=2)
        to_date = now() + timedelta(weeks=1)

        colour = CustomMemberFieldSettings.objects.create(
            name='colour', description='Favourite colour')

        BlueBottleUserFactory.create_batch(2)
        user = BlueBottleUserFactory.create(email='*****@*****.**')
        BlueBottleUserFactory.create_batch(2)

        CustomMemberField.objects.create(member=user,
                                         field=colour,
                                         value='Parblue Yellow')
        initiative = InitiativeFactory.create(owner=user)
        assignment = AssignmentFactory.create(owner=user,
                                              initiative=initiative)
        ApplicantFactory.create(activity=assignment, user=user)

        data = {'from_date': from_date, 'to_date': to_date, '_save': 'Confirm'}
        tenant = connection.tenant
        result = plain_export(Exporter, tenant=tenant, **data)
        book = xlrd.open_workbook(result)

        self.assertEqual(book.sheet_by_name('Users').ncols, 12)
        t = 1
        while t < book.sheet_by_name('Users').nrows:
            if book.sheet_by_name('Users').cell(
                    t, 5).value == '*****@*****.**':
                self.assertEqual(
                    book.sheet_by_name('Users').cell(t, 11).value,
                    'Parblue Yellow')
            t += 1

        self.assertEqual(
            book.sheet_by_name('Task contributions').cell(0, 13).value,
            'Favourite colour')
        self.assertEqual(
            book.sheet_by_name('Task contributions').cell(1, 13).value,
            'Parblue Yellow')