class ViewHandler (): """Instances of this class handle incoming GET requests and serve the appropriate HTTP responses""" pyntrest_handler = None """Handler to create the webpage context for incoming GET requests""" def __init__(self): """Constructor""" self.pyntrest_handler = PyntrestHandler(YOUR_IMAGE_FOLDER, STATIC_PATH) def set_pyntrest_handler(self, pyntrest_handler): self.pyntrest_handler = pyntrest_handler def get(self, request): """This method serves the GET requests to the web photo albums""" if not request: raise TypeError # Check whether to redirect on dirty request paths clean_path = cleanup_url_path(request.path) if not request.path == clean_path: return redirect(clean_path) if not request.path.endswith('/'): request.path = request.path + '/' if BOOKIFY_PATH: clean_bookify_path = cleanup_url_path('/' + BOOKIFY_PATH + '/') #print 'Testing path \'{}\' against bookify root path \'{}\''.format(request.path, clean_bookify_path) if request.path == clean_bookify_path: view_context = self.pyntrest_handler.generate_view_context_bookify() return render(request, 'pyntrest/bookify.html', view_context) if not self.pyntrest_handler.check_if_album_exists(request.path): # If path does not exist, redirect to root album if EXTERNAL_BASE_URL is not None: return redirect(EXTERNAL_BASE_URL) else: return redirect('/') else: if not self.pyntrest_handler: return render(request, 'pyntrest/index.html', None) # check if static files like css have been updated self.pyntrest_handler.upgrade_static_files() view_context = self.pyntrest_handler.generate_view_context( request.path) return render(request, 'pyntrest/index.html', view_context)
def test_get(self): views_h = ViewHandler() static_images_dir = self.generate_test_dirs() pyntrest_handler = PyntrestHandler(self.mip, static_images_dir) self.addCleanup(rmtree, path.join(self.sip, 'images'), True) views_h.set_pyntrest_handler(pyntrest_handler) self.assertRaises(TypeError, views_h.get) request = HttpRequest() request.path = '///' self.assertIs(HttpResponseRedirect, type(views_h.get(request))) request = HttpRequest() request.path = '/index.html' self.assertIs(HttpResponseRedirect, type(views_h.get(request))) request = HttpRequest() request.path = '' redirect = views_h.get(request) self.assertIs(HttpResponseRedirect, type(redirect)) self.assertEquals('/', redirect.url) request = HttpRequest() request.path = '/notexistingalbum' request = HttpRequest() request.path = '/' response = type(views_h.get(request)) self.assertIs(HttpResponse, response) self.assertEquals(200, response.status_code)
def test (self): test_images_dir, static_images_dir = self.generate_test_dirs() pyh = PyntrestHandler( test_images_dir, static_images_dir) context = pyh.generate_view_context('/') self.assertEquals(pyntrest_config.IMAGE_THUMB_WIDTH, context['col_width']) self.assertEquals(pyntrest_config.IMAGE_THUMB_HEIGHT, context['col_height']) self.assertEquals(pyntrest_config.WORDING_ALBUM, context['lang_albums']) self.assertEquals(pyntrest_config.WORDING_IMAGES, context['lang_images']) self.assertEquals('title', context['page_title']) self.assertEquals('desc', context['album_description']) breadcrumbs = context['breadcrumbs'] images = context['images'] subalbums = context['subalbums'] self.assertEqual(1, len(breadcrumbs)) self.assertEqual(4, len(images)) self.assertEqual(1, len(subalbums))
def test(self): test_images_dir, static_images_dir = self.generate_test_dirs() pyh = PyntrestHandler(test_images_dir, static_images_dir) context = pyh.generate_view_context('/') self.assertEquals(pyntrest_config.IMAGE_THUMB_WIDTH, context['col_width']) self.assertEquals(pyntrest_config.IMAGE_THUMB_HEIGHT, context['col_height']) self.assertEquals(pyntrest_config.WORDING_ALBUM, context['lang_albums']) self.assertEquals(pyntrest_config.WORDING_IMAGES, context['lang_images']) self.assertEquals('title', context['page_title']) self.assertEquals('desc', context['album_description']) breadcrumbs = context['breadcrumbs'] images = context['images'] subalbums = context['subalbums'] self.assertEqual(1, len(breadcrumbs)) self.assertEqual(4, len(images)) self.assertEqual(1, len(subalbums))
def test_init(self): self.assertRaises(TypeError, PyntrestHandler) self.assertRaises(TypeError, PyntrestHandler, '') test_images_dir, static_images_dir = self.generate_test_dirs() pyh = PyntrestHandler(test_images_dir, static_images_dir) self.assertIsNotNone(pyh)
"""WSGI-hook for Pyntrest to combine Django with a WSGI-compatible web server like Apache HTTP or Unicorn.""" # The following two lines MUST stick to the top in that order # right on top. Otherwise WSGI-usage will not work. from os import environ environ.setdefault('DJANGO_SETTINGS_MODULE', 'pyntrest_project.settings') from django.core.wsgi import get_wsgi_application from pyntrest.pyntrest_core import PyntrestHandler from pyntrest.pyntrest_config import YOUR_IMAGE_FOLDER, STATIC_PATH # run Pyntrest's system initialization procedure pyntrest_handler = PyntrestHandler(YOUR_IMAGE_FOLDER, STATIC_PATH) pyntrest_handler.on_startup() application = get_wsgi_application()
def __init__(self): """Constructor""" self.pyntrest_handler = PyntrestHandler(YOUR_IMAGE_FOLDER, STATIC_PATH)