def add_new_genre(request): # return jsonify(request.get_json()) type = request.json['type'] genre = request.json['genre'] if type is not None: new_genre = Genre(type=type, genre=genre) Genre.save(new_genre) headers = {"Description": "OK"} result = { 'id': new_genre.id, 'type': new_genre.type, 'genre': new_genre.genre } response = jsonify(result) response.status_code = 200 response.headers = headers return response else: headers = {"Description": "Invalid input"} result = { "invalid_fields": [{ "field": "type", "reason": "Type field is required" }] } response = jsonify(result) response.status_code = 400 response.headers = headers return response
def save_all_data(self, movies_iterator) -> int: """ Iterate over movies saving them in postgres content tables @param movies_iterator: the movies @return: number of movies saved """ cursor = self.cursor for movie in movies_iterator: film_work = FilmWork(title=movie['title'], description=movie['description'], rating=movie['imdb_rating']) film_work.save(cursor) for role in ['actor', 'director', 'writer']: for person_name in movie[role]: person = Person(name=person_name) person.save(cursor) FilmWorkPerson(film_work_id=film_work.id, person_id=person.id, role=role).save(cursor) for genre_name in movie['genre']: genre = Genre(name=genre_name) genre.save(cursor) FilmWorkGenre(film_work_id=film_work.id, genre_id=genre.id).save(cursor) self._counter += 1 print('.', end='', flush=True) return self._counter
def genre_save(request): if request.POST.get('genre_id'): genre = Genre.objects.get(id=request.POST.get('genre_id')) genre.name = request.POST.get('genre_name') genre.description = request.POST.get('genre_description') genre.save() else: genre = Genre( name=request.POST.get('genre_name'), description=request.POST.get('genre_description'), ) genre.save() return redirect('music.views.genre_detail', genre_id=genre.id)
def test_should_save_valid_genre(self): genre = Genre(name='TestGenre', description='This is a description') genre.save() all_genres = Genre.objects.all() self.assertEqual(len(all_genres), 1)
def create(self, validated_data): slug = validated_data['name'].lower().replace('-', '_') new_genre = Genre(name=validated_data['name'], slug=slug) new_genre.save() # add genre return new_genre
def addGenretoAlbum(genre: Genre, album: Album): if genre not in album.getGenres(): album.genres.connect(genre) genre.save() album.save()
def test_should_save_valid_genre(self): genre = Genre(name='TestGenre', description='This is a description') genre.save() all_genres = Genre.objects.all() self.assertEqual(len(all_genres), 1)