def post(self): data = parser.parse_args() if MediaTypeModel.find_by_name(data['name']): return {'message': 'Media Type with this Name Already Existed'}, 403 if not data['name'] or data['name'].isspace(): return {'message': 'Please Enter Media Type'}, 400 _created_at = str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) new_media_type = MediaTypeModel( name=data['name'], created_at=_created_at ) try: new_media_type.save_to_db() new_id = new_media_type.mediatypeid return { 'message': 'New Media Type Has been Added', 'MediaTypeDetails': { 'id': new_id, 'name': data['name'], 'created_at': _created_at } }, 201 except: return { 'message': 'Something Went Wrong' }, 500
def get(self): if MediaTypeModel.is_data_present() is None: return {'message': 'No Data Available.'} current_user = get_jwt_identity() if not current_user: return MediaTypeModel.return_two_records() try: return MediaTypeModel.return_all() except: return { 'message': 'Something Went Wrong' }, 500
def post(self): data = parser.parse_args() if not AlbumModel.find_by_id(data['album_id']): return {'message': 'No Such Album Exists'}, 404 if not GenreModel.find_by_id(data['genre_id']): return {'message': 'No Such Genre Exists'}, 404 if not MediaTypeModel.find_by_id(data['mediatype_id']): return {'message': 'No Such Media Type Exists'}, 404 if not data['name'] or data['name'].isspace(): return {'message': 'Please Enter Track Name'}, 400 if data['composer'].isspace(): return {'message': 'Please Enter Composer Name'}, 400 if TracksModel.find_song_already_created(data['name'], data['album_id'], data['genre_id'], data['mediatype_id']): return{'message': 'Track with all this information is already Exists'}, 403 _created_at = str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) try: new_track = TracksModel( name=data['name'], albumid=data['album_id'], mediatypeid=data['mediatype_id'], genreid=data['genre_id'], composer=data['composer'], created_at=_created_at ) new_track.save_to_db() new_id = new_track.trackid artist_id = AlbumModel.find_by_id(data['album_id']).artist_id artist_name = ArtistModel.find_by_id(artist_id).name return { 'message': 'New Track has been Added', 'data': { 'track_id': new_id, 'name': data['name'], 'composer': data['composer'], 'created_at': _created_at, 'track_info': { 'artist_name': artist_name, 'album': AlbumModel.find_by_id(data['album_id']).title, 'media_type': MediaTypeModel.find_by_id(data['mediatype_id']).name, 'genre': GenreModel.find_by_id(data['genre_id']).name } } },201 except: return { 'message': 'Something went wrong' }, 500
def put(self, track_id: int): data = parser.parse_args() if not AlbumModel.find_by_id(data['album_id']): return {'message': 'No Such Album Exists'}, 404 if not GenreModel.find_by_id(data['genre_id']): return {'message': 'No Such Genre Exists'}, 404 if not MediaTypeModel.find_by_id(data['mediatype_id']): return {'message': 'No Such Media Type Exists'}, 404 if not data['name'] or data['name'].isspace(): return {'message': 'Please Enter Track Name'}, 400 if data['composer'].isspace(): return {'message': 'Please Enter Composer Name'}, 400 track = TracksModel.find_by_id(track_id) if not track: return {'message': 'No Such Track found.'}, 404 if (TracksModel.find_song_already_created(data['name'], data['album_id'], data['genre_id'], data['mediatype_id']) and (data['composer'] is track.composer)): return {'message': 'Track with all this information is already Exists'}, 403 try: track.name = data['name'] track.albumid = data['album_id'] track.mediatypeid = data['mediatype_id'] track.genreid = data['genre_id'] track.composer = data['composer'] db.session.commit() artist_id = AlbumModel.find_by_id(data['album_id']).artist_id artist_name = ArtistModel.find_by_id(artist_id).name return { 'message': 'Track has been Updated', 'Track_Details': { 'track_id': track_id, 'name': data['name'], 'composer': data['composer'], 'created_at': track.created_at.strftime("%Y-%m-%d %H:%M:%S"), 'track_info': { 'artist_name': artist_name, 'album': AlbumModel.find_by_id(data['album_id']).title, 'media_type': MediaTypeModel.find_by_id(data['mediatype_id']).name, 'genre': GenreModel.find_by_id(data['genre_id']).name } } }, 200 except: return { 'message': 'Something went wrong' }, 500
def put(self, mediatype_id): id = MediaTypeModel.find_by_id(mediatype_id) if not id: return {'message': 'No Such Media Type Exist'}, 404 data = parser.parse_args() if MediaTypeModel.find_by_name(data['name']): return {'message': 'Media Type with this name already exists'}, 403 updated_mediaType = MediaTypeModel( name=data['name'], created_at=None ) if not data['name'] or data['name'].isspace(): return {'message': 'Please Enter Media Type'}, 400 try: mediatype = MediaTypeModel.find_by_id(mediatype_id) mediatype.name = data['name'] updated_mediaType.commit_db() return { 'message': 'Media Type Has been Updated', 'GenreDetails': { 'id': mediatype_id, 'name': data['name'] } }, 200 except Exception as e: return dict(message= e._message())
def get(self, media_type_id: int): media_type = MediaTypeModel.find_by_id(media_type_id) if not media_type: return {'message': 'Media Type Not Found'}, 404 try: return media_type.json(), 200 except: return { 'message': 'Something Went Wrong' }, 500
def delete(self, mediatype_id: int): mediatype_id = MediaTypeModel.find_by_id(mediatype_id) if not mediatype_id: return {'message': 'No Such Media Type Exist'}, 404 try: mediatype_id.delete_from_db(mediatype_id) return { 'message': 'Media Type has been deleted' } except IntegrityError as e: db.session.rollback() return dict(message=e._message())
def insert_media_types(): # delete records (if any) MediaTypeModel.objects().delete() # loop through records for media_type in media_types: # create record media_type_model = MediaTypeModel(**media_type) # insert record media_type_model.save()
def insert_events(): # delete records (if any) EventModel.objects().delete() # loop through records for event in events: # find media_type record event['media_type'] = MediaTypeModel.objects( media_type=event['media_type']).first() # find block record event['block'] = BlockModel.objects( block_number=event['block']).first()['block_id'] # create record event_model = EventModel(**event) # insert record event_model.save()