Skip to content

Commit

Permalink
Merge pull request #8 from moshthepitt/issue-7
Browse files Browse the repository at this point in the history
Make VegaCRUDView Configurable
  • Loading branch information
moshthepitt authored Dec 20, 2018
2 parents 4bb8b7d + 59b4233 commit 975c7be
Show file tree
Hide file tree
Showing 8 changed files with 629 additions and 264 deletions.
1 change: 1 addition & 0 deletions tests/artist_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Meta:
"""Meta definition for Song."""
verbose_name = 'Song'
verbose_name_plural = 'Songs'
ordering = ['name']

def __str__(self):
"""Unicode representation of Song."""
Expand Down
6 changes: 4 additions & 2 deletions tests/artist_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

from . import views

crud_patterns = views.ArtistCRUD().url_patterns()
artist_crud_patterns = views.ArtistCRUD().url_patterns()
custom_artist_crud_patterns = views.CustomArtistCRUD().url_patterns()
song_crud_patterns = views.SongCRUD().url_patterns()

urlpatterns = [
path('list/artists/', views.ArtistListView.as_view()),
path('edit/artists/create/', views.ArtistCreate.as_view()),
path('edit/artists/edit/<int:pk>', views.ArtistUpdate.as_view()),
path('edit/artists/delete/<int:pk>', views.ArtistDelete.as_view()),
] + crud_patterns
] + artist_crud_patterns + song_crud_patterns + custom_artist_crud_patterns
38 changes: 34 additions & 4 deletions tests/artist_app/views.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
"""
Module for vega-admin test views
"""
from vega_admin.views import (VegaCreateView, VegaDeleteView, VegaListView,
VegaUpdateView, VegaCRUDView)
from vega_admin.views import (VegaCreateView, VegaCRUDView, VegaDeleteView,
VegaListView, VegaUpdateView)

from .forms import ArtistForm
from .models import Artist
from .models import Artist, Song
from .tables import ArtistTable


class ArtistCRUD(VegaCRUDView):
"""
CRUD view for artists
"""

model = Artist


class ArtistCreate(VegaCreateView): # pylint: disable=too-many-ancestors
"""
Artist CreateView class
"""

form_class = ArtistForm
model = Artist

Expand All @@ -34,6 +36,7 @@ class ArtistUpdate(VegaUpdateView): # pylint: disable=too-many-ancestors
"""
Artist UpdateView class
"""

form_class = ArtistForm
model = Artist

Expand All @@ -48,6 +51,7 @@ class ArtistDelete(VegaDeleteView): # pylint: disable=too-many-ancestors
"""
Artist DeleteView class
"""

model = Artist

def get_success_url(self):
Expand All @@ -61,6 +65,32 @@ class ArtistListView(VegaListView): # pylint: disable=too-many-ancestors
"""
Artist list view
"""

model = Artist
table_class = ArtistTable
search_fields = ["name"]


class SongCRUD(VegaCRUDView):
"""
CRUD view for songs
"""

model = Song
list_fields = ["name", "artist", ]
table_attrs = {"class": "song-table"}
table_actions = ["create", "update", "delete", ]
create_fields = ["name", "artist", ]
update_fields = ["name", ]


class CustomArtistCRUD(VegaCRUDView):
"""
CRUD view for artists with custom forms and table
"""

model = Artist
crud_path = "custom-artist"
create_form_class = ArtistForm
update_form_class = ArtistForm
table_class = ArtistTable
search_fields = ['name']
47 changes: 47 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
vega-admin module to test utils
"""
from django.test import TestCase, override_settings

from tests.artist_app.models import Artist

from vega_admin.utils import get_table, get_modelform


class TestUtils(TestCase):
"""
class for testing vega-admin utils
"""

def test_get_modelform(self):
"""Test get_modelform"""
# basic form
form = get_modelform(model=Artist)
self.assertEqual(Artist, form.model)
self.assertEqual(["id", "name"], form.Meta.fields)
# form with all options
form2 = get_modelform(model=Artist, fields=['name'])
self.assertEqual(Artist, form2.model)
self.assertEqual(["name"], form2.Meta.fields)

@override_settings(VEGA_NOTHING_TO_SHOW="Nothing here")
def test_get_table(self):
"""
Test get_table
"""
# basic table
table = get_table(model=Artist)
self.assertEqual(Artist, table.Meta.model)
self.assertEqual("Nothing here", table.Meta.empty_text)
self.assertEqual(
["id", "name"], [_[0] for _ in table.base_columns.items()])

# table with all options set
table2 = get_table(
model=Artist, fields=["name"], attrs={"class": "mytable"})
self.assertEqual(Artist, table2.Meta.model)
self.assertEqual("Nothing here", table2.Meta.empty_text)
self.assertEqual(["name"], [_[0] for _ in table2.base_columns.items()])
self.assertEqual(["id"], table2.Meta.exclude)
self.assertEqual({"class": "mytable"}, table2.Meta.attrs)
self.assertEqual(("name", "..."), table2.Meta.sequence)
Loading

0 comments on commit 975c7be

Please sign in to comment.