def _do_edit_form_pdf(self, media_id): c.pdf = h.get_media_by_id(media_id) pdf_file = request.POST.get('userfile') if pdf_file is None: c.error = u'No file uploaded' response.content_type = 'application/javascript' return simplejson.dumps(dict(html=render('/media/error.mako'))) filename = pdf_file.filename mimetype, _ = mimetypes.guess_type(filename) if mimetype != 'application/pdf': c.error = u'Invalid pdf file' response.content_type = 'application/javascript' return simplejson.dumps(dict(html=render('/media/error.mako'))) pdf_file.make_file() pdf_data = pdf_file.file.read() f = open(c.pdf.path, 'w') f.write(pdf_data) f.close() c.pdf.filename = filename meta.Session.commit() c.editable = True response.content_type = 'application/javascript' return simplejson.dumps(dict(html=render('/media/pdf/item.mako')))
def _do_edit_form_audio(self, media_id): c.audio = h.get_media_by_id(media_id) audio_file = request.POST.get('userfile') if audio_file is None: c.error = u'No file uploaded' response.content_type = 'application/javascript' return simplejson.dumps(dict(html=render('/media/error.mako'))) filename = audio_file.filename mimetype, _ = mimetypes.guess_type(filename) if mimetype != 'audio/mpeg': c.error = u'Invalid audio file. You must upload an mp3 file' response.content_type = 'application/javascript' return simplejson.dumps(dict(html=render('/media/error.mako'))) audio_file.make_file() audio_data = audio_file.file.read() f = open(c.audio.path, 'w') f.write(audio_data) f.close() c.audio.filename = filename meta.Session.commit() c.editable = True c.flowplayer_id = 'pagemedia_%s' % c.audio.id c.audio_url = request.application_url + c.audio.url response.content_type = 'application/javascript' return simplejson.dumps(dict(html=render('/media/audio/item.mako'), flowplayer_id=c.flowplayer_id, audio_url=c.audio_url, ))
def _do_edit_form_image(self, media_id): c.image = h.get_media_by_id(media_id) image_file = request.POST.get('userfile') if image_file is None: c.error = u'No file uploaded' response.content_type = 'application/javascript' return simplejson.dumps(dict(html=render('/media/error.mako'))) filename = image_file.filename mimetype, _ = mimetypes.guess_type(filename) if not mimetype.startswith('image/'): c.error = u'Invalid image file' response.content_type = 'application/javascript' return simplejson.dumps(dict(html=render('/media/error.mako'))) image_file.make_file() image_data = image_file.file.read() f = open(c.image.path, 'w') f.write(image_data) f.close() c.image.create_scales(g.images_path) c.image.filename = filename meta.Session.commit() c.editable = True response.content_type = 'application/javascript' return simplejson.dumps(dict(html=render('/media/image/item.mako')))
def edit(self, almanac_slug, page_slug): c.almanac = h.get_almanac_by_slug(almanac_slug) c.page = h.get_page_by_slug(c.almanac, page_slug) c.media_items = h.render_media_items(c.page.media, editable=True) map_features = h.map_features_for_media(c.page.media) c.map_features = h.literal(simplejson.dumps(map_features)) flow_data = h.flowplayer_data_for_media(c.page.media) c.flow_data = h.literal(simplejson.dumps(flow_data)) c.is_add = False c.behalf = render('/page/behalf.mako') return render('/page/add_edit.mako')
def create(self, almanac_slug): c.almanac = h.get_almanac_by_slug(almanac_slug) c.page = page = c.almanac.new_page(self.ensure_user) media_items = page.media c.media_items = h.render_media_items(media_items, editable=True) map_features = h.map_features_for_media(media_items) c.map_features = h.literal(simplejson.dumps(map_features)) flow_data = h.flowplayer_data_for_media(media_items) c.flow_data = h.literal(simplejson.dumps(flow_data)) c.is_add = True c.behalf = render('/page/behalf.mako') return render('/page/add_edit.mako')
def _do_new_form_image(self, almanac_slug, page_slug=None): c.almanac = h.get_almanac_by_slug(almanac_slug) image_file = request.POST.get('userfile') if image_file is None: c.error = u'No file uploaded' response.content_type = 'application/javascript' return simplejson.dumps(dict(html=render('/media/error.mako'))) page = self._retrieve_page(c.almanac, page_slug) try: c.image = image = Image.from_file(image_file.filename, page, upload=image_file) except (ValueError, IOError), e: c.error = str(e.message) or str(e) response.content_type = 'application/javascript' return simplejson.dumps(dict(html=render('/media/error.mako')))
def almanacs_kml(self): json = request.params.get('extent') # We need to make sure we only select almanacs with pages here, query = meta.Session.query(Almanac).join(Almanac.pages).distinct() # ... and eager-load the pages since the ktml template uses them. query = query.options(eagerload(Almanac.pages)) # Tried also with contains_eager, not sure what the difference is # but I only get a fraction of the expected almanacs: #query = meta.Session.query(Almanac).join(Almanac.pages).distinct() #query = query.options(contains_eager(Almanac.pages)) # Also tried using a single, second query for the pages. # ... NOPE, requires sqlalchemy > 0.6.0 which blows up on us, # maybe not compatible w/ pylons 0.9.7? #query = meta.Session.query(Almanac).join(Almanac.pages).distinct() #query = query.options(subqueryload(Almanac.pages)) # Tried also without the explicit join().distinct(), this gives # back all almanac whether they have any pages or not: #query = meta.Session.query(Almanac).options(eagerload(Almanac.pages)) if json is not None: shape = simplejson.loads(json) # Stupid asShape returns an Adapter instead of a Geometry. We round # trip it through wkb to get the correct type. bbox = wkb.loads(asShape(shape).to_wkb()) query = query.filter(func.st_intersects(Almanac.location, func.st_transform('SRID=%s;%s' % ('4326', b2a_hex(bbox.to_wkb())), storage_SRID))) c.almanacs = query.order_by(Almanac.modified.desc()).limit(200).all() response.content_type = 'application/vnd.google-earth.kml+xml kml' return render('/almanac/kml.mako')
def pages_kml_link(self, almanac_slug, query): almanac = h.get_almanac_by_slug(almanac_slug) c.query = query c.name = almanac.name c.slug = almanac_slug response.content_type = 'application/vnd.google-earth.kml+xml kml' return render('/page/kml_link.mako')
def _do_new_form_map(self, almanac_slug, page_slug=None): c.almanac = h.get_almanac_by_slug(almanac_slug) page = self._retrieve_page(c.almanac, page_slug) json = request.POST.get('feature') if json is None: abort(400) shape = simplejson.loads(json) # Stupid asShape returns a PointAdapter instead of a Point. We round # trip it through wkb to get the correct type. location = wkb.loads(asShape(shape).to_wkb()) location.srid = 4326 c.map = map = Map() map.location = location map.page_id = page.id map.order = len(page.media) meta.Session.add(map) meta.Session.commit() geometry = c.map.location_4326.__geo_interface__ c.editable = True return dict(html=render('/media/map/item.mako'), map_id='pagemedia_%d' % map.id, geometry=geometry, )
def view(self, almanac_slug): c.almanac = h.get_almanac_by_slug(almanac_slug) loc = c.almanac.location_4326 c.lng, c.lat = loc.x, loc.y page_idx = request.GET.get('page', 1) try: page_idx = int(page_idx) except ValueError: page_idx = 1 pages_query = meta.Session.query(Page).filter(Page.almanac_id==c.almanac.id).filter(Page.published == True).order_by(Page.modified.desc()) try: c.next_page = pages_query[:1][0] except IndexError: pass else: c.next_page_url = h.url_for('page_view', almanac=c.almanac, page=c.next_page) c.next_page_text = c.next_page.name per_page = 10 pagination = PaginationPage(pages_query, page=page_idx, items_per_page=per_page) c.toc_pagination_data = h.pagination_data(pagination) c.pages = pagination.items c.npages = pagination.item_count return render('/almanac/view.mako')
def _do_on_behalf_of_form(self, page_id): c.page = h.get_page_by_id(page_id) on_behalf_of = request.POST.get('on_behalf_of') if on_behalf_of is None: abort(400) c.page.on_behalf_of = on_behalf_of meta.Session.commit() return render('/page/behalf.mako')
def login(self): c.no_maps = True c.active_section = request.params.get('show','login-new') c.captcha_html = h.literal(recaptcha.client.captcha.displayhtml( g.captcha_pubkey)) if request.environ.get('repoze.who.identity') == None: return render('/user/login.mako') redirect(request.params.get('came_from', h.url_for('home')))
def map_view(self, media_id): c.editable = True c.map = h.get_media_by_id(media_id) geometry = c.map.location_4326.__geo_interface__ return dict(html=render('/media/map/item.mako'), map_id='pagemedia_%d' % c.map.id, geometry=geometry, )
def pages_kml(self, almanac_slug, query): c.almanac = h.get_almanac_by_slug(almanac_slug) if query: c.pages = c.almanac.search(query).all() else: c.pages = c.almanac.pages response.content_type = 'application/vnd.google-earth.kml+xml kml' return render('/page/kml.mako')
def audio_view(self, media_id): c.editable = True c.audio = h.get_media_by_id(media_id) c.flowplayer_id = 'pagemedia_%s' % c.audio.id c.audio_url = request.application_url + c.audio.url return dict(html=render('/media/audio/item.mako'), flowplayer_id=c.flowplayer_id, audio_url=c.audio_url, )
def edit_form_map(self, media_id): c.media_item = c.map = h.get_media_by_id(media_id) geometry = c.map.location_4326.__geo_interface__ c.view_url = h.url_for('media_map_view', media_id=c.media_item.id) c.legend = u'Map' return dict(html=render('/media/map/form.mako'), map_id='pagemedia_%d' % c.map.id, geometry=geometry, )
def register(self): c.no_maps = True if request.environ.get('repoze.who.identity') == None: c.active_section = request.params.get('show','login-new') c.captcha_html = h.literal(recaptcha.client.captcha.displayhtml( g.captcha_pubkey)) return render('/user/login.mako') h.flash(u'Can\'t register while logged in. Please logout first.') redirect_to(h.url_for('home'))
def new_form_audio(self, almanac_slug, page_slug=None): c.almanac = h.get_almanac_by_slug(almanac_slug) page = self._retrieve_page(c.almanac, page_slug) c.file_id = str(uuid.uuid4()) c.file_upload_url = request.path_url c.legend = u'MP3 Audio' return dict(html=render('/media/audio/form.mako'), file_id=c.file_id, file_upload_url=c.file_upload_url, )
def edit_form_image(self, media_id): c.media_item = c.image = h.get_media_by_id(media_id) c.file_id = str(uuid.uuid4()) c.file_upload_url = request.path_url c.view_url = h.url_for('media_image_view', media_id=c.media_item.id) c.legend = u'Image' return dict(html=render('/media/image/form.mako'), file_id=c.file_id, file_upload_url=c.file_upload_url, )
def new_form_map(self, almanac_slug, page_slug=None): c.almanac = h.get_almanac_by_slug(almanac_slug) page = self._retrieve_page(c.almanac, page_slug) loc = c.almanac.location_4326 c.map_id = str(uuid.uuid4()) c.legend = u'Map' return dict(html=render('/media/map/form.mako'), lat=loc.y, lng=loc.x, map_id=c.map_id, )
def edit_form_text(self, media_id): c.media_item = h.get_media_by_id(media_id) c.view_url = h.url_for('media_story_view', media_id=c.media_item.id) c.legend = u'Text' new_uuid = str(uuid.uuid4()) c.textarea_id = 'mceSimple_%s' % new_uuid return dict(html=render('/media/story/form.mako'), storyinput_id=c.storyinput_id, textarea_id=c.textarea_id, )
def edit_form_pdf(self, media_id): c.media_item = c.pdf = h.get_media_by_id(media_id) c.file_id = str(uuid.uuid4()) c.file_upload_url = request.path_url c.view_url = h.url_for('media_pdf_view', media_id=c.media_item.id) c.legend = u'PDF' return dict(html=render('/media/pdf/form.mako'), file_id=c.file_id, file_upload_url=c.file_upload_url, )
def new_form_text(self, almanac_slug, page_slug=None): c.almanac = h.get_almanac_by_slug(almanac_slug) page = self._retrieve_page(c.almanac, page_slug) c.legend = u'Text' new_uuid = str(uuid.uuid4()) c.textarea_id = 'mceSimple_%s' % new_uuid return dict(html=render('/media/story/form.mako'), storyinput_id=c.storyinput_id, textarea_id=c.textarea_id, )
def _do_new_form_audio(self, almanac_slug, page_slug=None): c.almanac = h.get_almanac_by_slug(almanac_slug) audio_file = request.POST.get('userfile') if audio_file is None: c.error = u'No file uploaded' response.content_type = 'application/javascript' return simplejson.dumps(dict(html=render('/media/error.mako'))) filename = audio_file.filename mimetype, _ = mimetypes.guess_type(filename) if mimetype != 'audio/mpeg': c.error = u'Invalid audio file. You must upload an mp3 file' response.content_type = 'application/javascript' return simplejson.dumps(dict(html=render('/media/error.mako'))) page = self._retrieve_page(c.almanac, page_slug) audio_file.make_file() audio_data = audio_file.file.read() new_uuid = str(uuid.uuid4()) fname = '%s.mp3' % new_uuid path = os.path.join(g.audio_path, fname) f = open(path, 'w') f.write(audio_data) f.close() c.audio = audio = Audio() audio.path = path audio.page_id = page.id audio.order = len(page.media) audio.filename = filename meta.Session.add(audio) meta.Session.commit() c.editable = True c.flowplayer_id = 'pagemedia_%s' % c.audio.id c.audio_url = request.application_url + c.audio.url response.content_type = 'application/javascript' return simplejson.dumps(dict(html=render('/media/audio/item.mako'), flowplayer_id=c.flowplayer_id, audio_url=c.audio_url, ))
def _perform_reset(self, username, key): myparams = dict(request.params) myparams['username'] = username myparams['key'] = key schema = PerformResetSchema() try: form_result = schema.to_python(myparams) except validators.Invalid, error: c.form_result = error.value c.form_errors = error.error_dict or {} return render('/user/perform_reset.mako')
def _do_edit_form_video(self, media_id): c.video = h.get_media_by_id(media_id) body = request.POST.get('body', u'') if not body: abort(400) c.video.text = h.clean_embed_markup(body) meta.Session.commit() c.editable = True return dict(html=render('/media/video/item.mako'))
def pages_atom(self, almanac_slug, query): c.almanac = h.get_almanac_by_slug(almanac_slug) if query: c.pages = c.almanac.search(query).all() else: c.pages = c.almanac.pages c.query = query c.name = c.almanac.name c.slug = almanac_slug response.content_type = 'application/atom+xml' return render('/page/atom.mako')
def geocode(self): # The geocoder works from either a name or a point. location = request.GET.get('location') bbox = simplejson.loads(request.GET.get('bbox','false')) if location is None and not bbox: abort(400) geoc = geocoders.Google(g.map_key, output_format='json') name_based = False if not bbox: # We don't have a point, so we work with the name... name_based = True try: result = GeocoderController._result_with_locality(geoc.geocode(location, exactly_one=False)) if not result: return {} if not result.locality or not result.administrative: # We want the geocoder to give us an canonical name for # this location. If it hasn't (because the user searched # using a name that doesn't match up to the canonical name, # we use the location to find the canonical name. result = GeocoderController._result_with_locality(geoc.geocode('%f, %f' % (result.latitude, result.longitude), exactly_one=False)) place, (lat, lng) = result except ValueError: return {} else: bbox = asShape(bbox) try: result = GeocoderController._result_with_locality(geoc.geocode('%f, %f' % (bbox.centroid.y, bbox.centroid.x), exactly_one=False)) place, (lat, lng) = result except ValueError: return {} if result.locality and result.administrative: authoritative_name = '%s, %s' % (result.locality, result.administrative) else: authoritative_name = None try: meta.Session.query(Almanac).filter(Almanac.name==authoritative_name).one() almanac = True except exc.NoResultFound: almanac = False geopoint = Point(lng, lat) if bbox: c.almanacs = self._nearby_almanacs(bbox) else: c.almanacs = meta.Session.query(Almanac).join(Almanac.pages).distinct().filter(func.st_dwithin(Almanac.location, func.st_transform(func.st_geomfromtext('SRID=%s;POINT(%f %f)' % ('4326', result.longitude, result.latitude)), storage_SRID), 6233)).limit(10).all() nearby_kml = render('/almanac/kml.mako') return dict(lat=lat, lng=lng, layer=nearby_kml, geojson=simplejson.dumps(geopoint.__geo_interface__), authoritative_name=authoritative_name, name_based=name_based, almanac=almanac)
def home(self): page_idx = request.GET.get('page', 1) try: page_idx = int(page_idx) except ValueError: page_idx = 1 almanac_query = meta.Session.query(Almanac).join(Almanac.pages).distinct().order_by(Almanac.modified.desc()) c.count = almanac_query.count() h.setup_pagination(almanac_query, page_idx) c.almanacs = c.pagination.items # Almanacs are slightly smaller on the page, we need to show slightly less so that the almanacs are resting on the shelf. c.pages = Page.latest(limit=(max(len(c.almanacs)-2,0))) c.is_homepage = True return render('/home.mako')
def edit_form_audio(self, media_id): c.media_item = c.audio = h.get_media_by_id(media_id) c.file_id = str(uuid.uuid4()) c.file_upload_url = request.path_url c.flowplayer_id = 'pagemedia_%s' % c.media_item.id c.audio_url = request.application_url + c.audio.url c.view_url = h.url_for('media_audio_view', media_id=c.media_item.id) c.legend = u'MP3 Audio' return dict(html=render('/media/audio/form.mako'), file_id=c.file_id, file_upload_url=c.file_upload_url, flowplayer_id=c.flowplayer_id, audio_url=c.audio_url, )