def library_edit_wizard(library_id): # Get library object entry = db.Library.find_one({'_id':library_id}) if entry is None: flash('Cannot find ' + str(library_id) + ' for editing') return redirect(url_for('index')) # Permissions if session.get('user_email', None) != entry.user: flash('%s does not have permission to edit this entry') return redirect(url_for('index')) form = BaseWizard(request.form, obj=entry) if request.method == 'POST' and form.validate(): form.populate_obj(entry) # Setup the polygon geo_positional_data = None if request.form.get('geo') is not None: pts = [] geo_string = request.form.get('geo') point_array = geo_string.split('),(') try: for pt in filter(None, point_array): pt = pt.split(",") pts.append((float(pt[1].replace(")","")),float(pt[0].replace("(","")))) # Create the polygon geo_positional_data = unicode(Polygon(pts).wkt) except: app.logger.warning("Could not build Polygon from: %s" % point_array) lib = dict() lib['geometry'] = geo_positional_data lib['created'] = datetime.datetime.utcnow() lib['user'] = session['user_email'] # Safe because of @login_required decorator entry.copy_from_dictionary(lib) entry.build_keywords() db.libraries.ensure_index('_keywords') entry.save() # rebuild the indexes db.libraries.reindex() flash('Edited library entry %s' % str(entry._id)) return redirect(url_for('detail_view', library_id=entry._id )) else: marker_positions = [] # load the polygon if entry.geometry: polygon = loads(entry.geometry) for pt in polygon.exterior.coords: # Google maps is y,x not x,y marker_positions.append((pt[1], pt[0])) form.markers = marker_positions return render_template('library_wizard.html', form=form)
def library_wizard(): form = BaseWizard(request.form) if request.method == 'POST' and form.validate(): # Setup the polygon geo_positional_data = None if request.form.get('geo') is not None: pts = [] geo_string = request.form.get('geo') point_array = geo_string.split('),(') try: for pt in filter(None, point_array): pt = pt.split(",") pts.append((float(pt[1].replace(")","")),float(pt[0].replace("(","")))) # Create the polygon geo_positional_data = unicode(Polygon(pts).wkt) except: app.logger.warning("Could not build Polygon from: %s" % point_array) entry = db.Library() form.populate_obj(entry) lib = dict() lib['geometry'] = geo_positional_data lib['created'] = datetime.datetime.utcnow() lib['user'] = session['user_email'] # Safe because of @login_required decorator entry.copy_from_dictionary(lib) if entry.local_validate() is False: flash('%s already exists, please change the name to submit' % (entry.name)) else: entry.build_keywords() db.libraries.ensure_index('_keywords') entry.save() # rebuild the indexes db.libraries.reindex() flash('Created library entry %s' % str(entry._id)) return redirect(url_for('detail_view', library_id=entry._id )) return render_template('library_wizard.html', form=form)
def library_edit_wizard(library_id): # Get objects entry = db.Library.find_one({'_id':library_id}) user = db.User.find_one({ 'email' : session.get("user_email")}) if entry is None: flash('Cannot find ' + str(library_id) + ' for editing') return redirect(url_for('index')) # Permissions if not authorize_entry_write(entry=entry, user=user): flash("Not authorized edit this library item") return redirect(url_for('index')) form = BaseWizard(request.form, obj=entry) if request.method == 'POST' and form.validate(): form.populate_obj(entry) # Setup the polygon geo_positional_data = None if request.form.get('geo') is not None: pts = [] geo_string = request.form.get('geo') geo_string = geo_string.lstrip('(').rstrip(')') point_array = geo_string.split('),(') try: for pt in filter(None, point_array): pt = pt.split(',') pts.append((float(pt[1].strip()),float(pt[0].strip()))) # Create the polygon geo_positional_data = unicode(Polygon(pts).wkt) except: app.logger.warning("Could not build Polygon from: %s" % pts) entry['geometry'] = geo_positional_data entry['created'] = datetime.datetime.utcnow() entry['user'] = session['user_email'] # Safe because of @login_required decorator if entry.local_validate() is False: flash('Library with a name: %s and creator: %s already exists, please change the name to edit' % (entry.name, entry.user)) else: entry.build_keywords() db.libraries.ensure_index('_keywords') entry.save() # rebuild the indexes db.libraries.reindex() flash('Edited library entry %s' % str(entry._id)) return redirect(url_for('detail_view', library_id=entry._id )) else: marker_positions = [] # load the polygon if entry.geometry: polygon = loads(entry.geometry) for pt in polygon.exterior.coords: # Google maps is y,x not x,y marker_positions.append((pt[1], pt[0])) form.markers = marker_positions return render_template('library_wizard.html', form=form)