# Community Almanac - A place for your stories.
# Copyright (C) 2009  Douglas Mayle, Robert Marianski,
# Andy Cochran, Chris Patterson

# This file is part of Community Almanac.

# Community Almanac is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# Community Almanac is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with Community Almanac.  If not, see <http://www.gnu.org/licenses/>.

import logging

from communityalmanac.lib.helpers import name_almanac
from communityalmanac.model import Almanac
from communityalmanac.model import meta
from formencode import Invalid
from formencode import Schema
from formencode import validators
from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to
from pylons.decorators import jsonify
from pylons.decorators import validate
from pylons.decorators.rest import dispatch_on
from shapely.geometry.geo import asShape
from sqlalchemy.orm import exc
import communityalmanac.lib.helpers as h
import simplejson

from communityalmanac.lib.base import BaseController, render

log = logging.getLogger(__name__)

class AlmanacCreateForm(Schema):
    name = validators.String(not_empty=True)
    almanac_center = validators.String(not_empty=True)

class AlmanacController(BaseController):

    @dispatch_on(POST='_do_create')
    def create(self):
        redirect_to(h.url_for('home'))

    @validate(schema=AlmanacCreateForm(), form='create')
    def _do_create(self):
        name = self.form_result['name']

        # Prevent creation of duplicates
        almanac = meta.Session.query(Almanac).filter(Almanac.name==name).one()
        if almanac:
            return redirect_to(h.url_for('page_create', almanac_slug=almanac.slug))

        json = self.form_result['almanac_center']
        shape = simplejson.loads(json)
        point = asShape(shape)
        slug = name_almanac(name)
        almanac = Almanac(name, slug)
        almanac.location = point

        meta.Session.save(almanac)
        meta.Session.commit()

        redirect_to(h.url_for('page_create', almanac_slug=slug))

    def view(self, almanac_slug):
        c.almanac = h.get_almanac_by_slug(almanac_slug)
        loc = c.almanac.location
        c.lat, c.lng = loc.x, loc.y
        return render('/almanac/view.mako')

    @jsonify
    def center(self, almanac_slug):
        c.almanac = h.get_almanac_by_slug(almanac_slug)
        loc = c.almanac.location
        return dict(lat=loc.x, lng=loc.y)

@@ -2001,16 +2001,33 @@
 licates%0A
+        try:%0A    
         
@@ -2101,28 +2101,8 @@
 e()%0A
-        if almanac:%0A
     
@@ -2176,24 +2176,75 @@
 manac.slug))
+%0A        except exc.NoResultFound:%0A            pass
 %0A%0A        js

Fix non-existant case