def make_default_user_connections( ): # Used in reset_db() MUST CALL ADD_OBJECTS FIRST!!! user1 = User.objects(username='******').first() user2 = User.objects(username='******').first() user3 = User.objects(username='******').first() albany_artist = Artist.objects(name='Artist 3').first() artist1 = Artist.objects(name='Artist 1').first() add_member(user1, artist1) add_member(user3, albany_artist) follow_band(user2, artist1)
def edit_artist(artist_name): artist = Artist.objects(name=artist_name).first_or_404() if current_user not in artist.members: flash('You are not authorized to edit {}\'s information'.format( artist.name)) return redirect(url_for('main.artist', artist_name=artist.name)) form = EditArtistForm() form.location.choices = [(location.id, location.city + ', ' + location.state) for location in Location.objects()] form.genre.choices = [(genre.id, genre.name) for genre in Genre.objects.order_by('name')] if form.validate_on_submit(): artist.name = form.name.data artist.description = form.description.data artist.location = Location.objects(id=form.location.data).first() artist.genre = [ Genre.objects(id=genre).first() for genre in form.genre.data ] artist.save(cascade=True) flash('{} Edits Complete'.format(artist.name)) return redirect(url_for('main.artist', artist_name=artist.name)) elif request.method == 'GET': form.name.data = artist.name form.description.data = artist.description form.genre.data = [genre.id for genre in artist.genre] form.location.data = artist.location.id return render_template('edit_artist.html', form=form, title='Edit {}'.format(artist.name))
def add_artist_member(artist_name): artist = Artist.objects(name=artist_name).first_or_404() if current_user not in artist.members: flash('You are not authorized to add members to {}'.format( artist.name)) return redirect(url_for('main.artist', artist_name=artist.name)) form = AddArtistMemberForm() if form.validate_on_submit(): for member in artist.members: if form.username.data == member.username: flash('Cannot add a current member to your band!') return render_template('add_artist_member.html', form=form, artist=artist, title='Add Member') new_member = User.objects(username=form.username.data).first() if new_member is None: flash('User with that username does not exist!') return render_template('add_artist_member.html', form=form, artist=artist, title='Add Member') else: add_member(new_member, artist) flash('Added {} to {}'.format(new_member.username, artist.name)) return redirect(url_for('main.artist', artist_name=artist_name)) return render_template('add_artist_member.html', form=form, artist=artist, title='Add Member')
def add_song(self, **kwargs): artist_name = kwargs.get('title')[0] artist = Artist.objects(name=artist_name) song_name = kwargs.get('title')[-1] if not artist or Song.objects(name=song_name): self.add_artist(**kwargs) if self.is_song_exist(song_name): Log(type_added='song', name_added=song_name).save() return False self.download(kwargs.get('download_url')) size = kwargs.get('duration_size') size = ' '.join((size[2], size[3])) song = Song(artist=artist.get(), name=song_name, duration=kwargs.get('duration_size')[0], download_url=kwargs.get('download_url'), size=size) with open(TEMP_FILE, 'rb') as binary_file: song.audio_file.put(binary_file) shutil.rmtree(TEMP) if song: song.save() Log(type_added='song', name_added=song_name, added=True).save() return True Log(type_added='song', name_added=song_name).save() return False
def test_add_objects(self): db.connection.drop_database('porchfest_radio_test') add_objects() u1 = User.objects(username='******').first() self.assertIsNotNone(u1) # default user added a1 = Artist.objects(name='Artist 1').first() self.assertIsNotNone(a1) # default artist added
def artist(artist_name): artist = Artist.objects(name=artist_name).first_or_404() shows_for_artist = Show.objects(artist=artist, start_time__gt=datetime.utcnow) return render_template('artist.html', artist=artist, shows=shows_for_artist, title='{} Info'.format(artist.name))
def get_artist_by_song(song_name): song = Song.objects(name=song_name) if not song: return False artist = Artist.objects(_id=song.get().artist) if not artist: return False return artist
def get_artist_tracklist(artist_name): artist = Artist.objects(name=artist_name) if not artist: return False artist_id = artist.get().id tracklist = Song.objects(artist=artist_id) if not tracklist: raise False return tracklist
def unfollow_artist(artist_name): artist = Artist.objects(name=artist_name).first_or_404() user = User.objects(username=current_user.username).first() if artist is None: flash('Artist {} does not exist!'.format(artist_name)) elif current_user not in artist.followers: flash('You are not currently following {}'.format(artist.name)) else: unfollow_band(user, artist) flash('Successfully unfollowed {}'.format(artist.name)) return redirect(url_for('main.artist', artist_name=artist.name))
def logIn(): if current_user.is_authenticated: # flash("You are logged in!") return redirect(url_for('index')) form = LoginForm() if form.validate_on_submit(): user = Artist.objects(email=form.email.data).first() if user is None or not user.check_password(form.password.data): flash('Invalid username or password') return redirect(url_for('logIn')) login_user(user, remember=form.remember_me.data) flash('Login successful') next_page = request.args.get('next') if not next_page or url_parse(next_page).necloc != '': next_page = url_for('index') # maybe change this to artist's page return redirect(next_page) return render_template('login.html', form=form)
def test_make_user_connections(self): db.connection.drop_database('porchfest_radio_test') add_objects() make_default_user_connections() u1 = User.objects(username='******').first() u2 = User.objects(username='******').first() a1 = Artist.objects(name='Artist 1').first() # u1 is in band a1 self.assertTrue(u1 in a1.members) self.assertTrue(a1 in u1.member_of) # u2 is NOT in band a1 self.assertFalse(u2 in a1.members) self.assertFalse(a1 in u2.member_of) # u2 follows band a1 self.assertTrue(u2 in a1.followers) self.assertTrue(a1 in u2.follows) # u1 does not follow band a1 self.assertFalse(u1 in a1.followers) self.assertFalse(a1 in u1.follows)
def add_artist(self, **kwargs): artist_name = kwargs['title'][0] artist = Artist.objects(name=artist_name) if artist.count(): Log(type_added='artist', name_added=artist_name).save() return False self.download(kwargs.get('img')) artist = Artist(name=artist_name) if artist: Log(type_added='artist', name_added=artist_name, added=True).save() with open(TEMP_FILE, 'rb') as binary_file: artist.image.put(binary_file) shutil.rmtree(TEMP) artist.save() return True Log(type_added='artist', name_added=artist_name).save() return False
def porchfest_playlist( porchfest_id ): # Get tracks for every artist performing at specified Porchfest porchfest = Porchfest.objects.get(id=porchfest_id) porchfest_tracks = [] porchfest_artist_names = [] for show in porchfest.shows: artist_name = show.artist.name if artist_name not in porchfest_artist_names: porchfest_artist_names.append(artist_name) porchfest_artists = [] for porchfest_artist_name in porchfest_artist_names: porchfest_artists.append( Artist.objects(name=porchfest_artist_name).first()) for artist in porchfest_artists: for track in artist.tracks: if track not in porchfest_tracks: porchfest_tracks.append(track) return render_template('porchfest_radio.html', tracks=porchfest_tracks, porchfest=porchfest)
def validate_email(self, email): artist = Artist.objects(email=email.data).first() if artist is not None and current_user.email != artist.email: raise ValidationError('Email already being used!')
def get_artist(artist_name): artist = Artist.objects(name=artist_name) if not artist: return False return artist
def upload_track(artist_name): artist = Artist.objects(name=artist_name).first_or_404() if current_user not in artist.members: flash('You are not authorized to upload tracks for {}'.format( artist.name)) return redirect(url_for('main.artist', artist_name=artist.name)) form = UploadTrackForm() form.genre.choices = [(genre.id, genre.name) for genre in Genre.objects.order_by('name')] if form.validate_on_submit(): file = form.track.data if not allowed_file(file.filename): flash('Only music files are allowed') return render_template('upload_track.html', form=form, artist=artist, title='Upload Track') file.filename = secure_filename(file.filename) s3_filepath, cloudfront_filepath = upload_file_to_s3( file, current_app.config['S3_BUCKET']) track_exists = Track.objects(s3_filepath=s3_filepath, cloudfront_filepath=cloudfront_filepath ).first() is not None # 1 if track exists if s3_filepath is not None and cloudfront_filepath is not None and not track_exists: new_track = Track(track_name=form.track_name.data, artist=artist, duration=0, genre=[ Genre.objects(id=genre).first() for genre in form.genre.data ], s3_filepath=s3_filepath, cloudfront_filepath=cloudfront_filepath) new_track.save(cascade=True) # Saves the track in the mongoDB # filepath = os.path.join(current_app.config['UPLOAD_FOLDER'], file.filename) # new_track = Track(track_name=form.track_name.data, artist=artist, duration=0, # genre=[Genre.objects(id=genre).first() for genre in form.genre.data], filepath=filepath) # new_track.save(cascade=True) # Saves the track in the mongoDB # file.save(filepath) artist.tracks.append(new_track) artist.save(cascade=True) # Save artist with new track flash('Track successfully uploaded') return redirect(url_for('main.artist', artist_name=artist_name)) else: if track_exists: flash('Track already exists!') return render_template('upload_track.html', form=form, artist=artist, title='Upload Track') else: flash('Error uploading file') return render_template('upload_track.html', form=form, artist=artist, title='Upload Track') return render_template('upload_track.html', form=form, artist=artist, title='Upload Track')
def is_artist_exist(artist_name): return Artist.objects(name=artist_name).count()
def show_all_artist(): artists = Artist.objects() if not artists: return False return artists
def to_json_artist(media=False): artists = Artist.objects() if not artists: return False return artists.to_json(indent=4, ensure_ascii=False, media=media)
def artistFestSignUp(): form = ArtistPorchfestSignUpForm() porchfests = Porchfest.objects() form.porchfest.choices = [(str(p.id), p.location.city + ", " + p.location.state+" "+p.start_time.strftime("%m-%d-%Y %H:%M")+" to "+p.end_time.strftime("%m-%d-%Y %H:%M")) for p in porchfests] form.porch_selector.choices = [('None', '')] + [(str(p.id), p.address + " " + p.location.city + ", " + p.location.state) for p in Porch.objects()] form.time_slot.choices = [(t, t) for t in get_all_hours()] if form.validate_on_submit(): porchfest = Porchfest.objects(id=form.porchfest.data).first() porchfest_location = porchfest.location porchfest_time = porchfest.start_time form_time = form.time_slot.data hour_int = int(form_time.split()[0]) if 'AM' in form_time: if hour_int is 12: hour = 0 else: hour = hour_int else: if hour_int is 12: hour = hour_int else: hour = hour_int + 12 start_time = datetime(year=int(porchfest_time.year), month=int(porchfest_time.month), day=int(porchfest_time.day), hour=hour) end_time = start_time + timedelta(hours=1) artist = Artist.objects(name=current_user.name).first() if form.porch.data: location = Location.objects(city=form.city.data, state=form.state.data).first() if location is None: location = Location(city=form.city.data, state=form.state.data, zip_code=form.zip.data) location.save(cascade=True) porch = Porch.objects(address=form.address.data).first() if porch is None: time_slots = [] address = form.address.data.split(' ') reqStr = "https://maps.googleapis.com/maps/api/geocode/json?address=" for i in address: reqStr = reqStr + i + "+" reqStr = reqStr[:-1] reqStr = reqStr + location.city + ",+" + location.state + "&key=" + app.config['GOOGLEMAPS_KEY'] res = requests.get(reqStr) resJSON = res.json() data = resJSON['results'][0] lat = data['geometry']['location']['lat'] long = data['geometry']['location']['lng'] porch = Porch(name=form.porch_owner.data, email=form.porch_email.data, address=form.address.data, location=location, time_slots=time_slots, lat=str(lat), long=str(long)) porch.save(cascade=True) else: porch = Porch.objects(id=form.porch_selector.data).first() porch.time_slots.remove(start_time) porch.save(cascade=True) existing_show_for_artist = Show.objects(artist=artist, start_time=start_time).first() existing_show_for_porch = Show.objects(porch=porch, start_time=start_time).first() if existing_show_for_artist is not None or existing_show_for_porch is not None: flash('Show already exists!') else: show = Show(artist=artist, porch=porch, start_time=start_time, end_time=end_time) show.save(cascade=True) porchfest.shows.append(show) porchfest.save(cascade=True) flash('Signed up for ' + porchfest_location.city + ", " + porchfest_location.state + " porchfest!") return redirect(url_for('artist', artist_name=artist.name)) return render_template('artistToPorch.html', form=form)
def add_objects(): times = [ datetime(2019, 6, 26, 9, 0, 0), # start for porchfests datetime(2019, 6, 26, 17, 0, 0), # end for porchfests datetime(2019, 6, 26, 12, 0, 0), # first show end time datetime(2019, 6, 26, 15, 0, 0) # second show end time ] default_users = [ User(username='******', email='*****@*****.**', name='Ithaca One', member_of=[], follows=[]), User(username='******', email='*****@*****.**', name='Ithaca Two', member_of=[], follows=[]), User(username='******', email='*****@*****.**', name='Ithaca Three', member_of=[], follows=[]), User(username='******', email='*****@*****.**', name='Albany One', member_of=[], follows=[]) ] for user in default_users: user.set_password('default') user.save(cascade=True) default_locations = [ Location(city='Ithaca', state='NY', zip_code='14850'), Location(city='Albany', state='NY', zip_code='12203'), ] for location in default_locations: location.save(cascade=True) default_porches = [ Porch(name='Ithaca Porch 1', email='*****@*****.**', address='953 Danby Rd', location=Location.objects(city='Ithaca', state='NY').first(), time_slots=[times[2], times[3]]), Porch(name='Ithaca Porch 2', email='*****@*****.**', address='123 Ithaca Rd', location=Location.objects(city='Ithaca', state='NY').first(), time_slots=[times[0], times[1], times[3]]), Porch(name='Albany Porch 1', email='*****@*****.**', address='1200 Western Ave', location=Location.objects(city='Albany', state='NY').first(), time_slots=[times[0], times[1], times[2], times[3]]) ] for porch in default_porches: porch.save(cascade=True) default_artists = [ Artist(name='Artist 1', description='artist 1 desc', location=Location.objects(city='Ithaca', state='NY').first()), Artist(name='Artist 2', description='artist 2 desc', location=Location.objects(city='Ithaca', state='NY').first()), Artist(name='Artist 3', description='artist 3 desc', location=Location.objects(city='Albany', state='NY').first()) ] for artist in default_artists: artist.save(cascade=True) default_shows = [ Show(artist=Artist.objects(name='Artist 1').first(), porch=Porch.objects(name='Ithaca Porch 1').first(), start_time=times[0], end_time=times[2]), Show(artist=Artist.objects(name='Artist 2').first(), porch=Porch.objects(name='Ithaca Porch 2').first(), start_time=times[2], end_time=times[3]), Show(artist=Artist.objects(name='Artist 3').first(), porch=Porch.objects(name='Albany Porch 1').first(), start_time=times[0], end_time=times[2]) ] for show in default_shows: show.save(cascade=True) default_porchfests = [ Porchfest(location=Location.objects(city='Ithaca', state='NY').first(), start_time=times[0], end_time=times[1], porches=[ Porch.objects(name='Ithaca Porch 1').first(), Porch.objects(name='Ithaca Porch 2').first() ], shows=[ Show.objects(artist=Artist.objects( name='Artist 1').first()).first(), Show.objects(porch=Porch.objects( name='Ithaca Porch 2').first()).first() ]), Porchfest(location=Location.objects(city='Albany', state='NY').first(), start_time=times[0], end_time=times[1], porches=[Porch.objects(name='Albany Porch 1').first()], shows=[ Show.objects(artist=Artist.objects( name='Artist 3').first()).first() ]) ] for porchfest in default_porchfests: porchfest.save(cascade=True)
def reset_db(): db.connection.drop_database('porchfestBAG') for location in Location.objects: location.delete() for artist in Artist.objects: artist.delete() for porch in Porch.objects: porch.delete() for fest in Porchfest.objects: fest.delete() for show in Show.objects: show.delete() times = [ datetime(2018, 12, 26, 9, 0, 0), # start for porchfests datetime(2018, 12, 26, 17, 0, 0), # end for porchfests datetime(2018, 12, 26, 12, 0, 0), # first show end time datetime(2018, 12, 26, 15, 0, 0) # second show end time ] default_locations = [ Location(city='Ithaca', state='NY', zip_code='14850'), Location(city='Binghamton', state='NY', zip_code='13901'), Location(city='Albany', state='NY', zip_code='12203'), Location(city='Winchester', state='MA', zip_code='01890') ] for location in default_locations: location.save(cascade=True) default_porches = [ Porch(name='Ithaca Porch 1', email='*****@*****.**', address='953 Danby Rd', location=Location.objects(city='Ithaca', state='NY').first(), time_slots=[times[2], times[3]], lat='42.4199351', long='-76.4969643'), Porch(name='Ithaca Porch 2', email='*****@*****.**', address='123 Ithaca Rd', location=Location.objects(city='Ithaca', state='NY').first(), time_slots=[times[0], times[1], times[3]], lat='42.438657', long='-76.4800496'), Porch(name='Albany Porch 1', email='*****@*****.**', address='501 Hudson Ave', location=Location.objects(city='Albany', state='NY').first(), time_slots=[times[0], times[1], times[3]], lat='42.662079', long='-73.780703'), ] for porch in default_porches: porch.save(cascade=True) default_artists = [ Artist(email='*****@*****.**', name='Artist 1', description='artist 1 desc', media_links=['https://www.spotify.com/artist1'], location=Location.objects(city='Ithaca', state='NY').first(), image='https://miquon.org/wp-content/uploads/2016/02/GenericUser.png'), Artist(email='*****@*****.**', name='Artist 2', description='artist 2 desc', media_links=['https://myspotify.com'], location=Location.objects(city='Albany', state='NY').first()) ] for artist in default_artists: artist.set_password('default') artist.save(cascade=True) default_shows = [ Show(artist=Artist.objects(name='Artist 1').first(), porch=Porch.objects(name='Ithaca Porch 1').first(), start_time=times[0], end_time=times[2]), Show(artist=Artist.objects(name='Artist 1').first(), porch=Porch.objects(name='Ithaca Porch 2').first(), start_time=times[2], end_time=times[3]), Show(artist=Artist.objects(name='Artist 2').first(), porch=Porch.objects(name='Albany Porch 1').first(), start_time=times[2], end_time=times[3]) ] for show in default_shows: show.save(cascade=True) default_porchfests = [ Porchfest(location=Location.objects(city='Ithaca', state='NY').first(), start_time=times[0], end_time=times[1], porches=[Porch.objects(name='Ithaca Porch 1').first(), Porch.objects(name='Ithaca Porch 2').first()], shows=[Show.objects(artist=Artist.objects(name='Artist 1').first()).first(), Show.objects(porch=Porch.objects(name='Ithaca Porch 2').first()).first()], lat='42.4440', long='-76.5019'), Porchfest(location=Location.objects(city='Binghamton', state='NY').first(), start_time=times[0], end_time=times[1], lat='42.0987', long='-75.9180'), Porchfest(location=Location.objects(city='Albany', state='NY').first(), start_time=times[0], end_time=times[1], lat='42.6526', long='-73.7562', shows=[Show.objects(porch=Porch.objects(name='Albany Porch 1').first()).first()]) ] for porchfest in default_porchfests: porchfest.save(cascade=True) flash("Database has been reset!") return redirect(url_for('index'))