def new_mural(): new_mural_form = NewMural() artists = firebase.get('/', 'artists') if artists is None: flash("Must first create an artist") return redirect(url_for('all_murals'), code=302) sorted_artists = [] for a in artists: sorted_artists.append((artists[a]["uuid"], artists[a]["name"])) sorted_artists = sorted(sorted_artists, key=lambda x: x[1]) new_mural_form.artist.choices = sorted_artists if new_mural_form.validate_on_submit(): uuid_token = uuid.uuid4() # the new mural's index = 1 + the number of existing murals murals = firebase.get('/', 'murals') index = 1 + len(murals or []) put_data = {'Photo': new_mural_form.photo.data, 'Lat': new_mural_form.lat.data, 'Long': new_mural_form.longitude.data, 'Artist': new_mural_form.artist.data, 'Title': new_mural_form.title.data, 'Month': new_mural_form.month.data, 'Year': new_mural_form.year.data, 'Description': new_mural_form.description.data, 'Medium': new_mural_form.medium.data, 'uuid': str(uuid_token), 'Index': index} firebase.put('/murals', uuid_token, put_data) return redirect(url_for('all_murals'), code=302) return render_template('new_mural.html', form=new_mural_form)
def artist_put(): new_art_form = NewArtist() if new_art_form.validate_on_submit(): uuid_token = uuid.uuid4() put_data = {'name': new_art_form.name.data, 'city': new_art_form.city.data, 'bio': new_art_form.bio.data, 'link': new_art_form.link.data, 'uuid': str(uuid_token)} firebase.put('/artists', uuid_token, put_data) return redirect(url_for('all_artists'), code=302) return render_template('new_artist.html', form=new_art_form)
def edit_artist(): form = EditArtist() artist_id = str(request.args["artists"]) artists = firebase.get('/', 'artists') artist = artists[artist_id] if form.validate_on_submit(): put_data = {'name': form.name.data, 'city': form.city.data, 'bio': form.bio.data, 'link': form.link.data, 'uuid': str(artist_id)} firebase.delete('/artists', str(artist_id)) firebase.put('/artists', artist_id, put_data) return redirect(url_for('all_artists'), code=302) return render_template('edit_artist.html', form=form, artist=artist)
def change_mural_index(): mural_id = str(request.form["muralid"]) up_or_down = str(request.form["upOrDown"]) murals = firebase.get('/', 'murals') # from_mural is the mural corresponding to the muralid # to_mural is the mural with the Index that from_mural's Index should be changed to from_mural = None to_mural = None for m in murals: if m == mural_id: from_mural = murals[m] # If the muralID turns out to not be valid if from_mural is None: return redirect(url_for('all_murals'), code=302) from_mural_index = from_mural["Index"] to_mural_index = 0 if up_or_down == "UP": to_mural_index = from_mural_index - 1 elif up_or_down == "DOWN": to_mural_index = from_mural_index + 1 for m in murals: if murals[m]["Index"] == to_mural_index: to_mural = murals[m] # Handles when you want to move to an invalid index if to_mural is None: return redirect(url_for('all_murals'), code=302) from_mural["Index"] = to_mural_index to_mural["Index"] = from_mural_index # update firebase firebase.put('/murals', from_mural["uuid"], from_mural) firebase.put('/murals', to_mural["uuid"], to_mural) return redirect(url_for('all_murals'), code=302)
def delete_mural(): # reindex all the murals murals = firebase.get('/', 'murals') key = str(request.form["muralid"]) removed_index = murals[key]["Index"] # if a mural's index is > removed_index, decrement the index for m in murals: if murals[m]["Index"] > removed_index: murals[m]["Index"] -= 1 # remove the mural if key in murals: del murals[key] # update firebase firebase.delete('/murals', key) for id in murals: firebase.put('/murals', id, murals[id]) return redirect(url_for('all_murals'), code=302)
def edit_mural(): edit_form = EditMural() mural_id = str(request.args["muralid"]) murals = firebase.get('/', 'murals') mural = murals[mural_id] artists = firebase.get('/', 'artists') sorted_artists = [] for a in artists: sorted_artists.append((artists[a]["uuid"], artists[a]["name"])) sorted_artists = sorted(sorted_artists, key=lambda x: x[1]) edit_form.artist.choices = sorted_artists if edit_form.validate_on_submit(): put_data = {'Photo': edit_form.photo.data, 'Lat': edit_form.lat.data, 'Long': edit_form.longitude.data, 'Artist': edit_form.artist.data, 'Title': edit_form.title.data, 'Month': edit_form.month.data, 'Year': edit_form.year.data, 'Description': edit_form.description.data, 'Medium': edit_form.medium.data, 'uuid': str(mural_id), 'Index': mural["Index"]} firebase.delete('/murals', str(mural_id)) firebase.put('/murals', mural_id, put_data) return redirect(url_for('all_murals'), code=302) return render_template('edit_mural.html', form=edit_form, mural=mural, murals=murals, muralid=mural_id)