#!/usr/bin/env python # -*- coding: utf-8 -*- """ Super-duper web.py based ahtung. """ import web from web import form import os from datetime import datetime, timedelta from flickrplaces import Controller urls = ('/', 'index') render = web.template.render('templates/', cache=False) def IntegerTextbox(*args, **kwargs): args = args + (form.regexp('^\d+$', u'Требуется число'),) return form.Textbox(*args, **kwargs) notempty = form.Validator(u'Требуются данные', bool) SearchForm = form.Form( form.Textbox('place', notempty, description=u'Город'), IntegerTextbox('count', notempty, description=u'Количество фотографий', value=10), IntegerTextbox('threads_count', notempty, description=u'Количество нитей закачки', value=10), form.Textbox('resize_to', form.regexp(r'(\d+|)$', u'Требуется число'), description=u'Размер иконики', post='В случае пустого поля, иконка будет браться с flickr.com'), IntegerTextbox('days', notempty, description=u'Дни', post='Выбирать фотографии возраст, которых, не больше заданного', value=(365 * 5)) ) class index: def GET(self): search_form = SearchForm() print render.index(None, None, search_form) def POST(self): search_form = SearchForm(web.input()) if search_form.validates(): place = search_form.d.place count = int(search_form.d.count) threads_count = int(search_form.d.threads_count) #raise Exception(search_form.d.threads_count) resize_to = search_form.d.resize_to and int(search_form.d.resize_to) or None days = int(search_form.d.days) ctl = Controller(api_key='23d396907dea3933cde716a4c4b4f6d3', dir='static', debug_mode=False, threads_count=threads_count, resize_to=resize_to, size_priorities=['Large', 'Medium', 'Small', 'Thumbnail'], min_upload_date = datetime.now() - timedelta(days=days), ) photos = ctl.get_images_for_place(place, count) for photo in photos: if resize_to: photo['src'] = os.path.join('/static', os.path.split(photo['resized'])[1]) else: photo['src'] = photo['sizes']['Thumbnail']['source'] #photos = [{'src': '/static/2354410450.thumb.jpg', 'source': '', 'title': '', 'tags': ''}] else: place = web.input().get('place', '') photos = [] print render.index(place, photos, search_form) web.webapi.internalerror = web.debugerror if __name__ == "__main__": web.run(urls, globals(), web.reloader)