Exemplo n.º 1
0
def images_demo(request):
    imageTable = Table(
        name='imageTable',

        storage=Storage(
            querySet=models.Author.objects.all(),

            columns=[
                StringColumn('pk', width=100),
                StringColumn('last', width=150),
                ImageColumn(
                    'pk',
                    label='Random image',

                    serializer=URLSerializer(
                        "pk",
                        "images_server",
                    )
                )
            ],
        ),
    )

    if imageTable.willHandle(request):
        return imageTable.handleRequest(request)

    return render_to_response(
        "images.html", {'imageTable': imageTable})
Exemplo n.º 2
0
class TestTableGetSortColumn(TestCase):
    def setUp(self):

        class FakeRequest:
            GET = dict(sort='first_name', start=0, count=2)

        self.fakeRequest = FakeRequest()
        self.column = columns.StringColumn('first_name', 'label')
        self.d = Table("name", storage=Storage(
            querySet=User.objects.all(),
            columns=[self.column]))

    def test_getSortColumn_asc(self):
        self.assertEquals(
            (self.column, False),
            self.d.getSortColumn(self.fakeRequest.GET)
            )

    def test_getSortColumn_desc(self):
        self.fakeRequest.GET['sort'] = '-first_name'
        self.assertEquals(
            (self.column, True),
            self.d.getSortColumn(self.fakeRequest.GET)
            )

    def test_getSortColumn_err(self):
        self.fakeRequest.GET['sort'] = 'unEXistEnt'
        self.assertEquals(self.d.getSortColumn(self.fakeRequest.GET), None)
Exemplo n.º 3
0
    def setUp(self):
        class FakeRequest:
            GET = dict(sort='first_name', start=0, count=2)

        self.fakeRequest = FakeRequest()
        self.column = columns.StringColumn('first_name', 'label')
        self.d = Table("name",
                       storage=Storage(querySet=User.objects.all(),
                                       columns=[self.column]))
Exemplo n.º 4
0
def authors_demo(request):
    first_table = Table(
        name='first_table',

        storage=Storage(
            querySet=models.Author.objects.all(),

            columns=[
                StringColumn('pk', width=100),
                StringColumn('last', width=150),
                StringColumn('first', width=150),
                HrefColumn(
                    'author',
                    width=150,
                    sortable=False,
                    serializer=HrefSerializer(
                        "Check on Google",
                        FormatStringSerializer(
                            'https://www.google.com/search?q=%(last)s%%20%(first)s',
                        )
                    )
                )
            ],

            widgets=[
                StringWidget(
                    'author',
                    placeholder=_("Author"),
                    initialValue='joh',
                    # Filter reuse!
                    filter=AuthorStorageFilter('author')
                    ),
                AutocompleteStringWidget(
                    'first',
                    filter=AuthorStorageFilter('author'),
                    storage=Storage(
                        querySet=models.Author.objects.all().order_by('last'),
                        columns=[StringColumn('label', serializer=UnicodeSerializer())],
                        widgets=[Widget('label',
                            converter=DojoComboValueConverter('label'),
                            filter=StringFilter('pk', operation="eq")
                            )
                        ],
                    )
                )
                ]
            ),
        filename=_("My important export data %Y-%m-%d")
        )

    if first_table.willHandle(request):
        return first_table.handleRequest(request)

    return render_to_response(
        "authors.html", {'first_table': first_table})
Exemplo n.º 5
0
    def test_getExportFileName(self):
        d = Table('name', 'querySet', [])
        self.assertEquals("filename=name.xls",
                          d.getExportFileName(output=formats.XLS))

        self.assertEquals("filename=mine.xls",
                          d.getExportFileName(output=formats.XLS, name="mine"))

        self.assertEquals(
            "filename=%C5%82%C3%B3d%C5%BA_jest.xls",
            d.getExportFileName(output=formats.XLS, name=u"łódź jest"))
Exemplo n.º 6
0
    def test_defaultSort_good(self):
        kw = dict(name="fa",
                  querySet=None,
                  columns=[columns.Column('foo')],
                  initialSort='foo')
        d = Table(**kw)
        self.assertEquals(d.initialSort, 1)

        kw['initialSort'] = '-foo'
        d = Table(**kw)
        self.assertEquals(d.initialSort, -1)
Exemplo n.º 7
0
    def test_serializeRow(self):
        class FakeModel:
            foo = '123'

        fm = FakeModel()

        with Mock() as column:
            column.name >> "foo"
            column.serialize(fm, output=0) >> 'bar'

        d = Table("name", 'querySet', [column])
        self.assertEquals(d.serializeRow(fm, output=0), {'foo': 'bar'})
Exemplo n.º 8
0
    def test_willHandle(self):
        f = FakeRequest()
        f.GET = {'datable': 'json'}
        f.POST = {}
        d = Table('datable', None, [])

        self.assertEquals(d.willHandle(f), True)

        self.assertNotEquals(d.willHandle(f, method='POST'), True)

        f.GET = {'no': 'such table'}
        self.assertNotEquals(d.willHandle(f), True)
Exemplo n.º 9
0
    def test_willHandle(self):
        f = FakeRequest()
        f.GET = {'datable': 'json'}
        f.POST = {}
        d = Table('datable', None, [])

        self.assertEquals(d.willHandle(f), True)

        self.assertNotEquals(d.willHandle(f, method='POST'), True)

        f.GET = {'no':'such table'}
        self.assertNotEquals(d.willHandle(f), True)
Exemplo n.º 10
0
    def test_getExportFileName(self):
        d = Table('name', 'querySet', [])
        self.assertEquals(
            "filename=name.xls", d.getExportFileName(output=formats.XLS))

        self.assertEquals(
            "filename=mine.xls", d.getExportFileName(output=formats.XLS,
                                            name="mine"))

        self.assertEquals(
            "filename=%C5%82%C3%B3d%C5%BA_jest.xls",
            d.getExportFileName(output=formats.XLS, name=u"łódź jest"))
Exemplo n.º 11
0
    def test_serializeData(self):
        class FakeModel:
            foo = '1'

        queryset = [FakeModel(), FakeModel()]

        d = Table("name", 'querySet', [('foo', 'label')])

        with Mock() as d.serializeRow:
            d.serializeRow(1, None)
            d.serializeRow(1, None)

        d.serializeData(queryset, output=None)
Exemplo n.º 12
0
    def test_serializeRow(self):
        class FakeModel:
            foo = '123'

        fm = FakeModel()

        with Mock() as column:
            column.name >> "foo"
            column.serialize(fm, output=0) >> 'bar'

        d = Table("name", 'querySet', [column])
        self.assertEquals(
            d.serializeRow(fm, output=0),
            {'foo':'bar'})
Exemplo n.º 13
0
 def test_defaultSort_bad(self):
     kw = dict(name="fa",
               querySet=None,
               columns=[columns.Column('foo')],
               initialSort='unexistent')
     d = Table(**kw)
     self.assertEquals(d.initialSort, None)
Exemplo n.º 14
0
def getScriptsTable(_user):
    q = RobotScript.objects.filter(user=_user).order_by(
        'script_date', 'script_time')
    return Table(
        name='scriptstable',
        storage=storage.
        Storage(  #There's a clash between TamuzApp's "Storage" and datable's "Storage" so we use storage.Storage (see import...)
            querySet=q,
            columns=[
                StringColumn('script_description', 'script description'),
                StringColumn('type', 'type'),
                DateColumn('script_date', 'script date'),
                StringColumn('script_time', 'script time'),
                ButtonColumn('Edit Script details',
                             serializer=ButtonSerializer(
                                 '/robot_scripts_page/', 'Edit Script Details',
                                 'RobotScript_id'),
                             width=250),
            ],
            widgets=[
                StringWidget('type', placeholder='type'),
                StringWidget('script_description', placeholder='description'),
                DateWidget('script_date', placeholder='date '),
            ],
        ),
    )
Exemplo n.º 15
0
    def setUp(self):
        self.no_calls = 0

        self.req = FakeRequest()

        self.req.GET = {'start': 0, 'count': 25}
        self.req.POST = {}

        User.objects.create_user('username', 'email-username')
        User.objects.create_user('username2', 'email-username2')
        User.objects.create_user('username3', 'email')

        self.d = Table('datable',
                       storage=Storage(
                           querySet=User.objects.all(),
                           columns=[columns.StringColumn('email', 'E-mail')],
                           widgets=[widgets.StringWidget(name='exists')]))
Exemplo n.º 16
0
    def test_wrong_sort_column(self):
        class FakeRequest:
            GET = dict(sort='first_name', start=0, count=2)

        # No column 'first name' in the set:
        d = Table("name", User.objects.all(), [('column', 'label')])
        self.assertRaises(columns.ColumnNotFound, d.performFilteringAndSorting,
                          FakeRequest())
Exemplo n.º 17
0
    def setUp(self):

        class FakeRequest:
            GET = dict(sort='first_name', start=0, count=2)

        self.fakeRequest = FakeRequest()
        self.column = columns.StringColumn('first_name', 'label')
        self.d = Table("name", storage=Storage(
            querySet=User.objects.all(),
            columns=[self.column]))
Exemplo n.º 18
0
    def test_badInit(self):
        kw = dict(name="", querySet=None, columns=[])
        self.assertRaises(ValueError, Table, **kw)

        kw['name'] = None
        self.assertRaises(ValueError, Table, **kw)

        kw['name'] = 'sort'
        self.assertRaises(ValueError, Table, **kw)

        kw['name'] = 'lol'
        Table(**kw)
Exemplo n.º 19
0
class TestTableGetSortColumn(TestCase):
    def setUp(self):
        class FakeRequest:
            GET = dict(sort='first_name', start=0, count=2)

        self.fakeRequest = FakeRequest()
        self.column = columns.StringColumn('first_name', 'label')
        self.d = Table("name",
                       storage=Storage(querySet=User.objects.all(),
                                       columns=[self.column]))

    def test_getSortColumn_asc(self):
        self.assertEquals((self.column, False),
                          self.d.getSortColumn(self.fakeRequest.GET))

    def test_getSortColumn_desc(self):
        self.fakeRequest.GET['sort'] = '-first_name'
        self.assertEquals((self.column, True),
                          self.d.getSortColumn(self.fakeRequest.GET))

    def test_getSortColumn_err(self):
        self.fakeRequest.GET['sort'] = 'unEXistEnt'
        self.assertEquals(self.d.getSortColumn(self.fakeRequest.GET), None)
Exemplo n.º 20
0
    def setUp(self):
        for a in range(5):
            User.objects.create_user('datable-test-user%i' % a, '*****@*****.**')

        self.d = Table("name",
                       storage=Storage(
                           querySet=User.objects.all(),
                           columns=[columns.StringColumn('email', 'E-mail')],
                           widgets=[
                               widgets.StringWidget('email',
                                                    label="E-mail",
                                                    initialValue='foo')
                           ]))

        self.request = FakeRequest()
        self.request.GET = {'email': 'foo@'}
Exemplo n.º 21
0
class TestTableHandleRequest(TestCase):
    def call(self, *args, **kw):
        self.no_calls += 1

    def setUp(self):
        self.no_calls = 0

        self.req = FakeRequest()

        self.req.GET = {'start':0, 'count':25}
        self.req.POST = {}

        User.objects.create_user('username', 'email-username')
        User.objects.create_user('username2', 'email-username2')
        User.objects.create_user('username3', 'email')

        self.d = Table(
            'datable',
            storage=Storage(
                querySet=User.objects.all(),
                columns=[columns.StringColumn('email', 'E-mail')],
                widgets=[widgets.StringWidget(name='exists')]
                )
            )


    def test_handleRequest(self):

        for fmt in ['xls', 'csv', 'json']:
            self.req.GET['datable'] = fmt
            res = self.d.handleRequest(self.req)
            self.assertIn('username2', res.content)

        self.req.GET = {'datable': 'filter,exists' }
        self.assertRaises(
            Http404,
            self.d.handleRequest, self.req)


    def test_handleRequest_badFormat(self):
        self.req.GET = {'datable': 'unknown format'}
        self.assertRaises(Http404, self.d.handleRequest, self.req)

    def test_handleRequest_noSuchFilter(self):
        self.req.GET = {'datable': 'filter,does not exist'}
        self.assertRaises(Http404, self.d.handleRequest, self.req)
Exemplo n.º 22
0
    def test_serializeData(self):
        class FakeModel:
            foo = '1'

        queryset = [FakeModel(), FakeModel()]

        d = Table("name", 'querySet', [('foo', 'label')])

        with Mock() as d.serializeRow:
            d.serializeRow(1, None)
            d.serializeRow(1, None)

        d.serializeData(queryset, output=None)
Exemplo n.º 23
0
    def setUp(self):
        self.no_calls = 0

        self.req = FakeRequest()

        self.req.GET = {'start':0, 'count':25}
        self.req.POST = {}

        User.objects.create_user('username', 'email-username')
        User.objects.create_user('username2', 'email-username2')
        User.objects.create_user('username3', 'email')

        self.d = Table(
            'datable',
            storage=Storage(
                querySet=User.objects.all(),
                columns=[columns.StringColumn('email', 'E-mail')],
                widgets=[widgets.StringWidget(name='exists')]
                )
            )
Exemplo n.º 24
0
    def test_serialize(self):
        class F:
            no_calls = 0

        def call(*args, **kw):
            F.no_calls += 1

        d = Table("name", "querySet", [])
        d.serializeToJSON = call
        d.serializeToCSV = call
        d.serializeToXLS = call

        for fmt in [formats.XLS, formats.JSON, formats.CSV]:
            d.serialize(None, fmt)

        self.assertEquals(F.no_calls, 3)

        self.assertRaises(formats.UnknownFormat, d.serialize, None,
                          'unknown format')
Exemplo n.º 25
0
class TestTableHandleRequest(TestCase):
    def call(self, *args, **kw):
        self.no_calls += 1

    def setUp(self):
        self.no_calls = 0

        self.req = FakeRequest()

        self.req.GET = {'start': 0, 'count': 25}
        self.req.POST = {}

        User.objects.create_user('username', 'email-username')
        User.objects.create_user('username2', 'email-username2')
        User.objects.create_user('username3', 'email')

        self.d = Table('datable',
                       storage=Storage(
                           querySet=User.objects.all(),
                           columns=[columns.StringColumn('email', 'E-mail')],
                           widgets=[widgets.StringWidget(name='exists')]))

    def test_handleRequest(self):

        for fmt in ['xls', 'csv', 'json']:
            self.req.GET['datable'] = fmt
            res = self.d.handleRequest(self.req)
            self.assertIn('username2', res.content)

        self.req.GET = {'datable': 'filter,exists'}
        self.assertRaises(Http404, self.d.handleRequest, self.req)

    def test_handleRequest_badFormat(self):
        self.req.GET = {'datable': 'unknown format'}
        self.assertRaises(Http404, self.d.handleRequest, self.req)

    def test_handleRequest_noSuchFilter(self):
        self.req.GET = {'datable': 'filter,does not exist'}
        self.assertRaises(Http404, self.d.handleRequest, self.req)
Exemplo n.º 26
0
def getExperimentsTable(_user):
    q = Experiment.objects.filter(user=_user).order_by('date', 'time')

    return Table(
        name='experimentstable',
        storage=storage.
        Storage(  #There's a clash between TamuzApp's "Storage" and datable's "Storage" so we use storage.Storage (see import...)
            querySet=q,
            columns=[
                StringColumn('name', 'Name'),
                StringColumn('type', 'Type'),
                StringColumn('grade', 'Grade'),
                StringColumn('distance', 'Distance'),
                StringColumn('volume', 'Volume'),
                StringColumn('description', 'Description'),
                StringColumn('pipetingMode', 'Pipeting Mode'),
                StringColumn('liquidClass', 'Liquid Class'),
                StringColumn('tipType', 'Tip Type'),
                StringColumn('sourcePlastic', 'Source Plastic'),
                StringColumn('destPlastic', 'Dest Plastic'),
                #StringColumn('plateReader','plate reader'),
                DateColumn('date', 'date', type='date'),
                StringColumn('time', 'time', type='time'),
                ButtonColumn('Edit Experiment details',
                             serializer=ButtonSerializer(
                                 '/experiments/', 'Edit Experiment Details',
                                 'Experiment_id'),
                             width=250),
            ],
            widgets=[
                StringWidget('name', placeholder=' name'),
                StringWidget('description', placeholder='description'),
                StringWidget('volume', placeholder='volume'),
                DateWidget('date', placeholder='date '),
            ],
        ),
    )
Exemplo n.º 27
0
def getErrorsTable(script):
    q = RobotScriptError.objects.filter(robotscript=script).order_by(
        'date', 'time')
    return Table(
        name='errorstable',
        storage=storage.
        Storage(  #There's a clash between TamuzApp's "Storage" and datable's "Storage" so we use storage.Storage (see import...)
            querySet=q,
            columns=[
                StringColumn('description', 'Description'),
                DateColumn('date', 'date'),
                StringColumn('time', 'time'),
                ButtonColumn('Edit error  details',
                             serializer=ButtonSerializer(
                                 '/robot_scripts_page/robotscript/',
                                 'Edit Error  Details', 'RobotScriptError_id'),
                             width=250),
            ],
            widgets=[
                StringWidget('description', placeholder='description'),
                DateWidget('date', placeholder='date '),
            ],
        ),
    )
Exemplo n.º 28
0
    def test_serialize(self):

        class F:
            no_calls = 0

        def call(*args, **kw):
            F.no_calls += 1

        d = Table("name", "querySet", [])
        d.serializeToJSON = call
        d.serializeToCSV = call
        d.serializeToXLS = call

        for fmt in [formats.XLS, formats.JSON, formats.CSV]:
            d.serialize(None, fmt)

        self.assertEquals(F.no_calls, 3)

        self.assertRaises(
            formats.UnknownFormat,
            d.serialize, None, 'unknown format')
Exemplo n.º 29
0
from django.http import Http404

from ludibrio import Mock

from datable.core import filters
from datable.core import formats
from datable.web import columns
from datable.web.util import getFullPath
from datable.web.table import Table
from datable.web.storage import Storage
from datable.web import widgets

users_datable = Table(name='users',
                      storage=Storage(querySet=User.objects.all(),
                                      columns=[('username', 'Login'),
                                               ('first_name', 'First name'),
                                               ('last_name', 'Last name'),
                                               ('foo', 'FOO', lambda x: 'foo')
                                               ]))


class FakeRequest:
    secure = True
    is_secure = lambda self: self.secure
    META = {'HTTP_HOST': 'test.host'}
    path = '/lol'


class TestViews(TestCase):
    def test_getFullPath(self):
Exemplo n.º 30
0
    def test_serializeToJSON(self):
        class FakeRequest:
            GET = dict(sort='first_name', start=0, count=2)

        d = Table("name", User.objects.all(), [('first_name', 'label')])
        d.serializeToJSON(FakeRequest())
Exemplo n.º 31
0
 def test_header(self):
     d = Table("name", "querySet", [('lol', 'omg')])
     self.assertEquals(d.header(), ['omg'])
Exemplo n.º 32
0
    def test_performFilteringAndSorting(self):
        class FakeRequest:
            GET = dict(sort='first_name', start=0, count=2)

        d = Table("name", User.objects.all(), [('first_name', 'label')])
        d.performFilteringAndSorting(FakeRequest())
Exemplo n.º 33
0
 def test_header(self):
     d = Table("name", "querySet", [('lol', 'omg')])
     self.assertEquals(d.header(), ['omg'])
Exemplo n.º 34
0
 def test_serializeToJSON(self):
     class FakeRequest:
         GET = dict(sort='first_name', start=0, count=2)
     d = Table("name", User.objects.all(), [('first_name', 'label')])
     d.serializeToJSON(FakeRequest())
Exemplo n.º 35
0
def books_demo(request):

    first_table = Table(
        name='first_table',

        storage=Storage(
            querySet=models.Book.objects.all(),
            columns=[
                StringColumn('pk', width=100),
                StringColumn('title', width=350),
                DateColumn('published_on', width=100),
                DateTimeColumn('created_on', width=150),
                BooleanColumn('approved', width=30),
                Column(
                    'book_type__name',
                    width=100,
                    sortable=True,
                    serializer=ForeignKeySerializer(
                        'book_type',
                        StringSerializer(
                            'name'
                        )
                    )
                ),
                Column(
                    'authors',
                    width=100,
                    sortable=False,
                    serializer=AuthorsSerializer())
                ],
            widgets=[
                StringWidget('title', placeholder=_("Title")),
                DateWidget('published_on', placeholder=_('Published on')),
                DateTimeLessOrEqual('created_on', paired=True),
                DateTimeGreaterOrEqual('created_on', paired=True),

                PeriodicRefreshWidget('periodic', filterField='published_on'),

                BooleanWidget('approved', placeholder=_('Was approved before')),

                ForeignKeyComboBox(
                    'book_type', otherSet=models.BookType.objects.all(), otherField='name',
                    otherFormat='Book Type: %(name)s',
                    placeholder=_("book type")),

                Widget(
                    'authors',
                    placeholder=_("Authors"),
                    filter=IntegerFilter('bookauthor__author__pk'),
                    converter=IntegerConverter('authors', min=0),
                    templateName="autocomplete_string",
                    storage=Storage(
                        querySet=models.Author.objects.all(),
                        defaultSort='last',
                        columns=[
                            StringColumn(
                                'label',
                                serializer=FormatStringSerializer('%(last)s %(first)s')),
                            ],
                        widgets=[
                            Widget(
                                'label',
                                converter=DojoComboValueConverter('label'),
                                filter=AuthorStorageFilter('label'))
                        ]
                    )
                )


            ]
            ),
        filename=_("My important export data %Y-%m-%d")
        )

    if first_table.willHandle(request):
        return first_table.handleRequest(request)

    return render_to_response(
        "books.html", {'first_table': first_table})
Exemplo n.º 36
0
 def test_performFilteringAndSorting(self):
     class FakeRequest:
         GET = dict(sort='first_name', start=0, count=2)
     d = Table("name", User.objects.all(), [('first_name', 'label')])
     d.performFilteringAndSorting(FakeRequest())