Skip to content

Commit

Permalink
small fixes on gallery app and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
denibertovic committed Feb 10, 2014
1 parent 9be2ef1 commit a0c20dd
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 23 deletions.
12 changes: 7 additions & 5 deletions gallery/models.py
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-

import os

from django.conf import settings
from django.db import models

Expand Down Expand Up @@ -57,20 +59,20 @@ class Meta:
permissions = (
('view_album', 'View Album'),)

def save(self):
super(Album, self).save()
def save(self, *args, **kwargs):
super(Album, self).save(*args, **kwargs)
if (self.initial):
if (not self.upload_path.is_empty):
if (self.upload_path and not self.upload_path.is_empty):
self.initial = False
super(Album, self).save()
full_path_to_album = settings.MEDIA_ROOT+self.upload_path.path
full_path_to_album = os.path.join(settings.MEDIA_ROOT, self.upload_path.path)

for filename in exclude_fb_versions(full_path_to_album):
parsed = parse_filename(filename)
if (parsed):
new_image = Image.objects.create(title=parsed['name'],
slug=parsed['slug'],
upload_path=str(self.upload_path.path) + '/' + parsed['name_full'],
upload_path=os.path.join(self.upload_path.path, parsed['name_full']),
date_of_event=parsed['date'],
photographer=self.photographer)
new_image.album.add(self)
Expand Down
14 changes: 14 additions & 0 deletions gallery/urls.py
@@ -0,0 +1,14 @@
from django.conf.urls import patterns, url

urlpatterns = patterns('gallery.views',
url(r'^$', 'show_gallery',
name='gallery_index'),
url(r'^(?P<category>\w+)/$', 'list_albums',
name='gallery_category_albums'),
url(r'^(?P<category>\w+)/(?P<year>\d{4})/$', 'list_albums',
name='gallery_category_albums_by_year'),
url(r'^(?P<category>\w+)/(?P<album_slug>[-_a-zA-Z0-9]+)/$', 'view_album',
name='gallery_view_album'),
url(r'^(?P<category>\w+)/(?P<album_slug>[-_a-zA-Z0-9]+)/(?P<image_slug>[-_a-zA-Z0-9]+)/$', 'view_image',
name='gallery_view_image'),
)
4 changes: 2 additions & 2 deletions gallery/views.py
Expand Up @@ -17,7 +17,7 @@ def list_albums(request, category, year=datetime.today().year):
date_of_event__year=year).order_by('-date_of_event')

return render(request, 'gallery_spec.html', {
'list': albums, 'years': years, 'category': category})
'albums': albums, 'years': years, 'category': category})


def view_album(request, category, album_slug):
Expand All @@ -29,6 +29,6 @@ def view_album(request, category, album_slug):
{'images': images, 'category': category, 'album': album})


def view_image(request, category, image_slug):
def view_image(request, category, album_slug, image_slug):
image = get_object_or_404(Image, slug=image_slug)
return render(request, 'gallery_view_image.html', {'image': image})
2 changes: 1 addition & 1 deletion kset/templates/gallery_spec.html
Expand Up @@ -18,7 +18,7 @@
{% endfor %}
</nav>

{% for a in list %}
{% for a in albums %}

{% ifchanged %}
<header class="gallery-month"><h2>{{ a.date_of_event|date:"F Y." }}</h2></header>
Expand Down
16 changes: 2 additions & 14 deletions kset/urls.py
Expand Up @@ -6,6 +6,7 @@
from filebrowser.sites import site

import members.urls
import gallery.urls

admin.autodiscover()

Expand Down Expand Up @@ -55,28 +56,15 @@
url(r'^klub/$', 'subpages.views.by_slug',
name='club'),


url(r'^multimedia/$', 'subpages.views.multimedia',
name='multimedia'),

url(r'^gallery/$', 'gallery.views.show_gallery',
name='gallery_index'),
url(r'^gallery/(?P<category>\w+)/$', 'gallery.views.list_albums',
name='gallery_category_albums'),
url(r'^gallery/(?P<category>\w+)/(?P<year>\d{4})/$', 'gallery.views.list_albums',
name='gallery_category_albums_by_year'),
url(r'^gallery/(?P<category>\w+)/(?P<album_slug>[-_a-zA-Z0-9]+)/$', 'gallery.views.view_album',
name='gallery_view_album'),
url(r'^gallery/(?P<category>\w+)/[-_a-zA-Z0-9]+/(?P<image_slug>[-_a-zA-Z0-9]+)/$', 'gallery.views.view_image',
name='gallery_view_image'),


url(r'^dezurstva/$', 'savjet.views.list_attendance',
name='dezurstva'),

url(r'^gallery/', include(gallery.urls)),
url(r'^members/', include(members.urls)),


)

## Admin and similar
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 30 additions & 1 deletion tests/factories.py
Expand Up @@ -8,10 +8,12 @@
from events.models import Event
from subpages.models import Subpage, Category
from members.models import Group, Member, MemberGroupLink
from gallery.models import Photographer, Album, Image


__all__ = ['UserFactory', 'NewsFactory', 'EventFactory', 'SubpageCategoryFactory',
'SubpageFactory', 'MemberFactory', 'GroupFactory', 'MemberGroupLinkFactory']
'SubpageFactory', 'MemberFactory', 'GroupFactory', 'MemberGroupLinkFactory',
'PhotographerFactory', 'AlbumFactory', 'ImageFactory']


class UserFactory(factory.django.DjangoModelFactory):
Expand Down Expand Up @@ -116,3 +118,30 @@ class MemberGroupLinkFactory(factory.django.DjangoModelFactory):
group = factory.SubFactory(GroupFactory)
date_start = None
date_end = None


class PhotographerFactory(factory.django.DjangoModelFactory):
FACTORY_FOR = Photographer

name = factory.Sequence(lambda n: 'Test Photographer %s' % n)
url = 'http://myfaketestphotogallery.com'


class AlbumFactory(factory.django.DjangoModelFactory):
FACTORY_FOR = Album

title = factory.Sequence(lambda n: 'Fake Album %s' % n)
slug = factory.Sequence(lambda n: 'fake-album-%s' % n)
description = 'Fake description'
date_of_event = datetime.now()
photographer = factory.SubFactory(PhotographerFactory)
category = Album.LIVE


class ImageFactory(factory.django.DjangoModelFactory):
FACTORY_FOR = Image

title = factory.Sequence(lambda n: 'Fake Image %s' % n)
slug = factory.Sequence(lambda n: 'fake-image-%s' % n)
photographer = factory.SubFactory(PhotographerFactory)
date_of_event = datetime.now()
120 changes: 120 additions & 0 deletions tests/gallery_tests.py
@@ -0,0 +1,120 @@
import os
from datetime import datetime, timedelta

from django.test import TestCase

from gallery.models import Album, Image, Photographer
from .base import BaseTestClient
from .factories import PhotographerFactory, AlbumFactory, ImageFactory


__all__ = ['GalleryViewTest', 'GalleryTest']


class GalleryViewTest(TestCase):

def setUp(self):
super(GalleryViewTest, self).setUp()
self.client = BaseTestClient()

def test_gallery_index_page(self):
ret = self.client.get('gallery_index')
self.assertEquals(200, ret.status_code)

def test_list_albums_page(self):
album = AlbumFactory(category=Album.LIVE)
ret = self.client.get('gallery_category_albums', category=Album.LIVE)
self.assertEquals(200, ret.status_code)
self.assertEquals(1, len(ret.context['albums']))
self.assertEquals(ret.context['albums'][0].id, album.id)

def test_list_albums_page_by_category(self):
album1 = AlbumFactory(category=Album.LIVE)
album2 = AlbumFactory(category=Album.FOTO)
ret = self.client.get('gallery_category_albums', category=Album.LIVE)
self.assertEquals(200, ret.status_code)
self.assertEquals(1, len(ret.context['albums']))
self.assertEquals(ret.context['albums'][0].id, album1.id)

ret = self.client.get('gallery_category_albums', category=Album.FOTO)
self.assertEquals(200, ret.status_code)
self.assertEquals(1, len(ret.context['albums']))
self.assertEquals(ret.context['albums'][0].id, album2.id)

def test_list_albums_page_by_year(self):
AlbumFactory(
category=Album.LIVE,
date_of_event=(datetime.now() - timedelta(days=800)))
album = AlbumFactory(
category=Album.LIVE,
date_of_event=datetime.now())
ret = self.client.get(
'gallery_category_albums_by_year',
category=Album.LIVE,
year=datetime.now().year)
self.assertEquals(200, ret.status_code)
self.assertEquals(1, len(ret.context['albums']))
self.assertEquals(ret.context['albums'][0].id, album.id)

def test_view_album_page(self):
album = AlbumFactory(
category=Album.LIVE,
date_of_event=(datetime.now() - timedelta(days=800)))

ret = self.client.get(
'gallery_view_album',
category=album.category,
album_slug=album.slug)
self.assertEquals(200, ret.status_code)

def test_view_image_page(self):
album = AlbumFactory()
image = ImageFactory()
image.album.add(album)
ret = self.client.get(
'gallery_view_image',
category=album.category,
album_slug=album.slug,
image_slug=image.slug)
self.assertEquals(200, ret.status_code)


class GalleryTest(TestCase):

def test_saving_an_album_creates_images_and_photographers(self):
"""
Assert that by creating an album that points to a non empty directory
of images the images will be parsed and Image objects will be created
along with Photographer objects. Asserts that we ignore images
that have filebrowser suffixes.
"""
import django

# We need to monkeypatch django's default storage to let us use
# absolute paths for file fields otherwise it raise an
# SuspiciousOperation Exception
def _test_safe_join(base, *paths):
return os.path.abspath(os.path.join(base, *paths))
django.core.files.storage.safe_join = _test_safe_join

album_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'assets', 'test_album')

album = AlbumFactory(title='test_title1', upload_path=album_path)
self.assertEquals('test_title1', album.title)
self.assertEquals(1, Album.objects.all().count())
self.assertEquals(2, Image.objects.all().count())
self.assertEquals(1, Photographer.objects.all().count())

images = Image.objects.all()
self.assertEquals(images[0].title, '20081222_some_made_up_band_cb_name_surname_01')
self.assertEquals(images[1].title, '20081222_some_made_up_band_cb_name_surname_02')

# Test that we don't go and create duplicate Image or
# Photographer objects on album update
album.title = 'update_title'
album.save()

self.assertEquals(1, Album.objects.all().count())
self.assertEquals(2, Image.objects.all().count())
self.assertEquals(1, Photographer.objects.all().count())

0 comments on commit a0c20dd

Please sign in to comment.