def delete_lifestage(library_id, lifestage_id): entry = db.Library.find_one({'_id': library_id}) user = db.User.find_one({ 'email' : session.get("user_email")}) if not authorize_entry_write(entry=entry, user=user): flash("Not authorized to delete lifestages for this library item") return redirect(url_for('detail_view', library_id=entry._id )) else: lifestage = db.LifeStage.find_one({'_id': lifestage_id}) entry.lifestages.remove(lifestage) entry.save() for d in lifestage.diel: d.delete() for t in lifestage.taxis: t.delete() if lifestage.capability is not None: lifestage.capability.delete() if lifestage.settlement is not None: lifestage.settlement.delete() lifestage.capability = None lifestage.settlement = None lifestage.diel = [] lifestage.taxis = [] lifestage.delete() flash('Deleted LifeStage') return redirect(url_for('detail_view', library_id=entry._id ))
def detail_view(library_id): if library_id is None: flash('Recieved an entry without an id') return redirect(url_for('index')) 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 object ' + str(library_id)) return redirect(url_for('index')) 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])) entry['markers'] = marker_positions auth = False # Permissions if authorize_entry_write(entry=entry, user=user): auth = True return render_template('library_detail.html', entry=entry, auth=auth)
def clone_lifestage(library_id, lifestage_id): # Be sure the logged in user has access to this library item entry = db.Library.find_one({'_id': library_id}) user = db.User.find_one({ 'email' : session.get("user_email")}) if not authorize_entry_write(entry=entry, user=user): flash("Not authorized to clone lifestages for this library item") return redirect(url_for('detail_view', library_id=entry._id )) else: lifestage = db.LifeStage.find_one({'_id': lifestage_id}) newlifestage = lifestage.clone() newlifestage.name = lifestage.name + " (cloned)" newlifestage.save() entry.lifestages.append(newlifestage) entry.save() flash('Cloned LifeStage') return redirect(url_for('detail_view', library_id=entry._id ))
def reorder_lifestages(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')) lifestage_ids = request.form.get('lifestages').split(',') entry.lifestages = [] for lfid in lifestage_ids: ls = db.LifeStage.find_one({'_id': ObjectId(lfid)}) entry.lifestages.append(ls) entry.save() return jsonify({"results" : "success" })
def edit_lifestage(library_id, lifestage_id): entry = db.Library.find_one({'_id': library_id}) user = db.User.find_one({ 'email' : session.get("user_email")}) if not authorize_entry_write(entry=entry, user=user): flash("Not authorized to edit lifestages for this library item") return redirect(url_for('detail_view', library_id=entry._id )) else: lifestage = db.LifeStage.find_one({'_id': lifestage_id}) if lifestage is None: flash('Cannot find lifestage ' + str(lifestage_id)) return redirect(url_for('detail_view', library_id=entry._id )) form = LifeStageWizard(request.form, obj=lifestage) if request.method == 'POST' and form.validate(): lifestage.name = form.name.data lifestage.duration = form.duration.data lifestage.notes = form.notes.data if form.linear.data: if form.linear_a.data: lifestage.linear_a = float(form.linear_a.data) if form.linear_b.data: lifestage.linear_b = float(form.linear_b.data) lifestage.diel = [] lifestage.taxis = [] lifestage.capability = None lifestage.settlement = None # Capability if form.capability.data: c = db.Capability() c.vss = float(form.vss.data) c.variance = float(form.variance.data) c.swim_turning = form.swim_turning.data c.nonswim_turning = form.nonswim_turning.data c.save() lifestage.capability = c # Settlement if form.settlement.data: s = db.Settlement() s.type = form.settle_type.data s.upper = float(form.settle_upper.data) s.lower = float(form.settle_lower.data) s.save() lifestage.settlement = s # Diel if form.diel_data.data and len(form.diel_data.data) > 0: parsed_diel = json.loads(form.diel_data.data) for diel_data in parsed_diel: d = db.Diel() d.type = unicode(diel_data['diel_type']) d.min = float(diel_data['min']) d.max = float(diel_data['max']) if d.type == u'cycles': d.cycle = unicode(diel_data['cycle']) d.plus_or_minus = unicode(diel_data['plus_or_minus']) d.hours = int(diel_data['hours']) elif d.type == u'specifictime': t = parse("%s %s" % (diel_data['diel_time'], diel_data['timezone'])) try: d.time = pytz.utc.normalize(t).replace(tzinfo=None) except: # already in GMT d.time = t d.save() lifestage.diel.append(d) if form.taxis_data.data and len(form.taxis_data.data) > 0: parsed_taxis = json.loads(form.taxis_data.data) for taxis_data in parsed_taxis: t = db.Taxis() t.variable = taxis_data['variable'] t.units = taxis_data['units'] t.min = float(taxis_data['min']) t.max = float(taxis_data['max']) t.gradient = float(taxis_data['gradient']) t.save() lifestage.taxis.append(t) lifestage.save() flash('Edited LifeStage') return redirect(url_for('detail_view', library_id=entry._id )) else: diels = [] for diel in lifestage.diel: diels.append(diel.to_data()) form.diel_data.data = json.dumps(diels) taxis = [] for tx in lifestage.taxis: taxis.append(tx.to_data()) form.taxis_data.data = json.dumps(taxis) if lifestage.capability: form.vss.data = lifestage.capability.vss form.variance.data = lifestage.capability.variance form.swim_turning.data = lifestage.capability.swim_turning form.nonswim_turning.data = lifestage.capability.nonswim_turning if lifestage.settlement: form.settle_type.data = lifestage.settlement.type form.settle_upper.data = lifestage.settlement.upper form.settle_lower.data = lifestage.settlement.lower form.linear.data = isinstance(lifestage.linear_a, float) and isinstance(lifestage.linear_b, float) return render_template('lifestage_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)