示例#1
0
 def get(self):
     if GenreModel.is_data_present() is None:
         return {'message': 'No Data Available.'}
     current_user = get_jwt_identity()
     if not current_user:
         return GenreModel.return_two_records()
     try:
         return GenreModel.return_all()
     except:
         return {'message': 'Something Went Wrong'}, 500
示例#2
0
 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
示例#3
0
    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
示例#4
0
 def post(self):
     args = self.reqparse.parse_args()
     hoge = GenreModel(args.name)
     db.session.add(hoge)
     db.session.commit()
     res = GenreSchema().dump(hoge).data
     return res, 201
示例#5
0
 def get(self, genre_id: int):
     genre = GenreModel.find_by_id(genre_id)
     if not genre:
         return {'message': 'Genre Not Found'}, 404
     try:
         return genre.json(), 200
     except:
         return {'message': 'Something Went Wrong'}, 500
示例#6
0
 def delete(self, genre_id: int):
     genre_id = GenreModel.find_by_id(genre_id)
     if not genre_id:
         return {'message': 'No Such Genre Exist'}, 404
     try:
         genre_id.delete_from_db(genre_id)
         return {'message': 'Genre has been deleted'}
     except:
         return {'message': 'Something went Wrong'}, 500
示例#7
0
    def get(self, genre):
        '''Get books on a certain genre'''
        try:
            books = GenreModel.get_all_books_from_genre(genre)
        except Exception as e:
            return [], 404
        book_list = [book.json() for book in books]

        return book_list, 200
示例#8
0
 def post(self):
     data = parser.parse_args()
     if GenreModel.find_by_name(data['name']):
         return {'message': 'Genre with this Name Already Existed'}, 403
     if not data['name'] or data['name'].isspace():
         return {'message': 'Please Enter Genre Name'}, 400
     try:
         new_genre = GenreModel(name=data['name'], created_at=_created_at)
         new_genre.save_to_db()
         new_id = new_genre.id
         return {
             'message': 'New Genre Has been Added',
             'GenreDetails': {
                 'id': new_id,
                 'name': data['name'],
                 'created_at': _created_at
             }
         }, 201
     except:
         return {'message': 'Something Went Wrong'}, 500
示例#9
0
文件: genre.py 项目: gervaisx/rosm
    def post(self):

        data = Genre.parser.parse_args()

        try:

            famille = FamilleModel.query.filter_by(id = data['famille']).first()

            genre = GenreModel(nom_commun = data['nom_commun'],
                                    nom_latin = data['nom_latin'],
                                    nom_commun_en = data['nom_en'],
                                    descript = data['details'],
                                    famille = famille
                                    )

            genre.save_to_db()

            return {'message': 'Genre successfully added'}, 201

        except:

            return {'message': 'An error occurred adding the genre.'}, 500
示例#10
0
    def put(self, book_id, author, title, date, genre):
        '''Update a book by book id'''
        cur_book = BookModel.find_by_id(book_id)

        if not cur_book:
            return {'message' : 'book with id {} is not found.'.format(book_id)}, 404

        new_author = AuthorModel.search_and_add_author(author)
        new_genre = GenreModel.search_and_add_genre(genre)
        new_date = DateRead.read_date(date)

        cur_book = BookModel.update_book(book_id, title, new_author, new_date, new_genre)
        return cur_book.json(), 200
示例#11
0
    def post(self, title, author, date, genre):
        '''Add a new book'''
        cur_author = AuthorModel.search_and_add_author(author)

        cur_genre = GenreModel.search_and_add_genre(genre)

        release_date = DateRead.read_date(date)

        #create the new book, add into db
        new_book = BookModel(title, cur_author.id, release_date, cur_genre)
        new_book.save_to_db()

        if not new_book:
            return {"message" : "add book error"}, 403
        return new_book.json(), 201
示例#12
0
 def post(self, name):
     if GenreModel.find_by_name(name):
         return {'message': "A genre with name '{}' already exists.".format(name)}, 400
     
     genre = GenreModel(name)
     try:
         genre.save_to_db()
     except:
         return {'message': 'An error occured while creating the genre.'}, 500
     
     return genre.json(), 201
示例#13
0
 def put(self, genre_id):
     id = GenreModel.find_by_id(genre_id)
     if not id:
         return {'message': 'No Such Genre Exist'}, 404
     data = parser.parse_args()
     if GenreModel.find_by_name(data['name']):
         return {'message': 'Genre with this name already exists'}, 403
     updated_genre = GenreModel(name=data['name'], created_at=None)
     if not data['name'] or data['name'].isspace():
         return {'message': 'Please Enter Genre Name'}, 400
     try:
         genre = GenreModel.find_by_id(genre_id)
         genre.name = data['name']
         updated_genre.commit_db()
         return {
             'message': 'Genre Has been Updated',
             'GenreDetails': {
                 'id': genre_id,
                 'name': data['name']
             }
         }, 200
     except:
         return {'message': 'Something Went Wrong'}, 500
示例#14
0
 def get(self, name):
     genre = GenreModel.find_by_name(name)
     if genre:
         return genre.json()
     return {'message': 'Genre not found'}, 404
示例#15
0
 def delete(self, name):
     genre = GenreModel.find_by_name(name)
     if genre:
         genre.delete_from_db()
     
     return {'message': 'Genre deleted.'}