diff --git a/README.md b/README.md index 04d10aa..c79dda7 100644 --- a/README.md +++ b/README.md @@ -171,7 +171,7 @@ Follow these steps to set up and run **MovieRanker** locally: ``` OR - + ```bash python manage.py dbshell ``` diff --git a/movies/management/commands/query_movies.py b/movies/management/commands/query_movies.py index 2cd2b33..beab355 100644 --- a/movies/management/commands/query_movies.py +++ b/movies/management/commands/query_movies.py @@ -20,53 +20,75 @@ from django.core.management.base import BaseCommand from movies.models import Movie + class Command(BaseCommand): - help = 'Query movies from the database with different query types, insert a new movie, and delete a movie' + help = "Query movies from the database with different query types, insert a new movie, and delete a movie" def add_arguments(self, parser): - parser.add_argument('--type', type=str, help='Type of query: all, filter, exclude, insert, delete', required=True) - parser.add_argument('--field', type=str, help='Field to filter, exclude, or delete by') - parser.add_argument('--value', type=str, help='Value to filter, exclude, or delete by') - parser.add_argument('--title', type=str, help='Title of the movie to insert') - parser.add_argument('--director', type=str, help='Director of the movie to insert') - parser.add_argument('--year', type=int, help='Year of the movie to insert') + parser.add_argument( + "--type", + type=str, + help="Type of query: all, filter, exclude, insert, delete", + required=True, + ) + parser.add_argument( + "--field", type=str, help="Field to filter, exclude, or delete by" + ) + parser.add_argument( + "--value", type=str, help="Value to filter, exclude, or delete by" + ) + parser.add_argument("--title", type=str, help="Title of the movie to insert") + parser.add_argument( + "--director", type=str, help="Director of the movie to insert" + ) + parser.add_argument("--year", type=int, help="Year of the movie to insert") def handle(self, *args, **kwargs): - query_type = kwargs['type'] - field = kwargs.get('field') - value = kwargs.get('value') - title = kwargs.get('title') - director = kwargs.get('director') - year = kwargs.get('year') + query_type = kwargs["type"] + field = kwargs.get("field") + value = kwargs.get("value") + title = kwargs.get("title") + director = kwargs.get("director") + year = kwargs.get("year") - if query_type == 'all': + if query_type == "all": self.stdout.write("Querying all movies from the database:") movies = Movie.objects.all() - elif query_type == 'filter' and field and value: - self.stdout.write(f"Querying movies from the database where {field} is {value}:") + elif query_type == "filter" and field and value: + self.stdout.write( + f"Querying movies from the database where {field} is {value}:" + ) filter_kwargs = {field: value} movies = Movie.objects.filter(**filter_kwargs) - elif query_type == 'exclude' and field and value: - self.stdout.write(f"Querying movies from the database excluding where {field} is {value}:") + elif query_type == "exclude" and field and value: + self.stdout.write( + f"Querying movies from the database excluding where {field} is {value}:" + ) exclude_kwargs = {field: value} movies = Movie.objects.exclude(**exclude_kwargs) - elif query_type == 'insert' and title and director and year: - self.stdout.write(f"Inserting a new movie: {title}, directed by {director}, released in {year}") + elif query_type == "insert" and title and director and year: + self.stdout.write( + f"Inserting a new movie: {title}, directed by {director}, released in {year}" + ) new_movie = Movie(title=title, director=director, year=year) new_movie.save() self.stdout.write(f"Movie '{title}' inserted successfully.") return - elif query_type == 'delete' and field and value: - self.stdout.write(f"Deleting movies from the database where {field} is {value}:") + elif query_type == "delete" and field and value: + self.stdout.write( + f"Deleting movies from the database where {field} is {value}:" + ) delete_kwargs = {field: value} movies_to_delete = Movie.objects.filter(**delete_kwargs) count, _ = movies_to_delete.delete() self.stdout.write(f"Deleted {count} movie(s) where {field} is {value}.") return else: - self.stdout.write("Invalid query type or missing field/value for filter/exclude/delete, or missing details for insert.") + self.stdout.write( + "Invalid query type or missing field/value for filter/exclude/delete, or missing details for insert." + ) return for movie in movies: self.stdout.write(movie.title) - print(movie.title) \ No newline at end of file + print(movie.title)